blob: 7c1dca533af422f0d6cacf59b306ae8ae888564f [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/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004453 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 * - 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 Moolenaard345fb92021-03-10 18:43:09 +01004535 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004536 if (*op == '.')
4537 {
4538 if (may_generate_2STRING(-2, cctx) == FAIL
4539 || may_generate_2STRING(-1, cctx) == FAIL)
4540 return FAIL;
4541 generate_instr_drop(cctx, ISN_CONCAT, 1);
4542 }
4543 else
4544 generate_two_op(cctx, op);
4545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 }
4547
4548 return OK;
4549}
4550
4551/*
4552 * expr5a == expr5b
4553 * expr5a =~ expr5b
4554 * expr5a != expr5b
4555 * expr5a !~ expr5b
4556 * expr5a > expr5b
4557 * expr5a >= expr5b
4558 * expr5a < expr5b
4559 * expr5a <= expr5b
4560 * expr5a is expr5b
4561 * expr5a isnot expr5b
4562 *
4563 * Produces instructions:
4564 * EVAL expr5a Push result of "expr5a"
4565 * EVAL expr5b Push result of "expr5b"
4566 * COMPARE one of the compare instructions
4567 */
4568 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004571 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004573 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577
4578 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 return FAIL;
4581
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004582 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004583 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584
4585 /*
4586 * If there is a comparative operator, use it.
4587 */
4588 if (type != EXPR_UNKNOWN)
4589 {
4590 int ic = FALSE; // Default: do not ignore case
4591
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004592 if (next != NULL)
4593 {
4594 *arg = next_line_from_context(cctx, TRUE);
4595 p = skipwhite(*arg);
4596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 if (type_is && (p[len] == '?' || p[len] == '#'))
4598 {
4599 semsg(_(e_invexpr2), *arg);
4600 return FAIL;
4601 }
4602 // extra question mark appended: ignore case
4603 if (p[len] == '?')
4604 {
4605 ic = TRUE;
4606 ++len;
4607 }
4608 // extra '#' appended: match case (ignored)
4609 else if (p[len] == '#')
4610 ++len;
4611 // nothing appended: match case
4612
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004613 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004615 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004616 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 }
4618
4619 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004620 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004621 return FAIL;
4622
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 return FAIL;
4625
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 if (ppconst->pp_used == ppconst_used + 2)
4627 {
4628 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4629 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4630 int ret;
4631
4632 // Both sides are a constant, compute the result now.
4633 // First check for a valid combination of types, this is more
4634 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004635 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 ret = FAIL;
4637 else
4638 {
4639 ret = typval_compare(tv1, tv2, type, ic);
4640 tv1->v_type = VAR_BOOL;
4641 tv1->vval.v_number = tv1->vval.v_number
4642 ? VVAL_TRUE : VVAL_FALSE;
4643 clear_tv(tv2);
4644 --ppconst->pp_used;
4645 }
4646 return ret;
4647 }
4648
4649 generate_ppconst(cctx, ppconst);
4650 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004651 }
4652
4653 return OK;
4654}
4655
Bram Moolenaar7f141552020-05-09 17:35:53 +02004656static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658/*
4659 * Compile || or &&.
4660 */
4661 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662compile_and_or(
4663 char_u **arg,
4664 cctx_T *cctx,
4665 char *op,
4666 ppconst_T *ppconst,
4667 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004669 char_u *next;
4670 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 int opchar = *op;
4672
4673 if (p[0] == opchar && p[1] == opchar)
4674 {
4675 garray_T *instr = &cctx->ctx_instr;
4676 garray_T end_ga;
4677
4678 /*
4679 * Repeat until there is no following "||" or "&&"
4680 */
4681 ga_init2(&end_ga, sizeof(int), 10);
4682 while (p[0] == opchar && p[1] == opchar)
4683 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004684 if (next != NULL)
4685 {
4686 *arg = next_line_from_context(cctx, TRUE);
4687 p = skipwhite(*arg);
4688 }
4689
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004690 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4691 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004692 semsg(_(e_white_space_required_before_and_after_str_at_str),
4693 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004694 return FAIL;
4695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004697 // TODO: use ppconst if the value is a constant and check
4698 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004699 generate_ppconst(cctx, ppconst);
4700
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004701 // Every part must evaluate to a bool.
4702 if (bool_on_stack(cctx) == FAIL)
4703 {
4704 ga_clear(&end_ga);
4705 return FAIL;
4706 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 if (ga_grow(&end_ga, 1) == FAIL)
4709 {
4710 ga_clear(&end_ga);
4711 return FAIL;
4712 }
4713 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4714 ++end_ga.ga_len;
4715 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004716 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717
4718 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004719 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004720 {
4721 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004722 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004723 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004724
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4726 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 {
4728 ga_clear(&end_ga);
4729 return FAIL;
4730 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004731
4732 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004736 // Every part must evaluate to a bool.
4737 if (bool_on_stack(cctx) == FAIL)
4738 {
4739 ga_clear(&end_ga);
4740 return FAIL;
4741 }
4742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 // Fill in the end label in all jumps.
4744 while (end_ga.ga_len > 0)
4745 {
4746 isn_T *isn;
4747
4748 --end_ga.ga_len;
4749 isn = ((isn_T *)instr->ga_data)
4750 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4751 isn->isn_arg.jump.jump_where = instr->ga_len;
4752 }
4753 ga_clear(&end_ga);
4754 }
4755
4756 return OK;
4757}
4758
4759/*
4760 * expr4a && expr4a && expr4a logical AND
4761 *
4762 * Produces instructions:
4763 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004764 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004765 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004767 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 * EVAL expr4c Push result of "expr4c"
4769 * end:
4770 */
4771 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004772compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 int ppconst_used = ppconst->pp_used;
4775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 return FAIL;
4779
4780 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004781 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782}
4783
4784/*
4785 * expr3a || expr3b || expr3c logical OR
4786 *
4787 * Produces instructions:
4788 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004789 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004790 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004792 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 * EVAL expr3c Push result of "expr3c"
4794 * end:
4795 */
4796 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004799 int ppconst_used = ppconst->pp_used;
4800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004802 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 return FAIL;
4804
4805 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004807}
4808
4809/*
4810 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004812 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004813 * JUMP_IF_FALSE alt jump if false
4814 * EVAL expr1a
4815 * JUMP_ALWAYS end
4816 * alt: EVAL expr1b
4817 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004818 *
4819 * Toplevel expression: expr2 ?? expr1
4820 * Produces instructions:
4821 * EVAL expr2 Push result of "expr2"
4822 * JUMP_AND_KEEP_IF_TRUE end jump if true
4823 * EVAL expr1
4824 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 */
4826 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004827compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828{
4829 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004830 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004831 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832
Bram Moolenaar3988f642020-08-27 22:43:03 +02004833 // Ignore all kinds of errors when not producing code.
4834 if (cctx->ctx_skip == SKIP_YES)
4835 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004836 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004837 return OK;
4838 }
4839
Bram Moolenaar61a89812020-05-07 16:58:17 +02004840 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 return FAIL;
4843
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004844 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 if (*p == '?')
4846 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004847 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 garray_T *instr = &cctx->ctx_instr;
4849 garray_T *stack = &cctx->ctx_type_stack;
4850 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004851 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004853 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 int has_const_expr = FALSE;
4855 int const_value = FALSE;
4856 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004858 if (next != NULL)
4859 {
4860 *arg = next_line_from_context(cctx, TRUE);
4861 p = skipwhite(*arg);
4862 }
4863
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004864 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004865 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004866 semsg(_(e_white_space_required_before_and_after_str_at_str),
4867 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004868 return FAIL;
4869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870
Bram Moolenaara5565e42020-05-09 15:44:01 +02004871 if (ppconst->pp_used == ppconst_used + 1)
4872 {
4873 // the condition is a constant, we know whether the ? or the :
4874 // expression is to be evaluated.
4875 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004876 if (op_falsy)
4877 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4878 else
4879 {
4880 int error = FALSE;
4881
4882 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4883 &error);
4884 if (error)
4885 return FAIL;
4886 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004887 cctx->ctx_skip = save_skip == SKIP_YES ||
4888 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4889
4890 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4891 // "left ?? right" and "left" is truthy: produce "left"
4892 generate_ppconst(cctx, ppconst);
4893 else
4894 {
4895 clear_tv(&ppconst->pp_tv[ppconst_used]);
4896 --ppconst->pp_used;
4897 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004898 }
4899 else
4900 {
4901 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004902 if (op_falsy)
4903 end_idx = instr->ga_len;
4904 generate_JUMP(cctx, op_falsy
4905 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4906 if (op_falsy)
4907 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909
4910 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004911 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004912 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004913 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004914 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 if (!has_const_expr)
4917 {
4918 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004920 if (!op_falsy)
4921 {
4922 // remember the type and drop it
4923 --stack->ga_len;
4924 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004926 end_idx = instr->ga_len;
4927 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004928
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004929 // jump here from JUMP_IF_FALSE
4930 isn = ((isn_T *)instr->ga_data) + alt_idx;
4931 isn->isn_arg.jump.jump_where = instr->ga_len;
4932 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004935 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004937 // Check for the ":".
4938 p = may_peek_next_line(cctx, *arg, &next);
4939 if (*p != ':')
4940 {
4941 emsg(_(e_missing_colon));
4942 return FAIL;
4943 }
4944 if (next != NULL)
4945 {
4946 *arg = next_line_from_context(cctx, TRUE);
4947 p = skipwhite(*arg);
4948 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004949
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004950 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4951 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004952 semsg(_(e_white_space_required_before_and_after_str_at_str),
4953 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004954 return FAIL;
4955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004957 // evaluate the third expression
4958 if (has_const_expr)
4959 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004960 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004961 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004962 return FAIL;
4963 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4964 return FAIL;
4965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967 if (!has_const_expr)
4968 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004969 type_T **typep;
4970
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972
Bram Moolenaara5565e42020-05-09 15:44:01 +02004973 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004974 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4975 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004976
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004977 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004978 isn = ((isn_T *)instr->ga_data) + end_idx;
4979 isn->isn_arg.jump.jump_where = instr->ga_len;
4980 }
4981
4982 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 }
4984 return OK;
4985}
4986
4987/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004988 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004989 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4990 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004991 */
4992 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004993compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004994{
4995 ppconst_T ppconst;
4996
4997 CLEAR_FIELD(ppconst);
4998 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4999 {
5000 clear_ppconst(&ppconst);
5001 return FAIL;
5002 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005003 if (is_const != NULL)
5004 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005005 if (generate_ppconst(cctx, &ppconst) == FAIL)
5006 return FAIL;
5007 return OK;
5008}
5009
5010/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005011 * Toplevel expression.
5012 */
5013 static int
5014compile_expr0(char_u **arg, cctx_T *cctx)
5015{
5016 return compile_expr0_ext(arg, cctx, NULL);
5017}
5018
5019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 * compile "return [expr]"
5021 */
5022 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005023compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024{
5025 char_u *p = arg;
5026 garray_T *stack = &cctx->ctx_type_stack;
5027 type_T *stack_type;
5028
5029 if (*p != NUL && *p != '|' && *p != '\n')
5030 {
5031 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005032 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 return NULL;
5034
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005035 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005036 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005037 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005038 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005039 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5040 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005041 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005042 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005043 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005044 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005045 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005046 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5047 && stack_type->tt_type != VAR_VOID
5048 && stack_type->tt_type != VAR_UNKNOWN)
5049 {
5050 emsg(_(e_returning_value_in_function_without_return_type));
5051 return NULL;
5052 }
5053 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005054 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005055 return NULL;
5056 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 }
5059 else
5060 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005061 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005062 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005063 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5064 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005066 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 return NULL;
5068 }
5069
5070 // No argument, return zero.
5071 generate_PUSHNR(cctx, 0);
5072 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005073
5074 // Undo any command modifiers.
5075 generate_undo_cmdmods(cctx);
5076
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005077 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 return NULL;
5079
5080 // "return val | endif" is possible
5081 return skipwhite(p);
5082}
5083
5084/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005085 * Get a line from the compilation context, compatible with exarg_T getline().
5086 * Return a pointer to the line in allocated memory.
5087 * Return NULL for end-of-file or some error.
5088 */
5089 static char_u *
5090exarg_getline(
5091 int c UNUSED,
5092 void *cookie,
5093 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005094 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005095{
5096 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005097 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005098
Bram Moolenaar66250c92020-08-20 15:02:42 +02005099 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005100 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005101 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005102 return NULL;
5103 ++cctx->ctx_lnum;
5104 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5105 // Comment lines result in NULL pointers, skip them.
5106 if (p != NULL)
5107 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005108 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005109}
5110
5111/*
5112 * Compile a nested :def command.
5113 */
5114 static char_u *
5115compile_nested_function(exarg_T *eap, cctx_T *cctx)
5116{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005117 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005118 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005119 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005120 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005121 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005122 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005123
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005124 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005125 {
5126 emsg(_(e_cannot_use_bang_with_nested_def));
5127 return NULL;
5128 }
5129
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005130 if (*name_start == '/')
5131 {
5132 name_end = skip_regexp(name_start + 1, '/', TRUE);
5133 if (*name_end == '/')
5134 ++name_end;
5135 eap->nextcmd = check_nextcmd(name_end);
5136 }
5137 if (name_end == name_start || *skipwhite(name_end) != '(')
5138 {
5139 if (!ends_excmd2(name_start, name_end))
5140 {
5141 semsg(_(e_invalid_command_str), eap->cmd);
5142 return NULL;
5143 }
5144
5145 // "def" or "def Name": list functions
5146 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5147 return NULL;
5148 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5149 }
5150
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005151 // Only g:Func() can use a namespace.
5152 if (name_start[1] == ':' && !is_global)
5153 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005154 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005155 return NULL;
5156 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005157 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005158 return NULL;
5159
Bram Moolenaar04b12692020-05-04 23:24:44 +02005160 eap->arg = name_end;
5161 eap->getline = exarg_getline;
5162 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005163 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005164 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005165 lambda_name = vim_strsave(get_lambda_name());
5166 if (lambda_name == NULL)
5167 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005168 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005169
Bram Moolenaar822ba242020-05-24 23:00:18 +02005170 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005171 {
5172 r = eap->skip ? OK : FAIL;
5173 goto theend;
5174 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005175 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5176 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005177 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005178 {
5179 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005180 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005181 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005182
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005183 if (is_global)
5184 {
5185 char_u *func_name = vim_strnsave(name_start + 2,
5186 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005187
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005188 if (func_name == NULL)
5189 r = FAIL;
5190 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005191 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005192 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005193 lambda_name = NULL;
5194 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005195 }
5196 else
5197 {
5198 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005199 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005200 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005201 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005202
Bram Moolenaareef21022020-08-01 22:16:43 +02005203 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005204 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005205 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005206 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005207 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005208
5209 // copy over the block scope IDs
5210 if (block_depth > 0)
5211 {
5212 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5213 if (ufunc->uf_block_ids != NULL)
5214 {
5215 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5216 sizeof(int) * block_depth);
5217 ufunc->uf_block_depth = block_depth;
5218 }
5219 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005220 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005221 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005222
5223theend:
5224 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005225 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005226}
5227
5228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 * Return the length of an assignment operator, or zero if there isn't one.
5230 */
5231 int
5232assignment_len(char_u *p, int *heredoc)
5233{
5234 if (*p == '=')
5235 {
5236 if (p[1] == '<' && p[2] == '<')
5237 {
5238 *heredoc = TRUE;
5239 return 3;
5240 }
5241 return 1;
5242 }
5243 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5244 return 2;
5245 if (STRNCMP(p, "..=", 3) == 0)
5246 return 3;
5247 return 0;
5248}
5249
5250// words that cannot be used as a variable
5251static char *reserved[] = {
5252 "true",
5253 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005254 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 NULL
5256};
5257
Bram Moolenaar752fc692021-01-04 21:57:11 +01005258// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005259typedef enum {
5260 dest_local,
5261 dest_option,
5262 dest_env,
5263 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005264 dest_buffer,
5265 dest_window,
5266 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005267 dest_vimvar,
5268 dest_script,
5269 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005270 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005271} assign_dest_T;
5272
Bram Moolenaar752fc692021-01-04 21:57:11 +01005273// Used by compile_lhs() to store information about the LHS of an assignment
5274// and one argument of ":unlet" with an index.
5275typedef struct {
5276 assign_dest_T lhs_dest; // type of destination
5277
5278 char_u *lhs_name; // allocated name including
5279 // "[expr]" or ".name".
5280 size_t lhs_varlen; // length of the variable without
5281 // "[expr]" or ".name"
5282 char_u *lhs_dest_end; // end of the destination, including
5283 // "[expr]" or ".name".
5284
5285 int lhs_has_index; // has "[expr]" or ".name"
5286
5287 int lhs_new_local; // create new local variable
5288 int lhs_opt_flags; // for when destination is an option
5289 int lhs_vimvaridx; // for when destination is a v:var
5290
5291 lvar_T lhs_local_lvar; // used for existing local destination
5292 lvar_T lhs_arg_lvar; // used for argument destination
5293 lvar_T *lhs_lvar; // points to destination lvar
5294 int lhs_scriptvar_sid;
5295 int lhs_scriptvar_idx;
5296
5297 int lhs_has_type; // type was specified
5298 type_T *lhs_type;
5299 type_T *lhs_member_type;
5300} lhs_T;
5301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005303 * Generate the load instruction for "name".
5304 */
5305 static void
5306generate_loadvar(
5307 cctx_T *cctx,
5308 assign_dest_T dest,
5309 char_u *name,
5310 lvar_T *lvar,
5311 type_T *type)
5312{
5313 switch (dest)
5314 {
5315 case dest_option:
5316 // TODO: check the option exists
5317 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5318 break;
5319 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005320 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5321 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5322 else
5323 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 break;
5325 case dest_buffer:
5326 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5327 break;
5328 case dest_window:
5329 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5330 break;
5331 case dest_tab:
5332 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5333 break;
5334 case dest_script:
5335 compile_load_scriptvar(cctx,
5336 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5337 break;
5338 case dest_env:
5339 // Include $ in the name here
5340 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5341 break;
5342 case dest_reg:
5343 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5344 break;
5345 case dest_vimvar:
5346 generate_LOADV(cctx, name + 2, TRUE);
5347 break;
5348 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005349 if (lvar->lv_from_outer > 0)
5350 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5351 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005352 else
5353 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5354 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005355 case dest_expr:
5356 // list or dict value should already be on the stack.
5357 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005358 }
5359}
5360
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005361/*
5362 * Skip over "[expr]" or ".member".
5363 * Does not check for any errors.
5364 */
5365 static char_u *
5366skip_index(char_u *start)
5367{
5368 char_u *p = start;
5369
5370 if (*p == '[')
5371 {
5372 p = skipwhite(p + 1);
5373 (void)skip_expr(&p, NULL);
5374 p = skipwhite(p);
5375 if (*p == ']')
5376 return p + 1;
5377 return p;
5378 }
5379 // if (*p == '.')
5380 return to_name_end(p + 1, TRUE);
5381}
5382
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005383 void
5384vim9_declare_error(char_u *name)
5385{
5386 char *scope = "";
5387
5388 switch (*name)
5389 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005390 case 'g': scope = _("global"); break;
5391 case 'b': scope = _("buffer"); break;
5392 case 'w': scope = _("window"); break;
5393 case 't': scope = _("tab"); break;
5394 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005395 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005396 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005397 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005398 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005399 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005400 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005401 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005402 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005403 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005404}
5405
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005406/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005407 * For one assignment figure out the type of destination. Return it in "dest".
5408 * When not recognized "dest" is not set.
5409 * For an option "opt_flags" is set.
5410 * For a v:var "vimvaridx" is set.
5411 * "type" is set to the destination type if known, unchanted otherwise.
5412 * Return FAIL if an error message was given.
5413 */
5414 static int
5415get_var_dest(
5416 char_u *name,
5417 assign_dest_T *dest,
5418 int cmdidx,
5419 int *opt_flags,
5420 int *vimvaridx,
5421 type_T **type,
5422 cctx_T *cctx)
5423{
5424 char_u *p;
5425
5426 if (*name == '&')
5427 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005428 int cc;
5429 long numval;
5430 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005431
5432 *dest = dest_option;
5433 if (cmdidx == CMD_final || cmdidx == CMD_const)
5434 {
5435 emsg(_(e_const_option));
5436 return FAIL;
5437 }
5438 p = name;
5439 p = find_option_end(&p, opt_flags);
5440 if (p == NULL)
5441 {
5442 // cannot happen?
5443 emsg(_(e_letunexp));
5444 return FAIL;
5445 }
5446 cc = *p;
5447 *p = NUL;
5448 opt_type = get_option_value(skip_option_env_lead(name),
5449 &numval, NULL, *opt_flags);
5450 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005451 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005452 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005453 case gov_unknown:
5454 semsg(_(e_unknown_option), name);
5455 return FAIL;
5456 case gov_string:
5457 case gov_hidden_string:
5458 *type = &t_string;
5459 break;
5460 case gov_bool:
5461 case gov_hidden_bool:
5462 *type = &t_bool;
5463 break;
5464 case gov_number:
5465 case gov_hidden_number:
5466 *type = &t_number;
5467 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005468 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005469 }
5470 else if (*name == '$')
5471 {
5472 *dest = dest_env;
5473 *type = &t_string;
5474 }
5475 else if (*name == '@')
5476 {
5477 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5478 {
5479 emsg_invreg(name[1]);
5480 return FAIL;
5481 }
5482 *dest = dest_reg;
5483 *type = &t_string;
5484 }
5485 else if (STRNCMP(name, "g:", 2) == 0)
5486 {
5487 *dest = dest_global;
5488 }
5489 else if (STRNCMP(name, "b:", 2) == 0)
5490 {
5491 *dest = dest_buffer;
5492 }
5493 else if (STRNCMP(name, "w:", 2) == 0)
5494 {
5495 *dest = dest_window;
5496 }
5497 else if (STRNCMP(name, "t:", 2) == 0)
5498 {
5499 *dest = dest_tab;
5500 }
5501 else if (STRNCMP(name, "v:", 2) == 0)
5502 {
5503 typval_T *vtv;
5504 int di_flags;
5505
5506 *vimvaridx = find_vim_var(name + 2, &di_flags);
5507 if (*vimvaridx < 0)
5508 {
5509 semsg(_(e_variable_not_found_str), name);
5510 return FAIL;
5511 }
5512 // We use the current value of "sandbox" here, is that OK?
5513 if (var_check_ro(di_flags, name, FALSE))
5514 return FAIL;
5515 *dest = dest_vimvar;
5516 vtv = get_vim_var_tv(*vimvaridx);
5517 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5518 }
5519 return OK;
5520}
5521
5522/*
5523 * Generate a STORE instruction for "dest", not being "dest_local".
5524 * Return FAIL when out of memory.
5525 */
5526 static int
5527generate_store_var(
5528 cctx_T *cctx,
5529 assign_dest_T dest,
5530 int opt_flags,
5531 int vimvaridx,
5532 int scriptvar_idx,
5533 int scriptvar_sid,
5534 type_T *type,
5535 char_u *name)
5536{
5537 switch (dest)
5538 {
5539 case dest_option:
5540 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5541 opt_flags);
5542 case dest_global:
5543 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005544 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5545 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005546 case dest_buffer:
5547 // include b: with the name, easier to execute that way
5548 return generate_STORE(cctx, ISN_STOREB, 0, name);
5549 case dest_window:
5550 // include w: with the name, easier to execute that way
5551 return generate_STORE(cctx, ISN_STOREW, 0, name);
5552 case dest_tab:
5553 // include t: with the name, easier to execute that way
5554 return generate_STORE(cctx, ISN_STORET, 0, name);
5555 case dest_env:
5556 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5557 case dest_reg:
5558 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5559 case dest_vimvar:
5560 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5561 case dest_script:
5562 if (scriptvar_idx < 0)
5563 {
5564 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005565 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005566
Bram Moolenaar41d61962020-12-06 20:12:43 +01005567 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005568 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5569 scriptvar_sid, type);
5570 if (name_s != name)
5571 vim_free(name_s);
5572 return r;
5573 }
5574 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5575 scriptvar_sid, scriptvar_idx, type);
5576 case dest_local:
5577 case dest_expr:
5578 // cannot happen
5579 break;
5580 }
5581 return FAIL;
5582}
5583
Bram Moolenaar752fc692021-01-04 21:57:11 +01005584 static int
5585is_decl_command(int cmdidx)
5586{
5587 return cmdidx == CMD_let || cmdidx == CMD_var
5588 || cmdidx == CMD_final || cmdidx == CMD_const;
5589}
5590
5591/*
5592 * Figure out the LHS type and other properties for an assignment or one item
5593 * of ":unlet" with an index.
5594 * Returns OK or FAIL.
5595 */
5596 static int
5597compile_lhs(
5598 char_u *var_start,
5599 lhs_T *lhs,
5600 int cmdidx,
5601 int heredoc,
5602 int oplen,
5603 cctx_T *cctx)
5604{
5605 char_u *var_end;
5606 int is_decl = is_decl_command(cmdidx);
5607
5608 CLEAR_POINTER(lhs);
5609 lhs->lhs_dest = dest_local;
5610 lhs->lhs_vimvaridx = -1;
5611 lhs->lhs_scriptvar_idx = -1;
5612
5613 // "dest_end" is the end of the destination, including "[expr]" or
5614 // ".name".
5615 // "var_end" is the end of the variable/option/etc. name.
5616 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5617 if (*var_start == '@')
5618 var_end = var_start + 2;
5619 else
5620 {
5621 // skip over the leading "&", "&l:", "&g:" and "$"
5622 var_end = skip_option_env_lead(var_start);
5623 var_end = to_name_end(var_end, TRUE);
5624 }
5625
5626 // "a: type" is declaring variable "a" with a type, not dict "a:".
5627 if (is_decl && lhs->lhs_dest_end == var_start + 2
5628 && lhs->lhs_dest_end[-1] == ':')
5629 --lhs->lhs_dest_end;
5630 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5631 --var_end;
5632
5633 // compute the length of the destination without "[expr]" or ".name"
5634 lhs->lhs_varlen = var_end - var_start;
5635 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5636 if (lhs->lhs_name == NULL)
5637 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005638
5639 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5640 // Something follows after the variable: "var[idx]" or "var.key".
5641 lhs->lhs_has_index = TRUE;
5642
Bram Moolenaar752fc692021-01-04 21:57:11 +01005643 if (heredoc)
5644 lhs->lhs_type = &t_list_string;
5645 else
5646 lhs->lhs_type = &t_any;
5647
5648 if (cctx->ctx_skip != SKIP_YES)
5649 {
5650 int declare_error = FALSE;
5651
5652 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5653 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5654 &lhs->lhs_type, cctx) == FAIL)
5655 return FAIL;
5656 if (lhs->lhs_dest != dest_local)
5657 {
5658 // Specific kind of variable recognized.
5659 declare_error = is_decl;
5660 }
5661 else
5662 {
5663 int idx;
5664
5665 // No specific kind of variable recognized, just a name.
5666 for (idx = 0; reserved[idx] != NULL; ++idx)
5667 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5668 {
5669 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5670 return FAIL;
5671 }
5672
5673
5674 if (lookup_local(var_start, lhs->lhs_varlen,
5675 &lhs->lhs_local_lvar, cctx) == OK)
5676 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5677 else
5678 {
5679 CLEAR_FIELD(lhs->lhs_arg_lvar);
5680 if (arg_exists(var_start, lhs->lhs_varlen,
5681 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5682 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5683 {
5684 if (is_decl)
5685 {
5686 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5687 return FAIL;
5688 }
5689 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5690 }
5691 }
5692 if (lhs->lhs_lvar != NULL)
5693 {
5694 if (is_decl)
5695 {
5696 semsg(_(e_variable_already_declared), lhs->lhs_name);
5697 return FAIL;
5698 }
5699 }
5700 else
5701 {
5702 int script_namespace = lhs->lhs_varlen > 1
5703 && STRNCMP(var_start, "s:", 2) == 0;
5704 int script_var = (script_namespace
5705 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5706 FALSE, cctx)
5707 : script_var_exists(var_start, lhs->lhs_varlen,
5708 TRUE, cctx)) == OK;
5709 imported_T *import =
5710 find_imported(var_start, lhs->lhs_varlen, cctx);
5711
5712 if (script_namespace || script_var || import != NULL)
5713 {
5714 char_u *rawname = lhs->lhs_name
5715 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5716
5717 if (is_decl)
5718 {
5719 if (script_namespace)
5720 semsg(_(e_cannot_declare_script_variable_in_function),
5721 lhs->lhs_name);
5722 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005723 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005724 lhs->lhs_name);
5725 return FAIL;
5726 }
5727 else if (cctx->ctx_ufunc->uf_script_ctx_version
5728 == SCRIPT_VERSION_VIM9
5729 && script_namespace
5730 && !script_var && import == NULL)
5731 {
5732 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5733 return FAIL;
5734 }
5735
5736 lhs->lhs_dest = dest_script;
5737
5738 // existing script-local variables should have a type
5739 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5740 if (import != NULL)
5741 lhs->lhs_scriptvar_sid = import->imp_sid;
5742 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5743 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005744 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005745 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005746 lhs->lhs_scriptvar_sid, rawname,
5747 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5748 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005749 if (lhs->lhs_scriptvar_idx >= 0)
5750 {
5751 scriptitem_T *si = SCRIPT_ITEM(
5752 lhs->lhs_scriptvar_sid);
5753 svar_T *sv =
5754 ((svar_T *)si->sn_var_vals.ga_data)
5755 + lhs->lhs_scriptvar_idx;
5756 lhs->lhs_type = sv->sv_type;
5757 }
5758 }
5759 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005760 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005761 == FAIL)
5762 return FAIL;
5763 }
5764 }
5765
5766 if (declare_error)
5767 {
5768 vim9_declare_error(lhs->lhs_name);
5769 return FAIL;
5770 }
5771 }
5772
5773 // handle "a:name" as a name, not index "name" on "a"
5774 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5775 var_end = lhs->lhs_dest_end;
5776
5777 if (lhs->lhs_dest != dest_option)
5778 {
5779 if (is_decl && *var_end == ':')
5780 {
5781 char_u *p;
5782
5783 // parse optional type: "let var: type = expr"
5784 if (!VIM_ISWHITE(var_end[1]))
5785 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005786 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005787 return FAIL;
5788 }
5789 p = skipwhite(var_end + 1);
5790 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5791 if (lhs->lhs_type == NULL)
5792 return FAIL;
5793 lhs->lhs_has_type = TRUE;
5794 }
5795 else if (lhs->lhs_lvar != NULL)
5796 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5797 }
5798
5799 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5800 && lhs->lhs_type->tt_type != VAR_STRING
5801 && lhs->lhs_type->tt_type != VAR_ANY)
5802 {
5803 emsg(_(e_can_only_concatenate_to_string));
5804 return FAIL;
5805 }
5806
5807 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5808 && cctx->ctx_skip != SKIP_YES)
5809 {
5810 if (oplen > 1 && !heredoc)
5811 {
5812 // +=, /=, etc. require an existing variable
5813 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5814 return FAIL;
5815 }
5816 if (!is_decl)
5817 {
5818 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5819 return FAIL;
5820 }
5821
5822 // new local variable
5823 if ((lhs->lhs_type->tt_type == VAR_FUNC
5824 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5825 && var_wrong_func_name(lhs->lhs_name, TRUE))
5826 return FAIL;
5827 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5828 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5829 if (lhs->lhs_lvar == NULL)
5830 return FAIL;
5831 lhs->lhs_new_local = TRUE;
5832 }
5833
5834 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005835 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005836 {
5837 // Something follows after the variable: "var[idx]" or "var.key".
5838 // TODO: should we also handle "->func()" here?
5839 if (is_decl)
5840 {
5841 emsg(_(e_cannot_use_index_when_declaring_variable));
5842 return FAIL;
5843 }
5844
5845 if (var_start[lhs->lhs_varlen] == '['
5846 || var_start[lhs->lhs_varlen] == '.')
5847 {
5848 char_u *after = var_start + lhs->lhs_varlen;
5849 char_u *p;
5850
5851 // Only the last index is used below, if there are others
5852 // before it generate code for the expression. Thus for
5853 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5854 for (;;)
5855 {
5856 p = skip_index(after);
5857 if (*p != '[' && *p != '.')
5858 break;
5859 after = p;
5860 }
5861 if (after > var_start + lhs->lhs_varlen)
5862 {
5863 lhs->lhs_varlen = after - var_start;
5864 lhs->lhs_dest = dest_expr;
5865 // We don't know the type before evaluating the expression,
5866 // use "any" until then.
5867 lhs->lhs_type = &t_any;
5868 }
5869
Bram Moolenaar752fc692021-01-04 21:57:11 +01005870 if (lhs->lhs_type->tt_member == NULL)
5871 lhs->lhs_member_type = &t_any;
5872 else
5873 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5874 }
5875 else
5876 {
5877 semsg("Not supported yet: %s", var_start);
5878 return FAIL;
5879 }
5880 }
5881 return OK;
5882}
5883
5884/*
5885 * Assignment to a list or dict member, or ":unlet" for the item, using the
5886 * information in "lhs".
5887 * Returns OK or FAIL.
5888 */
5889 static int
5890compile_assign_unlet(
5891 char_u *var_start,
5892 lhs_T *lhs,
5893 int is_assign,
5894 type_T *rhs_type,
5895 cctx_T *cctx)
5896{
5897 char_u *p;
5898 int r;
5899 vartype_T dest_type;
5900 size_t varlen = lhs->lhs_varlen;
5901 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005902 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005903
5904 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5905 p = var_start + varlen;
5906 if (*p == '[')
5907 {
5908 p = skipwhite(p + 1);
5909 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005910
5911 if (r == OK && *skipwhite(p) == ':')
5912 {
5913 // unlet var[idx : idx]
5914 if (is_assign)
5915 {
5916 semsg(_(e_cannot_use_range_with_assignment_str), p);
5917 return FAIL;
5918 }
5919 range = TRUE;
5920 p = skipwhite(p);
5921 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5922 {
5923 semsg(_(e_white_space_required_before_and_after_str_at_str),
5924 ":", p);
5925 return FAIL;
5926 }
5927 p = skipwhite(p + 1);
5928 r = compile_expr0(&p, cctx);
5929 }
5930
Bram Moolenaar752fc692021-01-04 21:57:11 +01005931 if (r == OK && *skipwhite(p) != ']')
5932 {
5933 // this should not happen
5934 emsg(_(e_missbrac));
5935 r = FAIL;
5936 }
5937 }
5938 else // if (*p == '.')
5939 {
5940 char_u *key_end = to_name_end(p + 1, TRUE);
5941 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5942
5943 r = generate_PUSHS(cctx, key);
5944 }
5945 if (r == FAIL)
5946 return FAIL;
5947
5948 if (lhs->lhs_type == &t_any)
5949 {
5950 // Index on variable of unknown type: check at runtime.
5951 dest_type = VAR_ANY;
5952 }
5953 else
5954 {
5955 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005956 if (dest_type == VAR_DICT && range)
5957 {
5958 emsg(e_cannot_use_range_with_dictionary);
5959 return FAIL;
5960 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005961 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5962 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005963 if (dest_type == VAR_LIST)
5964 {
5965 if (range
5966 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005967 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005968 return FAIL;
5969 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5970 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
5971 return FAIL;
5972 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005973 }
5974
5975 // Load the dict or list. On the stack we then have:
5976 // - value (for assignment, not for :unlet)
5977 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005978 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01005979 // - variable
5980 if (lhs->lhs_dest == dest_expr)
5981 {
5982 int c = var_start[varlen];
5983
5984 // Evaluate "ll[expr]" of "ll[expr][idx]"
5985 p = var_start;
5986 var_start[varlen] = NUL;
5987 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5988 {
5989 // this should not happen
5990 emsg(_(e_missbrac));
5991 return FAIL;
5992 }
5993 var_start[varlen] = c;
5994
5995 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5996 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5997 // now we can properly check the type
5998 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005999 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006000 FALSE, FALSE) == FAIL)
6001 return FAIL;
6002 }
6003 else
6004 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6005 lhs->lhs_lvar, lhs->lhs_type);
6006
6007 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6008 {
6009 if (is_assign)
6010 {
6011 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6012
6013 if (isn == NULL)
6014 return FAIL;
6015 isn->isn_arg.vartype = dest_type;
6016 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006017 else if (range)
6018 {
6019 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6020 return FAIL;
6021 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006022 else
6023 {
6024 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6025 return FAIL;
6026 }
6027 }
6028 else
6029 {
6030 emsg(_(e_indexable_type_required));
6031 return FAIL;
6032 }
6033
6034 return OK;
6035}
6036
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006037/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006038 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006039 * "let name"
6040 * "var name = expr"
6041 * "final name = expr"
6042 * "const name = expr"
6043 * "name = expr"
6044 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006045 * Return NULL for an error.
6046 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006047 */
6048 static char_u *
6049compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6050{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006051 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006053 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 char_u *ret = NULL;
6055 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006056 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006059 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 int oplen = 0;
6062 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006063 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 int is_decl = is_decl_command(cmdidx);
6066 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006068 // Skip over the "var" or "[var, var]" to get to any "=".
6069 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6070 if (p == NULL)
6071 return *arg == '[' ? arg : NULL;
6072
6073 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006074 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006075 // TODO: should we allow this, and figure out type inference from list
6076 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006077 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 return NULL;
6079 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006080 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006082 sp = p;
6083 p = skipwhite(p);
6084 op = p;
6085 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006086
6087 if (var_count > 0 && oplen == 0)
6088 // can be something like "[1, 2]->func()"
6089 return arg;
6090
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006091 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006093 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006094 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006095 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006097 if (heredoc)
6098 {
6099 list_T *l;
6100 listitem_T *li;
6101
6102 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006103 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006104 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006105 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006106 if (l == NULL)
6107 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108
Bram Moolenaar078269b2020-09-21 20:35:55 +02006109 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006111 // Push each line and the create the list.
6112 FOR_ALL_LIST_ITEMS(l, li)
6113 {
6114 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6115 li->li_tv.vval.v_string = NULL;
6116 }
6117 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006119 list_free(l);
6120 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006121 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006123 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006125 char_u *wp;
6126
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006127 // for "[var, var] = expr" evaluate the expression here, loop over the
6128 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006129 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006130
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006131 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006132 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006133 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006134 if (compile_expr0(&p, cctx) == FAIL)
6135 return NULL;
6136 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006138 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006139 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006140 type_T *stacktype;
6141
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006142 stacktype = stack->ga_len == 0 ? &t_void
6143 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006144 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006145 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006146 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006147 goto theend;
6148 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006149 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006150 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006151 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006152 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006153 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6154 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006155 if (stacktype->tt_member != NULL)
6156 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006157 }
6158 }
6159
6160 /*
6161 * Loop over variables in "[var, var] = expr".
6162 * For "var = expr" and "let var: type" this is done only once.
6163 */
6164 if (var_count > 0)
6165 var_start = skipwhite(arg + 1); // skip over the "["
6166 else
6167 var_start = arg;
6168 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6169 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006170 int instr_count = -1;
6171
Bram Moolenaar752fc692021-01-04 21:57:11 +01006172 vim_free(lhs.lhs_name);
6173
6174 /*
6175 * Figure out the LHS type and other properties.
6176 */
6177 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6178 goto theend;
6179
6180 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006181 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006182 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006183 goto theend;
6184 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006185 if (!is_decl && lhs.lhs_lvar != NULL
6186 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006187 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006188 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006189 goto theend;
6190 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006191
6192 if (!heredoc)
6193 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006194 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006195 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006196 if (oplen > 0 && var_count == 0)
6197 {
6198 // skip over the "=" and the expression
6199 p = skipwhite(op + oplen);
6200 compile_expr0(&p, cctx);
6201 }
6202 }
6203 else if (oplen > 0)
6204 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006205 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006206 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006207
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006208 // For "var = expr" evaluate the expression.
6209 if (var_count == 0)
6210 {
6211 int r;
6212
6213 // for "+=", "*=", "..=" etc. first load the current value
6214 if (*op != '=')
6215 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6217 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006218
Bram Moolenaar752fc692021-01-04 21:57:11 +01006219 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006220 {
6221 // TODO: get member from list or dict
6222 emsg("Index with operation not supported yet");
6223 goto theend;
6224 }
6225 }
6226
6227 // Compile the expression. Temporarily hide the new local
6228 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006229 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006230 --cctx->ctx_locals.ga_len;
6231 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006232 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006233 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006234 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006235 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006236 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006237 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006238 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006239 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006240 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006241 ++cctx->ctx_locals.ga_len;
6242 if (r == FAIL)
6243 goto theend;
6244 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006245 else if (semicolon && var_idx == var_count - 1)
6246 {
6247 // For "[var; var] = expr" get the rest of the list
6248 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6249 goto theend;
6250 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006251 else
6252 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006253 // For "[var, var] = expr" get the "var_idx" item from the
6254 // list.
6255 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006256 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006257 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006258
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006259 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006260 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006261 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006262 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006263 if ((rhs_type->tt_type == VAR_FUNC
6264 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006265 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006266 goto theend;
6267
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006269 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006270 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006271 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006272 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006273 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006274 }
6275 else
6276 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006277 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006278 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006279 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006281 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006282 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006283 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006285 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006286 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006287 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006288 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006289 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006290 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006291 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006292
Bram Moolenaar71abe482020-09-14 22:28:30 +02006293 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006294 if (lhs.lhs_has_index)
6295 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006296 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006297 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006298 goto theend;
6299 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006300 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006302 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006303 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006305 else if (cmdidx == CMD_final)
6306 {
6307 emsg(_(e_final_requires_a_value));
6308 goto theend;
6309 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006312 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313 goto theend;
6314 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006316 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006317 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006318 goto theend;
6319 }
6320 else
6321 {
6322 // variables are always initialized
6323 if (ga_grow(instr, 1) == FAIL)
6324 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006325 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006326 {
6327 case VAR_BOOL:
6328 generate_PUSHBOOL(cctx, VVAL_FALSE);
6329 break;
6330 case VAR_FLOAT:
6331#ifdef FEAT_FLOAT
6332 generate_PUSHF(cctx, 0.0);
6333#endif
6334 break;
6335 case VAR_STRING:
6336 generate_PUSHS(cctx, NULL);
6337 break;
6338 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006339 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006340 break;
6341 case VAR_FUNC:
6342 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6343 break;
6344 case VAR_LIST:
6345 generate_NEWLIST(cctx, 0);
6346 break;
6347 case VAR_DICT:
6348 generate_NEWDICT(cctx, 0);
6349 break;
6350 case VAR_JOB:
6351 generate_PUSHJOB(cctx, NULL);
6352 break;
6353 case VAR_CHANNEL:
6354 generate_PUSHCHANNEL(cctx, NULL);
6355 break;
6356 case VAR_NUMBER:
6357 case VAR_UNKNOWN:
6358 case VAR_ANY:
6359 case VAR_PARTIAL:
6360 case VAR_VOID:
6361 case VAR_SPECIAL: // cannot happen
6362 generate_PUSHNR(cctx, 0);
6363 break;
6364 }
6365 }
6366 if (var_count == 0)
6367 end = p;
6368 }
6369
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006370 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006371 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006372 break;
6373
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006374 if (oplen > 0 && *op != '=')
6375 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006376 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006377 type_T *stacktype;
6378
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006379 if (*op == '.')
6380 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006381 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006382 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006383 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006384 if (
6385#ifdef FEAT_FLOAT
6386 // If variable is float operation with number is OK.
6387 !(expected == &t_float && stacktype == &t_number) &&
6388#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006389 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006390 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006391 goto theend;
6392
6393 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006394 {
6395 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6396 goto theend;
6397 }
6398 else if (*op == '+')
6399 {
6400 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 operator_type(lhs.lhs_member_type, stacktype),
6402 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006403 goto theend;
6404 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006405 else if (generate_two_op(cctx, op) == FAIL)
6406 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006410 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006411 // Use the info in "lhs" to store the value at the index in the
6412 // list or dict.
6413 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6414 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006415 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006416 }
6417 else
6418 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006419 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6420 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006421 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006422 generate_LOCKCONST(cctx);
6423
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006424 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006425 && (lhs.lhs_type->tt_type == VAR_DICT
6426 || lhs.lhs_type->tt_type == VAR_LIST)
6427 && lhs.lhs_type->tt_member != NULL
6428 && lhs.lhs_type->tt_member != &t_any
6429 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006430 // Set the type in the list or dict, so that it can be checked,
6431 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006432 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006433
Bram Moolenaar752fc692021-01-04 21:57:11 +01006434 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006435 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006436 if (generate_store_var(cctx, lhs.lhs_dest,
6437 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6438 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6439 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006440 goto theend;
6441 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006442 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006443 {
6444 isn_T *isn = ((isn_T *)instr->ga_data)
6445 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006446
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006447 // optimization: turn "var = 123" from ISN_PUSHNR +
6448 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006449 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006450 && instr->ga_len == instr_count + 1
6451 && isn->isn_type == ISN_PUSHNR)
6452 {
6453 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006455 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006456 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006457 isn->isn_arg.storenr.stnr_val = val;
6458 if (stack->ga_len > 0)
6459 --stack->ga_len;
6460 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006461 else if (lhs.lhs_lvar->lv_from_outer > 0)
6462 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6463 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006464 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006466 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006467 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006468
6469 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006470 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006471 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006472
6473 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006474 if (var_count > 0 && !semicolon)
6475 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006476 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006477 goto theend;
6478 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006479
Bram Moolenaarb2097502020-07-19 17:17:02 +02006480 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006481
6482theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006484 return ret;
6485}
6486
6487/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006488 * Check for an assignment at "eap->cmd", compile it if found.
6489 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6490 */
6491 static int
6492may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6493{
6494 char_u *pskip;
6495 char_u *p;
6496
6497 // Assuming the command starts with a variable or function name,
6498 // find what follows.
6499 // Skip over "var.member", "var[idx]" and the like.
6500 // Also "&opt = val", "$ENV = val" and "@r = val".
6501 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6502 ? eap->cmd + 1 : eap->cmd;
6503 p = to_name_end(pskip, TRUE);
6504 if (p > eap->cmd && *p != NUL)
6505 {
6506 char_u *var_end;
6507 int oplen;
6508 int heredoc;
6509
6510 if (eap->cmd[0] == '@')
6511 var_end = eap->cmd + 2;
6512 else
6513 var_end = find_name_end(pskip, NULL, NULL,
6514 FNE_CHECK_START | FNE_INCL_BR);
6515 oplen = assignment_len(skipwhite(var_end), &heredoc);
6516 if (oplen > 0)
6517 {
6518 size_t len = p - eap->cmd;
6519
6520 // Recognize an assignment if we recognize the variable
6521 // name:
6522 // "g:var = expr"
6523 // "local = expr" where "local" is a local var.
6524 // "script = expr" where "script" is a script-local var.
6525 // "import = expr" where "import" is an imported var
6526 // "&opt = expr"
6527 // "$ENV = expr"
6528 // "@r = expr"
6529 if (*eap->cmd == '&'
6530 || *eap->cmd == '$'
6531 || *eap->cmd == '@'
6532 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006533 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006534 {
6535 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6536 if (*line == NULL || *line == eap->cmd)
6537 return FAIL;
6538 return OK;
6539 }
6540 }
6541 }
6542
6543 if (*eap->cmd == '[')
6544 {
6545 // [var, var] = expr
6546 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6547 if (*line == NULL)
6548 return FAIL;
6549 if (*line != eap->cmd)
6550 return OK;
6551 }
6552 return NOTDONE;
6553}
6554
6555/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006556 * Check if "name" can be "unlet".
6557 */
6558 int
6559check_vim9_unlet(char_u *name)
6560{
6561 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6562 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006563 // "unlet s:var" is allowed in legacy script.
6564 if (*name == 's' && !script_is_vim9())
6565 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006566 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006567 return FAIL;
6568 }
6569 return OK;
6570}
6571
6572/*
6573 * Callback passed to ex_unletlock().
6574 */
6575 static int
6576compile_unlet(
6577 lval_T *lvp,
6578 char_u *name_end,
6579 exarg_T *eap,
6580 int deep UNUSED,
6581 void *coookie)
6582{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006583 cctx_T *cctx = coookie;
6584 char_u *p = lvp->ll_name;
6585 int cc = *name_end;
6586 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006587
Bram Moolenaar752fc692021-01-04 21:57:11 +01006588 if (cctx->ctx_skip == SKIP_YES)
6589 return OK;
6590
6591 *name_end = NUL;
6592 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006593 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006594 // :unlet $ENV_VAR
6595 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6596 }
6597 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6598 {
6599 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006600
Bram Moolenaar752fc692021-01-04 21:57:11 +01006601 // This is similar to assigning: lookup the list/dict, compile the
6602 // idx/key. Then instead of storing the value unlet the item.
6603 // unlet {list}[idx]
6604 // unlet {dict}[key] dict.key
6605 //
6606 // Figure out the LHS type and other properties.
6607 //
6608 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6609
6610 // : unlet an indexed item
6611 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006612 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 iemsg("called compile_lhs() without an index");
6614 ret = FAIL;
6615 }
6616 else
6617 {
6618 // Use the info in "lhs" to unlet the item at the index in the
6619 // list or dict.
6620 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006621 }
6622
Bram Moolenaar752fc692021-01-04 21:57:11 +01006623 vim_free(lhs.lhs_name);
6624 }
6625 else if (check_vim9_unlet(p) == FAIL)
6626 {
6627 ret = FAIL;
6628 }
6629 else
6630 {
6631 // Normal name. Only supports g:, w:, t: and b: namespaces.
6632 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006633 }
6634
Bram Moolenaar752fc692021-01-04 21:57:11 +01006635 *name_end = cc;
6636 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006637}
6638
6639/*
6640 * compile "unlet var", "lock var" and "unlock var"
6641 * "arg" points to "var".
6642 */
6643 static char_u *
6644compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6645{
6646 char_u *p = arg;
6647
6648 if (eap->cmdidx != CMD_unlet)
6649 {
6650 emsg("Sorry, :lock and unlock not implemented yet");
6651 return NULL;
6652 }
6653
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006654 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6655 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006656 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6657}
6658
6659/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006660 * Compile an :import command.
6661 */
6662 static char_u *
6663compile_import(char_u *arg, cctx_T *cctx)
6664{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006665 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666}
6667
6668/*
6669 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6670 */
6671 static int
6672compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6673{
6674 garray_T *instr = &cctx->ctx_instr;
6675 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6676
6677 if (endlabel == NULL)
6678 return FAIL;
6679 endlabel->el_next = *el;
6680 *el = endlabel;
6681 endlabel->el_end_label = instr->ga_len;
6682
6683 generate_JUMP(cctx, when, 0);
6684 return OK;
6685}
6686
6687 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006688compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006689{
6690 garray_T *instr = &cctx->ctx_instr;
6691
6692 while (*el != NULL)
6693 {
6694 endlabel_T *cur = (*el);
6695 isn_T *isn;
6696
6697 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006698 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006699 *el = cur->el_next;
6700 vim_free(cur);
6701 }
6702}
6703
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006704 static void
6705compile_free_jump_to_end(endlabel_T **el)
6706{
6707 while (*el != NULL)
6708 {
6709 endlabel_T *cur = (*el);
6710
6711 *el = cur->el_next;
6712 vim_free(cur);
6713 }
6714}
6715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716/*
6717 * Create a new scope and set up the generic items.
6718 */
6719 static scope_T *
6720new_scope(cctx_T *cctx, scopetype_T type)
6721{
6722 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6723
6724 if (scope == NULL)
6725 return NULL;
6726 scope->se_outer = cctx->ctx_scope;
6727 cctx->ctx_scope = scope;
6728 scope->se_type = type;
6729 scope->se_local_count = cctx->ctx_locals.ga_len;
6730 return scope;
6731}
6732
6733/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006734 * Free the current scope and go back to the outer scope.
6735 */
6736 static void
6737drop_scope(cctx_T *cctx)
6738{
6739 scope_T *scope = cctx->ctx_scope;
6740
6741 if (scope == NULL)
6742 {
6743 iemsg("calling drop_scope() without a scope");
6744 return;
6745 }
6746 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006747 switch (scope->se_type)
6748 {
6749 case IF_SCOPE:
6750 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6751 case FOR_SCOPE:
6752 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6753 case WHILE_SCOPE:
6754 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6755 case TRY_SCOPE:
6756 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6757 case NO_SCOPE:
6758 case BLOCK_SCOPE:
6759 break;
6760 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006761 vim_free(scope);
6762}
6763
6764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 * compile "if expr"
6766 *
6767 * "if expr" Produces instructions:
6768 * EVAL expr Push result of "expr"
6769 * JUMP_IF_FALSE end
6770 * ... body ...
6771 * end:
6772 *
6773 * "if expr | else" Produces instructions:
6774 * EVAL expr Push result of "expr"
6775 * JUMP_IF_FALSE else
6776 * ... body ...
6777 * JUMP_ALWAYS end
6778 * else:
6779 * ... body ...
6780 * end:
6781 *
6782 * "if expr1 | elseif expr2 | else" Produces instructions:
6783 * EVAL expr Push result of "expr"
6784 * JUMP_IF_FALSE elseif
6785 * ... body ...
6786 * JUMP_ALWAYS end
6787 * elseif:
6788 * EVAL expr Push result of "expr"
6789 * JUMP_IF_FALSE else
6790 * ... body ...
6791 * JUMP_ALWAYS end
6792 * else:
6793 * ... body ...
6794 * end:
6795 */
6796 static char_u *
6797compile_if(char_u *arg, cctx_T *cctx)
6798{
6799 char_u *p = arg;
6800 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006801 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006802 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006803 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006804 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
Bram Moolenaara5565e42020-05-09 15:44:01 +02006806 CLEAR_FIELD(ppconst);
6807 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006808 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006809 clear_ppconst(&ppconst);
6810 return NULL;
6811 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006812 if (!ends_excmd2(arg, skipwhite(p)))
6813 {
6814 semsg(_(e_trailing_arg), p);
6815 return NULL;
6816 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006817 if (cctx->ctx_skip == SKIP_YES)
6818 clear_ppconst(&ppconst);
6819 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006820 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006821 int error = FALSE;
6822 int v;
6823
Bram Moolenaara5565e42020-05-09 15:44:01 +02006824 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006825 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006826 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006827 if (error)
6828 return NULL;
6829 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006830 }
6831 else
6832 {
6833 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006834 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006835 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006836 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006837 if (bool_on_stack(cctx) == FAIL)
6838 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006840
6841 scope = new_scope(cctx, IF_SCOPE);
6842 if (scope == NULL)
6843 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006844 scope->se_skip_save = skip_save;
6845 // "is_had_return" will be reset if any block does not end in :return
6846 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006848 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006849 {
6850 // "where" is set when ":elseif", "else" or ":endif" is found
6851 scope->se_u.se_if.is_if_label = instr->ga_len;
6852 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6853 }
6854 else
6855 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006856
Bram Moolenaarced68a02021-01-24 17:53:47 +01006857#ifdef FEAT_PROFILE
6858 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6859 && skip_save != SKIP_YES)
6860 {
6861 // generated a profile start, need to generate a profile end, since it
6862 // won't be done after returning
6863 cctx->ctx_skip = SKIP_NOT;
6864 generate_instr(cctx, ISN_PROF_END);
6865 cctx->ctx_skip = SKIP_YES;
6866 }
6867#endif
6868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869 return p;
6870}
6871
6872 static char_u *
6873compile_elseif(char_u *arg, cctx_T *cctx)
6874{
6875 char_u *p = arg;
6876 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006877 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 isn_T *isn;
6879 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006880 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006881 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006882
6883 if (scope == NULL || scope->se_type != IF_SCOPE)
6884 {
6885 emsg(_(e_elseif_without_if));
6886 return NULL;
6887 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006888 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006889 if (!cctx->ctx_had_return)
6890 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006891
Bram Moolenaarced68a02021-01-24 17:53:47 +01006892 if (cctx->ctx_skip == SKIP_NOT)
6893 {
6894 // previous block was executed, this one and following will not
6895 cctx->ctx_skip = SKIP_YES;
6896 scope->se_u.se_if.is_seen_skip_not = TRUE;
6897 }
6898 if (scope->se_u.se_if.is_seen_skip_not)
6899 {
6900 // A previous block was executed, skip over expression and bail out.
6901 // Do not count the "elseif" for profiling.
6902#ifdef FEAT_PROFILE
6903 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6904 .isn_type == ISN_PROF_START)
6905 --instr->ga_len;
6906#endif
6907 skip_expr_cctx(&p, cctx);
6908 return p;
6909 }
6910
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006911 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006912 {
6913 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006915 return NULL;
6916 // previous "if" or "elseif" jumps here
6917 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6918 isn->isn_arg.jump.jump_where = instr->ga_len;
6919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006921 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006922 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006923 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006924 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006925 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006926#ifdef FEAT_PROFILE
6927 if (cctx->ctx_profiling)
6928 {
6929 // the previous block was skipped, need to profile this line
6930 generate_instr(cctx, ISN_PROF_START);
6931 instr_count = instr->ga_len;
6932 }
6933#endif
6934 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006935 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006936 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006937 clear_ppconst(&ppconst);
6938 return NULL;
6939 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006940 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006941 if (!ends_excmd2(arg, skipwhite(p)))
6942 {
6943 semsg(_(e_trailing_arg), p);
6944 return NULL;
6945 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006946 if (scope->se_skip_save == SKIP_YES)
6947 clear_ppconst(&ppconst);
6948 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006949 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006950 int error = FALSE;
6951 int v;
6952
Bram Moolenaar7f141552020-05-09 17:35:53 +02006953 // The expression results in a constant.
6954 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006955 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6956 if (error)
6957 return NULL;
6958 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006959 clear_ppconst(&ppconst);
6960 scope->se_u.se_if.is_if_label = -1;
6961 }
6962 else
6963 {
6964 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006965 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006966 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006967 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006968 if (bool_on_stack(cctx) == FAIL)
6969 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006971 // "where" is set when ":elseif", "else" or ":endif" is found
6972 scope->se_u.se_if.is_if_label = instr->ga_len;
6973 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
6976 return p;
6977}
6978
6979 static char_u *
6980compile_else(char_u *arg, cctx_T *cctx)
6981{
6982 char_u *p = arg;
6983 garray_T *instr = &cctx->ctx_instr;
6984 isn_T *isn;
6985 scope_T *scope = cctx->ctx_scope;
6986
6987 if (scope == NULL || scope->se_type != IF_SCOPE)
6988 {
6989 emsg(_(e_else_without_if));
6990 return NULL;
6991 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006992 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006993 if (!cctx->ctx_had_return)
6994 scope->se_u.se_if.is_had_return = FALSE;
6995 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006996
Bram Moolenaarced68a02021-01-24 17:53:47 +01006997#ifdef FEAT_PROFILE
6998 if (cctx->ctx_profiling)
6999 {
7000 if (cctx->ctx_skip == SKIP_NOT
7001 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7002 .isn_type == ISN_PROF_START)
7003 // the previous block was executed, do not count "else" for profiling
7004 --instr->ga_len;
7005 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7006 {
7007 // the previous block was not executed, this one will, do count the
7008 // "else" for profiling
7009 cctx->ctx_skip = SKIP_NOT;
7010 generate_instr(cctx, ISN_PROF_END);
7011 generate_instr(cctx, ISN_PROF_START);
7012 cctx->ctx_skip = SKIP_YES;
7013 }
7014 }
7015#endif
7016
7017 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007018 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007019 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007020 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007021 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007022 if (!cctx->ctx_had_return
7023 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7024 JUMP_ALWAYS, cctx) == FAIL)
7025 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007026 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007027
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007028 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007029 {
7030 if (scope->se_u.se_if.is_if_label >= 0)
7031 {
7032 // previous "if" or "elseif" jumps here
7033 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7034 isn->isn_arg.jump.jump_where = instr->ga_len;
7035 scope->se_u.se_if.is_if_label = -1;
7036 }
7037 }
7038
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007039 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007040 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042
7043 return p;
7044}
7045
7046 static char_u *
7047compile_endif(char_u *arg, cctx_T *cctx)
7048{
7049 scope_T *scope = cctx->ctx_scope;
7050 ifscope_T *ifscope;
7051 garray_T *instr = &cctx->ctx_instr;
7052 isn_T *isn;
7053
7054 if (scope == NULL || scope->se_type != IF_SCOPE)
7055 {
7056 emsg(_(e_endif_without_if));
7057 return NULL;
7058 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007059 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007060 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007061 if (!cctx->ctx_had_return)
7062 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007064 if (scope->se_u.se_if.is_if_label >= 0)
7065 {
7066 // previous "if" or "elseif" jumps here
7067 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7068 isn->isn_arg.jump.jump_where = instr->ga_len;
7069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007070 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007071 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007072
7073#ifdef FEAT_PROFILE
7074 // even when skipping we count the endif as executed, unless the block it's
7075 // in is skipped
7076 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7077 && scope->se_skip_save != SKIP_YES)
7078 {
7079 cctx->ctx_skip = SKIP_NOT;
7080 generate_instr(cctx, ISN_PROF_START);
7081 }
7082#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007083 cctx->ctx_skip = scope->se_skip_save;
7084
7085 // If all the blocks end in :return and there is an :else then the
7086 // had_return flag is set.
7087 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007089 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 return arg;
7091}
7092
7093/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007094 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 *
7096 * Produces instructions:
7097 * PUSHNR -1
7098 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007099 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7101 * - if beyond end, jump to "end"
7102 * - otherwise get item from list and push it
7103 * STORE var Store item in "var"
7104 * ... body ...
7105 * JUMP top Jump back to repeat
7106 * end: DROP Drop the result of "expr"
7107 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007108 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7109 * UNPACK 2 Split item in 2
7110 * STORE var1 Store item in "var1"
7111 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 */
7113 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007114compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007116 char_u *arg;
7117 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007118 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007119 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007120 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007121 int var_count = 0;
7122 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 size_t varlen;
7124 garray_T *instr = &cctx->ctx_instr;
7125 garray_T *stack = &cctx->ctx_type_stack;
7126 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007127 lvar_T *loop_lvar; // loop iteration variable
7128 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007130 type_T *item_type = &t_any;
7131 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132
Bram Moolenaar792f7862020-11-23 08:31:18 +01007133 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007134 if (p == NULL)
7135 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007136 if (var_count == 0)
7137 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138
7139 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007140 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007141 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7142 return NULL;
7143 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 {
7145 emsg(_(e_missing_in));
7146 return NULL;
7147 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007148 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007149 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7150 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 scope = new_scope(cctx, FOR_SCOPE);
7153 if (scope == NULL)
7154 return NULL;
7155
Bram Moolenaar792f7862020-11-23 08:31:18 +01007156 // Reserve a variable to store the loop iteration counter and initialize it
7157 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007158 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7159 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007160 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007161 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007162 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007163 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007164 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007165 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166
7167 // compile "expr", it remains on the stack until "endfor"
7168 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007169 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007170 {
7171 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007172 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007173 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007174 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007176 // Now that we know the type of "var", check that it is a list, now or at
7177 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007178 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007179 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007181 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182 return NULL;
7183 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007184
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007185 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007186 {
7187 if (var_count == 1)
7188 item_type = vartype->tt_member;
7189 else if (vartype->tt_member->tt_type == VAR_LIST
7190 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7191 item_type = vartype->tt_member->tt_member;
7192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193
7194 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007195 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007196 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197
Bram Moolenaar792f7862020-11-23 08:31:18 +01007198 arg = arg_start;
7199 if (var_count > 1)
7200 {
7201 generate_UNPACK(cctx, var_count, semicolon);
7202 arg = skipwhite(arg + 1); // skip white after '['
7203
7204 // the list item is replaced by a number of items
7205 if (ga_grow(stack, var_count - 1) == FAIL)
7206 {
7207 drop_scope(cctx);
7208 return NULL;
7209 }
7210 --stack->ga_len;
7211 for (idx = 0; idx < var_count; ++idx)
7212 {
7213 ((type_T **)stack->ga_data)[stack->ga_len] =
7214 (semicolon && idx == 0) ? vartype : item_type;
7215 ++stack->ga_len;
7216 }
7217 }
7218
7219 for (idx = 0; idx < var_count; ++idx)
7220 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007221 assign_dest_T dest = dest_local;
7222 int opt_flags = 0;
7223 int vimvaridx = -1;
7224 type_T *type = &t_any;
7225
7226 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007227 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007228 name = vim_strnsave(arg, varlen);
7229 if (name == NULL)
7230 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007231
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007232 // TODO: script var not supported?
7233 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7234 &vimvaridx, &type, cctx) == FAIL)
7235 goto failed;
7236 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007237 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007238 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7239 0, 0, type, name) == FAIL)
7240 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007241 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007242 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007243 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007244 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007245 {
7246 semsg(_(e_variable_already_declared), arg);
7247 goto failed;
7248 }
7249
Bram Moolenaarea870692020-12-02 14:24:30 +01007250 if (STRNCMP(name, "s:", 2) == 0)
7251 {
7252 semsg(_(e_cannot_declare_script_variable_in_function), name);
7253 goto failed;
7254 }
7255
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007256 // Reserve a variable to store "var".
7257 // TODO: check for type
7258 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7259 if (var_lvar == NULL)
7260 // out of memory or used as an argument
7261 goto failed;
7262
7263 if (semicolon && idx == var_count - 1)
7264 var_lvar->lv_type = vartype;
7265 else
7266 var_lvar->lv_type = item_type;
7267 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7268 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007269
Bram Moolenaar036d0712021-01-17 20:23:38 +01007270 if (*p == ':')
7271 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007272 if (*p == ',' || *p == ';')
7273 ++p;
7274 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007275 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007276 }
7277
7278 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007279
7280failed:
7281 vim_free(name);
7282 drop_scope(cctx);
7283 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284}
7285
7286/*
7287 * compile "endfor"
7288 */
7289 static char_u *
7290compile_endfor(char_u *arg, cctx_T *cctx)
7291{
7292 garray_T *instr = &cctx->ctx_instr;
7293 scope_T *scope = cctx->ctx_scope;
7294 forscope_T *forscope;
7295 isn_T *isn;
7296
7297 if (scope == NULL || scope->se_type != FOR_SCOPE)
7298 {
7299 emsg(_(e_for));
7300 return NULL;
7301 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007302 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007304 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305
7306 // At end of ":for" scope jump back to the FOR instruction.
7307 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7308
7309 // Fill in the "end" label in the FOR statement so it can jump here
7310 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7311 isn->isn_arg.forloop.for_end = instr->ga_len;
7312
7313 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007314 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315
7316 // Below the ":for" scope drop the "expr" list from the stack.
7317 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7318 return NULL;
7319
7320 vim_free(scope);
7321
7322 return arg;
7323}
7324
7325/*
7326 * compile "while expr"
7327 *
7328 * Produces instructions:
7329 * top: EVAL expr Push result of "expr"
7330 * JUMP_IF_FALSE end jump if false
7331 * ... body ...
7332 * JUMP top Jump back to repeat
7333 * end:
7334 *
7335 */
7336 static char_u *
7337compile_while(char_u *arg, cctx_T *cctx)
7338{
7339 char_u *p = arg;
7340 garray_T *instr = &cctx->ctx_instr;
7341 scope_T *scope;
7342
7343 scope = new_scope(cctx, WHILE_SCOPE);
7344 if (scope == NULL)
7345 return NULL;
7346
Bram Moolenaarb2049902021-01-24 12:53:53 +01007347 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007348 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007349#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007350 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7351 .isn_type == ISN_PROF_START)
7352 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007353#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354
7355 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007356 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007358 if (!ends_excmd2(arg, skipwhite(p)))
7359 {
7360 semsg(_(e_trailing_arg), p);
7361 return NULL;
7362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363
Bram Moolenaar13106602020-10-04 16:06:05 +02007364 if (bool_on_stack(cctx) == FAIL)
7365 return FAIL;
7366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007368 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 JUMP_IF_FALSE, cctx) == FAIL)
7370 return FAIL;
7371
7372 return p;
7373}
7374
7375/*
7376 * compile "endwhile"
7377 */
7378 static char_u *
7379compile_endwhile(char_u *arg, cctx_T *cctx)
7380{
7381 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007382 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383
7384 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7385 {
7386 emsg(_(e_while));
7387 return NULL;
7388 }
7389 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007390 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391
Bram Moolenaarf002a412021-01-24 13:34:18 +01007392#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007393 // count the endwhile before jumping
7394 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007395#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007398 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399
7400 // Fill in the "end" label in the WHILE statement so it can jump here.
7401 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007402 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7403 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404
7405 vim_free(scope);
7406
7407 return arg;
7408}
7409
7410/*
7411 * compile "continue"
7412 */
7413 static char_u *
7414compile_continue(char_u *arg, cctx_T *cctx)
7415{
7416 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007417 int try_scopes = 0;
7418 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419
7420 for (;;)
7421 {
7422 if (scope == NULL)
7423 {
7424 emsg(_(e_continue));
7425 return NULL;
7426 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007427 if (scope->se_type == FOR_SCOPE)
7428 {
7429 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007431 }
7432 if (scope->se_type == WHILE_SCOPE)
7433 {
7434 loop_label = scope->se_u.se_while.ws_top_label;
7435 break;
7436 }
7437 if (scope->se_type == TRY_SCOPE)
7438 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 scope = scope->se_outer;
7440 }
7441
Bram Moolenaarc150c092021-02-13 15:02:46 +01007442 if (try_scopes > 0)
7443 // Inside one or more try/catch blocks we first need to jump to the
7444 // "finally" or "endtry" to cleanup.
7445 generate_TRYCONT(cctx, try_scopes, loop_label);
7446 else
7447 // Jump back to the FOR or WHILE instruction.
7448 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 return arg;
7451}
7452
7453/*
7454 * compile "break"
7455 */
7456 static char_u *
7457compile_break(char_u *arg, cctx_T *cctx)
7458{
7459 scope_T *scope = cctx->ctx_scope;
7460 endlabel_T **el;
7461
7462 for (;;)
7463 {
7464 if (scope == NULL)
7465 {
7466 emsg(_(e_break));
7467 return NULL;
7468 }
7469 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7470 break;
7471 scope = scope->se_outer;
7472 }
7473
7474 // Jump to the end of the FOR or WHILE loop.
7475 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007476 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007478 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7480 return FAIL;
7481
7482 return arg;
7483}
7484
7485/*
7486 * compile "{" start of block
7487 */
7488 static char_u *
7489compile_block(char_u *arg, cctx_T *cctx)
7490{
7491 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7492 return NULL;
7493 return skipwhite(arg + 1);
7494}
7495
7496/*
7497 * compile end of block: drop one scope
7498 */
7499 static void
7500compile_endblock(cctx_T *cctx)
7501{
7502 scope_T *scope = cctx->ctx_scope;
7503
7504 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007505 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 vim_free(scope);
7507}
7508
7509/*
7510 * compile "try"
7511 * Creates a new scope for the try-endtry, pointing to the first catch and
7512 * finally.
7513 * Creates another scope for the "try" block itself.
7514 * TRY instruction sets up exception handling at runtime.
7515 *
7516 * "try"
7517 * TRY -> catch1, -> finally push trystack entry
7518 * ... try block
7519 * "throw {exception}"
7520 * EVAL {exception}
7521 * THROW create exception
7522 * ... try block
7523 * " catch {expr}"
7524 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007525 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526 * EVAL {expr}
7527 * MATCH
7528 * JUMP nomatch -> catch2
7529 * CATCH remove exception
7530 * ... catch block
7531 * " catch"
7532 * JUMP -> finally
7533 * catch2: CATCH remove exception
7534 * ... catch block
7535 * " finally"
7536 * finally:
7537 * ... finally block
7538 * " endtry"
7539 * ENDTRY pop trystack entry, may rethrow
7540 */
7541 static char_u *
7542compile_try(char_u *arg, cctx_T *cctx)
7543{
7544 garray_T *instr = &cctx->ctx_instr;
7545 scope_T *try_scope;
7546 scope_T *scope;
7547
7548 // scope that holds the jumps that go to catch/finally/endtry
7549 try_scope = new_scope(cctx, TRY_SCOPE);
7550 if (try_scope == NULL)
7551 return NULL;
7552
Bram Moolenaar69f70502021-01-01 16:10:46 +01007553 if (cctx->ctx_skip != SKIP_YES)
7554 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007555 isn_T *isn;
7556
7557 // "try_catch" is set when the first ":catch" is found or when no catch
7558 // is found and ":finally" is found.
7559 // "try_finally" is set when ":finally" is found
7560 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007561 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007562 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7563 return NULL;
7564 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7565 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007566 return NULL;
7567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568
7569 // scope for the try block itself
7570 scope = new_scope(cctx, BLOCK_SCOPE);
7571 if (scope == NULL)
7572 return NULL;
7573
7574 return arg;
7575}
7576
7577/*
7578 * compile "catch {expr}"
7579 */
7580 static char_u *
7581compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7582{
7583 scope_T *scope = cctx->ctx_scope;
7584 garray_T *instr = &cctx->ctx_instr;
7585 char_u *p;
7586 isn_T *isn;
7587
7588 // end block scope from :try or :catch
7589 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7590 compile_endblock(cctx);
7591 scope = cctx->ctx_scope;
7592
7593 // Error if not in a :try scope
7594 if (scope == NULL || scope->se_type != TRY_SCOPE)
7595 {
7596 emsg(_(e_catch));
7597 return NULL;
7598 }
7599
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007600 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007601 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007602 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603 return NULL;
7604 }
7605
Bram Moolenaar69f70502021-01-01 16:10:46 +01007606 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007608#ifdef FEAT_PROFILE
7609 // the profile-start should be after the jump
7610 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7611 .isn_type == ISN_PROF_START)
7612 --instr->ga_len;
7613#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007614 // Jump from end of previous block to :finally or :endtry
7615 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7616 JUMP_ALWAYS, cctx) == FAIL)
7617 return NULL;
7618
7619 // End :try or :catch scope: set value in ISN_TRY instruction
7620 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007621 if (isn->isn_arg.try.try_ref->try_catch == 0)
7622 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007623 if (scope->se_u.se_try.ts_catch_label != 0)
7624 {
7625 // Previous catch without match jumps here
7626 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7627 isn->isn_arg.jump.jump_where = instr->ga_len;
7628 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007629#ifdef FEAT_PROFILE
7630 if (cctx->ctx_profiling)
7631 {
7632 // a "throw" that jumps here needs to be counted
7633 generate_instr(cctx, ISN_PROF_END);
7634 // the "catch" is also counted
7635 generate_instr(cctx, ISN_PROF_START);
7636 }
7637#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638 }
7639
7640 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007641 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007643 scope->se_u.se_try.ts_caught_all = TRUE;
7644 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645 }
7646 else
7647 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007648 char_u *end;
7649 char_u *pat;
7650 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007651 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007652 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 // Push v:exception, push {expr} and MATCH
7655 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7656
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007657 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007658 if (*end != *p)
7659 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007660 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007661 vim_free(tofree);
7662 return FAIL;
7663 }
7664 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007665 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007666 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007667 len = (int)(end - tofree);
7668 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007669 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007670 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007671 if (pat == NULL)
7672 return FAIL;
7673 if (generate_PUSHS(cctx, pat) == FAIL)
7674 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7677 return NULL;
7678
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007679 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7681 return NULL;
7682 }
7683
Bram Moolenaar69f70502021-01-01 16:10:46 +01007684 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 return NULL;
7686
7687 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7688 return NULL;
7689 return p;
7690}
7691
7692 static char_u *
7693compile_finally(char_u *arg, cctx_T *cctx)
7694{
7695 scope_T *scope = cctx->ctx_scope;
7696 garray_T *instr = &cctx->ctx_instr;
7697 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007698 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699
7700 // end block scope from :try or :catch
7701 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7702 compile_endblock(cctx);
7703 scope = cctx->ctx_scope;
7704
7705 // Error if not in a :try scope
7706 if (scope == NULL || scope->se_type != TRY_SCOPE)
7707 {
7708 emsg(_(e_finally));
7709 return NULL;
7710 }
7711
7712 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007713 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007714 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715 {
7716 emsg(_(e_finally_dup));
7717 return NULL;
7718 }
7719
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007720 this_instr = instr->ga_len;
7721#ifdef FEAT_PROFILE
7722 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7723 .isn_type == ISN_PROF_START)
7724 // jump to the profile start of the "finally"
7725 --this_instr;
7726#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007727
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007728 // Fill in the "end" label in jumps at the end of the blocks.
7729 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7730 this_instr, cctx);
7731
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007732 // If there is no :catch then an exception jumps to :finally.
7733 if (isn->isn_arg.try.try_ref->try_catch == 0)
7734 isn->isn_arg.try.try_ref->try_catch = this_instr;
7735 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007736 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 {
7738 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007739 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007740 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007741 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007742 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007743 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7744 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 // TODO: set index in ts_finally_label jumps
7747
7748 return arg;
7749}
7750
7751 static char_u *
7752compile_endtry(char_u *arg, cctx_T *cctx)
7753{
7754 scope_T *scope = cctx->ctx_scope;
7755 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007756 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007757
7758 // end block scope from :catch or :finally
7759 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7760 compile_endblock(cctx);
7761 scope = cctx->ctx_scope;
7762
7763 // Error if not in a :try scope
7764 if (scope == NULL || scope->se_type != TRY_SCOPE)
7765 {
7766 if (scope == NULL)
7767 emsg(_(e_no_endtry));
7768 else if (scope->se_type == WHILE_SCOPE)
7769 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007770 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007771 emsg(_(e_endfor));
7772 else
7773 emsg(_(e_endif));
7774 return NULL;
7775 }
7776
Bram Moolenaarc150c092021-02-13 15:02:46 +01007777 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007778 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007780 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7781 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007782 {
7783 emsg(_(e_missing_catch_or_finally));
7784 return NULL;
7785 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007786
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007787#ifdef FEAT_PROFILE
7788 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7789 .isn_type == ISN_PROF_START)
7790 // move the profile start after "endtry" so that it's not counted when
7791 // the exception is rethrown.
7792 --instr->ga_len;
7793#endif
7794
Bram Moolenaar69f70502021-01-01 16:10:46 +01007795 // Fill in the "end" label in jumps at the end of the blocks, if not
7796 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007797 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7798 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799
Bram Moolenaar69f70502021-01-01 16:10:46 +01007800 if (scope->se_u.se_try.ts_catch_label != 0)
7801 {
7802 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007803 isn_T *isn = ((isn_T *)instr->ga_data)
7804 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007805 isn->isn_arg.jump.jump_where = instr->ga_len;
7806 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007807 }
7808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 compile_endblock(cctx);
7810
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007811 if (cctx->ctx_skip != SKIP_YES)
7812 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007813 // End :catch or :finally scope: set instruction index in ISN_TRY
7814 // instruction
7815 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007816 if (cctx->ctx_skip != SKIP_YES
7817 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7818 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007819#ifdef FEAT_PROFILE
7820 if (cctx->ctx_profiling)
7821 generate_instr(cctx, ISN_PROF_START);
7822#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007824 return arg;
7825}
7826
7827/*
7828 * compile "throw {expr}"
7829 */
7830 static char_u *
7831compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7832{
7833 char_u *p = skipwhite(arg);
7834
Bram Moolenaara5565e42020-05-09 15:44:01 +02007835 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007837 if (cctx->ctx_skip == SKIP_YES)
7838 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839 if (may_generate_2STRING(-1, cctx) == FAIL)
7840 return NULL;
7841 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7842 return NULL;
7843
7844 return p;
7845}
7846
7847/*
7848 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007849 * compile "echomsg expr"
7850 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007851 * compile "execute expr"
7852 */
7853 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007854compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007855{
7856 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007857 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007858 int count = 0;
7859
7860 for (;;)
7861 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007862 if (ends_excmd2(prev, p))
7863 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007864 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007865 return NULL;
7866 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007867 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007868 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007869 }
7870
Bram Moolenaare4984292020-12-13 14:19:25 +01007871 if (count > 0)
7872 {
7873 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7874 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7875 else if (cmdidx == CMD_execute)
7876 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7877 else if (cmdidx == CMD_echomsg)
7878 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7879 else
7880 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 return p;
7883}
7884
7885/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007886 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007887 * instruction to compute it and return OK.
7888 * Otherwise return FAIL, the caller must deal with any range.
7889 */
7890 static int
7891compile_variable_range(exarg_T *eap, cctx_T *cctx)
7892{
7893 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7894 char_u *p = skipdigits(eap->cmd);
7895
7896 if (p == range_end)
7897 return FAIL;
7898 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7899}
7900
7901/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007902 * :put r
7903 * :put ={expr}
7904 */
7905 static char_u *
7906compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7907{
7908 char_u *line = arg;
7909 linenr_T lnum;
7910 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007911 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007912
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007913 eap->regname = *line;
7914
7915 if (eap->regname == '=')
7916 {
7917 char_u *p = line + 1;
7918
7919 if (compile_expr0(&p, cctx) == FAIL)
7920 return NULL;
7921 line = p;
7922 }
7923 else if (eap->regname != NUL)
7924 ++line;
7925
Bram Moolenaar08597872020-12-10 19:43:40 +01007926 if (compile_variable_range(eap, cctx) == OK)
7927 {
7928 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7929 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007930 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007931 {
7932 // Either no range or a number.
7933 // "errormsg" will not be set because the range is ADDR_LINES.
7934 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007935 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007936 return NULL;
7937 if (eap->addr_count == 0)
7938 lnum = -1;
7939 else
7940 lnum = eap->line2;
7941 if (above)
7942 --lnum;
7943 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007944
7945 generate_PUT(cctx, eap->regname, lnum);
7946 return line;
7947}
7948
7949/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007950 * A command that is not compiled, execute with legacy code.
7951 */
7952 static char_u *
7953compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7954{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007955 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007956 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007957 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007958
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007959 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007960 goto theend;
7961
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007962 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007963 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007964 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007965 int usefilter = FALSE;
7966
7967 has_expr = argt & (EX_XFILE | EX_EXPAND);
7968
7969 // If the command can be followed by a bar, find the bar and truncate
7970 // it, so that the following command can be compiled.
7971 // The '|' is overwritten with a NUL, it is put back below.
7972 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7973 && *eap->arg == '!')
7974 // :w !filter or :r !filter or :r! filter
7975 usefilter = TRUE;
7976 if ((argt & EX_TRLBAR) && !usefilter)
7977 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007978 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007979 separate_nextcmd(eap);
7980 if (eap->nextcmd != NULL)
7981 nextcmd = eap->nextcmd;
7982 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007983 else if (eap->cmdidx == CMD_wincmd)
7984 {
7985 p = eap->arg;
7986 if (*p != NUL)
7987 ++p;
7988 if (*p == 'g' || *p == Ctrl_G)
7989 ++p;
7990 p = skipwhite(p);
7991 if (*p == '|')
7992 {
7993 *p = NUL;
7994 nextcmd = p + 1;
7995 }
7996 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007997 }
7998
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007999 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8000 {
8001 // expand filename in "syntax include [@group] filename"
8002 has_expr = TRUE;
8003 eap->arg = skipwhite(eap->arg + 7);
8004 if (*eap->arg == '@')
8005 eap->arg = skiptowhite(eap->arg);
8006 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008007
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008008 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8009 && STRLEN(eap->arg) > 4)
8010 {
8011 int delim = *eap->arg;
8012
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008013 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008014 if (*p == delim)
8015 {
8016 eap->arg = p + 1;
8017 has_expr = TRUE;
8018 }
8019 }
8020
Bram Moolenaarecac5912021-01-05 19:23:28 +01008021 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8022 {
8023 // TODO: should only expand when appropriate for the command
8024 eap->arg = skiptowhite(eap->arg);
8025 has_expr = TRUE;
8026 }
8027
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008028 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008029 {
8030 int count = 0;
8031 char_u *start = skipwhite(line);
8032
8033 // :cmd xxx`=expr1`yyy`=expr2`zzz
8034 // PUSHS ":cmd xxx"
8035 // eval expr1
8036 // PUSHS "yyy"
8037 // eval expr2
8038 // PUSHS "zzz"
8039 // EXECCONCAT 5
8040 for (;;)
8041 {
8042 if (p > start)
8043 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008044 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008045 ++count;
8046 }
8047 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008048 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008049 return NULL;
8050 may_generate_2STRING(-1, cctx);
8051 ++count;
8052 p = skipwhite(p);
8053 if (*p != '`')
8054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008055 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008056 return NULL;
8057 }
8058 start = p + 1;
8059
8060 p = (char_u *)strstr((char *)start, "`=");
8061 if (p == NULL)
8062 {
8063 if (*skipwhite(start) != NUL)
8064 {
8065 generate_PUSHS(cctx, vim_strsave(start));
8066 ++count;
8067 }
8068 break;
8069 }
8070 }
8071 generate_EXECCONCAT(cctx, count);
8072 }
8073 else
8074 generate_EXEC(cctx, line);
8075
8076theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008077 if (*nextcmd != NUL)
8078 {
8079 // the parser expects a pointer to the bar, put it back
8080 --nextcmd;
8081 *nextcmd = '|';
8082 }
8083
8084 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008085}
8086
8087/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008088 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008089 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008090 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008091 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008092add_def_function(ufunc_T *ufunc)
8093{
8094 dfunc_T *dfunc;
8095
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008096 if (def_functions.ga_len == 0)
8097 {
8098 // The first position is not used, so that a zero uf_dfunc_idx means it
8099 // wasn't set.
8100 if (ga_grow(&def_functions, 1) == FAIL)
8101 return FAIL;
8102 ++def_functions.ga_len;
8103 }
8104
Bram Moolenaar09689a02020-05-09 22:50:08 +02008105 // Add the function to "def_functions".
8106 if (ga_grow(&def_functions, 1) == FAIL)
8107 return FAIL;
8108 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8109 CLEAR_POINTER(dfunc);
8110 dfunc->df_idx = def_functions.ga_len;
8111 ufunc->uf_dfunc_idx = dfunc->df_idx;
8112 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008113 dfunc->df_name = vim_strsave(ufunc->uf_name);
8114 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008115 ++def_functions.ga_len;
8116 return OK;
8117}
8118
8119/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 * After ex_function() has collected all the function lines: parse and compile
8121 * the lines into instructions.
8122 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008123 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8124 * the return statement (used for lambda). When uf_ret_type is already set
8125 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008126 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008127 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008128 * This can be used recursively through compile_lambda(), which may reallocate
8129 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008130 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008132 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008133compile_def_function(
8134 ufunc_T *ufunc,
8135 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008136 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008137 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 char_u *line = NULL;
8140 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 cctx_T cctx;
8143 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008144 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 int ret = FAIL;
8146 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008147 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008148 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008149 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008150#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008151 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008152#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008154 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008155 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008156 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8159 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008160 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008161 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008162 else
8163 {
8164 if (add_def_function(ufunc) == FAIL)
8165 return FAIL;
8166 new_def_function = TRUE;
8167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168
Bram Moolenaar985116a2020-07-12 17:31:09 +02008169 ufunc->uf_def_status = UF_COMPILING;
8170
Bram Moolenaara80faa82020-04-12 19:37:17 +02008171 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008172
Bram Moolenaarf002a412021-01-24 13:34:18 +01008173#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008174 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008175#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 cctx.ctx_ufunc = ufunc;
8177 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008178 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8180 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8181 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8182 cctx.ctx_type_list = &ufunc->uf_type_list;
8183 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8184 instr = &cctx.ctx_instr;
8185
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008186 // Set the context to the function, it may be compiled when called from
8187 // another script. Set the script version to the most modern one.
8188 // The line number will be set in next_line_from_context().
8189 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8191
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008192 // Make sure error messages are OK.
8193 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8194 if (do_estack_push)
8195 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008196 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008197
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008198 if (ufunc->uf_def_args.ga_len > 0)
8199 {
8200 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008201 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008202 int i;
8203 char_u *arg;
8204 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008205 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008206
8207 // Produce instructions for the default values of optional arguments.
8208 // Store the instruction index in uf_def_arg_idx[] so that we know
8209 // where to start when the function is called, depending on the number
8210 // of arguments.
8211 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8212 if (ufunc->uf_def_arg_idx == NULL)
8213 goto erret;
8214 for (i = 0; i < count; ++i)
8215 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008216 garray_T *stack = &cctx.ctx_type_stack;
8217 type_T *val_type;
8218 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008219 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008220
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008221 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8222 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008223 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008224 goto erret;
8225
8226 // If no type specified use the type of the default value.
8227 // Otherwise check that the default value type matches the
8228 // specified type.
8229 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008230 where.wt_index = arg_idx + 1;
8231 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008232 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008233 {
8234 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008235 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008236 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008237 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008238 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008239 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008240
8241 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008242 goto erret;
8243 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008244 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008245
8246 if (did_set_arg_type)
8247 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008248 }
8249
8250 /*
8251 * Loop over all the lines of the function and generate instructions.
8252 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 for (;;)
8254 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008255 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008256 int starts_with_colon = FALSE;
8257 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008258 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008259
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008260 // Bail out on the first error to avoid a flood of errors and report
8261 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008262 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008263 goto erret;
8264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 if (line != NULL && *line == '|')
8266 // the line continues after a '|'
8267 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008268 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008269 && !(*line == '#' && (line == cctx.ctx_line_start
8270 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008271 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008272 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008273 goto erret;
8274 }
8275 else
8276 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008277 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008278 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008279 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008280 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008281#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008282 if (cctx.ctx_skip != SKIP_YES)
8283 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008284#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 }
8288
Bram Moolenaara80faa82020-04-12 19:37:17 +02008289 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008290 ea.cmdlinep = &line;
8291 ea.cmd = skipwhite(line);
8292
Bram Moolenaarb2049902021-01-24 12:53:53 +01008293 if (*ea.cmd == '#')
8294 {
8295 // "#" starts a comment
8296 line = (char_u *)"";
8297 continue;
8298 }
8299
Bram Moolenaarf002a412021-01-24 13:34:18 +01008300#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008301 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8302 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008303 {
8304 may_generate_prof_end(&cctx, prof_lnum);
8305
8306 prof_lnum = cctx.ctx_lnum;
8307 generate_instr(&cctx, ISN_PROF_START);
8308 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008309#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008310
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008311 // Some things can be recognized by the first character.
8312 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008313 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008314 case '}':
8315 {
8316 // "}" ends a block scope
8317 scopetype_T stype = cctx.ctx_scope == NULL
8318 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008319
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008320 if (stype == BLOCK_SCOPE)
8321 {
8322 compile_endblock(&cctx);
8323 line = ea.cmd;
8324 }
8325 else
8326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008327 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008328 goto erret;
8329 }
8330 if (line != NULL)
8331 line = skipwhite(ea.cmd + 1);
8332 continue;
8333 }
8334
8335 case '{':
8336 // "{" starts a block scope
8337 // "{'a': 1}->func() is something else
8338 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8339 {
8340 line = compile_block(ea.cmd, &cctx);
8341 continue;
8342 }
8343 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 }
8345
8346 /*
8347 * COMMAND MODIFIERS
8348 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008349 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008350 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8351 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008352 {
8353 if (errormsg != NULL)
8354 goto erret;
8355 // empty line or comment
8356 line = (char_u *)"";
8357 continue;
8358 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008359 generate_cmdmods(&cctx, &local_cmdmod);
8360 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008362 // Check if there was a colon after the last command modifier or before
8363 // the current position.
8364 for (p = ea.cmd; p >= line; --p)
8365 {
8366 if (*p == ':')
8367 starts_with_colon = TRUE;
8368 if (p < ea.cmd && !VIM_ISWHITE(*p))
8369 break;
8370 }
8371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008373 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008374 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008375 {
8376 if (*ea.cmd == '(')
8377 // not for "call()"
8378 ea.cmd = p;
8379 else
8380 ea.cmd = skipwhite(ea.cmd);
8381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008383 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008384 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008385 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008386
Bram Moolenaar17126b12021-01-07 22:03:02 +01008387 // Check for assignment after command modifiers.
8388 assign = may_compile_assignment(&ea, &line, &cctx);
8389 if (assign == OK)
8390 goto nextline;
8391 if (assign == FAIL)
8392 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008393 }
8394
8395 /*
8396 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008397 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008398 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008399 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008400 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008401 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008402 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008403 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008404 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008405 if (!starts_with_colon)
8406 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008407 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008408 goto erret;
8409 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008410 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008411 if (ends_excmd2(line, ea.cmd))
8412 {
8413 // A range without a command: jump to the line.
8414 // TODO: compile to a more efficient command, possibly
8415 // calling parse_cmd_address().
8416 ea.cmdidx = CMD_SIZE;
8417 line = compile_exec(line, &ea, &cctx);
8418 goto nextline;
8419 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008420 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008421 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008422 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar6914e872021-03-06 21:01:09 +01008423 : (int (*)(char_u *, size_t, cctx_T *))item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008424
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008425 if (p == NULL)
8426 {
8427 if (cctx.ctx_skip != SKIP_YES)
8428 emsg(_(e_ambiguous_use_of_user_defined_command));
8429 goto erret;
8430 }
8431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8433 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008434 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008435 {
8436 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008437 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008438 }
8439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008441 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008443 // CMD_var cannot happen, compile_assignment() above would be
8444 // used. Most likely an assignment to a non-existing variable.
8445 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008446 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 }
8449
Bram Moolenaar3988f642020-08-27 22:43:03 +02008450 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008451 && ea.cmdidx != CMD_elseif
8452 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008453 && ea.cmdidx != CMD_endif
8454 && ea.cmdidx != CMD_endfor
8455 && ea.cmdidx != CMD_endwhile
8456 && ea.cmdidx != CMD_catch
8457 && ea.cmdidx != CMD_finally
8458 && ea.cmdidx != CMD_endtry)
8459 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008460 emsg(_(e_unreachable_code_after_return));
8461 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008462 }
8463
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008464 p = skipwhite(p);
8465 if (ea.cmdidx != CMD_SIZE
8466 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8467 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008468 if (ea.cmdidx >= 0)
8469 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008470 if ((ea.argt & EX_BANG) && *p == '!')
8471 {
8472 ea.forceit = TRUE;
8473 p = skipwhite(p + 1);
8474 }
8475 }
8476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477 switch (ea.cmdidx)
8478 {
8479 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008480 ea.arg = p;
8481 line = compile_nested_function(&ea, &cctx);
8482 break;
8483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008485 // TODO: should we allow this, e.g. to declare a global
8486 // function?
8487 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 goto erret;
8489
8490 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008491 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008492 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493 break;
8494
8495 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008496 emsg(_(e_cannot_use_let_in_vim9_script));
8497 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008498 case CMD_var:
8499 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 case CMD_const:
8501 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008502 if (line == p)
8503 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008504 break;
8505
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008506 case CMD_unlet:
8507 case CMD_unlockvar:
8508 case CMD_lockvar:
8509 line = compile_unletlock(p, &ea, &cctx);
8510 break;
8511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 case CMD_import:
8513 line = compile_import(p, &cctx);
8514 break;
8515
8516 case CMD_if:
8517 line = compile_if(p, &cctx);
8518 break;
8519 case CMD_elseif:
8520 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008521 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008522 break;
8523 case CMD_else:
8524 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008525 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008526 break;
8527 case CMD_endif:
8528 line = compile_endif(p, &cctx);
8529 break;
8530
8531 case CMD_while:
8532 line = compile_while(p, &cctx);
8533 break;
8534 case CMD_endwhile:
8535 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008536 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008537 break;
8538
8539 case CMD_for:
8540 line = compile_for(p, &cctx);
8541 break;
8542 case CMD_endfor:
8543 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008544 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008545 break;
8546 case CMD_continue:
8547 line = compile_continue(p, &cctx);
8548 break;
8549 case CMD_break:
8550 line = compile_break(p, &cctx);
8551 break;
8552
8553 case CMD_try:
8554 line = compile_try(p, &cctx);
8555 break;
8556 case CMD_catch:
8557 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008558 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008559 break;
8560 case CMD_finally:
8561 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008562 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 break;
8564 case CMD_endtry:
8565 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008566 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 break;
8568 case CMD_throw:
8569 line = compile_throw(p, &cctx);
8570 break;
8571
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008572 case CMD_eval:
8573 if (compile_expr0(&p, &cctx) == FAIL)
8574 goto erret;
8575
Bram Moolenaar3988f642020-08-27 22:43:03 +02008576 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008577 generate_instr_drop(&cctx, ISN_DROP, 1);
8578
8579 line = skipwhite(p);
8580 break;
8581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008584 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008585 case CMD_echomsg:
8586 case CMD_echoerr:
8587 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008588 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008589
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008590 case CMD_put:
8591 ea.cmd = cmd;
8592 line = compile_put(p, &ea, &cctx);
8593 break;
8594
Bram Moolenaar3988f642020-08-27 22:43:03 +02008595 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008596
Bram Moolenaarae616492020-07-28 20:07:27 +02008597 case CMD_append:
8598 case CMD_change:
8599 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008600 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008601 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008602 case CMD_xit:
8603 not_in_vim9(&ea);
8604 goto erret;
8605
Bram Moolenaar002262f2020-07-08 17:47:57 +02008606 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008607 if (cctx.ctx_skip != SKIP_YES)
8608 {
8609 semsg(_(e_invalid_command_str), ea.cmd);
8610 goto erret;
8611 }
8612 // We don't check for a next command here.
8613 line = (char_u *)"";
8614 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008617 if (cctx.ctx_skip == SKIP_YES)
8618 {
8619 // We don't check for a next command here.
8620 line = (char_u *)"";
8621 }
8622 else
8623 {
8624 // Not recognized, execute with do_cmdline_cmd().
8625 ea.arg = p;
8626 line = compile_exec(line, &ea, &cctx);
8627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008628 break;
8629 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008630nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008631 if (line == NULL)
8632 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008633 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008635 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008636 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 if (cctx.ctx_type_stack.ga_len < 0)
8639 {
8640 iemsg("Type stack underflow");
8641 goto erret;
8642 }
8643 }
8644
8645 if (cctx.ctx_scope != NULL)
8646 {
8647 if (cctx.ctx_scope->se_type == IF_SCOPE)
8648 emsg(_(e_endif));
8649 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8650 emsg(_(e_endwhile));
8651 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8652 emsg(_(e_endfor));
8653 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008654 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 goto erret;
8656 }
8657
Bram Moolenaarefd88552020-06-18 20:50:10 +02008658 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008659 {
8660 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8661 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008662 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663 goto erret;
8664 }
8665
8666 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008667 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 }
8669
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008670 {
8671 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8672 + ufunc->uf_dfunc_idx;
8673 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008674 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008675#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008676 if (cctx.ctx_profiling)
8677 {
8678 dfunc->df_instr_prof = instr->ga_data;
8679 dfunc->df_instr_prof_count = instr->ga_len;
8680 }
8681 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008682#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008683 {
8684 dfunc->df_instr = instr->ga_data;
8685 dfunc->df_instr_count = instr->ga_len;
8686 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008687 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008688 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008689 if (cctx.ctx_outer_used)
8690 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008691 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008693
8694 ret = OK;
8695
8696erret:
8697 if (ret == FAIL)
8698 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008699 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008700 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8701 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008702
8703 for (idx = 0; idx < instr->ga_len; ++idx)
8704 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008706 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008707
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008708 // If using the last entry in the table and it was added above, we
8709 // might as well remove it.
8710 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008711 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008712 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008713 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008714 ufunc->uf_dfunc_idx = 0;
8715 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008716 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008717
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008718 while (cctx.ctx_scope != NULL)
8719 drop_scope(&cctx);
8720
Bram Moolenaar20431c92020-03-20 18:39:46 +01008721 // Don't execute this function body.
8722 ga_clear_strings(&ufunc->uf_lines);
8723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008724 if (errormsg != NULL)
8725 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008726 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008727 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728 }
8729
8730 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008731 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008732 if (do_estack_push)
8733 estack_pop();
8734
Bram Moolenaar20431c92020-03-20 18:39:46 +01008735 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008736 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008738 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008739}
8740
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008741 void
8742set_function_type(ufunc_T *ufunc)
8743{
8744 int varargs = ufunc->uf_va_name != NULL;
8745 int argcount = ufunc->uf_args.ga_len;
8746
8747 // Create a type for the function, with the return type and any
8748 // argument types.
8749 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8750 // The type is included in "tt_args".
8751 if (argcount > 0 || varargs)
8752 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008753 if (ufunc->uf_type_list.ga_itemsize == 0)
8754 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008755 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8756 argcount, &ufunc->uf_type_list);
8757 // Add argument types to the function type.
8758 if (func_type_add_arg_types(ufunc->uf_func_type,
8759 argcount + varargs,
8760 &ufunc->uf_type_list) == FAIL)
8761 return;
8762 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8763 ufunc->uf_func_type->tt_min_argcount =
8764 argcount - ufunc->uf_def_args.ga_len;
8765 if (ufunc->uf_arg_types == NULL)
8766 {
8767 int i;
8768
8769 // lambda does not have argument types.
8770 for (i = 0; i < argcount; ++i)
8771 ufunc->uf_func_type->tt_args[i] = &t_any;
8772 }
8773 else
8774 mch_memmove(ufunc->uf_func_type->tt_args,
8775 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8776 if (varargs)
8777 {
8778 ufunc->uf_func_type->tt_args[argcount] =
8779 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8780 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8781 }
8782 }
8783 else
8784 // No arguments, can use a predefined type.
8785 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8786 argcount, &ufunc->uf_type_list);
8787}
8788
8789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008790/*
8791 * Delete an instruction, free what it contains.
8792 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008793 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008794delete_instr(isn_T *isn)
8795{
8796 switch (isn->isn_type)
8797 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008798 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008800 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008801 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008802 case ISN_LOADENV:
8803 case ISN_LOADG:
8804 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008805 case ISN_LOADT:
8806 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008807 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008808 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008810 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008811 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008812 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008813 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008814 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008815 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008816 case ISN_STOREW:
8817 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008818 vim_free(isn->isn_arg.string);
8819 break;
8820
8821 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008822 case ISN_STORES:
8823 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824 break;
8825
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008826 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008827 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008828 vim_free(isn->isn_arg.unlet.ul_name);
8829 break;
8830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008831 case ISN_STOREOPT:
8832 vim_free(isn->isn_arg.storeopt.so_name);
8833 break;
8834
8835 case ISN_PUSHBLOB: // push blob isn_arg.blob
8836 blob_unref(isn->isn_arg.blob);
8837 break;
8838
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008839 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008840#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008841 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008842#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008843 break;
8844
8845 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008846#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008847 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008848#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008849 break;
8850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 case ISN_UCALL:
8852 vim_free(isn->isn_arg.ufunc.cuf_name);
8853 break;
8854
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008855 case ISN_FUNCREF:
8856 {
8857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8858 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008859 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008860
Bram Moolenaar077a4232020-12-22 18:33:27 +01008861 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8862 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008863 }
8864 break;
8865
8866 case ISN_DCALL:
8867 {
8868 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8869 + isn->isn_arg.dfunc.cdf_idx;
8870
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008871 if (dfunc->df_ufunc != NULL
8872 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008873 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008874 }
8875 break;
8876
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008877 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008878 {
8879 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8880 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8881
8882 if (ufunc != NULL)
8883 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008884 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008885 func_ptr_unref(ufunc);
8886 }
8887
8888 vim_free(lambda);
8889 vim_free(isn->isn_arg.newfunc.nf_global);
8890 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008891 break;
8892
Bram Moolenaar5e654232020-09-16 15:22:00 +02008893 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008894 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008895 free_type(isn->isn_arg.type.ct_type);
8896 break;
8897
Bram Moolenaar02194d22020-10-24 23:08:38 +02008898 case ISN_CMDMOD:
8899 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8900 ->cmod_filter_regmatch.regprog);
8901 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8902 break;
8903
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008904 case ISN_LOADSCRIPT:
8905 case ISN_STORESCRIPT:
8906 vim_free(isn->isn_arg.script.scriptref);
8907 break;
8908
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008909 case ISN_TRY:
8910 vim_free(isn->isn_arg.try.try_ref);
8911 break;
8912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008913 case ISN_2BOOL:
8914 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008915 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008916 case ISN_ADDBLOB:
8917 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008918 case ISN_ANYINDEX:
8919 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008920 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008921 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008923 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008924 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008925 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 case ISN_COMPAREANY:
8927 case ISN_COMPAREBLOB:
8928 case ISN_COMPAREBOOL:
8929 case ISN_COMPAREDICT:
8930 case ISN_COMPAREFLOAT:
8931 case ISN_COMPAREFUNC:
8932 case ISN_COMPARELIST:
8933 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008934 case ISN_COMPARESPECIAL:
8935 case ISN_COMPARESTRING:
8936 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008937 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 case ISN_DROP:
8939 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008940 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008941 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008942 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008943 case ISN_EXECCONCAT:
8944 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008945 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008946 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008947 case ISN_GETITEM:
8948 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008949 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008950 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008951 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008953 case ISN_LOADBDICT:
8954 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008955 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008957 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008958 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008959 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008960 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008961 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008962 case ISN_NEGATENR:
8963 case ISN_NEWDICT:
8964 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008965 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008966 case ISN_OPFLOAT:
8967 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008969 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008970 case ISN_PROF_END:
8971 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008972 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008973 case ISN_PUSHF:
8974 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008976 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008977 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008978 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008979 case ISN_SHUFFLE:
8980 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008981 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008982 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008983 case ISN_STORENR:
8984 case ISN_STOREOUTER:
8985 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008986 case ISN_STOREV:
8987 case ISN_STRINDEX:
8988 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008989 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01008990 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008991 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01008992 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008993 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 // nothing allocated
8995 break;
8996 }
8997}
8998
8999/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009000 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009001 */
9002 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009003delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009004{
9005 int idx;
9006
9007 ga_clear(&dfunc->df_def_args_isn);
9008
9009 if (dfunc->df_instr != NULL)
9010 {
9011 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9012 delete_instr(dfunc->df_instr + idx);
9013 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009014 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009015 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009016#ifdef FEAT_PROFILE
9017 if (dfunc->df_instr_prof != NULL)
9018 {
9019 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9020 delete_instr(dfunc->df_instr_prof + idx);
9021 VIM_CLEAR(dfunc->df_instr_prof);
9022 dfunc->df_instr_prof = NULL;
9023 }
9024#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009025
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009026 if (mark_deleted)
9027 dfunc->df_deleted = TRUE;
9028 if (dfunc->df_ufunc != NULL)
9029 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009030}
9031
9032/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009033 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009034 * function, unless another user function still uses it.
9035 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009036 */
9037 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009038unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009039{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009040 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009041 {
9042 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9043 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009045 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009046 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009047 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009048 ufunc->uf_dfunc_idx = 0;
9049 if (dfunc->df_ufunc == ufunc)
9050 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009051 }
9052}
9053
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009054/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009055 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009056 */
9057 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009058link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009059{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009060 if (ufunc->uf_dfunc_idx > 0)
9061 {
9062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9063 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009064
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009065 ++dfunc->df_refcount;
9066 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009067}
9068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009069#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009070/*
9071 * Free all functions defined with ":def".
9072 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009073 void
9074free_def_functions(void)
9075{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009076 int idx;
9077
9078 for (idx = 0; idx < def_functions.ga_len; ++idx)
9079 {
9080 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9081
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009082 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009083 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009084 }
9085
9086 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087}
9088#endif
9089
9090
9091#endif // FEAT_EVAL