blob: c7810602373c098a4177631a73b91c1d26d4f21a [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 Moolenaarfbbcd002020-10-15 12:46:44 +02002648 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002649 *p = cc;
2650 p = skipwhite(p);
2651
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002652 // TODO: what if it is a function?
2653 if (idx < 0)
2654 return FAIL;
2655 *end = p;
2656
2657 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2658 import->imp_sid,
2659 idx,
2660 type);
2661 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002662 else if (import->imp_funcname != NULL)
2663 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002664 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002665 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2666 import->imp_sid,
2667 import->imp_var_vals_idx,
2668 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 return OK;
2670 }
2671
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002672 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002673 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 return FAIL;
2675}
2676
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002677 static int
2678generate_funcref(cctx_T *cctx, char_u *name)
2679{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002680 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002681
2682 if (ufunc == NULL)
2683 return FAIL;
2684
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002685 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002686 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2687 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002688 == FAIL)
2689 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002690 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002691}
2692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693/*
2694 * Compile a variable name into a load instruction.
2695 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002696 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 * When "error" is FALSE do not give an error when not found.
2698 */
2699 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002700compile_load(
2701 char_u **arg,
2702 char_u *end_arg,
2703 cctx_T *cctx,
2704 int is_expr,
2705 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706{
2707 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002708 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002709 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002711 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712
2713 if (*(*arg + 1) == ':')
2714 {
2715 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002716 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002717 {
2718 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002720 switch (**arg)
2721 {
2722 case 'g': isn_type = ISN_LOADGDICT; break;
2723 case 'w': isn_type = ISN_LOADWDICT; break;
2724 case 't': isn_type = ISN_LOADTDICT; break;
2725 case 'b': isn_type = ISN_LOADBDICT; break;
2726 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002727 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002728 goto theend;
2729 }
2730 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2731 goto theend;
2732 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 else
2735 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002736 isntype_T isn_type = ISN_DROP;
2737
2738 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2739 if (name == NULL)
2740 return FAIL;
2741
2742 switch (**arg)
2743 {
2744 case 'v': res = generate_LOADV(cctx, name, error);
2745 break;
2746 case 's': res = compile_load_scriptvar(cctx, name,
2747 NULL, NULL, error);
2748 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002749 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2750 isn_type = ISN_LOADG;
2751 else
2752 {
2753 isn_type = ISN_LOADAUTO;
2754 vim_free(name);
2755 name = vim_strnsave(*arg, end - *arg);
2756 if (name == NULL)
2757 return FAIL;
2758 }
2759 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002760 case 'w': isn_type = ISN_LOADW; break;
2761 case 't': isn_type = ISN_LOADT; break;
2762 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002763 default: // cannot happen, just in case
2764 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002765 goto theend;
2766 }
2767 if (isn_type != ISN_DROP)
2768 {
2769 // Global, Buffer-local, Window-local and Tabpage-local
2770 // variables can be defined later, thus we don't check if it
2771 // exists, give error at runtime.
2772 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 }
2775 }
2776 else
2777 {
2778 size_t len = end - *arg;
2779 int idx;
2780 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002781 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
2783 name = vim_strnsave(*arg, end - *arg);
2784 if (name == NULL)
2785 return FAIL;
2786
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002787 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002789 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002790 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 }
2792 else
2793 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002794 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002795
Bram Moolenaar709664c2020-12-12 14:33:41 +01002796 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002798 type = lvar.lv_type;
2799 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002800 if (lvar.lv_from_outer != 0)
2801 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002802 else
2803 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 }
2805 else
2806 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002807 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002808 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002809 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002810 || find_imported(name, 0, cctx) != NULL)
2811 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002812
Bram Moolenaar0f769812020-09-12 18:32:34 +02002813 // When evaluating an expression and the name starts with an
2814 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002815 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002816 if (res == FAIL && is_expr
2817 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002818 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 }
2820 }
2821 if (gen_load)
2822 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002823 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002824 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002825 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002826 cctx->ctx_outer_used = TRUE;
2827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 }
2829
2830 *arg = end;
2831
2832theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002833 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002834 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 vim_free(name);
2836 return res;
2837}
2838
2839/*
2840 * Compile the argument expressions.
2841 * "arg" points to just after the "(" and is advanced to after the ")"
2842 */
2843 static int
2844compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2845{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002846 char_u *p = *arg;
2847 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002848 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849
Bram Moolenaare6085c52020-04-12 20:19:16 +02002850 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002852 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2853 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002854 if (*p == ')')
2855 {
2856 *arg = p + 1;
2857 return OK;
2858 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002859 if (must_end)
2860 {
2861 semsg(_(e_missing_comma_before_argument_str), p);
2862 return FAIL;
2863 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002864
Bram Moolenaara5565e42020-05-09 15:44:01 +02002865 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 return FAIL;
2867 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002868
2869 if (*p != ',' && *skipwhite(p) == ',')
2870 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002871 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002872 p = skipwhite(p);
2873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002875 {
2876 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002877 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002878 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002879 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002880 else
2881 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002882 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002883 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002885failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002886 emsg(_(e_missing_close));
2887 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888}
2889
2890/*
2891 * Compile a function call: name(arg1, arg2)
2892 * "arg" points to "name", "arg + varlen" to the "(".
2893 * "argcount_init" is 1 for "value->method()"
2894 * Instructions:
2895 * EVAL arg1
2896 * EVAL arg2
2897 * BCALL / DCALL / UCALL
2898 */
2899 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002900compile_call(
2901 char_u **arg,
2902 size_t varlen,
2903 cctx_T *cctx,
2904 ppconst_T *ppconst,
2905 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906{
2907 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002908 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 int argcount = argcount_init;
2910 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002911 char_u fname_buf[FLEN_FIXED + 1];
2912 char_u *tofree = NULL;
2913 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002914 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002915 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002916 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917
Bram Moolenaara5565e42020-05-09 15:44:01 +02002918 // we can evaluate "has('name')" at compile time
2919 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2920 {
2921 char_u *s = skipwhite(*arg + varlen + 1);
2922 typval_T argvars[2];
2923
2924 argvars[0].v_type = VAR_UNKNOWN;
2925 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002926 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002927 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002928 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002929 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002930 if (*s == ')' && argvars[0].v_type == VAR_STRING
2931 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002932 {
2933 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2934
2935 *arg = s + 1;
2936 argvars[1].v_type = VAR_UNKNOWN;
2937 tv->v_type = VAR_NUMBER;
2938 tv->vval.v_number = 0;
2939 f_has(argvars, tv);
2940 clear_tv(&argvars[0]);
2941 ++ppconst->pp_used;
2942 return OK;
2943 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002944 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002945 }
2946
2947 if (generate_ppconst(cctx, ppconst) == FAIL)
2948 return FAIL;
2949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 if (varlen >= sizeof(namebuf))
2951 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002952 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 return FAIL;
2954 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002955 vim_strncpy(namebuf, *arg, varlen);
2956 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
2958 *arg = skipwhite(*arg + varlen + 1);
2959 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961
Bram Moolenaar03290b82020-12-19 16:30:44 +01002962 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002963 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 {
2965 int idx;
2966
2967 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002968 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002970 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002971 if (STRCMP(name, "flatten") == 0)
2972 {
2973 emsg(_(e_cannot_use_flatten_in_vim9_script));
2974 goto theend;
2975 }
2976
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002977 if (STRCMP(name, "add") == 0 && argcount == 2)
2978 {
2979 garray_T *stack = &cctx->ctx_type_stack;
2980 type_T *type = ((type_T **)stack->ga_data)[
2981 stack->ga_len - 2];
2982
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002983 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002984 if (type->tt_type == VAR_LIST)
2985 {
2986 // inline "add(list, item)" so that the type can be checked
2987 res = generate_LISTAPPEND(cctx);
2988 idx = -1;
2989 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002990 else if (type->tt_type == VAR_BLOB)
2991 {
2992 // inline "add(blob, nr)" so that the type can be checked
2993 res = generate_BLOBAPPEND(cctx);
2994 idx = -1;
2995 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002996 }
2997
2998 if (idx >= 0)
2999 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3000 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003001 else
3002 semsg(_(e_unknownfunc), namebuf);
3003 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 }
3005
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003006 // An argument or local variable can be a function reference, this
3007 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003008 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003009 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003010 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003011 // If we can find the function by name generate the right call.
3012 // Skip global functions here, a local funcref takes precedence.
3013 ufunc = find_func(name, FALSE, cctx);
3014 if (ufunc != NULL && !func_is_global(ufunc))
3015 {
3016 res = generate_CALL(cctx, ufunc, argcount);
3017 goto theend;
3018 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
3021 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003022 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003023 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003025 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003026 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003027 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003028 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003029 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003030
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003031 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003032 goto theend;
3033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
Bram Moolenaar0f769812020-09-12 18:32:34 +02003035 // If we can find a global function by name generate the right call.
3036 if (ufunc != NULL)
3037 {
3038 res = generate_CALL(cctx, ufunc, argcount);
3039 goto theend;
3040 }
3041
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003042 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003043 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003044 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003045 res = generate_UCALL(cctx, name, argcount);
3046 else
3047 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003048
3049theend:
3050 vim_free(tofree);
3051 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052}
3053
3054// like NAMESPACE_CHAR but with 'a' and 'l'.
3055#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3056
3057/*
3058 * Find the end of a variable or function name. Unlike find_name_end() this
3059 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003060 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 * Return a pointer to just after the name. Equal to "arg" if there is no
3062 * valid name.
3063 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003064 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003065to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066{
3067 char_u *p;
3068
3069 // Quick check for valid starting character.
3070 if (!eval_isnamec1(*arg))
3071 return arg;
3072
3073 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3074 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3075 // and can be used in slice "[n:]".
3076 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003077 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3079 break;
3080 return p;
3081}
3082
3083/*
3084 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003085 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 */
3087 char_u *
3088to_name_const_end(char_u *arg)
3089{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003090 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 typval_T rettv;
3092
3093 if (p == arg && *arg == '[')
3094 {
3095
3096 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003097 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 p = arg;
3099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 return p;
3101}
3102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103/*
3104 * parse a list: [expr, expr]
3105 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003106 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 */
3108 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003109compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110{
3111 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003112 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003114 int is_const;
3115 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003117 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003119 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003120 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003121 semsg(_(e_list_end), *arg);
3122 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003123 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003124 if (*p == ',')
3125 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003126 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003127 return FAIL;
3128 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003129 if (*p == ']')
3130 {
3131 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003132 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003133 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003134 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003135 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003136 if (!is_const)
3137 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 ++count;
3139 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003142 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3143 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003144 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003145 return FAIL;
3146 }
3147 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003148 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 p = skipwhite(p);
3150 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003151 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003153 ppconst->pp_is_const = is_all_const;
3154 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155}
3156
3157/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003158 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003160 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 */
3162 static int
3163compile_lambda(char_u **arg, cctx_T *cctx)
3164{
Bram Moolenaare462f522020-12-27 14:43:30 +01003165 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 typval_T rettv;
3167 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003168 evalarg_T evalarg;
3169
3170 CLEAR_FIELD(evalarg);
3171 evalarg.eval_flags = EVAL_EVALUATE;
3172 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173
3174 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003175 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3176 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003177 {
3178 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003179 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003180 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003181
Bram Moolenaar65c44152020-12-24 15:14:01 +01003182 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003184 ++ufunc->uf_refcount;
3185 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186
Bram Moolenaar65c44152020-12-24 15:14:01 +01003187 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003188 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003190 clear_evalarg(&evalarg, NULL);
3191
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003192 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003193 {
3194 // The return type will now be known.
3195 set_function_type(ufunc);
3196
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003197 // The function reference count will be 1. When the ISN_FUNCREF
3198 // instruction is deleted the reference count is decremented and the
3199 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003200 return generate_FUNCREF(cctx, ufunc);
3201 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003202
3203 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 return FAIL;
3205}
3206
3207/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003208 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003210 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 */
3212 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003213compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214{
3215 garray_T *instr = &cctx->ctx_instr;
3216 int count = 0;
3217 dict_T *d = dict_alloc();
3218 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003219 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003220 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003221 int is_const;
3222 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223
3224 if (d == NULL)
3225 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003226 if (generate_ppconst(cctx, ppconst) == FAIL)
3227 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003228 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003230 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231
Bram Moolenaar23c55272020-06-21 16:58:13 +02003232 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003233 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003234 *arg = NULL;
3235 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003236 }
3237
3238 if (**arg == '}')
3239 break;
3240
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003241 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003243 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003245 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003246 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003247 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003250 if (isn->isn_type == ISN_PUSHNR)
3251 {
3252 char buf[NUMBUFLEN];
3253
3254 // Convert to string at compile time.
3255 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3256 isn->isn_type = ISN_PUSHS;
3257 isn->isn_arg.string = vim_strsave((char_u *)buf);
3258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 if (isn->isn_type == ISN_PUSHS)
3260 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003261 else if (may_generate_2STRING(-1, cctx) == FAIL)
3262 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003263 *arg = skipwhite(*arg);
3264 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003265 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003266 emsg(_(e_missing_matching_bracket_after_dict_key));
3267 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003268 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003269 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003271 else
3272 {
3273 // {"name": value},
3274 // {'name': value},
3275 // {name: value} use "name" as a literal key
3276 key = get_literal_key(arg);
3277 if (key == NULL)
3278 return FAIL;
3279 if (generate_PUSHS(cctx, key) == FAIL)
3280 return FAIL;
3281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282
3283 // Check for duplicate keys, if using string keys.
3284 if (key != NULL)
3285 {
3286 item = dict_find(d, key, -1);
3287 if (item != NULL)
3288 {
3289 semsg(_(e_duplicate_key), key);
3290 goto failret;
3291 }
3292 item = dictitem_alloc(key);
3293 if (item != NULL)
3294 {
3295 item->di_tv.v_type = VAR_UNKNOWN;
3296 item->di_tv.v_lock = 0;
3297 if (dict_add(d, item) == FAIL)
3298 dictitem_free(item);
3299 }
3300 }
3301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 if (**arg != ':')
3303 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003304 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003305 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003306 else
3307 semsg(_(e_missing_dict_colon), *arg);
3308 return FAIL;
3309 }
3310 whitep = *arg + 1;
3311 if (!IS_WHITE_OR_NUL(*whitep))
3312 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003313 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 return FAIL;
3315 }
3316
Bram Moolenaar23c55272020-06-21 16:58:13 +02003317 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003318 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003319 *arg = NULL;
3320 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003321 }
3322
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003323 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003325 if (!is_const)
3326 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 ++count;
3328
Bram Moolenaar2c330432020-04-13 14:41:35 +02003329 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003330 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003331 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003332 *arg = NULL;
3333 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 if (**arg == '}')
3336 break;
3337 if (**arg != ',')
3338 {
3339 semsg(_(e_missing_dict_comma), *arg);
3340 goto failret;
3341 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003342 if (IS_WHITE_OR_NUL(*whitep))
3343 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003344 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003345 return FAIL;
3346 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003347 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003348 if (!IS_WHITE_OR_NUL(*whitep))
3349 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003350 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003351 return FAIL;
3352 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003353 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 }
3355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 *arg = *arg + 1;
3357
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003358 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003359 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003360 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003361 *arg += STRLEN(*arg);
3362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003364 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 return generate_NEWDICT(cctx, count);
3366
3367failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003368 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003369 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003370 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003371 *arg = (char_u *)"";
3372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 dict_unref(d);
3374 return FAIL;
3375}
3376
3377/*
3378 * Compile "&option".
3379 */
3380 static int
3381compile_get_option(char_u **arg, cctx_T *cctx)
3382{
3383 typval_T rettv;
3384 char_u *start = *arg;
3385 int ret;
3386
3387 // parse the option and get the current value to get the type.
3388 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003389 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 if (ret == OK)
3391 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003392 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003393 char_u *name = vim_strnsave(start, *arg - start);
3394 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3395 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396
3397 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3398 vim_free(name);
3399 }
3400 clear_tv(&rettv);
3401
3402 return ret;
3403}
3404
3405/*
3406 * Compile "$VAR".
3407 */
3408 static int
3409compile_get_env(char_u **arg, cctx_T *cctx)
3410{
3411 char_u *start = *arg;
3412 int len;
3413 int ret;
3414 char_u *name;
3415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 ++*arg;
3417 len = get_env_len(arg);
3418 if (len == 0)
3419 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003420 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 return FAIL;
3422 }
3423
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003424 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 name = vim_strnsave(start, len + 1);
3426 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3427 vim_free(name);
3428 return ret;
3429}
3430
3431/*
3432 * Compile "@r".
3433 */
3434 static int
3435compile_get_register(char_u **arg, cctx_T *cctx)
3436{
3437 int ret;
3438
3439 ++*arg;
3440 if (**arg == NUL)
3441 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003442 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 return FAIL;
3444 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003445 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 {
3447 emsg_invreg(**arg);
3448 return FAIL;
3449 }
3450 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3451 ++*arg;
3452 return ret;
3453}
3454
3455/*
3456 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003457 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 */
3459 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003460apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003462 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463
3464 // this works from end to start
3465 while (p > start)
3466 {
3467 --p;
3468 if (*p == '-' || *p == '+')
3469 {
3470 // only '-' has an effect, for '+' we only check the type
3471#ifdef FEAT_FLOAT
3472 if (rettv->v_type == VAR_FLOAT)
3473 {
3474 if (*p == '-')
3475 rettv->vval.v_float = -rettv->vval.v_float;
3476 }
3477 else
3478#endif
3479 {
3480 varnumber_T val;
3481 int error = FALSE;
3482
3483 // tv_get_number_chk() accepts a string, but we don't want that
3484 // here
3485 if (check_not_string(rettv) == FAIL)
3486 return FAIL;
3487 val = tv_get_number_chk(rettv, &error);
3488 clear_tv(rettv);
3489 if (error)
3490 return FAIL;
3491 if (*p == '-')
3492 val = -val;
3493 rettv->v_type = VAR_NUMBER;
3494 rettv->vval.v_number = val;
3495 }
3496 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003497 else if (numeric_only)
3498 {
3499 ++p;
3500 break;
3501 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003502 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 {
3504 int v = tv2bool(rettv);
3505
3506 // '!' is permissive in the type.
3507 clear_tv(rettv);
3508 rettv->v_type = VAR_BOOL;
3509 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3510 }
3511 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003512 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 return OK;
3514}
3515
3516/*
3517 * Recognize v: variables that are constants and set "rettv".
3518 */
3519 static void
3520get_vim_constant(char_u **arg, typval_T *rettv)
3521{
3522 if (STRNCMP(*arg, "v:true", 6) == 0)
3523 {
3524 rettv->v_type = VAR_BOOL;
3525 rettv->vval.v_number = VVAL_TRUE;
3526 *arg += 6;
3527 }
3528 else if (STRNCMP(*arg, "v:false", 7) == 0)
3529 {
3530 rettv->v_type = VAR_BOOL;
3531 rettv->vval.v_number = VVAL_FALSE;
3532 *arg += 7;
3533 }
3534 else if (STRNCMP(*arg, "v:null", 6) == 0)
3535 {
3536 rettv->v_type = VAR_SPECIAL;
3537 rettv->vval.v_number = VVAL_NULL;
3538 *arg += 6;
3539 }
3540 else if (STRNCMP(*arg, "v:none", 6) == 0)
3541 {
3542 rettv->v_type = VAR_SPECIAL;
3543 rettv->vval.v_number = VVAL_NONE;
3544 *arg += 6;
3545 }
3546}
3547
Bram Moolenaar657137c2021-01-09 15:45:23 +01003548 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003549get_compare_type(char_u *p, int *len, int *type_is)
3550{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003551 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003552 int i;
3553
3554 switch (p[0])
3555 {
3556 case '=': if (p[1] == '=')
3557 type = EXPR_EQUAL;
3558 else if (p[1] == '~')
3559 type = EXPR_MATCH;
3560 break;
3561 case '!': if (p[1] == '=')
3562 type = EXPR_NEQUAL;
3563 else if (p[1] == '~')
3564 type = EXPR_NOMATCH;
3565 break;
3566 case '>': if (p[1] != '=')
3567 {
3568 type = EXPR_GREATER;
3569 *len = 1;
3570 }
3571 else
3572 type = EXPR_GEQUAL;
3573 break;
3574 case '<': if (p[1] != '=')
3575 {
3576 type = EXPR_SMALLER;
3577 *len = 1;
3578 }
3579 else
3580 type = EXPR_SEQUAL;
3581 break;
3582 case 'i': if (p[1] == 's')
3583 {
3584 // "is" and "isnot"; but not a prefix of a name
3585 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3586 *len = 5;
3587 i = p[*len];
3588 if (!isalnum(i) && i != '_')
3589 {
3590 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3591 *type_is = TRUE;
3592 }
3593 }
3594 break;
3595 }
3596 return type;
3597}
3598
3599/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003600 * Skip over an expression, ignoring most errors.
3601 */
3602 static void
3603skip_expr_cctx(char_u **arg, cctx_T *cctx)
3604{
3605 evalarg_T evalarg;
3606
3607 CLEAR_FIELD(evalarg);
3608 evalarg.eval_cctx = cctx;
3609 skip_expr(arg, &evalarg);
3610}
3611
3612/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003614 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 */
3616 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003617compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003619 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620
3621 // this works from end to start
3622 while (p > start)
3623 {
3624 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003625 while (VIM_ISWHITE(*p))
3626 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 if (*p == '-' || *p == '+')
3628 {
3629 int negate = *p == '-';
3630 isn_T *isn;
3631
3632 // TODO: check type
3633 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3634 {
3635 --p;
3636 if (*p == '-')
3637 negate = !negate;
3638 }
3639 // only '-' has an effect, for '+' we only check the type
3640 if (negate)
3641 isn = generate_instr(cctx, ISN_NEGATENR);
3642 else
3643 isn = generate_instr(cctx, ISN_CHECKNR);
3644 if (isn == NULL)
3645 return FAIL;
3646 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003647 else if (numeric_only)
3648 {
3649 ++p;
3650 break;
3651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 else
3653 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003654 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003656 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003658 if (p[-1] == '!')
3659 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 if (generate_2BOOL(cctx, invert) == FAIL)
3663 return FAIL;
3664 }
3665 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003666 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 return OK;
3668}
3669
3670/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003671 * Compile "(expression)": recursive!
3672 * Return FAIL/OK.
3673 */
3674 static int
3675compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3676{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003677 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003678 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003679
Bram Moolenaar24156692021-01-14 20:35:49 +01003680 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3681 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003682 if (ppconst->pp_used <= PPSIZE - 10)
3683 {
3684 ret = compile_expr1(arg, cctx, ppconst);
3685 }
3686 else
3687 {
3688 // Not enough space in ppconst, flush constants.
3689 if (generate_ppconst(cctx, ppconst) == FAIL)
3690 return FAIL;
3691 ret = compile_expr0(arg, cctx);
3692 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003693 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3694 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003695 if (**arg == ')')
3696 ++*arg;
3697 else if (ret == OK)
3698 {
3699 emsg(_(e_missing_close));
3700 ret = FAIL;
3701 }
3702 return ret;
3703}
3704
3705/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003707 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 */
3709 static int
3710compile_subscript(
3711 char_u **arg,
3712 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003713 char_u *start_leader,
3714 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003715 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003717 char_u *name_start = *end_leader;
3718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 for (;;)
3720 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003721 char_u *p = skipwhite(*arg);
3722
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003723 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003724 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003725 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003726
3727 // If a following line starts with "->{" or "->X" advance to that
3728 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003729 // Also if a following line starts with ".x".
3730 if (next != NULL &&
3731 ((next[0] == '-' && next[1] == '>'
3732 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003733 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003734 {
3735 next = next_line_from_context(cctx, TRUE);
3736 if (next == NULL)
3737 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003738 *arg = next;
3739 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003740 }
3741 }
3742
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003743 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003744 // not a function call.
3745 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003747 garray_T *stack = &cctx->ctx_type_stack;
3748 type_T *type;
3749 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003751 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003752 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003753 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003756 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3757
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003758 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3760 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003761 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 return FAIL;
3763 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003764 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003766 char_u *pstart = p;
3767
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003768 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003769 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003770 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 // something->method()
3773 // Apply the '!', '-' and '+' first:
3774 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003775 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003778 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003779 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003780 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003781 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003782 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003783 int argcount = 1;
3784 char_u *expr;
3785 garray_T *stack;
3786 type_T *type;
3787
3788 // Funcref call: list->(Refs[2])(arg)
3789 // or lambda: list->((arg) => expr)(arg)
3790 // Fist compile the arguments.
3791 expr = *arg;
3792 *arg = skipwhite(*arg + 1);
3793 skip_expr_cctx(arg, cctx);
3794 *arg = skipwhite(*arg);
3795 if (**arg != ')')
3796 {
3797 semsg(_(e_missing_paren), *arg);
3798 return FAIL;
3799 }
3800 ++*arg;
3801 if (**arg != '(')
3802 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003803 if (*skipwhite(*arg) == '(')
3804 emsg(_(e_nowhitespace));
3805 else
3806 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003807 return FAIL;
3808 }
3809
3810 *arg = skipwhite(*arg + 1);
3811 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3812 return FAIL;
3813
3814 // Compile the function expression.
3815 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3816 return FAIL;
3817
3818 stack = &cctx->ctx_type_stack;
3819 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3820 if (generate_PCALL(cctx, argcount,
3821 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 return FAIL;
3823 }
3824 else
3825 {
3826 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003827 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003828 if (!eval_isnamec1(*p))
3829 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003830 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003831 return FAIL;
3832 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003833 if (ASCII_ISALPHA(*p) && p[1] == ':')
3834 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003835 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 ;
3837 if (*p != '(')
3838 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003839 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 return FAIL;
3841 }
3842 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003843 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 return FAIL;
3845 }
3846 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003847 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003849 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003850 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003851 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003852 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003853 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003854
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003855 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003856 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003857 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003858 // TODO: blob index
3859 // TODO: more arguments
3860 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003861 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003862 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003863 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003864
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003865 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003866 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003867 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003868 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003869 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003870 // missing first index is equal to zero
3871 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003872 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003873 else
3874 {
3875 if (compile_expr0(arg, cctx) == FAIL)
3876 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003877 if (**arg == ':')
3878 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003879 semsg(_(e_white_space_required_before_and_after_str_at_str),
3880 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003881 return FAIL;
3882 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003883 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003884 return FAIL;
3885 *arg = skipwhite(*arg);
3886 }
3887 if (**arg == ':')
3888 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003889 is_slice = TRUE;
3890 ++*arg;
3891 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3892 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003893 semsg(_(e_white_space_required_before_and_after_str_at_str),
3894 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003895 return FAIL;
3896 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003897 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003898 return FAIL;
3899 if (**arg == ']')
3900 // missing second index is equal to end of string
3901 generate_PUSHNR(cctx, -1);
3902 else
3903 {
3904 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003905 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003906 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003907 return FAIL;
3908 *arg = skipwhite(*arg);
3909 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911
3912 if (**arg != ']')
3913 {
3914 emsg(_(e_missbrac));
3915 return FAIL;
3916 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003917 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003919 // We can index a list and a dict. If we don't know the type
3920 // we can use the index value type.
3921 // TODO: If we don't know use an instruction to figure it out at
3922 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003923 typep = ((type_T **)stack->ga_data) + stack->ga_len
3924 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003925 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003926 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3927 // If the index is a string, the variable must be a Dict.
3928 if (*typep == &t_any && valtype == &t_string)
3929 vtype = VAR_DICT;
3930 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003931 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003932 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003933 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003934 return FAIL;
3935 if (is_slice)
3936 {
3937 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003938 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003939 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003940 return FAIL;
3941 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003942 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003943
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003944 if (vtype == VAR_DICT)
3945 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003946 if (is_slice)
3947 {
3948 emsg(_(e_cannot_slice_dictionary));
3949 return FAIL;
3950 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003951 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003952 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003953 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003954 if (*typep == &t_unknown)
3955 // empty dict was used
3956 *typep = &t_any;
3957 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003958 else
3959 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003960 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003961 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003962 return FAIL;
3963 *typep = &t_any;
3964 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003965 if (may_generate_2STRING(-1, cctx) == FAIL)
3966 return FAIL;
3967 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3968 return FAIL;
3969 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003970 else if (vtype == VAR_STRING)
3971 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003972 *typep = &t_string;
3973 if ((is_slice
3974 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3975 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003976 return FAIL;
3977 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003978 else if (vtype == VAR_BLOB)
3979 {
3980 emsg("Sorry, blob index and slice not implemented yet");
3981 return FAIL;
3982 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003983 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003984 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003985 if (is_slice)
3986 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003987 if (generate_instr_drop(cctx,
3988 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3989 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003990 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003991 }
Bram Moolenaared591872020-08-15 22:14:53 +02003992 else
3993 {
3994 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003995 {
Bram Moolenaared591872020-08-15 22:14:53 +02003996 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003997 if (*typep == &t_unknown)
3998 // empty list was used
3999 *typep = &t_any;
4000 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004001 if (generate_instr_drop(cctx,
4002 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4003 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004004 return FAIL;
4005 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004006 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004007 else
4008 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004009 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004010 return FAIL;
4011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004013 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004015 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004016 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004017 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004018 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004020 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004021 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004022 {
4023 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004024 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004025 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004026 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004027 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 while (eval_isnamec(*p))
4029 MB_PTR_ADV(p);
4030 if (p == *arg)
4031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004032 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 return FAIL;
4034 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004035 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 return FAIL;
4037 *arg = p;
4038 }
4039 else
4040 break;
4041 }
4042
4043 // TODO - see handle_subscript():
4044 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4045 // Don't do this when "Func" is already a partial that was bound
4046 // explicitly (pt_auto is FALSE).
4047
4048 return OK;
4049}
4050
4051/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004052 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4053 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004055 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004056 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004057 *
4058 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 */
4060
4061/*
4062 * number number constant
4063 * 0zFFFFFFFF Blob constant
4064 * "string" string constant
4065 * 'string' literal string constant
4066 * &option-name option value
4067 * @r register contents
4068 * identifier variable value
4069 * function() function call
4070 * $VAR environment variable
4071 * (expression) nested expression
4072 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004073 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 *
4075 * Also handle:
4076 * ! in front logical NOT
4077 * - in front unary minus
4078 * + in front unary plus (ignored)
4079 * trailing (arg) funcref/partial call
4080 * trailing [] subscript in String or List
4081 * trailing .name entry in Dictionary
4082 * trailing ->name() method call
4083 */
4084 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085compile_expr7(
4086 char_u **arg,
4087 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004088 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 char_u *start_leader, *end_leader;
4091 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004092 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004093 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004095 ppconst->pp_is_const = FALSE;
4096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 /*
4098 * Skip '!', '-' and '+' characters. They are handled later.
4099 */
4100 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004101 if (eval_leader(arg, TRUE) == FAIL)
4102 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 end_leader = *arg;
4104
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 switch (**arg)
4107 {
4108 /*
4109 * Number constant.
4110 */
4111 case '0': // also for blob starting with 0z
4112 case '1':
4113 case '2':
4114 case '3':
4115 case '4':
4116 case '5':
4117 case '6':
4118 case '7':
4119 case '8':
4120 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004121 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004123 // Apply "-" and "+" just before the number now, right to
4124 // left. Matters especially when "->" follows. Stops at
4125 // '!'.
4126 if (apply_leader(rettv, TRUE,
4127 start_leader, &end_leader) == FAIL)
4128 {
4129 clear_tv(rettv);
4130 return FAIL;
4131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 break;
4133
4134 /*
4135 * String constant: "string".
4136 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004137 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 return FAIL;
4139 break;
4140
4141 /*
4142 * Literal string constant: 'str''ing'.
4143 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004144 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 return FAIL;
4146 break;
4147
4148 /*
4149 * Constant Vim variable.
4150 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004151 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 ret = NOTDONE;
4153 break;
4154
4155 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156 * "true" constant
4157 */
4158 case 't': if (STRNCMP(*arg, "true", 4) == 0
4159 && !eval_isnamec((*arg)[4]))
4160 {
4161 *arg += 4;
4162 rettv->v_type = VAR_BOOL;
4163 rettv->vval.v_number = VVAL_TRUE;
4164 }
4165 else
4166 ret = NOTDONE;
4167 break;
4168
4169 /*
4170 * "false" constant
4171 */
4172 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4173 && !eval_isnamec((*arg)[5]))
4174 {
4175 *arg += 5;
4176 rettv->v_type = VAR_BOOL;
4177 rettv->vval.v_number = VVAL_FALSE;
4178 }
4179 else
4180 ret = NOTDONE;
4181 break;
4182
4183 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004184 * "null" constant
4185 */
4186 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4187 && !eval_isnamec((*arg)[5]))
4188 {
4189 *arg += 4;
4190 rettv->v_type = VAR_SPECIAL;
4191 rettv->vval.v_number = VVAL_NULL;
4192 }
4193 else
4194 ret = NOTDONE;
4195 break;
4196
4197 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 * List: [expr, expr]
4199 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004200 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4201 return FAIL;
4202 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 break;
4204
4205 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 * Dictionary: {'key': val, 'key': val}
4207 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004208 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4209 return FAIL;
4210 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 break;
4212
4213 /*
4214 * Option value: &name
4215 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004216 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4217 return FAIL;
4218 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 break;
4220
4221 /*
4222 * Environment variable: $VAR.
4223 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004224 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4225 return FAIL;
4226 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 break;
4228
4229 /*
4230 * Register contents: @r.
4231 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004232 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4233 return FAIL;
4234 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 break;
4236 /*
4237 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004238 * lambda: (arg, arg) => expr
4239 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004241 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4242 ret = compile_lambda(arg, cctx);
4243 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004244 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 break;
4246
4247 default: ret = NOTDONE;
4248 break;
4249 }
4250 if (ret == FAIL)
4251 return FAIL;
4252
Bram Moolenaar1c747212020-05-09 18:28:34 +02004253 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004255 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 clear_tv(rettv);
4257 else
4258 // A constant expression can possibly be handled compile time,
4259 // return the value instead of generating code.
4260 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 }
4262 else if (ret == NOTDONE)
4263 {
4264 char_u *p;
4265 int r;
4266
4267 if (!eval_isnamec1(**arg))
4268 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004269 if (ends_excmd(*skipwhite(*arg)))
4270 semsg(_(e_empty_expression_str), *arg);
4271 else
4272 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 return FAIL;
4274 }
4275
4276 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004277 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004279 {
4280 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004283 {
4284 if (generate_ppconst(cctx, ppconst) == FAIL)
4285 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004286 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 if (r == FAIL)
4289 return FAIL;
4290 }
4291
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004292 // Handle following "[]", ".member", etc.
4293 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004294 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295 ppconst) == FAIL)
4296 return FAIL;
4297 if (ppconst->pp_used > 0)
4298 {
4299 // apply the '!', '-' and '+' before the constant
4300 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004301 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004302 return FAIL;
4303 return OK;
4304 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004305 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004307 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308}
4309
4310/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004311 * Give the "white on both sides" error, taking the operator from "p[len]".
4312 */
4313 void
4314error_white_both(char_u *op, int len)
4315{
4316 char_u buf[10];
4317
4318 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004319 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004320}
4321
4322/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004323 * <type>expr7: runtime type check / conversion
4324 */
4325 static int
4326compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4327{
4328 type_T *want_type = NULL;
4329
4330 // Recognize <type>
4331 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4332 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004333 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004334 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4335 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004336 return FAIL;
4337
4338 if (**arg != '>')
4339 {
4340 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004341 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004342 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004343 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004344 return FAIL;
4345 }
4346 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004347 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004348 return FAIL;
4349 }
4350
4351 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4352 return FAIL;
4353
4354 if (want_type != NULL)
4355 {
4356 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004357 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004358 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004359
Bram Moolenaard1103582020-08-14 22:44:25 +02004360 generate_ppconst(cctx, ppconst);
4361 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004362 where.wt_index = 0;
4363 where.wt_variable = FALSE;
4364 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004365 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004366 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004367 return FAIL;
4368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 * * number multiplication
4376 * / number division
4377 * % number modulo
4378 */
4379 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381{
4382 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004383 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004384 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004386 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004387 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 return FAIL;
4389
4390 /*
4391 * Repeat computing, until no "*", "/" or "%" is following.
4392 */
4393 for (;;)
4394 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004395 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 if (*op != '*' && *op != '/' && *op != '%')
4397 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004398 if (next != NULL)
4399 {
4400 *arg = next_line_from_context(cctx, TRUE);
4401 op = skipwhite(*arg);
4402 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004403
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004404 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004406 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004407 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004409 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004410 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004412 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004413 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004415
4416 if (ppconst->pp_used == ppconst_used + 2
4417 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4418 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004419 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004420 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4421 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4422 varnumber_T res = 0;
4423 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004425 // both are numbers: compute the result
4426 switch (*op)
4427 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004428 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004429 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004430 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004431 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004432 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004433 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004434 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435 break;
4436 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004437 if (failed)
4438 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004439 tv1->vval.v_number = res;
4440 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004441 }
4442 else
4443 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004444 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004445 generate_two_op(cctx, op);
4446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 }
4448
4449 return OK;
4450}
4451
4452/*
4453 * + number addition
4454 * - number subtraction
4455 * .. string concatenation
4456 */
4457 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459{
4460 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004461 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464
4465 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 return FAIL;
4468
4469 /*
4470 * Repeat computing, until no "+", "-" or ".." is following.
4471 */
4472 for (;;)
4473 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004474 op = may_peek_next_line(cctx, *arg, &next);
4475 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 break;
4477 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004478 if (next != NULL)
4479 {
4480 *arg = next_line_from_context(cctx, TRUE);
4481 op = skipwhite(*arg);
4482 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004484 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004486 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004487 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 }
4489
Bram Moolenaare0de1712020-12-02 17:36:54 +01004490 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004491 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004493 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004494 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 return FAIL;
4496
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004498 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004499 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4500 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4501 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4502 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4505 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004506
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004507 // concat/subtract/add constant numbers
4508 if (*op == '+')
4509 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4510 else if (*op == '-')
4511 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4512 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004513 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004514 // concatenate constant strings
4515 char_u *s1 = tv1->vval.v_string;
4516 char_u *s2 = tv2->vval.v_string;
4517 size_t len1 = STRLEN(s1);
4518
4519 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4520 if (tv1->vval.v_string == NULL)
4521 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004523 return FAIL;
4524 }
4525 mch_memmove(tv1->vval.v_string, s1, len1);
4526 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004527 vim_free(s1);
4528 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004529 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 }
4532 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004533 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004534 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004535 if (*op == '.')
4536 {
4537 if (may_generate_2STRING(-2, cctx) == FAIL
4538 || may_generate_2STRING(-1, cctx) == FAIL)
4539 return FAIL;
4540 generate_instr_drop(cctx, ISN_CONCAT, 1);
4541 }
4542 else
4543 generate_two_op(cctx, op);
4544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546
4547 return OK;
4548}
4549
4550/*
4551 * expr5a == expr5b
4552 * expr5a =~ expr5b
4553 * expr5a != expr5b
4554 * expr5a !~ expr5b
4555 * expr5a > expr5b
4556 * expr5a >= expr5b
4557 * expr5a < expr5b
4558 * expr5a <= expr5b
4559 * expr5a is expr5b
4560 * expr5a isnot expr5b
4561 *
4562 * Produces instructions:
4563 * EVAL expr5a Push result of "expr5a"
4564 * EVAL expr5b Push result of "expr5b"
4565 * COMPARE one of the compare instructions
4566 */
4567 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004568compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004570 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004572 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576
4577 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004578 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 return FAIL;
4580
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004581 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004582 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583
4584 /*
4585 * If there is a comparative operator, use it.
4586 */
4587 if (type != EXPR_UNKNOWN)
4588 {
4589 int ic = FALSE; // Default: do not ignore case
4590
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004591 if (next != NULL)
4592 {
4593 *arg = next_line_from_context(cctx, TRUE);
4594 p = skipwhite(*arg);
4595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 if (type_is && (p[len] == '?' || p[len] == '#'))
4597 {
4598 semsg(_(e_invexpr2), *arg);
4599 return FAIL;
4600 }
4601 // extra question mark appended: ignore case
4602 if (p[len] == '?')
4603 {
4604 ic = TRUE;
4605 ++len;
4606 }
4607 // extra '#' appended: match case (ignored)
4608 else if (p[len] == '#')
4609 ++len;
4610 // nothing appended: match case
4611
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004612 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004614 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 }
4617
4618 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004619 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004620 return FAIL;
4621
Bram Moolenaara5565e42020-05-09 15:44:01 +02004622 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 return FAIL;
4624
Bram Moolenaara5565e42020-05-09 15:44:01 +02004625 if (ppconst->pp_used == ppconst_used + 2)
4626 {
4627 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4628 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4629 int ret;
4630
4631 // Both sides are a constant, compute the result now.
4632 // First check for a valid combination of types, this is more
4633 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004634 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 ret = FAIL;
4636 else
4637 {
4638 ret = typval_compare(tv1, tv2, type, ic);
4639 tv1->v_type = VAR_BOOL;
4640 tv1->vval.v_number = tv1->vval.v_number
4641 ? VVAL_TRUE : VVAL_FALSE;
4642 clear_tv(tv2);
4643 --ppconst->pp_used;
4644 }
4645 return ret;
4646 }
4647
4648 generate_ppconst(cctx, ppconst);
4649 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 }
4651
4652 return OK;
4653}
4654
Bram Moolenaar7f141552020-05-09 17:35:53 +02004655static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657/*
4658 * Compile || or &&.
4659 */
4660 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661compile_and_or(
4662 char_u **arg,
4663 cctx_T *cctx,
4664 char *op,
4665 ppconst_T *ppconst,
4666 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004668 char_u *next;
4669 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 int opchar = *op;
4671
4672 if (p[0] == opchar && p[1] == opchar)
4673 {
4674 garray_T *instr = &cctx->ctx_instr;
4675 garray_T end_ga;
4676
4677 /*
4678 * Repeat until there is no following "||" or "&&"
4679 */
4680 ga_init2(&end_ga, sizeof(int), 10);
4681 while (p[0] == opchar && p[1] == opchar)
4682 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004683 if (next != NULL)
4684 {
4685 *arg = next_line_from_context(cctx, TRUE);
4686 p = skipwhite(*arg);
4687 }
4688
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004689 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4690 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004691 semsg(_(e_white_space_required_before_and_after_str_at_str),
4692 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004693 return FAIL;
4694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004696 // TODO: use ppconst if the value is a constant and check
4697 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 generate_ppconst(cctx, ppconst);
4699
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004700 // Every part must evaluate to a bool.
4701 if (bool_on_stack(cctx) == FAIL)
4702 {
4703 ga_clear(&end_ga);
4704 return FAIL;
4705 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 if (ga_grow(&end_ga, 1) == FAIL)
4708 {
4709 ga_clear(&end_ga);
4710 return FAIL;
4711 }
4712 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4713 ++end_ga.ga_len;
4714 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004715 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716
4717 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004718 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004719 {
4720 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004721 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004722 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004723
Bram Moolenaara5565e42020-05-09 15:44:01 +02004724 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4725 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 {
4727 ga_clear(&end_ga);
4728 return FAIL;
4729 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004730
4731 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004733 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004735 // Every part must evaluate to a bool.
4736 if (bool_on_stack(cctx) == FAIL)
4737 {
4738 ga_clear(&end_ga);
4739 return FAIL;
4740 }
4741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 // Fill in the end label in all jumps.
4743 while (end_ga.ga_len > 0)
4744 {
4745 isn_T *isn;
4746
4747 --end_ga.ga_len;
4748 isn = ((isn_T *)instr->ga_data)
4749 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4750 isn->isn_arg.jump.jump_where = instr->ga_len;
4751 }
4752 ga_clear(&end_ga);
4753 }
4754
4755 return OK;
4756}
4757
4758/*
4759 * expr4a && expr4a && expr4a logical AND
4760 *
4761 * Produces instructions:
4762 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004763 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004764 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004766 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 * EVAL expr4c Push result of "expr4c"
4768 * end:
4769 */
4770 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 int ppconst_used = ppconst->pp_used;
4774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004776 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 return FAIL;
4778
4779 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004780 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781}
4782
4783/*
4784 * expr3a || expr3b || expr3c logical OR
4785 *
4786 * Produces instructions:
4787 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004788 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004789 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004791 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 * EVAL expr3c Push result of "expr3c"
4793 * end:
4794 */
4795 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004798 int ppconst_used = ppconst->pp_used;
4799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004801 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 return FAIL;
4803
4804 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004805 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806}
4807
4808/*
4809 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004811 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 * JUMP_IF_FALSE alt jump if false
4813 * EVAL expr1a
4814 * JUMP_ALWAYS end
4815 * alt: EVAL expr1b
4816 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004817 *
4818 * Toplevel expression: expr2 ?? expr1
4819 * Produces instructions:
4820 * EVAL expr2 Push result of "expr2"
4821 * JUMP_AND_KEEP_IF_TRUE end jump if true
4822 * EVAL expr1
4823 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 */
4825 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004826compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004827{
4828 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004829 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004830 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831
Bram Moolenaar3988f642020-08-27 22:43:03 +02004832 // Ignore all kinds of errors when not producing code.
4833 if (cctx->ctx_skip == SKIP_YES)
4834 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004835 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004836 return OK;
4837 }
4838
Bram Moolenaar61a89812020-05-07 16:58:17 +02004839 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004840 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 return FAIL;
4842
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004843 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 if (*p == '?')
4845 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004846 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 garray_T *instr = &cctx->ctx_instr;
4848 garray_T *stack = &cctx->ctx_type_stack;
4849 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004850 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004852 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004853 int has_const_expr = FALSE;
4854 int const_value = FALSE;
4855 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004857 if (next != NULL)
4858 {
4859 *arg = next_line_from_context(cctx, TRUE);
4860 p = skipwhite(*arg);
4861 }
4862
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004863 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004864 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004865 semsg(_(e_white_space_required_before_and_after_str_at_str),
4866 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004867 return FAIL;
4868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
Bram Moolenaara5565e42020-05-09 15:44:01 +02004870 if (ppconst->pp_used == ppconst_used + 1)
4871 {
4872 // the condition is a constant, we know whether the ? or the :
4873 // expression is to be evaluated.
4874 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004875 if (op_falsy)
4876 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4877 else
4878 {
4879 int error = FALSE;
4880
4881 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4882 &error);
4883 if (error)
4884 return FAIL;
4885 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004886 cctx->ctx_skip = save_skip == SKIP_YES ||
4887 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4888
4889 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4890 // "left ?? right" and "left" is truthy: produce "left"
4891 generate_ppconst(cctx, ppconst);
4892 else
4893 {
4894 clear_tv(&ppconst->pp_tv[ppconst_used]);
4895 --ppconst->pp_used;
4896 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004897 }
4898 else
4899 {
4900 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004901 if (op_falsy)
4902 end_idx = instr->ga_len;
4903 generate_JUMP(cctx, op_falsy
4904 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4905 if (op_falsy)
4906 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908
4909 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004910 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004911 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004913 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914
Bram Moolenaara5565e42020-05-09 15:44:01 +02004915 if (!has_const_expr)
4916 {
4917 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004919 if (!op_falsy)
4920 {
4921 // remember the type and drop it
4922 --stack->ga_len;
4923 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004925 end_idx = instr->ga_len;
4926 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004927
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004928 // jump here from JUMP_IF_FALSE
4929 isn = ((isn_T *)instr->ga_data) + alt_idx;
4930 isn->isn_arg.jump.jump_where = instr->ga_len;
4931 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004934 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004936 // Check for the ":".
4937 p = may_peek_next_line(cctx, *arg, &next);
4938 if (*p != ':')
4939 {
4940 emsg(_(e_missing_colon));
4941 return FAIL;
4942 }
4943 if (next != NULL)
4944 {
4945 *arg = next_line_from_context(cctx, TRUE);
4946 p = skipwhite(*arg);
4947 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004948
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004949 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4950 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004951 semsg(_(e_white_space_required_before_and_after_str_at_str),
4952 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004953 return FAIL;
4954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004956 // evaluate the third expression
4957 if (has_const_expr)
4958 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004959 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004960 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004961 return FAIL;
4962 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4963 return FAIL;
4964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965
Bram Moolenaara5565e42020-05-09 15:44:01 +02004966 if (!has_const_expr)
4967 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004968 type_T **typep;
4969
Bram Moolenaara5565e42020-05-09 15:44:01 +02004970 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
Bram Moolenaara5565e42020-05-09 15:44:01 +02004972 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004973 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4974 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004975
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004976 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004977 isn = ((isn_T *)instr->ga_data) + end_idx;
4978 isn->isn_arg.jump.jump_where = instr->ga_len;
4979 }
4980
4981 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 }
4983 return OK;
4984}
4985
4986/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004988 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4989 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 */
4991 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004992compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004993{
4994 ppconst_T ppconst;
4995
4996 CLEAR_FIELD(ppconst);
4997 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4998 {
4999 clear_ppconst(&ppconst);
5000 return FAIL;
5001 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005002 if (is_const != NULL)
5003 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 if (generate_ppconst(cctx, &ppconst) == FAIL)
5005 return FAIL;
5006 return OK;
5007}
5008
5009/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005010 * Toplevel expression.
5011 */
5012 static int
5013compile_expr0(char_u **arg, cctx_T *cctx)
5014{
5015 return compile_expr0_ext(arg, cctx, NULL);
5016}
5017
5018/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 * compile "return [expr]"
5020 */
5021 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005022compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023{
5024 char_u *p = arg;
5025 garray_T *stack = &cctx->ctx_type_stack;
5026 type_T *stack_type;
5027
5028 if (*p != NUL && *p != '|' && *p != '\n')
5029 {
5030 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 return NULL;
5033
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005034 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005035 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005036 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005037 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005038 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5039 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005040 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005041 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005042 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005043 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005044 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005045 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5046 && stack_type->tt_type != VAR_VOID
5047 && stack_type->tt_type != VAR_UNKNOWN)
5048 {
5049 emsg(_(e_returning_value_in_function_without_return_type));
5050 return NULL;
5051 }
5052 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005053 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005054 return NULL;
5055 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 }
5058 else
5059 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005060 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005061 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005062 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5063 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005065 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 return NULL;
5067 }
5068
5069 // No argument, return zero.
5070 generate_PUSHNR(cctx, 0);
5071 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005072
5073 // Undo any command modifiers.
5074 generate_undo_cmdmods(cctx);
5075
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005076 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 return NULL;
5078
5079 // "return val | endif" is possible
5080 return skipwhite(p);
5081}
5082
5083/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005084 * Get a line from the compilation context, compatible with exarg_T getline().
5085 * Return a pointer to the line in allocated memory.
5086 * Return NULL for end-of-file or some error.
5087 */
5088 static char_u *
5089exarg_getline(
5090 int c UNUSED,
5091 void *cookie,
5092 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005093 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005094{
5095 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005096 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005097
Bram Moolenaar66250c92020-08-20 15:02:42 +02005098 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005099 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005100 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005101 return NULL;
5102 ++cctx->ctx_lnum;
5103 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5104 // Comment lines result in NULL pointers, skip them.
5105 if (p != NULL)
5106 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005107 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005108}
5109
5110/*
5111 * Compile a nested :def command.
5112 */
5113 static char_u *
5114compile_nested_function(exarg_T *eap, cctx_T *cctx)
5115{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005116 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005117 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005118 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005119 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005120 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005121 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005122
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005123 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005124 {
5125 emsg(_(e_cannot_use_bang_with_nested_def));
5126 return NULL;
5127 }
5128
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005129 if (*name_start == '/')
5130 {
5131 name_end = skip_regexp(name_start + 1, '/', TRUE);
5132 if (*name_end == '/')
5133 ++name_end;
5134 eap->nextcmd = check_nextcmd(name_end);
5135 }
5136 if (name_end == name_start || *skipwhite(name_end) != '(')
5137 {
5138 if (!ends_excmd2(name_start, name_end))
5139 {
5140 semsg(_(e_invalid_command_str), eap->cmd);
5141 return NULL;
5142 }
5143
5144 // "def" or "def Name": list functions
5145 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5146 return NULL;
5147 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5148 }
5149
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005150 // Only g:Func() can use a namespace.
5151 if (name_start[1] == ':' && !is_global)
5152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005153 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005154 return NULL;
5155 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005156 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005157 return NULL;
5158
Bram Moolenaar04b12692020-05-04 23:24:44 +02005159 eap->arg = name_end;
5160 eap->getline = exarg_getline;
5161 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005162 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005163 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005164 lambda_name = vim_strsave(get_lambda_name());
5165 if (lambda_name == NULL)
5166 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005167 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005168
Bram Moolenaar822ba242020-05-24 23:00:18 +02005169 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005170 {
5171 r = eap->skip ? OK : FAIL;
5172 goto theend;
5173 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005174 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5175 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005176 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005177 {
5178 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005179 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005180 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005181
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005182 if (is_global)
5183 {
5184 char_u *func_name = vim_strnsave(name_start + 2,
5185 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005186
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005187 if (func_name == NULL)
5188 r = FAIL;
5189 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005190 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005191 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005192 lambda_name = NULL;
5193 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005194 }
5195 else
5196 {
5197 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005198 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005199 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005200 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005201
Bram Moolenaareef21022020-08-01 22:16:43 +02005202 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005203 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005204 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005205 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005206 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005207
5208 // copy over the block scope IDs
5209 if (block_depth > 0)
5210 {
5211 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5212 if (ufunc->uf_block_ids != NULL)
5213 {
5214 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5215 sizeof(int) * block_depth);
5216 ufunc->uf_block_depth = block_depth;
5217 }
5218 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005219 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005220 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005221
5222theend:
5223 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005224 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005225}
5226
5227/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 * Return the length of an assignment operator, or zero if there isn't one.
5229 */
5230 int
5231assignment_len(char_u *p, int *heredoc)
5232{
5233 if (*p == '=')
5234 {
5235 if (p[1] == '<' && p[2] == '<')
5236 {
5237 *heredoc = TRUE;
5238 return 3;
5239 }
5240 return 1;
5241 }
5242 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5243 return 2;
5244 if (STRNCMP(p, "..=", 3) == 0)
5245 return 3;
5246 return 0;
5247}
5248
5249// words that cannot be used as a variable
5250static char *reserved[] = {
5251 "true",
5252 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005253 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005254 NULL
5255};
5256
Bram Moolenaar752fc692021-01-04 21:57:11 +01005257// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005258typedef enum {
5259 dest_local,
5260 dest_option,
5261 dest_env,
5262 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005263 dest_buffer,
5264 dest_window,
5265 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005266 dest_vimvar,
5267 dest_script,
5268 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005269 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005270} assign_dest_T;
5271
Bram Moolenaar752fc692021-01-04 21:57:11 +01005272// Used by compile_lhs() to store information about the LHS of an assignment
5273// and one argument of ":unlet" with an index.
5274typedef struct {
5275 assign_dest_T lhs_dest; // type of destination
5276
5277 char_u *lhs_name; // allocated name including
5278 // "[expr]" or ".name".
5279 size_t lhs_varlen; // length of the variable without
5280 // "[expr]" or ".name"
5281 char_u *lhs_dest_end; // end of the destination, including
5282 // "[expr]" or ".name".
5283
5284 int lhs_has_index; // has "[expr]" or ".name"
5285
5286 int lhs_new_local; // create new local variable
5287 int lhs_opt_flags; // for when destination is an option
5288 int lhs_vimvaridx; // for when destination is a v:var
5289
5290 lvar_T lhs_local_lvar; // used for existing local destination
5291 lvar_T lhs_arg_lvar; // used for argument destination
5292 lvar_T *lhs_lvar; // points to destination lvar
5293 int lhs_scriptvar_sid;
5294 int lhs_scriptvar_idx;
5295
5296 int lhs_has_type; // type was specified
5297 type_T *lhs_type;
5298 type_T *lhs_member_type;
5299} lhs_T;
5300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005302 * Generate the load instruction for "name".
5303 */
5304 static void
5305generate_loadvar(
5306 cctx_T *cctx,
5307 assign_dest_T dest,
5308 char_u *name,
5309 lvar_T *lvar,
5310 type_T *type)
5311{
5312 switch (dest)
5313 {
5314 case dest_option:
5315 // TODO: check the option exists
5316 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5317 break;
5318 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005319 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5320 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5321 else
5322 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 break;
5324 case dest_buffer:
5325 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5326 break;
5327 case dest_window:
5328 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5329 break;
5330 case dest_tab:
5331 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5332 break;
5333 case dest_script:
5334 compile_load_scriptvar(cctx,
5335 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5336 break;
5337 case dest_env:
5338 // Include $ in the name here
5339 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5340 break;
5341 case dest_reg:
5342 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5343 break;
5344 case dest_vimvar:
5345 generate_LOADV(cctx, name + 2, TRUE);
5346 break;
5347 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005348 if (lvar->lv_from_outer > 0)
5349 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5350 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005351 else
5352 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5353 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005354 case dest_expr:
5355 // list or dict value should already be on the stack.
5356 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005357 }
5358}
5359
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005360/*
5361 * Skip over "[expr]" or ".member".
5362 * Does not check for any errors.
5363 */
5364 static char_u *
5365skip_index(char_u *start)
5366{
5367 char_u *p = start;
5368
5369 if (*p == '[')
5370 {
5371 p = skipwhite(p + 1);
5372 (void)skip_expr(&p, NULL);
5373 p = skipwhite(p);
5374 if (*p == ']')
5375 return p + 1;
5376 return p;
5377 }
5378 // if (*p == '.')
5379 return to_name_end(p + 1, TRUE);
5380}
5381
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005382 void
5383vim9_declare_error(char_u *name)
5384{
5385 char *scope = "";
5386
5387 switch (*name)
5388 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005389 case 'g': scope = _("global"); break;
5390 case 'b': scope = _("buffer"); break;
5391 case 'w': scope = _("window"); break;
5392 case 't': scope = _("tab"); break;
5393 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005394 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005395 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005396 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005397 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005398 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005399 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005400 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005401 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005402 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005403}
5404
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005405/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005406 * For one assignment figure out the type of destination. Return it in "dest".
5407 * When not recognized "dest" is not set.
5408 * For an option "opt_flags" is set.
5409 * For a v:var "vimvaridx" is set.
5410 * "type" is set to the destination type if known, unchanted otherwise.
5411 * Return FAIL if an error message was given.
5412 */
5413 static int
5414get_var_dest(
5415 char_u *name,
5416 assign_dest_T *dest,
5417 int cmdidx,
5418 int *opt_flags,
5419 int *vimvaridx,
5420 type_T **type,
5421 cctx_T *cctx)
5422{
5423 char_u *p;
5424
5425 if (*name == '&')
5426 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005427 int cc;
5428 long numval;
5429 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005430
5431 *dest = dest_option;
5432 if (cmdidx == CMD_final || cmdidx == CMD_const)
5433 {
5434 emsg(_(e_const_option));
5435 return FAIL;
5436 }
5437 p = name;
5438 p = find_option_end(&p, opt_flags);
5439 if (p == NULL)
5440 {
5441 // cannot happen?
5442 emsg(_(e_letunexp));
5443 return FAIL;
5444 }
5445 cc = *p;
5446 *p = NUL;
5447 opt_type = get_option_value(skip_option_env_lead(name),
5448 &numval, NULL, *opt_flags);
5449 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005450 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005451 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005452 case gov_unknown:
5453 semsg(_(e_unknown_option), name);
5454 return FAIL;
5455 case gov_string:
5456 case gov_hidden_string:
5457 *type = &t_string;
5458 break;
5459 case gov_bool:
5460 case gov_hidden_bool:
5461 *type = &t_bool;
5462 break;
5463 case gov_number:
5464 case gov_hidden_number:
5465 *type = &t_number;
5466 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005467 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005468 }
5469 else if (*name == '$')
5470 {
5471 *dest = dest_env;
5472 *type = &t_string;
5473 }
5474 else if (*name == '@')
5475 {
5476 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5477 {
5478 emsg_invreg(name[1]);
5479 return FAIL;
5480 }
5481 *dest = dest_reg;
5482 *type = &t_string;
5483 }
5484 else if (STRNCMP(name, "g:", 2) == 0)
5485 {
5486 *dest = dest_global;
5487 }
5488 else if (STRNCMP(name, "b:", 2) == 0)
5489 {
5490 *dest = dest_buffer;
5491 }
5492 else if (STRNCMP(name, "w:", 2) == 0)
5493 {
5494 *dest = dest_window;
5495 }
5496 else if (STRNCMP(name, "t:", 2) == 0)
5497 {
5498 *dest = dest_tab;
5499 }
5500 else if (STRNCMP(name, "v:", 2) == 0)
5501 {
5502 typval_T *vtv;
5503 int di_flags;
5504
5505 *vimvaridx = find_vim_var(name + 2, &di_flags);
5506 if (*vimvaridx < 0)
5507 {
5508 semsg(_(e_variable_not_found_str), name);
5509 return FAIL;
5510 }
5511 // We use the current value of "sandbox" here, is that OK?
5512 if (var_check_ro(di_flags, name, FALSE))
5513 return FAIL;
5514 *dest = dest_vimvar;
5515 vtv = get_vim_var_tv(*vimvaridx);
5516 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5517 }
5518 return OK;
5519}
5520
5521/*
5522 * Generate a STORE instruction for "dest", not being "dest_local".
5523 * Return FAIL when out of memory.
5524 */
5525 static int
5526generate_store_var(
5527 cctx_T *cctx,
5528 assign_dest_T dest,
5529 int opt_flags,
5530 int vimvaridx,
5531 int scriptvar_idx,
5532 int scriptvar_sid,
5533 type_T *type,
5534 char_u *name)
5535{
5536 switch (dest)
5537 {
5538 case dest_option:
5539 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5540 opt_flags);
5541 case dest_global:
5542 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005543 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5544 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005545 case dest_buffer:
5546 // include b: with the name, easier to execute that way
5547 return generate_STORE(cctx, ISN_STOREB, 0, name);
5548 case dest_window:
5549 // include w: with the name, easier to execute that way
5550 return generate_STORE(cctx, ISN_STOREW, 0, name);
5551 case dest_tab:
5552 // include t: with the name, easier to execute that way
5553 return generate_STORE(cctx, ISN_STORET, 0, name);
5554 case dest_env:
5555 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5556 case dest_reg:
5557 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5558 case dest_vimvar:
5559 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5560 case dest_script:
5561 if (scriptvar_idx < 0)
5562 {
5563 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005564 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005565
Bram Moolenaar41d61962020-12-06 20:12:43 +01005566 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005567 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5568 scriptvar_sid, type);
5569 if (name_s != name)
5570 vim_free(name_s);
5571 return r;
5572 }
5573 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5574 scriptvar_sid, scriptvar_idx, type);
5575 case dest_local:
5576 case dest_expr:
5577 // cannot happen
5578 break;
5579 }
5580 return FAIL;
5581}
5582
Bram Moolenaar752fc692021-01-04 21:57:11 +01005583 static int
5584is_decl_command(int cmdidx)
5585{
5586 return cmdidx == CMD_let || cmdidx == CMD_var
5587 || cmdidx == CMD_final || cmdidx == CMD_const;
5588}
5589
5590/*
5591 * Figure out the LHS type and other properties for an assignment or one item
5592 * of ":unlet" with an index.
5593 * Returns OK or FAIL.
5594 */
5595 static int
5596compile_lhs(
5597 char_u *var_start,
5598 lhs_T *lhs,
5599 int cmdidx,
5600 int heredoc,
5601 int oplen,
5602 cctx_T *cctx)
5603{
5604 char_u *var_end;
5605 int is_decl = is_decl_command(cmdidx);
5606
5607 CLEAR_POINTER(lhs);
5608 lhs->lhs_dest = dest_local;
5609 lhs->lhs_vimvaridx = -1;
5610 lhs->lhs_scriptvar_idx = -1;
5611
5612 // "dest_end" is the end of the destination, including "[expr]" or
5613 // ".name".
5614 // "var_end" is the end of the variable/option/etc. name.
5615 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5616 if (*var_start == '@')
5617 var_end = var_start + 2;
5618 else
5619 {
5620 // skip over the leading "&", "&l:", "&g:" and "$"
5621 var_end = skip_option_env_lead(var_start);
5622 var_end = to_name_end(var_end, TRUE);
5623 }
5624
5625 // "a: type" is declaring variable "a" with a type, not dict "a:".
5626 if (is_decl && lhs->lhs_dest_end == var_start + 2
5627 && lhs->lhs_dest_end[-1] == ':')
5628 --lhs->lhs_dest_end;
5629 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5630 --var_end;
5631
5632 // compute the length of the destination without "[expr]" or ".name"
5633 lhs->lhs_varlen = var_end - var_start;
5634 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5635 if (lhs->lhs_name == NULL)
5636 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005637
5638 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5639 // Something follows after the variable: "var[idx]" or "var.key".
5640 lhs->lhs_has_index = TRUE;
5641
Bram Moolenaar752fc692021-01-04 21:57:11 +01005642 if (heredoc)
5643 lhs->lhs_type = &t_list_string;
5644 else
5645 lhs->lhs_type = &t_any;
5646
5647 if (cctx->ctx_skip != SKIP_YES)
5648 {
5649 int declare_error = FALSE;
5650
5651 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5652 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5653 &lhs->lhs_type, cctx) == FAIL)
5654 return FAIL;
5655 if (lhs->lhs_dest != dest_local)
5656 {
5657 // Specific kind of variable recognized.
5658 declare_error = is_decl;
5659 }
5660 else
5661 {
5662 int idx;
5663
5664 // No specific kind of variable recognized, just a name.
5665 for (idx = 0; reserved[idx] != NULL; ++idx)
5666 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5667 {
5668 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5669 return FAIL;
5670 }
5671
5672
5673 if (lookup_local(var_start, lhs->lhs_varlen,
5674 &lhs->lhs_local_lvar, cctx) == OK)
5675 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5676 else
5677 {
5678 CLEAR_FIELD(lhs->lhs_arg_lvar);
5679 if (arg_exists(var_start, lhs->lhs_varlen,
5680 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5681 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5682 {
5683 if (is_decl)
5684 {
5685 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5686 return FAIL;
5687 }
5688 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5689 }
5690 }
5691 if (lhs->lhs_lvar != NULL)
5692 {
5693 if (is_decl)
5694 {
5695 semsg(_(e_variable_already_declared), lhs->lhs_name);
5696 return FAIL;
5697 }
5698 }
5699 else
5700 {
5701 int script_namespace = lhs->lhs_varlen > 1
5702 && STRNCMP(var_start, "s:", 2) == 0;
5703 int script_var = (script_namespace
5704 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5705 FALSE, cctx)
5706 : script_var_exists(var_start, lhs->lhs_varlen,
5707 TRUE, cctx)) == OK;
5708 imported_T *import =
5709 find_imported(var_start, lhs->lhs_varlen, cctx);
5710
5711 if (script_namespace || script_var || import != NULL)
5712 {
5713 char_u *rawname = lhs->lhs_name
5714 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5715
5716 if (is_decl)
5717 {
5718 if (script_namespace)
5719 semsg(_(e_cannot_declare_script_variable_in_function),
5720 lhs->lhs_name);
5721 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005722 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005723 lhs->lhs_name);
5724 return FAIL;
5725 }
5726 else if (cctx->ctx_ufunc->uf_script_ctx_version
5727 == SCRIPT_VERSION_VIM9
5728 && script_namespace
5729 && !script_var && import == NULL)
5730 {
5731 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5732 return FAIL;
5733 }
5734
5735 lhs->lhs_dest = dest_script;
5736
5737 // existing script-local variables should have a type
5738 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5739 if (import != NULL)
5740 lhs->lhs_scriptvar_sid = import->imp_sid;
5741 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5742 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005743 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005744 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005745 lhs->lhs_scriptvar_sid, rawname,
5746 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5747 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005748 if (lhs->lhs_scriptvar_idx >= 0)
5749 {
5750 scriptitem_T *si = SCRIPT_ITEM(
5751 lhs->lhs_scriptvar_sid);
5752 svar_T *sv =
5753 ((svar_T *)si->sn_var_vals.ga_data)
5754 + lhs->lhs_scriptvar_idx;
5755 lhs->lhs_type = sv->sv_type;
5756 }
5757 }
5758 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005759 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005760 == FAIL)
5761 return FAIL;
5762 }
5763 }
5764
5765 if (declare_error)
5766 {
5767 vim9_declare_error(lhs->lhs_name);
5768 return FAIL;
5769 }
5770 }
5771
5772 // handle "a:name" as a name, not index "name" on "a"
5773 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5774 var_end = lhs->lhs_dest_end;
5775
5776 if (lhs->lhs_dest != dest_option)
5777 {
5778 if (is_decl && *var_end == ':')
5779 {
5780 char_u *p;
5781
5782 // parse optional type: "let var: type = expr"
5783 if (!VIM_ISWHITE(var_end[1]))
5784 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005785 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005786 return FAIL;
5787 }
5788 p = skipwhite(var_end + 1);
5789 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5790 if (lhs->lhs_type == NULL)
5791 return FAIL;
5792 lhs->lhs_has_type = TRUE;
5793 }
5794 else if (lhs->lhs_lvar != NULL)
5795 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5796 }
5797
5798 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5799 && lhs->lhs_type->tt_type != VAR_STRING
5800 && lhs->lhs_type->tt_type != VAR_ANY)
5801 {
5802 emsg(_(e_can_only_concatenate_to_string));
5803 return FAIL;
5804 }
5805
5806 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5807 && cctx->ctx_skip != SKIP_YES)
5808 {
5809 if (oplen > 1 && !heredoc)
5810 {
5811 // +=, /=, etc. require an existing variable
5812 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5813 return FAIL;
5814 }
5815 if (!is_decl)
5816 {
5817 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5818 return FAIL;
5819 }
5820
5821 // new local variable
5822 if ((lhs->lhs_type->tt_type == VAR_FUNC
5823 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5824 && var_wrong_func_name(lhs->lhs_name, TRUE))
5825 return FAIL;
5826 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5827 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5828 if (lhs->lhs_lvar == NULL)
5829 return FAIL;
5830 lhs->lhs_new_local = TRUE;
5831 }
5832
5833 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005834 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005835 {
5836 // Something follows after the variable: "var[idx]" or "var.key".
5837 // TODO: should we also handle "->func()" here?
5838 if (is_decl)
5839 {
5840 emsg(_(e_cannot_use_index_when_declaring_variable));
5841 return FAIL;
5842 }
5843
5844 if (var_start[lhs->lhs_varlen] == '['
5845 || var_start[lhs->lhs_varlen] == '.')
5846 {
5847 char_u *after = var_start + lhs->lhs_varlen;
5848 char_u *p;
5849
5850 // Only the last index is used below, if there are others
5851 // before it generate code for the expression. Thus for
5852 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5853 for (;;)
5854 {
5855 p = skip_index(after);
5856 if (*p != '[' && *p != '.')
5857 break;
5858 after = p;
5859 }
5860 if (after > var_start + lhs->lhs_varlen)
5861 {
5862 lhs->lhs_varlen = after - var_start;
5863 lhs->lhs_dest = dest_expr;
5864 // We don't know the type before evaluating the expression,
5865 // use "any" until then.
5866 lhs->lhs_type = &t_any;
5867 }
5868
Bram Moolenaar752fc692021-01-04 21:57:11 +01005869 if (lhs->lhs_type->tt_member == NULL)
5870 lhs->lhs_member_type = &t_any;
5871 else
5872 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5873 }
5874 else
5875 {
5876 semsg("Not supported yet: %s", var_start);
5877 return FAIL;
5878 }
5879 }
5880 return OK;
5881}
5882
5883/*
5884 * Assignment to a list or dict member, or ":unlet" for the item, using the
5885 * information in "lhs".
5886 * Returns OK or FAIL.
5887 */
5888 static int
5889compile_assign_unlet(
5890 char_u *var_start,
5891 lhs_T *lhs,
5892 int is_assign,
5893 type_T *rhs_type,
5894 cctx_T *cctx)
5895{
5896 char_u *p;
5897 int r;
5898 vartype_T dest_type;
5899 size_t varlen = lhs->lhs_varlen;
5900 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005901 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005902
5903 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5904 p = var_start + varlen;
5905 if (*p == '[')
5906 {
5907 p = skipwhite(p + 1);
5908 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005909
5910 if (r == OK && *skipwhite(p) == ':')
5911 {
5912 // unlet var[idx : idx]
5913 if (is_assign)
5914 {
5915 semsg(_(e_cannot_use_range_with_assignment_str), p);
5916 return FAIL;
5917 }
5918 range = TRUE;
5919 p = skipwhite(p);
5920 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5921 {
5922 semsg(_(e_white_space_required_before_and_after_str_at_str),
5923 ":", p);
5924 return FAIL;
5925 }
5926 p = skipwhite(p + 1);
5927 r = compile_expr0(&p, cctx);
5928 }
5929
Bram Moolenaar752fc692021-01-04 21:57:11 +01005930 if (r == OK && *skipwhite(p) != ']')
5931 {
5932 // this should not happen
5933 emsg(_(e_missbrac));
5934 r = FAIL;
5935 }
5936 }
5937 else // if (*p == '.')
5938 {
5939 char_u *key_end = to_name_end(p + 1, TRUE);
5940 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5941
5942 r = generate_PUSHS(cctx, key);
5943 }
5944 if (r == FAIL)
5945 return FAIL;
5946
5947 if (lhs->lhs_type == &t_any)
5948 {
5949 // Index on variable of unknown type: check at runtime.
5950 dest_type = VAR_ANY;
5951 }
5952 else
5953 {
5954 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005955 if (dest_type == VAR_DICT && range)
5956 {
5957 emsg(e_cannot_use_range_with_dictionary);
5958 return FAIL;
5959 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005960 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5961 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005962 if (dest_type == VAR_LIST)
5963 {
5964 if (range
5965 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005966 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005967 return FAIL;
5968 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5969 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
5970 return FAIL;
5971 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005972 }
5973
5974 // Load the dict or list. On the stack we then have:
5975 // - value (for assignment, not for :unlet)
5976 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005977 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01005978 // - variable
5979 if (lhs->lhs_dest == dest_expr)
5980 {
5981 int c = var_start[varlen];
5982
5983 // Evaluate "ll[expr]" of "ll[expr][idx]"
5984 p = var_start;
5985 var_start[varlen] = NUL;
5986 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5987 {
5988 // this should not happen
5989 emsg(_(e_missbrac));
5990 return FAIL;
5991 }
5992 var_start[varlen] = c;
5993
5994 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5995 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5996 // now we can properly check the type
5997 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005998 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005999 FALSE, FALSE) == FAIL)
6000 return FAIL;
6001 }
6002 else
6003 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6004 lhs->lhs_lvar, lhs->lhs_type);
6005
6006 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6007 {
6008 if (is_assign)
6009 {
6010 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6011
6012 if (isn == NULL)
6013 return FAIL;
6014 isn->isn_arg.vartype = dest_type;
6015 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006016 else if (range)
6017 {
6018 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6019 return FAIL;
6020 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006021 else
6022 {
6023 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6024 return FAIL;
6025 }
6026 }
6027 else
6028 {
6029 emsg(_(e_indexable_type_required));
6030 return FAIL;
6031 }
6032
6033 return OK;
6034}
6035
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006036/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006037 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006038 * "let name"
6039 * "var name = expr"
6040 * "final name = expr"
6041 * "const name = expr"
6042 * "name = expr"
6043 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006044 * Return NULL for an error.
6045 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006046 */
6047 static char_u *
6048compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6049{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006050 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006052 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 char_u *ret = NULL;
6054 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006055 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006058 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 int oplen = 0;
6061 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006062 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006063 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064 int is_decl = is_decl_command(cmdidx);
6065 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006067 // Skip over the "var" or "[var, var]" to get to any "=".
6068 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6069 if (p == NULL)
6070 return *arg == '[' ? arg : NULL;
6071
6072 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006074 // TODO: should we allow this, and figure out type inference from list
6075 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006076 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006077 return NULL;
6078 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006079 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081 sp = p;
6082 p = skipwhite(p);
6083 op = p;
6084 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006085
6086 if (var_count > 0 && oplen == 0)
6087 // can be something like "[1, 2]->func()"
6088 return arg;
6089
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006090 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006092 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006093 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006094 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 if (heredoc)
6097 {
6098 list_T *l;
6099 listitem_T *li;
6100
6101 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006102 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006103 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006104 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006105 if (l == NULL)
6106 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107
Bram Moolenaar078269b2020-09-21 20:35:55 +02006108 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006110 // Push each line and the create the list.
6111 FOR_ALL_LIST_ITEMS(l, li)
6112 {
6113 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6114 li->li_tv.vval.v_string = NULL;
6115 }
6116 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 list_free(l);
6119 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006120 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006121 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006124 char_u *wp;
6125
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006126 // for "[var, var] = expr" evaluate the expression here, loop over the
6127 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006128 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006129
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006130 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006131 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006132 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006133 if (compile_expr0(&p, cctx) == FAIL)
6134 return NULL;
6135 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006136
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006137 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006139 type_T *stacktype;
6140
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006141 stacktype = stack->ga_len == 0 ? &t_void
6142 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006143 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006145 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006146 goto theend;
6147 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006148 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006149 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006150 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006151 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006152 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6153 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006154 if (stacktype->tt_member != NULL)
6155 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006156 }
6157 }
6158
6159 /*
6160 * Loop over variables in "[var, var] = expr".
6161 * For "var = expr" and "let var: type" this is done only once.
6162 */
6163 if (var_count > 0)
6164 var_start = skipwhite(arg + 1); // skip over the "["
6165 else
6166 var_start = arg;
6167 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6168 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006169 int instr_count = -1;
6170
Bram Moolenaar752fc692021-01-04 21:57:11 +01006171 vim_free(lhs.lhs_name);
6172
6173 /*
6174 * Figure out the LHS type and other properties.
6175 */
6176 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6177 goto theend;
6178
6179 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006180 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006181 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006182 goto theend;
6183 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 if (!is_decl && lhs.lhs_lvar != NULL
6185 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006186 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006187 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006188 goto theend;
6189 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006190
6191 if (!heredoc)
6192 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006193 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006194 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006195 if (oplen > 0 && var_count == 0)
6196 {
6197 // skip over the "=" and the expression
6198 p = skipwhite(op + oplen);
6199 compile_expr0(&p, cctx);
6200 }
6201 }
6202 else if (oplen > 0)
6203 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006204 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006205 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006206
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006207 // For "var = expr" evaluate the expression.
6208 if (var_count == 0)
6209 {
6210 int r;
6211
6212 // for "+=", "*=", "..=" etc. first load the current value
6213 if (*op != '=')
6214 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006215 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6216 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006217
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006219 {
6220 // TODO: get member from list or dict
6221 emsg("Index with operation not supported yet");
6222 goto theend;
6223 }
6224 }
6225
6226 // Compile the expression. Temporarily hide the new local
6227 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229 --cctx->ctx_locals.ga_len;
6230 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006231 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006232 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006233 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006234 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006235 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006236 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006237 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006238 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 ++cctx->ctx_locals.ga_len;
6241 if (r == FAIL)
6242 goto theend;
6243 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006244 else if (semicolon && var_idx == var_count - 1)
6245 {
6246 // For "[var; var] = expr" get the rest of the list
6247 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6248 goto theend;
6249 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006250 else
6251 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006252 // For "[var, var] = expr" get the "var_idx" item from the
6253 // list.
6254 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006255 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006256 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006257
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006258 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006259 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006260 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006261 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006262 if ((rhs_type->tt_type == VAR_FUNC
6263 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006265 goto theend;
6266
Bram Moolenaar752fc692021-01-04 21:57:11 +01006267 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006268 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006269 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006270 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006271 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006272 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006273 }
6274 else
6275 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006276 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006277 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006278 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006280 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006281 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006282 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006283 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006284 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006286 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006287 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006288 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006289 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006291
Bram Moolenaar71abe482020-09-14 22:28:30 +02006292 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006293 if (lhs.lhs_has_index)
6294 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006295 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006296 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006297 goto theend;
6298 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006299 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006300 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006301 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006302 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006304 else if (cmdidx == CMD_final)
6305 {
6306 emsg(_(e_final_requires_a_value));
6307 goto theend;
6308 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006309 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006311 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006312 goto theend;
6313 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006315 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006316 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 goto theend;
6318 }
6319 else
6320 {
6321 // variables are always initialized
6322 if (ga_grow(instr, 1) == FAIL)
6323 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006324 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006325 {
6326 case VAR_BOOL:
6327 generate_PUSHBOOL(cctx, VVAL_FALSE);
6328 break;
6329 case VAR_FLOAT:
6330#ifdef FEAT_FLOAT
6331 generate_PUSHF(cctx, 0.0);
6332#endif
6333 break;
6334 case VAR_STRING:
6335 generate_PUSHS(cctx, NULL);
6336 break;
6337 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006338 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006339 break;
6340 case VAR_FUNC:
6341 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6342 break;
6343 case VAR_LIST:
6344 generate_NEWLIST(cctx, 0);
6345 break;
6346 case VAR_DICT:
6347 generate_NEWDICT(cctx, 0);
6348 break;
6349 case VAR_JOB:
6350 generate_PUSHJOB(cctx, NULL);
6351 break;
6352 case VAR_CHANNEL:
6353 generate_PUSHCHANNEL(cctx, NULL);
6354 break;
6355 case VAR_NUMBER:
6356 case VAR_UNKNOWN:
6357 case VAR_ANY:
6358 case VAR_PARTIAL:
6359 case VAR_VOID:
6360 case VAR_SPECIAL: // cannot happen
6361 generate_PUSHNR(cctx, 0);
6362 break;
6363 }
6364 }
6365 if (var_count == 0)
6366 end = p;
6367 }
6368
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006369 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006370 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006371 break;
6372
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006373 if (oplen > 0 && *op != '=')
6374 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006375 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006376 type_T *stacktype;
6377
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006378 if (*op == '.')
6379 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006380 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006382 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006383 if (
6384#ifdef FEAT_FLOAT
6385 // If variable is float operation with number is OK.
6386 !(expected == &t_float && stacktype == &t_number) &&
6387#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006388 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006389 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006390 goto theend;
6391
6392 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006393 {
6394 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6395 goto theend;
6396 }
6397 else if (*op == '+')
6398 {
6399 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006400 operator_type(lhs.lhs_member_type, stacktype),
6401 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006402 goto theend;
6403 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006404 else if (generate_two_op(cctx, op) == FAIL)
6405 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407
Bram Moolenaar752fc692021-01-04 21:57:11 +01006408 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006409 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006410 // Use the info in "lhs" to store the value at the index in the
6411 // list or dict.
6412 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6413 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006414 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006415 }
6416 else
6417 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6419 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006420 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006421 generate_LOCKCONST(cctx);
6422
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006423 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006424 && (lhs.lhs_type->tt_type == VAR_DICT
6425 || lhs.lhs_type->tt_type == VAR_LIST)
6426 && lhs.lhs_type->tt_member != NULL
6427 && lhs.lhs_type->tt_member != &t_any
6428 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006429 // Set the type in the list or dict, so that it can be checked,
6430 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006431 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006432
Bram Moolenaar752fc692021-01-04 21:57:11 +01006433 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006434 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006435 if (generate_store_var(cctx, lhs.lhs_dest,
6436 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6437 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6438 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006439 goto theend;
6440 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006442 {
6443 isn_T *isn = ((isn_T *)instr->ga_data)
6444 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006445
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006446 // optimization: turn "var = 123" from ISN_PUSHNR +
6447 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006448 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006449 && instr->ga_len == instr_count + 1
6450 && isn->isn_type == ISN_PUSHNR)
6451 {
6452 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006453
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006454 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006455 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006456 isn->isn_arg.storenr.stnr_val = val;
6457 if (stack->ga_len > 0)
6458 --stack->ga_len;
6459 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006460 else if (lhs.lhs_lvar->lv_from_outer > 0)
6461 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6462 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006463 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006464 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006465 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006466 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006467
6468 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006470 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006471
6472 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006473 if (var_count > 0 && !semicolon)
6474 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006475 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006476 goto theend;
6477 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006478
Bram Moolenaarb2097502020-07-19 17:17:02 +02006479 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006480
6481theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 return ret;
6484}
6485
6486/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006487 * Check for an assignment at "eap->cmd", compile it if found.
6488 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6489 */
6490 static int
6491may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6492{
6493 char_u *pskip;
6494 char_u *p;
6495
6496 // Assuming the command starts with a variable or function name,
6497 // find what follows.
6498 // Skip over "var.member", "var[idx]" and the like.
6499 // Also "&opt = val", "$ENV = val" and "@r = val".
6500 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6501 ? eap->cmd + 1 : eap->cmd;
6502 p = to_name_end(pskip, TRUE);
6503 if (p > eap->cmd && *p != NUL)
6504 {
6505 char_u *var_end;
6506 int oplen;
6507 int heredoc;
6508
6509 if (eap->cmd[0] == '@')
6510 var_end = eap->cmd + 2;
6511 else
6512 var_end = find_name_end(pskip, NULL, NULL,
6513 FNE_CHECK_START | FNE_INCL_BR);
6514 oplen = assignment_len(skipwhite(var_end), &heredoc);
6515 if (oplen > 0)
6516 {
6517 size_t len = p - eap->cmd;
6518
6519 // Recognize an assignment if we recognize the variable
6520 // name:
6521 // "g:var = expr"
6522 // "local = expr" where "local" is a local var.
6523 // "script = expr" where "script" is a script-local var.
6524 // "import = expr" where "import" is an imported var
6525 // "&opt = expr"
6526 // "$ENV = expr"
6527 // "@r = expr"
6528 if (*eap->cmd == '&'
6529 || *eap->cmd == '$'
6530 || *eap->cmd == '@'
6531 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006532 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006533 {
6534 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6535 if (*line == NULL || *line == eap->cmd)
6536 return FAIL;
6537 return OK;
6538 }
6539 }
6540 }
6541
6542 if (*eap->cmd == '[')
6543 {
6544 // [var, var] = expr
6545 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6546 if (*line == NULL)
6547 return FAIL;
6548 if (*line != eap->cmd)
6549 return OK;
6550 }
6551 return NOTDONE;
6552}
6553
6554/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006555 * Check if "name" can be "unlet".
6556 */
6557 int
6558check_vim9_unlet(char_u *name)
6559{
6560 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6561 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006562 // "unlet s:var" is allowed in legacy script.
6563 if (*name == 's' && !script_is_vim9())
6564 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006565 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006566 return FAIL;
6567 }
6568 return OK;
6569}
6570
6571/*
6572 * Callback passed to ex_unletlock().
6573 */
6574 static int
6575compile_unlet(
6576 lval_T *lvp,
6577 char_u *name_end,
6578 exarg_T *eap,
6579 int deep UNUSED,
6580 void *coookie)
6581{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006582 cctx_T *cctx = coookie;
6583 char_u *p = lvp->ll_name;
6584 int cc = *name_end;
6585 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006586
Bram Moolenaar752fc692021-01-04 21:57:11 +01006587 if (cctx->ctx_skip == SKIP_YES)
6588 return OK;
6589
6590 *name_end = NUL;
6591 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006592 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006593 // :unlet $ENV_VAR
6594 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6595 }
6596 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6597 {
6598 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006599
Bram Moolenaar752fc692021-01-04 21:57:11 +01006600 // This is similar to assigning: lookup the list/dict, compile the
6601 // idx/key. Then instead of storing the value unlet the item.
6602 // unlet {list}[idx]
6603 // unlet {dict}[key] dict.key
6604 //
6605 // Figure out the LHS type and other properties.
6606 //
6607 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6608
6609 // : unlet an indexed item
6610 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006611 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 iemsg("called compile_lhs() without an index");
6613 ret = FAIL;
6614 }
6615 else
6616 {
6617 // Use the info in "lhs" to unlet the item at the index in the
6618 // list or dict.
6619 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006620 }
6621
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 vim_free(lhs.lhs_name);
6623 }
6624 else if (check_vim9_unlet(p) == FAIL)
6625 {
6626 ret = FAIL;
6627 }
6628 else
6629 {
6630 // Normal name. Only supports g:, w:, t: and b: namespaces.
6631 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006632 }
6633
Bram Moolenaar752fc692021-01-04 21:57:11 +01006634 *name_end = cc;
6635 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006636}
6637
6638/*
6639 * compile "unlet var", "lock var" and "unlock var"
6640 * "arg" points to "var".
6641 */
6642 static char_u *
6643compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6644{
6645 char_u *p = arg;
6646
6647 if (eap->cmdidx != CMD_unlet)
6648 {
6649 emsg("Sorry, :lock and unlock not implemented yet");
6650 return NULL;
6651 }
6652
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006653 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6654 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006655 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6656}
6657
6658/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006659 * Compile an :import command.
6660 */
6661 static char_u *
6662compile_import(char_u *arg, cctx_T *cctx)
6663{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006664 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665}
6666
6667/*
6668 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6669 */
6670 static int
6671compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6672{
6673 garray_T *instr = &cctx->ctx_instr;
6674 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6675
6676 if (endlabel == NULL)
6677 return FAIL;
6678 endlabel->el_next = *el;
6679 *el = endlabel;
6680 endlabel->el_end_label = instr->ga_len;
6681
6682 generate_JUMP(cctx, when, 0);
6683 return OK;
6684}
6685
6686 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006687compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688{
6689 garray_T *instr = &cctx->ctx_instr;
6690
6691 while (*el != NULL)
6692 {
6693 endlabel_T *cur = (*el);
6694 isn_T *isn;
6695
6696 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006697 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006698 *el = cur->el_next;
6699 vim_free(cur);
6700 }
6701}
6702
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006703 static void
6704compile_free_jump_to_end(endlabel_T **el)
6705{
6706 while (*el != NULL)
6707 {
6708 endlabel_T *cur = (*el);
6709
6710 *el = cur->el_next;
6711 vim_free(cur);
6712 }
6713}
6714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006715/*
6716 * Create a new scope and set up the generic items.
6717 */
6718 static scope_T *
6719new_scope(cctx_T *cctx, scopetype_T type)
6720{
6721 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6722
6723 if (scope == NULL)
6724 return NULL;
6725 scope->se_outer = cctx->ctx_scope;
6726 cctx->ctx_scope = scope;
6727 scope->se_type = type;
6728 scope->se_local_count = cctx->ctx_locals.ga_len;
6729 return scope;
6730}
6731
6732/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006733 * Free the current scope and go back to the outer scope.
6734 */
6735 static void
6736drop_scope(cctx_T *cctx)
6737{
6738 scope_T *scope = cctx->ctx_scope;
6739
6740 if (scope == NULL)
6741 {
6742 iemsg("calling drop_scope() without a scope");
6743 return;
6744 }
6745 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006746 switch (scope->se_type)
6747 {
6748 case IF_SCOPE:
6749 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6750 case FOR_SCOPE:
6751 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6752 case WHILE_SCOPE:
6753 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6754 case TRY_SCOPE:
6755 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6756 case NO_SCOPE:
6757 case BLOCK_SCOPE:
6758 break;
6759 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006760 vim_free(scope);
6761}
6762
6763/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 * compile "if expr"
6765 *
6766 * "if expr" Produces instructions:
6767 * EVAL expr Push result of "expr"
6768 * JUMP_IF_FALSE end
6769 * ... body ...
6770 * end:
6771 *
6772 * "if expr | else" Produces instructions:
6773 * EVAL expr Push result of "expr"
6774 * JUMP_IF_FALSE else
6775 * ... body ...
6776 * JUMP_ALWAYS end
6777 * else:
6778 * ... body ...
6779 * end:
6780 *
6781 * "if expr1 | elseif expr2 | else" Produces instructions:
6782 * EVAL expr Push result of "expr"
6783 * JUMP_IF_FALSE elseif
6784 * ... body ...
6785 * JUMP_ALWAYS end
6786 * elseif:
6787 * EVAL expr Push result of "expr"
6788 * JUMP_IF_FALSE else
6789 * ... body ...
6790 * JUMP_ALWAYS end
6791 * else:
6792 * ... body ...
6793 * end:
6794 */
6795 static char_u *
6796compile_if(char_u *arg, cctx_T *cctx)
6797{
6798 char_u *p = arg;
6799 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006800 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006802 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006803 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804
Bram Moolenaara5565e42020-05-09 15:44:01 +02006805 CLEAR_FIELD(ppconst);
6806 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006807 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006808 clear_ppconst(&ppconst);
6809 return NULL;
6810 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006811 if (!ends_excmd2(arg, skipwhite(p)))
6812 {
6813 semsg(_(e_trailing_arg), p);
6814 return NULL;
6815 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006816 if (cctx->ctx_skip == SKIP_YES)
6817 clear_ppconst(&ppconst);
6818 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006819 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006820 int error = FALSE;
6821 int v;
6822
Bram Moolenaara5565e42020-05-09 15:44:01 +02006823 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006824 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006825 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006826 if (error)
6827 return NULL;
6828 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006829 }
6830 else
6831 {
6832 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006833 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006834 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006835 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006836 if (bool_on_stack(cctx) == FAIL)
6837 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006839
6840 scope = new_scope(cctx, IF_SCOPE);
6841 if (scope == NULL)
6842 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006843 scope->se_skip_save = skip_save;
6844 // "is_had_return" will be reset if any block does not end in :return
6845 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006846
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006847 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006848 {
6849 // "where" is set when ":elseif", "else" or ":endif" is found
6850 scope->se_u.se_if.is_if_label = instr->ga_len;
6851 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6852 }
6853 else
6854 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855
Bram Moolenaarced68a02021-01-24 17:53:47 +01006856#ifdef FEAT_PROFILE
6857 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6858 && skip_save != SKIP_YES)
6859 {
6860 // generated a profile start, need to generate a profile end, since it
6861 // won't be done after returning
6862 cctx->ctx_skip = SKIP_NOT;
6863 generate_instr(cctx, ISN_PROF_END);
6864 cctx->ctx_skip = SKIP_YES;
6865 }
6866#endif
6867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006868 return p;
6869}
6870
6871 static char_u *
6872compile_elseif(char_u *arg, cctx_T *cctx)
6873{
6874 char_u *p = arg;
6875 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006876 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 isn_T *isn;
6878 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006879 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006880 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881
6882 if (scope == NULL || scope->se_type != IF_SCOPE)
6883 {
6884 emsg(_(e_elseif_without_if));
6885 return NULL;
6886 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006887 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006888 if (!cctx->ctx_had_return)
6889 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006890
Bram Moolenaarced68a02021-01-24 17:53:47 +01006891 if (cctx->ctx_skip == SKIP_NOT)
6892 {
6893 // previous block was executed, this one and following will not
6894 cctx->ctx_skip = SKIP_YES;
6895 scope->se_u.se_if.is_seen_skip_not = TRUE;
6896 }
6897 if (scope->se_u.se_if.is_seen_skip_not)
6898 {
6899 // A previous block was executed, skip over expression and bail out.
6900 // Do not count the "elseif" for profiling.
6901#ifdef FEAT_PROFILE
6902 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6903 .isn_type == ISN_PROF_START)
6904 --instr->ga_len;
6905#endif
6906 skip_expr_cctx(&p, cctx);
6907 return p;
6908 }
6909
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006910 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006911 {
6912 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006914 return NULL;
6915 // previous "if" or "elseif" jumps here
6916 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6917 isn->isn_arg.jump.jump_where = instr->ga_len;
6918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006920 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006921 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006922 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006923 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006924 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006925#ifdef FEAT_PROFILE
6926 if (cctx->ctx_profiling)
6927 {
6928 // the previous block was skipped, need to profile this line
6929 generate_instr(cctx, ISN_PROF_START);
6930 instr_count = instr->ga_len;
6931 }
6932#endif
6933 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006934 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006935 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006936 clear_ppconst(&ppconst);
6937 return NULL;
6938 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006939 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006940 if (!ends_excmd2(arg, skipwhite(p)))
6941 {
6942 semsg(_(e_trailing_arg), p);
6943 return NULL;
6944 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006945 if (scope->se_skip_save == SKIP_YES)
6946 clear_ppconst(&ppconst);
6947 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006948 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006949 int error = FALSE;
6950 int v;
6951
Bram Moolenaar7f141552020-05-09 17:35:53 +02006952 // The expression results in a constant.
6953 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006954 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6955 if (error)
6956 return NULL;
6957 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006958 clear_ppconst(&ppconst);
6959 scope->se_u.se_if.is_if_label = -1;
6960 }
6961 else
6962 {
6963 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006964 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006965 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006966 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006967 if (bool_on_stack(cctx) == FAIL)
6968 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006970 // "where" is set when ":elseif", "else" or ":endif" is found
6971 scope->se_u.se_if.is_if_label = instr->ga_len;
6972 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974
6975 return p;
6976}
6977
6978 static char_u *
6979compile_else(char_u *arg, cctx_T *cctx)
6980{
6981 char_u *p = arg;
6982 garray_T *instr = &cctx->ctx_instr;
6983 isn_T *isn;
6984 scope_T *scope = cctx->ctx_scope;
6985
6986 if (scope == NULL || scope->se_type != IF_SCOPE)
6987 {
6988 emsg(_(e_else_without_if));
6989 return NULL;
6990 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006991 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006992 if (!cctx->ctx_had_return)
6993 scope->se_u.se_if.is_had_return = FALSE;
6994 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995
Bram Moolenaarced68a02021-01-24 17:53:47 +01006996#ifdef FEAT_PROFILE
6997 if (cctx->ctx_profiling)
6998 {
6999 if (cctx->ctx_skip == SKIP_NOT
7000 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7001 .isn_type == ISN_PROF_START)
7002 // the previous block was executed, do not count "else" for profiling
7003 --instr->ga_len;
7004 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7005 {
7006 // the previous block was not executed, this one will, do count the
7007 // "else" for profiling
7008 cctx->ctx_skip = SKIP_NOT;
7009 generate_instr(cctx, ISN_PROF_END);
7010 generate_instr(cctx, ISN_PROF_START);
7011 cctx->ctx_skip = SKIP_YES;
7012 }
7013 }
7014#endif
7015
7016 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007017 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007018 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007019 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007020 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007021 if (!cctx->ctx_had_return
7022 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7023 JUMP_ALWAYS, cctx) == FAIL)
7024 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007025 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007026
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007027 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007028 {
7029 if (scope->se_u.se_if.is_if_label >= 0)
7030 {
7031 // previous "if" or "elseif" jumps here
7032 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7033 isn->isn_arg.jump.jump_where = instr->ga_len;
7034 scope->se_u.se_if.is_if_label = -1;
7035 }
7036 }
7037
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007038 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007039 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041
7042 return p;
7043}
7044
7045 static char_u *
7046compile_endif(char_u *arg, cctx_T *cctx)
7047{
7048 scope_T *scope = cctx->ctx_scope;
7049 ifscope_T *ifscope;
7050 garray_T *instr = &cctx->ctx_instr;
7051 isn_T *isn;
7052
7053 if (scope == NULL || scope->se_type != IF_SCOPE)
7054 {
7055 emsg(_(e_endif_without_if));
7056 return NULL;
7057 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007058 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007059 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007060 if (!cctx->ctx_had_return)
7061 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007063 if (scope->se_u.se_if.is_if_label >= 0)
7064 {
7065 // previous "if" or "elseif" jumps here
7066 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7067 isn->isn_arg.jump.jump_where = instr->ga_len;
7068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007070 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007071
7072#ifdef FEAT_PROFILE
7073 // even when skipping we count the endif as executed, unless the block it's
7074 // in is skipped
7075 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7076 && scope->se_skip_save != SKIP_YES)
7077 {
7078 cctx->ctx_skip = SKIP_NOT;
7079 generate_instr(cctx, ISN_PROF_START);
7080 }
7081#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007082 cctx->ctx_skip = scope->se_skip_save;
7083
7084 // If all the blocks end in :return and there is an :else then the
7085 // had_return flag is set.
7086 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007088 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 return arg;
7090}
7091
7092/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007093 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094 *
7095 * Produces instructions:
7096 * PUSHNR -1
7097 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007098 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7100 * - if beyond end, jump to "end"
7101 * - otherwise get item from list and push it
7102 * STORE var Store item in "var"
7103 * ... body ...
7104 * JUMP top Jump back to repeat
7105 * end: DROP Drop the result of "expr"
7106 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007107 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7108 * UNPACK 2 Split item in 2
7109 * STORE var1 Store item in "var1"
7110 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 */
7112 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007113compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007115 char_u *arg;
7116 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007117 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007119 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007120 int var_count = 0;
7121 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007122 size_t varlen;
7123 garray_T *instr = &cctx->ctx_instr;
7124 garray_T *stack = &cctx->ctx_type_stack;
7125 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007126 lvar_T *loop_lvar; // loop iteration variable
7127 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007128 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007129 type_T *item_type = &t_any;
7130 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131
Bram Moolenaar792f7862020-11-23 08:31:18 +01007132 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007133 if (p == NULL)
7134 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007135 if (var_count == 0)
7136 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007137
7138 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007139 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007140 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7141 return NULL;
7142 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143 {
7144 emsg(_(e_missing_in));
7145 return NULL;
7146 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007147 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007148 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7149 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151 scope = new_scope(cctx, FOR_SCOPE);
7152 if (scope == NULL)
7153 return NULL;
7154
Bram Moolenaar792f7862020-11-23 08:31:18 +01007155 // Reserve a variable to store the loop iteration counter and initialize it
7156 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007157 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7158 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007159 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007160 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007161 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007163 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007164 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165
7166 // compile "expr", it remains on the stack until "endfor"
7167 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007168 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007169 {
7170 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007172 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007173 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007175 // Now that we know the type of "var", check that it is a list, now or at
7176 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007178 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007180 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181 return NULL;
7182 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007183
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007184 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007185 {
7186 if (var_count == 1)
7187 item_type = vartype->tt_member;
7188 else if (vartype->tt_member->tt_type == VAR_LIST
7189 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7190 item_type = vartype->tt_member->tt_member;
7191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
7193 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007194 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007195 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196
Bram Moolenaar792f7862020-11-23 08:31:18 +01007197 arg = arg_start;
7198 if (var_count > 1)
7199 {
7200 generate_UNPACK(cctx, var_count, semicolon);
7201 arg = skipwhite(arg + 1); // skip white after '['
7202
7203 // the list item is replaced by a number of items
7204 if (ga_grow(stack, var_count - 1) == FAIL)
7205 {
7206 drop_scope(cctx);
7207 return NULL;
7208 }
7209 --stack->ga_len;
7210 for (idx = 0; idx < var_count; ++idx)
7211 {
7212 ((type_T **)stack->ga_data)[stack->ga_len] =
7213 (semicolon && idx == 0) ? vartype : item_type;
7214 ++stack->ga_len;
7215 }
7216 }
7217
7218 for (idx = 0; idx < var_count; ++idx)
7219 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007220 assign_dest_T dest = dest_local;
7221 int opt_flags = 0;
7222 int vimvaridx = -1;
7223 type_T *type = &t_any;
7224
7225 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007226 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007227 name = vim_strnsave(arg, varlen);
7228 if (name == NULL)
7229 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007230
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007231 // TODO: script var not supported?
7232 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7233 &vimvaridx, &type, cctx) == FAIL)
7234 goto failed;
7235 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007236 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007237 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7238 0, 0, type, name) == FAIL)
7239 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007240 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007241 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007242 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007243 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007244 {
7245 semsg(_(e_variable_already_declared), arg);
7246 goto failed;
7247 }
7248
Bram Moolenaarea870692020-12-02 14:24:30 +01007249 if (STRNCMP(name, "s:", 2) == 0)
7250 {
7251 semsg(_(e_cannot_declare_script_variable_in_function), name);
7252 goto failed;
7253 }
7254
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007255 // Reserve a variable to store "var".
7256 // TODO: check for type
7257 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7258 if (var_lvar == NULL)
7259 // out of memory or used as an argument
7260 goto failed;
7261
7262 if (semicolon && idx == var_count - 1)
7263 var_lvar->lv_type = vartype;
7264 else
7265 var_lvar->lv_type = item_type;
7266 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7267 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007268
Bram Moolenaar036d0712021-01-17 20:23:38 +01007269 if (*p == ':')
7270 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007271 if (*p == ',' || *p == ';')
7272 ++p;
7273 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007274 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007275 }
7276
7277 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007278
7279failed:
7280 vim_free(name);
7281 drop_scope(cctx);
7282 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283}
7284
7285/*
7286 * compile "endfor"
7287 */
7288 static char_u *
7289compile_endfor(char_u *arg, cctx_T *cctx)
7290{
7291 garray_T *instr = &cctx->ctx_instr;
7292 scope_T *scope = cctx->ctx_scope;
7293 forscope_T *forscope;
7294 isn_T *isn;
7295
7296 if (scope == NULL || scope->se_type != FOR_SCOPE)
7297 {
7298 emsg(_(e_for));
7299 return NULL;
7300 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007301 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007303 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304
7305 // At end of ":for" scope jump back to the FOR instruction.
7306 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7307
7308 // Fill in the "end" label in the FOR statement so it can jump here
7309 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7310 isn->isn_arg.forloop.for_end = instr->ga_len;
7311
7312 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007313 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314
7315 // Below the ":for" scope drop the "expr" list from the stack.
7316 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7317 return NULL;
7318
7319 vim_free(scope);
7320
7321 return arg;
7322}
7323
7324/*
7325 * compile "while expr"
7326 *
7327 * Produces instructions:
7328 * top: EVAL expr Push result of "expr"
7329 * JUMP_IF_FALSE end jump if false
7330 * ... body ...
7331 * JUMP top Jump back to repeat
7332 * end:
7333 *
7334 */
7335 static char_u *
7336compile_while(char_u *arg, cctx_T *cctx)
7337{
7338 char_u *p = arg;
7339 garray_T *instr = &cctx->ctx_instr;
7340 scope_T *scope;
7341
7342 scope = new_scope(cctx, WHILE_SCOPE);
7343 if (scope == NULL)
7344 return NULL;
7345
Bram Moolenaarb2049902021-01-24 12:53:53 +01007346 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007347 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007348#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007349 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7350 .isn_type == ISN_PROF_START)
7351 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007352#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353
7354 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007355 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007357 if (!ends_excmd2(arg, skipwhite(p)))
7358 {
7359 semsg(_(e_trailing_arg), p);
7360 return NULL;
7361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362
Bram Moolenaar13106602020-10-04 16:06:05 +02007363 if (bool_on_stack(cctx) == FAIL)
7364 return FAIL;
7365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007367 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 JUMP_IF_FALSE, cctx) == FAIL)
7369 return FAIL;
7370
7371 return p;
7372}
7373
7374/*
7375 * compile "endwhile"
7376 */
7377 static char_u *
7378compile_endwhile(char_u *arg, cctx_T *cctx)
7379{
7380 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007381 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382
7383 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7384 {
7385 emsg(_(e_while));
7386 return NULL;
7387 }
7388 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007389 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390
Bram Moolenaarf002a412021-01-24 13:34:18 +01007391#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007392 // count the endwhile before jumping
7393 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007394#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007397 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398
7399 // Fill in the "end" label in the WHILE statement so it can jump here.
7400 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007401 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7402 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403
7404 vim_free(scope);
7405
7406 return arg;
7407}
7408
7409/*
7410 * compile "continue"
7411 */
7412 static char_u *
7413compile_continue(char_u *arg, cctx_T *cctx)
7414{
7415 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007416 int try_scopes = 0;
7417 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418
7419 for (;;)
7420 {
7421 if (scope == NULL)
7422 {
7423 emsg(_(e_continue));
7424 return NULL;
7425 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007426 if (scope->se_type == FOR_SCOPE)
7427 {
7428 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007430 }
7431 if (scope->se_type == WHILE_SCOPE)
7432 {
7433 loop_label = scope->se_u.se_while.ws_top_label;
7434 break;
7435 }
7436 if (scope->se_type == TRY_SCOPE)
7437 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 scope = scope->se_outer;
7439 }
7440
Bram Moolenaarc150c092021-02-13 15:02:46 +01007441 if (try_scopes > 0)
7442 // Inside one or more try/catch blocks we first need to jump to the
7443 // "finally" or "endtry" to cleanup.
7444 generate_TRYCONT(cctx, try_scopes, loop_label);
7445 else
7446 // Jump back to the FOR or WHILE instruction.
7447 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 return arg;
7450}
7451
7452/*
7453 * compile "break"
7454 */
7455 static char_u *
7456compile_break(char_u *arg, cctx_T *cctx)
7457{
7458 scope_T *scope = cctx->ctx_scope;
7459 endlabel_T **el;
7460
7461 for (;;)
7462 {
7463 if (scope == NULL)
7464 {
7465 emsg(_(e_break));
7466 return NULL;
7467 }
7468 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7469 break;
7470 scope = scope->se_outer;
7471 }
7472
7473 // Jump to the end of the FOR or WHILE loop.
7474 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007475 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007477 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7479 return FAIL;
7480
7481 return arg;
7482}
7483
7484/*
7485 * compile "{" start of block
7486 */
7487 static char_u *
7488compile_block(char_u *arg, cctx_T *cctx)
7489{
7490 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7491 return NULL;
7492 return skipwhite(arg + 1);
7493}
7494
7495/*
7496 * compile end of block: drop one scope
7497 */
7498 static void
7499compile_endblock(cctx_T *cctx)
7500{
7501 scope_T *scope = cctx->ctx_scope;
7502
7503 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007504 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007505 vim_free(scope);
7506}
7507
7508/*
7509 * compile "try"
7510 * Creates a new scope for the try-endtry, pointing to the first catch and
7511 * finally.
7512 * Creates another scope for the "try" block itself.
7513 * TRY instruction sets up exception handling at runtime.
7514 *
7515 * "try"
7516 * TRY -> catch1, -> finally push trystack entry
7517 * ... try block
7518 * "throw {exception}"
7519 * EVAL {exception}
7520 * THROW create exception
7521 * ... try block
7522 * " catch {expr}"
7523 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007524 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 * EVAL {expr}
7526 * MATCH
7527 * JUMP nomatch -> catch2
7528 * CATCH remove exception
7529 * ... catch block
7530 * " catch"
7531 * JUMP -> finally
7532 * catch2: CATCH remove exception
7533 * ... catch block
7534 * " finally"
7535 * finally:
7536 * ... finally block
7537 * " endtry"
7538 * ENDTRY pop trystack entry, may rethrow
7539 */
7540 static char_u *
7541compile_try(char_u *arg, cctx_T *cctx)
7542{
7543 garray_T *instr = &cctx->ctx_instr;
7544 scope_T *try_scope;
7545 scope_T *scope;
7546
7547 // scope that holds the jumps that go to catch/finally/endtry
7548 try_scope = new_scope(cctx, TRY_SCOPE);
7549 if (try_scope == NULL)
7550 return NULL;
7551
Bram Moolenaar69f70502021-01-01 16:10:46 +01007552 if (cctx->ctx_skip != SKIP_YES)
7553 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007554 isn_T *isn;
7555
7556 // "try_catch" is set when the first ":catch" is found or when no catch
7557 // is found and ":finally" is found.
7558 // "try_finally" is set when ":finally" is found
7559 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007560 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007561 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7562 return NULL;
7563 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7564 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007565 return NULL;
7566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567
7568 // scope for the try block itself
7569 scope = new_scope(cctx, BLOCK_SCOPE);
7570 if (scope == NULL)
7571 return NULL;
7572
7573 return arg;
7574}
7575
7576/*
7577 * compile "catch {expr}"
7578 */
7579 static char_u *
7580compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7581{
7582 scope_T *scope = cctx->ctx_scope;
7583 garray_T *instr = &cctx->ctx_instr;
7584 char_u *p;
7585 isn_T *isn;
7586
7587 // end block scope from :try or :catch
7588 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7589 compile_endblock(cctx);
7590 scope = cctx->ctx_scope;
7591
7592 // Error if not in a :try scope
7593 if (scope == NULL || scope->se_type != TRY_SCOPE)
7594 {
7595 emsg(_(e_catch));
7596 return NULL;
7597 }
7598
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007599 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007601 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602 return NULL;
7603 }
7604
Bram Moolenaar69f70502021-01-01 16:10:46 +01007605 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007607#ifdef FEAT_PROFILE
7608 // the profile-start should be after the jump
7609 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7610 .isn_type == ISN_PROF_START)
7611 --instr->ga_len;
7612#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007613 // Jump from end of previous block to :finally or :endtry
7614 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7615 JUMP_ALWAYS, cctx) == FAIL)
7616 return NULL;
7617
7618 // End :try or :catch scope: set value in ISN_TRY instruction
7619 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007620 if (isn->isn_arg.try.try_ref->try_catch == 0)
7621 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007622 if (scope->se_u.se_try.ts_catch_label != 0)
7623 {
7624 // Previous catch without match jumps here
7625 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7626 isn->isn_arg.jump.jump_where = instr->ga_len;
7627 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007628#ifdef FEAT_PROFILE
7629 if (cctx->ctx_profiling)
7630 {
7631 // a "throw" that jumps here needs to be counted
7632 generate_instr(cctx, ISN_PROF_END);
7633 // the "catch" is also counted
7634 generate_instr(cctx, ISN_PROF_START);
7635 }
7636#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 }
7638
7639 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007640 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007642 scope->se_u.se_try.ts_caught_all = TRUE;
7643 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007644 }
7645 else
7646 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007647 char_u *end;
7648 char_u *pat;
7649 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007650 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007651 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 // Push v:exception, push {expr} and MATCH
7654 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7655
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007656 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007657 if (*end != *p)
7658 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007659 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007660 vim_free(tofree);
7661 return FAIL;
7662 }
7663 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007664 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007665 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007666 len = (int)(end - tofree);
7667 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007668 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007669 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007670 if (pat == NULL)
7671 return FAIL;
7672 if (generate_PUSHS(cctx, pat) == FAIL)
7673 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7676 return NULL;
7677
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007678 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7680 return NULL;
7681 }
7682
Bram Moolenaar69f70502021-01-01 16:10:46 +01007683 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 return NULL;
7685
7686 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7687 return NULL;
7688 return p;
7689}
7690
7691 static char_u *
7692compile_finally(char_u *arg, cctx_T *cctx)
7693{
7694 scope_T *scope = cctx->ctx_scope;
7695 garray_T *instr = &cctx->ctx_instr;
7696 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007697 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698
7699 // end block scope from :try or :catch
7700 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7701 compile_endblock(cctx);
7702 scope = cctx->ctx_scope;
7703
7704 // Error if not in a :try scope
7705 if (scope == NULL || scope->se_type != TRY_SCOPE)
7706 {
7707 emsg(_(e_finally));
7708 return NULL;
7709 }
7710
7711 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007712 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007713 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 {
7715 emsg(_(e_finally_dup));
7716 return NULL;
7717 }
7718
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007719 this_instr = instr->ga_len;
7720#ifdef FEAT_PROFILE
7721 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7722 .isn_type == ISN_PROF_START)
7723 // jump to the profile start of the "finally"
7724 --this_instr;
7725#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007727 // Fill in the "end" label in jumps at the end of the blocks.
7728 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7729 this_instr, cctx);
7730
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007731 // If there is no :catch then an exception jumps to :finally.
7732 if (isn->isn_arg.try.try_ref->try_catch == 0)
7733 isn->isn_arg.try.try_ref->try_catch = this_instr;
7734 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007735 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 {
7737 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007738 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007739 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007740 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007742 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7743 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 // TODO: set index in ts_finally_label jumps
7746
7747 return arg;
7748}
7749
7750 static char_u *
7751compile_endtry(char_u *arg, cctx_T *cctx)
7752{
7753 scope_T *scope = cctx->ctx_scope;
7754 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007755 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756
7757 // end block scope from :catch or :finally
7758 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7759 compile_endblock(cctx);
7760 scope = cctx->ctx_scope;
7761
7762 // Error if not in a :try scope
7763 if (scope == NULL || scope->se_type != TRY_SCOPE)
7764 {
7765 if (scope == NULL)
7766 emsg(_(e_no_endtry));
7767 else if (scope->se_type == WHILE_SCOPE)
7768 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007769 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007770 emsg(_(e_endfor));
7771 else
7772 emsg(_(e_endif));
7773 return NULL;
7774 }
7775
Bram Moolenaarc150c092021-02-13 15:02:46 +01007776 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007777 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007778 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007779 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7780 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007781 {
7782 emsg(_(e_missing_catch_or_finally));
7783 return NULL;
7784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007786#ifdef FEAT_PROFILE
7787 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7788 .isn_type == ISN_PROF_START)
7789 // move the profile start after "endtry" so that it's not counted when
7790 // the exception is rethrown.
7791 --instr->ga_len;
7792#endif
7793
Bram Moolenaar69f70502021-01-01 16:10:46 +01007794 // Fill in the "end" label in jumps at the end of the blocks, if not
7795 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007796 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7797 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007798
Bram Moolenaar69f70502021-01-01 16:10:46 +01007799 if (scope->se_u.se_try.ts_catch_label != 0)
7800 {
7801 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007802 isn_T *isn = ((isn_T *)instr->ga_data)
7803 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007804 isn->isn_arg.jump.jump_where = instr->ga_len;
7805 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007806 }
7807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007808 compile_endblock(cctx);
7809
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007810 if (cctx->ctx_skip != SKIP_YES)
7811 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007812 // End :catch or :finally scope: set instruction index in ISN_TRY
7813 // instruction
7814 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007815 if (cctx->ctx_skip != SKIP_YES
7816 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7817 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007818#ifdef FEAT_PROFILE
7819 if (cctx->ctx_profiling)
7820 generate_instr(cctx, ISN_PROF_START);
7821#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007823 return arg;
7824}
7825
7826/*
7827 * compile "throw {expr}"
7828 */
7829 static char_u *
7830compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7831{
7832 char_u *p = skipwhite(arg);
7833
Bram Moolenaara5565e42020-05-09 15:44:01 +02007834 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007835 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007836 if (cctx->ctx_skip == SKIP_YES)
7837 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 if (may_generate_2STRING(-1, cctx) == FAIL)
7839 return NULL;
7840 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7841 return NULL;
7842
7843 return p;
7844}
7845
7846/*
7847 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007848 * compile "echomsg expr"
7849 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007850 * compile "execute expr"
7851 */
7852 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007853compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007854{
7855 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007856 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007857 int count = 0;
7858
7859 for (;;)
7860 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007861 if (ends_excmd2(prev, p))
7862 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007863 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007864 return NULL;
7865 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007866 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007867 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007868 }
7869
Bram Moolenaare4984292020-12-13 14:19:25 +01007870 if (count > 0)
7871 {
7872 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7873 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7874 else if (cmdidx == CMD_execute)
7875 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7876 else if (cmdidx == CMD_echomsg)
7877 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7878 else
7879 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 return p;
7882}
7883
7884/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007885 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007886 * instruction to compute it and return OK.
7887 * Otherwise return FAIL, the caller must deal with any range.
7888 */
7889 static int
7890compile_variable_range(exarg_T *eap, cctx_T *cctx)
7891{
7892 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7893 char_u *p = skipdigits(eap->cmd);
7894
7895 if (p == range_end)
7896 return FAIL;
7897 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7898}
7899
7900/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007901 * :put r
7902 * :put ={expr}
7903 */
7904 static char_u *
7905compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7906{
7907 char_u *line = arg;
7908 linenr_T lnum;
7909 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007910 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007911
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007912 eap->regname = *line;
7913
7914 if (eap->regname == '=')
7915 {
7916 char_u *p = line + 1;
7917
7918 if (compile_expr0(&p, cctx) == FAIL)
7919 return NULL;
7920 line = p;
7921 }
7922 else if (eap->regname != NUL)
7923 ++line;
7924
Bram Moolenaar08597872020-12-10 19:43:40 +01007925 if (compile_variable_range(eap, cctx) == OK)
7926 {
7927 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7928 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007929 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007930 {
7931 // Either no range or a number.
7932 // "errormsg" will not be set because the range is ADDR_LINES.
7933 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007934 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007935 return NULL;
7936 if (eap->addr_count == 0)
7937 lnum = -1;
7938 else
7939 lnum = eap->line2;
7940 if (above)
7941 --lnum;
7942 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007943
7944 generate_PUT(cctx, eap->regname, lnum);
7945 return line;
7946}
7947
7948/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007949 * A command that is not compiled, execute with legacy code.
7950 */
7951 static char_u *
7952compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7953{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007954 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007955 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007956 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007957
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007958 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007959 goto theend;
7960
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007961 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007962 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007963 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007964 int usefilter = FALSE;
7965
7966 has_expr = argt & (EX_XFILE | EX_EXPAND);
7967
7968 // If the command can be followed by a bar, find the bar and truncate
7969 // it, so that the following command can be compiled.
7970 // The '|' is overwritten with a NUL, it is put back below.
7971 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7972 && *eap->arg == '!')
7973 // :w !filter or :r !filter or :r! filter
7974 usefilter = TRUE;
7975 if ((argt & EX_TRLBAR) && !usefilter)
7976 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007977 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007978 separate_nextcmd(eap);
7979 if (eap->nextcmd != NULL)
7980 nextcmd = eap->nextcmd;
7981 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007982 else if (eap->cmdidx == CMD_wincmd)
7983 {
7984 p = eap->arg;
7985 if (*p != NUL)
7986 ++p;
7987 if (*p == 'g' || *p == Ctrl_G)
7988 ++p;
7989 p = skipwhite(p);
7990 if (*p == '|')
7991 {
7992 *p = NUL;
7993 nextcmd = p + 1;
7994 }
7995 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007996 }
7997
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007998 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7999 {
8000 // expand filename in "syntax include [@group] filename"
8001 has_expr = TRUE;
8002 eap->arg = skipwhite(eap->arg + 7);
8003 if (*eap->arg == '@')
8004 eap->arg = skiptowhite(eap->arg);
8005 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008006
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008007 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8008 && STRLEN(eap->arg) > 4)
8009 {
8010 int delim = *eap->arg;
8011
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008012 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008013 if (*p == delim)
8014 {
8015 eap->arg = p + 1;
8016 has_expr = TRUE;
8017 }
8018 }
8019
Bram Moolenaarecac5912021-01-05 19:23:28 +01008020 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8021 {
8022 // TODO: should only expand when appropriate for the command
8023 eap->arg = skiptowhite(eap->arg);
8024 has_expr = TRUE;
8025 }
8026
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008027 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008028 {
8029 int count = 0;
8030 char_u *start = skipwhite(line);
8031
8032 // :cmd xxx`=expr1`yyy`=expr2`zzz
8033 // PUSHS ":cmd xxx"
8034 // eval expr1
8035 // PUSHS "yyy"
8036 // eval expr2
8037 // PUSHS "zzz"
8038 // EXECCONCAT 5
8039 for (;;)
8040 {
8041 if (p > start)
8042 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008043 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008044 ++count;
8045 }
8046 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008047 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008048 return NULL;
8049 may_generate_2STRING(-1, cctx);
8050 ++count;
8051 p = skipwhite(p);
8052 if (*p != '`')
8053 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008054 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008055 return NULL;
8056 }
8057 start = p + 1;
8058
8059 p = (char_u *)strstr((char *)start, "`=");
8060 if (p == NULL)
8061 {
8062 if (*skipwhite(start) != NUL)
8063 {
8064 generate_PUSHS(cctx, vim_strsave(start));
8065 ++count;
8066 }
8067 break;
8068 }
8069 }
8070 generate_EXECCONCAT(cctx, count);
8071 }
8072 else
8073 generate_EXEC(cctx, line);
8074
8075theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008076 if (*nextcmd != NUL)
8077 {
8078 // the parser expects a pointer to the bar, put it back
8079 --nextcmd;
8080 *nextcmd = '|';
8081 }
8082
8083 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008084}
8085
8086/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008087 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008088 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008089 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008090 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008091add_def_function(ufunc_T *ufunc)
8092{
8093 dfunc_T *dfunc;
8094
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008095 if (def_functions.ga_len == 0)
8096 {
8097 // The first position is not used, so that a zero uf_dfunc_idx means it
8098 // wasn't set.
8099 if (ga_grow(&def_functions, 1) == FAIL)
8100 return FAIL;
8101 ++def_functions.ga_len;
8102 }
8103
Bram Moolenaar09689a02020-05-09 22:50:08 +02008104 // Add the function to "def_functions".
8105 if (ga_grow(&def_functions, 1) == FAIL)
8106 return FAIL;
8107 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8108 CLEAR_POINTER(dfunc);
8109 dfunc->df_idx = def_functions.ga_len;
8110 ufunc->uf_dfunc_idx = dfunc->df_idx;
8111 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008112 dfunc->df_name = vim_strsave(ufunc->uf_name);
8113 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008114 ++def_functions.ga_len;
8115 return OK;
8116}
8117
8118/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 * After ex_function() has collected all the function lines: parse and compile
8120 * the lines into instructions.
8121 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008122 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8123 * the return statement (used for lambda). When uf_ret_type is already set
8124 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008125 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008126 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008127 * This can be used recursively through compile_lambda(), which may reallocate
8128 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008129 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008130 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008131 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008132compile_def_function(
8133 ufunc_T *ufunc,
8134 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008135 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008136 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 char_u *line = NULL;
8139 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 cctx_T cctx;
8142 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008143 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 int ret = FAIL;
8145 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008146 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008147 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008148 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008149#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008150 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008151#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008153 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008154 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008155 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008157 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8158 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008159 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008161 else
8162 {
8163 if (add_def_function(ufunc) == FAIL)
8164 return FAIL;
8165 new_def_function = TRUE;
8166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008167
Bram Moolenaar985116a2020-07-12 17:31:09 +02008168 ufunc->uf_def_status = UF_COMPILING;
8169
Bram Moolenaara80faa82020-04-12 19:37:17 +02008170 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008171
Bram Moolenaarf002a412021-01-24 13:34:18 +01008172#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008173 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008174#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175 cctx.ctx_ufunc = ufunc;
8176 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008177 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8179 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8180 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8181 cctx.ctx_type_list = &ufunc->uf_type_list;
8182 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8183 instr = &cctx.ctx_instr;
8184
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008185 // Set the context to the function, it may be compiled when called from
8186 // another script. Set the script version to the most modern one.
8187 // The line number will be set in next_line_from_context().
8188 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8190
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008191 // Make sure error messages are OK.
8192 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8193 if (do_estack_push)
8194 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008195 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008196
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008197 if (ufunc->uf_def_args.ga_len > 0)
8198 {
8199 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008200 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008201 int i;
8202 char_u *arg;
8203 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008204 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008205
8206 // Produce instructions for the default values of optional arguments.
8207 // Store the instruction index in uf_def_arg_idx[] so that we know
8208 // where to start when the function is called, depending on the number
8209 // of arguments.
8210 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8211 if (ufunc->uf_def_arg_idx == NULL)
8212 goto erret;
8213 for (i = 0; i < count; ++i)
8214 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008215 garray_T *stack = &cctx.ctx_type_stack;
8216 type_T *val_type;
8217 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008218 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008219
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008220 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8221 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008222 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008223 goto erret;
8224
8225 // If no type specified use the type of the default value.
8226 // Otherwise check that the default value type matches the
8227 // specified type.
8228 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008229 where.wt_index = arg_idx + 1;
8230 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008231 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008232 {
8233 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008234 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008235 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008236 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008237 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008238 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008239
8240 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008241 goto erret;
8242 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008243 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008244
8245 if (did_set_arg_type)
8246 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008247 }
8248
8249 /*
8250 * Loop over all the lines of the function and generate instructions.
8251 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252 for (;;)
8253 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008254 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008255 int starts_with_colon = FALSE;
8256 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008257 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008258
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008259 // Bail out on the first error to avoid a flood of errors and report
8260 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008261 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008262 goto erret;
8263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 if (line != NULL && *line == '|')
8265 // the line continues after a '|'
8266 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008267 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008268 && !(*line == '#' && (line == cctx.ctx_line_start
8269 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008270 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008271 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 goto erret;
8273 }
8274 else
8275 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008276 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008277 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008278 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008279 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008280#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008281 if (cctx.ctx_skip != SKIP_YES)
8282 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008283#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008284 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 }
8287
Bram Moolenaara80faa82020-04-12 19:37:17 +02008288 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008289 ea.cmdlinep = &line;
8290 ea.cmd = skipwhite(line);
8291
Bram Moolenaarb2049902021-01-24 12:53:53 +01008292 if (*ea.cmd == '#')
8293 {
8294 // "#" starts a comment
8295 line = (char_u *)"";
8296 continue;
8297 }
8298
Bram Moolenaarf002a412021-01-24 13:34:18 +01008299#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008300 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8301 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008302 {
8303 may_generate_prof_end(&cctx, prof_lnum);
8304
8305 prof_lnum = cctx.ctx_lnum;
8306 generate_instr(&cctx, ISN_PROF_START);
8307 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008308#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008309
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008310 // Some things can be recognized by the first character.
8311 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008313 case '}':
8314 {
8315 // "}" ends a block scope
8316 scopetype_T stype = cctx.ctx_scope == NULL
8317 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008319 if (stype == BLOCK_SCOPE)
8320 {
8321 compile_endblock(&cctx);
8322 line = ea.cmd;
8323 }
8324 else
8325 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008326 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008327 goto erret;
8328 }
8329 if (line != NULL)
8330 line = skipwhite(ea.cmd + 1);
8331 continue;
8332 }
8333
8334 case '{':
8335 // "{" starts a block scope
8336 // "{'a': 1}->func() is something else
8337 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8338 {
8339 line = compile_block(ea.cmd, &cctx);
8340 continue;
8341 }
8342 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008343 }
8344
8345 /*
8346 * COMMAND MODIFIERS
8347 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008348 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008349 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8350 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351 {
8352 if (errormsg != NULL)
8353 goto erret;
8354 // empty line or comment
8355 line = (char_u *)"";
8356 continue;
8357 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008358 generate_cmdmods(&cctx, &local_cmdmod);
8359 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008360
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008361 // Check if there was a colon after the last command modifier or before
8362 // the current position.
8363 for (p = ea.cmd; p >= line; --p)
8364 {
8365 if (*p == ':')
8366 starts_with_colon = TRUE;
8367 if (p < ea.cmd && !VIM_ISWHITE(*p))
8368 break;
8369 }
8370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008372 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008374 {
8375 if (*ea.cmd == '(')
8376 // not for "call()"
8377 ea.cmd = p;
8378 else
8379 ea.cmd = skipwhite(ea.cmd);
8380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008382 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008383 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008384 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008385
Bram Moolenaar17126b12021-01-07 22:03:02 +01008386 // Check for assignment after command modifiers.
8387 assign = may_compile_assignment(&ea, &line, &cctx);
8388 if (assign == OK)
8389 goto nextline;
8390 if (assign == FAIL)
8391 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 }
8393
8394 /*
8395 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008396 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008398 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008399 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008400 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008401 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008402 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008403 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008404 if (!starts_with_colon)
8405 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008406 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008407 goto erret;
8408 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008409 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008410 if (ends_excmd2(line, ea.cmd))
8411 {
8412 // A range without a command: jump to the line.
8413 // TODO: compile to a more efficient command, possibly
8414 // calling parse_cmd_address().
8415 ea.cmdidx = CMD_SIZE;
8416 line = compile_exec(line, &ea, &cctx);
8417 goto nextline;
8418 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008419 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008420 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008421 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar6914e872021-03-06 21:01:09 +01008422 : (int (*)(char_u *, size_t, cctx_T *))item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008424 if (p == NULL)
8425 {
8426 if (cctx.ctx_skip != SKIP_YES)
8427 emsg(_(e_ambiguous_use_of_user_defined_command));
8428 goto erret;
8429 }
8430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8432 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008433 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008434 {
8435 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008436 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008437 }
8438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008440 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008442 // CMD_var cannot happen, compile_assignment() above would be
8443 // used. Most likely an assignment to a non-existing variable.
8444 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008445 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 }
8448
Bram Moolenaar3988f642020-08-27 22:43:03 +02008449 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008450 && ea.cmdidx != CMD_elseif
8451 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008452 && ea.cmdidx != CMD_endif
8453 && ea.cmdidx != CMD_endfor
8454 && ea.cmdidx != CMD_endwhile
8455 && ea.cmdidx != CMD_catch
8456 && ea.cmdidx != CMD_finally
8457 && ea.cmdidx != CMD_endtry)
8458 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008459 emsg(_(e_unreachable_code_after_return));
8460 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008461 }
8462
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008463 p = skipwhite(p);
8464 if (ea.cmdidx != CMD_SIZE
8465 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8466 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008467 if (ea.cmdidx >= 0)
8468 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008469 if ((ea.argt & EX_BANG) && *p == '!')
8470 {
8471 ea.forceit = TRUE;
8472 p = skipwhite(p + 1);
8473 }
8474 }
8475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 switch (ea.cmdidx)
8477 {
8478 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008479 ea.arg = p;
8480 line = compile_nested_function(&ea, &cctx);
8481 break;
8482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008483 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008484 // TODO: should we allow this, e.g. to declare a global
8485 // function?
8486 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487 goto erret;
8488
8489 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008490 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008491 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008492 break;
8493
8494 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008495 emsg(_(e_cannot_use_let_in_vim9_script));
8496 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008497 case CMD_var:
8498 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 case CMD_const:
8500 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008501 if (line == p)
8502 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503 break;
8504
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008505 case CMD_unlet:
8506 case CMD_unlockvar:
8507 case CMD_lockvar:
8508 line = compile_unletlock(p, &ea, &cctx);
8509 break;
8510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 case CMD_import:
8512 line = compile_import(p, &cctx);
8513 break;
8514
8515 case CMD_if:
8516 line = compile_if(p, &cctx);
8517 break;
8518 case CMD_elseif:
8519 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008520 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008521 break;
8522 case CMD_else:
8523 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008524 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008525 break;
8526 case CMD_endif:
8527 line = compile_endif(p, &cctx);
8528 break;
8529
8530 case CMD_while:
8531 line = compile_while(p, &cctx);
8532 break;
8533 case CMD_endwhile:
8534 line = compile_endwhile(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
8538 case CMD_for:
8539 line = compile_for(p, &cctx);
8540 break;
8541 case CMD_endfor:
8542 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008543 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 break;
8545 case CMD_continue:
8546 line = compile_continue(p, &cctx);
8547 break;
8548 case CMD_break:
8549 line = compile_break(p, &cctx);
8550 break;
8551
8552 case CMD_try:
8553 line = compile_try(p, &cctx);
8554 break;
8555 case CMD_catch:
8556 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008557 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 break;
8559 case CMD_finally:
8560 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008561 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562 break;
8563 case CMD_endtry:
8564 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008565 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008566 break;
8567 case CMD_throw:
8568 line = compile_throw(p, &cctx);
8569 break;
8570
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008571 case CMD_eval:
8572 if (compile_expr0(&p, &cctx) == FAIL)
8573 goto erret;
8574
Bram Moolenaar3988f642020-08-27 22:43:03 +02008575 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008576 generate_instr_drop(&cctx, ISN_DROP, 1);
8577
8578 line = skipwhite(p);
8579 break;
8580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008583 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008584 case CMD_echomsg:
8585 case CMD_echoerr:
8586 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008587 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008589 case CMD_put:
8590 ea.cmd = cmd;
8591 line = compile_put(p, &ea, &cctx);
8592 break;
8593
Bram Moolenaar3988f642020-08-27 22:43:03 +02008594 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008595
Bram Moolenaarae616492020-07-28 20:07:27 +02008596 case CMD_append:
8597 case CMD_change:
8598 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008599 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008600 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008601 case CMD_xit:
8602 not_in_vim9(&ea);
8603 goto erret;
8604
Bram Moolenaar002262f2020-07-08 17:47:57 +02008605 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008606 if (cctx.ctx_skip != SKIP_YES)
8607 {
8608 semsg(_(e_invalid_command_str), ea.cmd);
8609 goto erret;
8610 }
8611 // We don't check for a next command here.
8612 line = (char_u *)"";
8613 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008615 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008616 if (cctx.ctx_skip == SKIP_YES)
8617 {
8618 // We don't check for a next command here.
8619 line = (char_u *)"";
8620 }
8621 else
8622 {
8623 // Not recognized, execute with do_cmdline_cmd().
8624 ea.arg = p;
8625 line = compile_exec(line, &ea, &cctx);
8626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008627 break;
8628 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008629nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008630 if (line == NULL)
8631 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008632 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008633
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008634 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008635 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637 if (cctx.ctx_type_stack.ga_len < 0)
8638 {
8639 iemsg("Type stack underflow");
8640 goto erret;
8641 }
8642 }
8643
8644 if (cctx.ctx_scope != NULL)
8645 {
8646 if (cctx.ctx_scope->se_type == IF_SCOPE)
8647 emsg(_(e_endif));
8648 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8649 emsg(_(e_endwhile));
8650 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8651 emsg(_(e_endfor));
8652 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008653 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008654 goto erret;
8655 }
8656
Bram Moolenaarefd88552020-06-18 20:50:10 +02008657 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008658 {
8659 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8660 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008661 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662 goto erret;
8663 }
8664
8665 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008666 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008667 }
8668
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008669 {
8670 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8671 + ufunc->uf_dfunc_idx;
8672 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008673 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008674#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008675 if (cctx.ctx_profiling)
8676 {
8677 dfunc->df_instr_prof = instr->ga_data;
8678 dfunc->df_instr_prof_count = instr->ga_len;
8679 }
8680 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008681#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008682 {
8683 dfunc->df_instr = instr->ga_data;
8684 dfunc->df_instr_count = instr->ga_len;
8685 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008686 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008687 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008688 if (cctx.ctx_outer_used)
8689 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008690 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692
8693 ret = OK;
8694
8695erret:
8696 if (ret == FAIL)
8697 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008698 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008699 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8700 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008701
8702 for (idx = 0; idx < instr->ga_len; ++idx)
8703 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008704 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008705 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008706
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008707 // If using the last entry in the table and it was added above, we
8708 // might as well remove it.
8709 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008710 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008711 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008712 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008713 ufunc->uf_dfunc_idx = 0;
8714 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008715 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008716
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008717 while (cctx.ctx_scope != NULL)
8718 drop_scope(&cctx);
8719
Bram Moolenaar20431c92020-03-20 18:39:46 +01008720 // Don't execute this function body.
8721 ga_clear_strings(&ufunc->uf_lines);
8722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 if (errormsg != NULL)
8724 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008725 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008726 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 }
8728
8729 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008730 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008731 if (do_estack_push)
8732 estack_pop();
8733
Bram Moolenaar20431c92020-03-20 18:39:46 +01008734 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008735 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008736 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008737 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008738}
8739
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008740 void
8741set_function_type(ufunc_T *ufunc)
8742{
8743 int varargs = ufunc->uf_va_name != NULL;
8744 int argcount = ufunc->uf_args.ga_len;
8745
8746 // Create a type for the function, with the return type and any
8747 // argument types.
8748 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8749 // The type is included in "tt_args".
8750 if (argcount > 0 || varargs)
8751 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008752 if (ufunc->uf_type_list.ga_itemsize == 0)
8753 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008754 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8755 argcount, &ufunc->uf_type_list);
8756 // Add argument types to the function type.
8757 if (func_type_add_arg_types(ufunc->uf_func_type,
8758 argcount + varargs,
8759 &ufunc->uf_type_list) == FAIL)
8760 return;
8761 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8762 ufunc->uf_func_type->tt_min_argcount =
8763 argcount - ufunc->uf_def_args.ga_len;
8764 if (ufunc->uf_arg_types == NULL)
8765 {
8766 int i;
8767
8768 // lambda does not have argument types.
8769 for (i = 0; i < argcount; ++i)
8770 ufunc->uf_func_type->tt_args[i] = &t_any;
8771 }
8772 else
8773 mch_memmove(ufunc->uf_func_type->tt_args,
8774 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8775 if (varargs)
8776 {
8777 ufunc->uf_func_type->tt_args[argcount] =
8778 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8779 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8780 }
8781 }
8782 else
8783 // No arguments, can use a predefined type.
8784 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8785 argcount, &ufunc->uf_type_list);
8786}
8787
8788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789/*
8790 * Delete an instruction, free what it contains.
8791 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008792 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793delete_instr(isn_T *isn)
8794{
8795 switch (isn->isn_type)
8796 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008797 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008799 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008800 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 case ISN_LOADENV:
8802 case ISN_LOADG:
8803 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008804 case ISN_LOADT:
8805 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008807 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008809 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008810 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008811 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008812 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008814 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008815 case ISN_STOREW:
8816 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817 vim_free(isn->isn_arg.string);
8818 break;
8819
8820 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008821 case ISN_STORES:
8822 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823 break;
8824
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008825 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008826 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008827 vim_free(isn->isn_arg.unlet.ul_name);
8828 break;
8829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 case ISN_STOREOPT:
8831 vim_free(isn->isn_arg.storeopt.so_name);
8832 break;
8833
8834 case ISN_PUSHBLOB: // push blob isn_arg.blob
8835 blob_unref(isn->isn_arg.blob);
8836 break;
8837
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008838 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008840 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008841#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008842 break;
8843
8844 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008845#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008846 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008847#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008848 break;
8849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008850 case ISN_UCALL:
8851 vim_free(isn->isn_arg.ufunc.cuf_name);
8852 break;
8853
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008854 case ISN_FUNCREF:
8855 {
8856 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8857 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008858 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008859
Bram Moolenaar077a4232020-12-22 18:33:27 +01008860 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8861 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008862 }
8863 break;
8864
8865 case ISN_DCALL:
8866 {
8867 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8868 + isn->isn_arg.dfunc.cdf_idx;
8869
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008870 if (dfunc->df_ufunc != NULL
8871 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008872 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008873 }
8874 break;
8875
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008876 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008877 {
8878 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8879 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8880
8881 if (ufunc != NULL)
8882 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008883 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008884 func_ptr_unref(ufunc);
8885 }
8886
8887 vim_free(lambda);
8888 vim_free(isn->isn_arg.newfunc.nf_global);
8889 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008890 break;
8891
Bram Moolenaar5e654232020-09-16 15:22:00 +02008892 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008893 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008894 free_type(isn->isn_arg.type.ct_type);
8895 break;
8896
Bram Moolenaar02194d22020-10-24 23:08:38 +02008897 case ISN_CMDMOD:
8898 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8899 ->cmod_filter_regmatch.regprog);
8900 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8901 break;
8902
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008903 case ISN_LOADSCRIPT:
8904 case ISN_STORESCRIPT:
8905 vim_free(isn->isn_arg.script.scriptref);
8906 break;
8907
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008908 case ISN_TRY:
8909 vim_free(isn->isn_arg.try.try_ref);
8910 break;
8911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912 case ISN_2BOOL:
8913 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008914 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008915 case ISN_ADDBLOB:
8916 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008917 case ISN_ANYINDEX:
8918 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008919 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008920 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008921 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008922 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008923 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008924 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008925 case ISN_COMPAREANY:
8926 case ISN_COMPAREBLOB:
8927 case ISN_COMPAREBOOL:
8928 case ISN_COMPAREDICT:
8929 case ISN_COMPAREFLOAT:
8930 case ISN_COMPAREFUNC:
8931 case ISN_COMPARELIST:
8932 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008933 case ISN_COMPARESPECIAL:
8934 case ISN_COMPARESTRING:
8935 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008936 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008937 case ISN_DROP:
8938 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008939 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008940 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008941 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008942 case ISN_EXECCONCAT:
8943 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008944 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008946 case ISN_GETITEM:
8947 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008948 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008949 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008950 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008951 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008952 case ISN_LOADBDICT:
8953 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008954 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008956 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008958 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008959 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008960 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008961 case ISN_NEGATENR:
8962 case ISN_NEWDICT:
8963 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008964 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008965 case ISN_OPFLOAT:
8966 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008968 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008969 case ISN_PROF_END:
8970 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008971 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008972 case ISN_PUSHF:
8973 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008974 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008975 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008976 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008977 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008978 case ISN_SHUFFLE:
8979 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008980 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008981 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008982 case ISN_STORENR:
8983 case ISN_STOREOUTER:
8984 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008985 case ISN_STOREV:
8986 case ISN_STRINDEX:
8987 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01008989 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008990 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01008991 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008992 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008993 // nothing allocated
8994 break;
8995 }
8996}
8997
8998/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008999 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009000 */
9001 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009002delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009003{
9004 int idx;
9005
9006 ga_clear(&dfunc->df_def_args_isn);
9007
9008 if (dfunc->df_instr != NULL)
9009 {
9010 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9011 delete_instr(dfunc->df_instr + idx);
9012 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009013 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009014 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009015#ifdef FEAT_PROFILE
9016 if (dfunc->df_instr_prof != NULL)
9017 {
9018 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9019 delete_instr(dfunc->df_instr_prof + idx);
9020 VIM_CLEAR(dfunc->df_instr_prof);
9021 dfunc->df_instr_prof = NULL;
9022 }
9023#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009024
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009025 if (mark_deleted)
9026 dfunc->df_deleted = TRUE;
9027 if (dfunc->df_ufunc != NULL)
9028 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009029}
9030
9031/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009032 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009033 * function, unless another user function still uses it.
9034 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009035 */
9036 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009037unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009038{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009039 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009040 {
9041 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9042 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009043
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009044 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009045 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009046 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009047 ufunc->uf_dfunc_idx = 0;
9048 if (dfunc->df_ufunc == ufunc)
9049 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009050 }
9051}
9052
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009053/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009054 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009055 */
9056 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009057link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009058{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009059 if (ufunc->uf_dfunc_idx > 0)
9060 {
9061 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9062 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009063
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009064 ++dfunc->df_refcount;
9065 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009066}
9067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009068#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009069/*
9070 * Free all functions defined with ":def".
9071 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009072 void
9073free_def_functions(void)
9074{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009075 int idx;
9076
9077 for (idx = 0; idx < def_functions.ga_len; ++idx)
9078 {
9079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9080
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009081 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009082 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009083 }
9084
9085 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009086}
9087#endif
9088
9089
9090#endif // FEAT_EVAL