blob: 71a8831623d934e3c5e61af07740a024453bbfeb [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 Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaarda479c72021-04-10 21:01:38 +0200425 // underscore argument is OK
426 if (len == 1 && *p == '_')
427 return OK;
428
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200429 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 {
431 if (is_arg)
432 semsg(_(e_argument_already_declared_in_script_str), p);
433 else
434 semsg(_(e_variable_already_declared_in_script_str), p);
435 return FAIL;
436 }
437
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100439 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100440 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200441 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200442 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200443 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100444 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200445 // A local or script-local function can shadow a global function.
446 if (ufunc == NULL || !func_is_global(ufunc)
447 || (p[0] == 'g' && p[1] == ':'))
448 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100449 if (is_arg)
450 semsg(_(e_argument_name_shadows_existing_variable_str), p);
451 else
452 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200453 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200454 return FAIL;
455 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100456 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200457 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100458 return OK;
459}
460
Bram Moolenaar65b95452020-07-19 14:03:09 +0200461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462/////////////////////////////////////////////////////////////////////
463// Following generate_ functions expect the caller to call ga_grow().
464
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200465#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
466#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Generate an instruction without arguments.
470 * Returns a pointer to the new instruction, NULL if failed.
471 */
472 static isn_T *
473generate_instr(cctx_T *cctx, isntype_T isn_type)
474{
475 garray_T *instr = &cctx->ctx_instr;
476 isn_T *isn;
477
Bram Moolenaar080457c2020-03-03 21:53:32 +0100478 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 if (ga_grow(instr, 1) == FAIL)
480 return NULL;
481 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
482 isn->isn_type = isn_type;
483 isn->isn_lnum = cctx->ctx_lnum + 1;
484 ++instr->ga_len;
485
486 return isn;
487}
488
489/*
490 * Generate an instruction without arguments.
491 * "drop" will be removed from the stack.
492 * Returns a pointer to the new instruction, NULL if failed.
493 */
494 static isn_T *
495generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
496{
497 garray_T *stack = &cctx->ctx_type_stack;
498
Bram Moolenaar080457c2020-03-03 21:53:32 +0100499 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 stack->ga_len -= drop;
501 return generate_instr(cctx, isn_type);
502}
503
504/*
505 * Generate instruction "isn_type" and put "type" on the type stack.
506 */
507 static isn_T *
508generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
509{
510 isn_T *isn;
511 garray_T *stack = &cctx->ctx_type_stack;
512
513 if ((isn = generate_instr(cctx, isn_type)) == NULL)
514 return NULL;
515
516 if (ga_grow(stack, 1) == FAIL)
517 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200518 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 ++stack->ga_len;
520
521 return isn;
522}
523
524/*
525 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200526 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 */
528 static int
529may_generate_2STRING(int offset, cctx_T *cctx)
530{
531 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200532 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100534 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100536 RETURN_OK_IF_SKIP(cctx);
537 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200538 switch ((*type)->tt_type)
539 {
540 // nothing to be done
541 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200543 // conversion possible
544 case VAR_SPECIAL:
545 case VAR_BOOL:
546 case VAR_NUMBER:
547 case VAR_FLOAT:
548 break;
549
550 // conversion possible (with runtime check)
551 case VAR_ANY:
552 case VAR_UNKNOWN:
553 isntype = ISN_2STRING_ANY;
554 break;
555
556 // conversion not possible
557 case VAR_VOID:
558 case VAR_BLOB:
559 case VAR_FUNC:
560 case VAR_PARTIAL:
561 case VAR_LIST:
562 case VAR_DICT:
563 case VAR_JOB:
564 case VAR_CHANNEL:
565 to_string_error((*type)->tt_type);
566 return FAIL;
567 }
568
569 *type = &t_string;
570 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return FAIL;
572 isn->isn_arg.number = offset;
573
574 return OK;
575}
576
577 static int
578check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
579{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200580 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200582 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 {
584 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200585 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200587 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 }
590 return OK;
591}
592
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 static int
594generate_add_instr(
595 cctx_T *cctx,
596 vartype_T vartype,
597 type_T *type1,
598 type_T *type2)
599{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100600 garray_T *stack = &cctx->ctx_type_stack;
601 isn_T *isn = generate_instr_drop(cctx,
602 vartype == VAR_NUMBER ? ISN_OPNR
603 : vartype == VAR_LIST ? ISN_ADDLIST
604 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100606 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200607#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100608 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200609
610 if (vartype != VAR_LIST && vartype != VAR_BLOB
611 && type1->tt_type != VAR_ANY
612 && type2->tt_type != VAR_ANY
613 && check_number_or_float(
614 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
615 return FAIL;
616
617 if (isn != NULL)
618 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100619
620 // When concatenating two lists with different member types the member type
621 // becomes "any".
622 if (vartype == VAR_LIST
623 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
624 && type1->tt_member != type2->tt_member)
625 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
626
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200627 return isn == NULL ? FAIL : OK;
628}
629
630/*
631 * Get the type to use for an instruction for an operation on "type1" and
632 * "type2". If they are matching use a type-specific instruction. Otherwise
633 * fall back to runtime type checking.
634 */
635 static vartype_T
636operator_type(type_T *type1, type_T *type2)
637{
638 if (type1->tt_type == type2->tt_type
639 && (type1->tt_type == VAR_NUMBER
640 || type1->tt_type == VAR_LIST
641#ifdef FEAT_FLOAT
642 || type1->tt_type == VAR_FLOAT
643#endif
644 || type1->tt_type == VAR_BLOB))
645 return type1->tt_type;
646 return VAR_ANY;
647}
648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649/*
650 * Generate an instruction with two arguments. The instruction depends on the
651 * type of the arguments.
652 */
653 static int
654generate_two_op(cctx_T *cctx, char_u *op)
655{
656 garray_T *stack = &cctx->ctx_type_stack;
657 type_T *type1;
658 type_T *type2;
659 vartype_T vartype;
660 isn_T *isn;
661
Bram Moolenaar080457c2020-03-03 21:53:32 +0100662 RETURN_OK_IF_SKIP(cctx);
663
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200664 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668
669 switch (*op)
670 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200671 case '+':
672 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 break;
675
676 case '-':
677 case '*':
678 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
679 op) == FAIL)
680 return FAIL;
681 if (vartype == VAR_NUMBER)
682 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
683#ifdef FEAT_FLOAT
684 else if (vartype == VAR_FLOAT)
685 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
686#endif
687 else
688 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
689 if (isn != NULL)
690 isn->isn_arg.op.op_type = *op == '*'
691 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
692 break;
693
Bram Moolenaar4c683752020-04-05 21:38:23 +0200694 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200696 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && type2->tt_type != VAR_NUMBER))
698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200699 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 return FAIL;
701 }
702 isn = generate_instr_drop(cctx,
703 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
704 if (isn != NULL)
705 isn->isn_arg.op.op_type = EXPR_REM;
706 break;
707 }
708
709 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200710 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 {
712 type_T *type = &t_any;
713
714#ifdef FEAT_FLOAT
715 // float+number and number+float results in float
716 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
717 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
718 type = &t_float;
719#endif
720 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
721 }
722
723 return OK;
724}
725
726/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 * Get the instruction to use for comparing "type1" with "type2"
728 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100731get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732{
733 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
Bram Moolenaar4c683752020-04-05 21:38:23 +0200735 if (type1 == VAR_UNKNOWN)
736 type1 = VAR_ANY;
737 if (type2 == VAR_UNKNOWN)
738 type2 = VAR_ANY;
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 if (type1 == type2)
741 {
742 switch (type1)
743 {
744 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
745 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
746 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
747 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
748 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
749 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
750 case VAR_LIST: isntype = ISN_COMPARELIST; break;
751 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
752 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 default: isntype = ISN_COMPAREANY; break;
754 }
755 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200756 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100758 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 isntype = ISN_COMPAREANY;
760
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 && (isntype == ISN_COMPAREBOOL
763 || isntype == ISN_COMPARESPECIAL
764 || isntype == ISN_COMPARENR
765 || isntype == ISN_COMPAREFLOAT))
766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200767 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200769 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 }
771 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100772 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
774 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
776 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 && (type1 == VAR_BLOB || type2 == VAR_BLOB
778 || type1 == VAR_LIST || type2 == VAR_LIST))))
779 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200780 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200782 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200784 return isntype;
785}
786
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200787 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100788check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200789{
790 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
791 return FAIL;
792 return OK;
793}
794
Bram Moolenaara5565e42020-05-09 15:44:01 +0200795/*
796 * Generate an ISN_COMPARE* instruction with a boolean result.
797 */
798 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100799generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200800{
801 isntype_T isntype;
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804 vartype_T type1;
805 vartype_T type2;
806
807 RETURN_OK_IF_SKIP(cctx);
808
809 // Get the known type of the two items on the stack. If they are matching
810 // use a type-specific instruction. Otherwise fall back to runtime type
811 // checking.
812 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
813 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200815 if (isntype == ISN_DROP)
816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
818 if ((isn = generate_instr(cctx, isntype)) == NULL)
819 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100820 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 isn->isn_arg.op.op_ic = ic;
822
823 // takes two arguments, puts one bool back
824 if (stack->ga_len >= 2)
825 {
826 --stack->ga_len;
827 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
828 }
829
830 return OK;
831}
832
833/*
834 * Generate an ISN_2BOOL instruction.
835 */
836 static int
837generate_2BOOL(cctx_T *cctx, int invert)
838{
839 isn_T *isn;
840 garray_T *stack = &cctx->ctx_type_stack;
841
Bram Moolenaar080457c2020-03-03 21:53:32 +0100842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
844 return FAIL;
845 isn->isn_arg.number = invert;
846
847 // type becomes bool
848 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
849
850 return OK;
851}
852
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200853/*
854 * Generate an ISN_COND2BOOL instruction.
855 */
856 static int
857generate_COND2BOOL(cctx_T *cctx)
858{
859 isn_T *isn;
860 garray_T *stack = &cctx->ctx_type_stack;
861
862 RETURN_OK_IF_SKIP(cctx);
863 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
864 return FAIL;
865
866 // type becomes bool
867 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
868
869 return OK;
870}
871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873generate_TYPECHECK(
874 cctx_T *cctx,
875 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100876 int offset,
877 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878{
879 isn_T *isn;
880 garray_T *stack = &cctx->ctx_type_stack;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
884 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100886 isn->isn_arg.type.ct_off = (int8_T)offset;
887 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar5e654232020-09-16 15:22:00 +0200889 // type becomes expected
890 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
892 return OK;
893}
894
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100895 static int
896generate_SETTYPE(
897 cctx_T *cctx,
898 type_T *expected)
899{
900 isn_T *isn;
901
902 RETURN_OK_IF_SKIP(cctx);
903 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
904 return FAIL;
905 isn->isn_arg.type.ct_type = alloc_type(expected);
906 return OK;
907}
908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
911 * used. Return FALSE if the types will never match.
912 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100913 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200914use_typecheck(type_T *actual, type_T *expected)
915{
916 if (actual->tt_type == VAR_ANY
917 || actual->tt_type == VAR_UNKNOWN
918 || (actual->tt_type == VAR_FUNC
919 && (expected->tt_type == VAR_FUNC
920 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100921 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
922 && ((actual->tt_member == &t_void)
923 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200924 return TRUE;
925 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
926 && actual->tt_type == expected->tt_type)
927 // This takes care of a nested list or dict.
928 return use_typecheck(actual->tt_member, expected->tt_member);
929 return FALSE;
930}
931
932/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200933 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200934 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 * - "actual" is a type that can be "expected" type: add a runtime check; or
936 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200937 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
938 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200939 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100940 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200941need_type(
942 type_T *actual,
943 type_T *expected,
944 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100945 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200946 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200947 int silent,
948 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200949{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100950 where_T where;
951
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200952 if (expected == &t_bool && actual != &t_bool
953 && (actual->tt_flags & TTFLAG_BOOL_OK))
954 {
955 // Using "0", "1" or the result of an expression with "&&" or "||" as a
956 // boolean is OK but requires a conversion.
957 generate_2BOOL(cctx, FALSE);
958 return OK;
959 }
960
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100961 where.wt_index = arg_idx;
962 where.wt_variable = FALSE;
963 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200964 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200965
966 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200967 // If it's a constant a runtime check makes no sense.
968 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200969 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100970 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200973
974 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100975 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200977}
978
979/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100980 * Check that the top of the type stack has a type that can be used as a
981 * condition. Give an error and return FAIL if not.
982 */
983 static int
984bool_on_stack(cctx_T *cctx)
985{
986 garray_T *stack = &cctx->ctx_type_stack;
987 type_T *type;
988
989 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
990 if (type == &t_bool)
991 return OK;
992
Bram Moolenaaraf8ea0d2021-04-11 18:24:46 +0200993 if (type == &t_any || type == &t_number || type == &t_number_bool)
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100994 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
995 // This requires a runtime type check.
996 return generate_COND2BOOL(cctx);
997
Bram Moolenaar351ead02021-01-16 16:07:01 +0100998 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100999}
1000
1001/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 * Generate an ISN_PUSHNR instruction.
1003 */
1004 static int
1005generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1006{
1007 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001008 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009
Bram Moolenaar080457c2020-03-03 21:53:32 +01001010 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1012 return FAIL;
1013 isn->isn_arg.number = number;
1014
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001015 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001016 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001017 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 return OK;
1019}
1020
1021/*
1022 * Generate an ISN_PUSHBOOL instruction.
1023 */
1024 static int
1025generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.number = number;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHSPEC instruction.
1039 */
1040 static int
1041generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1042{
1043 isn_T *isn;
1044
Bram Moolenaar080457c2020-03-03 21:53:32 +01001045 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1047 return FAIL;
1048 isn->isn_arg.number = number;
1049
1050 return OK;
1051}
1052
1053#ifdef FEAT_FLOAT
1054/*
1055 * Generate an ISN_PUSHF instruction.
1056 */
1057 static int
1058generate_PUSHF(cctx_T *cctx, float_T fnumber)
1059{
1060 isn_T *isn;
1061
Bram Moolenaar080457c2020-03-03 21:53:32 +01001062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.fnumber = fnumber;
1066
1067 return OK;
1068}
1069#endif
1070
1071/*
1072 * Generate an ISN_PUSHS instruction.
1073 * Consumes "str".
1074 */
1075 static int
1076generate_PUSHS(cctx_T *cctx, char_u *str)
1077{
1078 isn_T *isn;
1079
Bram Moolenaar508b5612021-01-02 18:17:26 +01001080 if (cctx->ctx_skip == SKIP_YES)
1081 {
1082 vim_free(str);
1083 return OK;
1084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.string = str;
1088
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 * Generate an ISN_PUSHCHANNEL instruction.
1094 * Consumes "channel".
1095 */
1096 static int
1097generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1098{
1099 isn_T *isn;
1100
Bram Moolenaar080457c2020-03-03 21:53:32 +01001101 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001102 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1103 return FAIL;
1104 isn->isn_arg.channel = channel;
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_PUSHJOB instruction.
1111 * Consumes "job".
1112 */
1113 static int
1114generate_PUSHJOB(cctx_T *cctx, job_T *job)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001120 return FAIL;
1121 isn->isn_arg.job = job;
1122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 * Generate an ISN_PUSHBLOB instruction.
1128 * Consumes "blob".
1129 */
1130 static int
1131generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.blob = blob;
1139
1140 return OK;
1141}
1142
1143/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001144 * Generate an ISN_PUSHFUNC instruction with name "name".
1145 * Consumes "name".
1146 */
1147 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001148generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001153 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001154 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001155 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001161 * Generate an ISN_GETITEM instruction with "index".
1162 */
1163 static int
1164generate_GETITEM(cctx_T *cctx, int index)
1165{
1166 isn_T *isn;
1167 garray_T *stack = &cctx->ctx_type_stack;
1168 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1169 type_T *item_type = &t_any;
1170
1171 RETURN_OK_IF_SKIP(cctx);
1172
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001173 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001174 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 emsg(_(e_listreq));
1177 return FAIL;
1178 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001179 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001180 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1181 return FAIL;
1182 isn->isn_arg.number = index;
1183
1184 // add the item type to the type stack
1185 if (ga_grow(stack, 1) == FAIL)
1186 return FAIL;
1187 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1188 ++stack->ga_len;
1189 return OK;
1190}
1191
1192/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001193 * Generate an ISN_SLICE instruction with "count".
1194 */
1195 static int
1196generate_SLICE(cctx_T *cctx, int count)
1197{
1198 isn_T *isn;
1199
1200 RETURN_OK_IF_SKIP(cctx);
1201 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.number = count;
1204 return OK;
1205}
1206
1207/*
1208 * Generate an ISN_CHECKLEN instruction with "min_len".
1209 */
1210 static int
1211generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1212{
1213 isn_T *isn;
1214
1215 RETURN_OK_IF_SKIP(cctx);
1216
1217 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1218 return FAIL;
1219 isn->isn_arg.checklen.cl_min_len = min_len;
1220 isn->isn_arg.checklen.cl_more_OK = more_OK;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 * Generate an ISN_STORE instruction.
1227 */
1228 static int
1229generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1235 return FAIL;
1236 if (name != NULL)
1237 isn->isn_arg.string = vim_strsave(name);
1238 else
1239 isn->isn_arg.number = idx;
1240
1241 return OK;
1242}
1243
1244/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001245 * Generate an ISN_STOREOUTER instruction.
1246 */
1247 static int
1248generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1249{
1250 isn_T *isn;
1251
1252 RETURN_OK_IF_SKIP(cctx);
1253 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1254 return FAIL;
1255 isn->isn_arg.outer.outer_idx = idx;
1256 isn->isn_arg.outer.outer_depth = level;
1257
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1263 */
1264 static int
1265generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1266{
1267 isn_T *isn;
1268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1271 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001272 isn->isn_arg.storenr.stnr_idx = idx;
1273 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_STOREOPT instruction
1280 */
1281 static int
1282generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001287 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 return FAIL;
1289 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1290 isn->isn_arg.storeopt.so_flags = opt_flags;
1291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_LOAD or similar instruction.
1297 */
1298 static int
1299generate_LOAD(
1300 cctx_T *cctx,
1301 isntype_T isn_type,
1302 int idx,
1303 char_u *name,
1304 type_T *type)
1305{
1306 isn_T *isn;
1307
Bram Moolenaar080457c2020-03-03 21:53:32 +01001308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1310 return FAIL;
1311 if (name != NULL)
1312 isn->isn_arg.string = vim_strsave(name);
1313 else
1314 isn->isn_arg.number = idx;
1315
1316 return OK;
1317}
1318
1319/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001320 * Generate an ISN_LOADOUTER instruction
1321 */
1322 static int
1323generate_LOADOUTER(
1324 cctx_T *cctx,
1325 int idx,
1326 int nesting,
1327 type_T *type)
1328{
1329 isn_T *isn;
1330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.outer.outer_idx = idx;
1335 isn->isn_arg.outer.outer_depth = nesting;
1336
1337 return OK;
1338}
1339
1340/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001341 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 */
1343 static int
1344generate_LOADV(
1345 cctx_T *cctx,
1346 char_u *name,
1347 int error)
1348{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001349 int di_flags;
1350 int vidx = find_vim_var(name, &di_flags);
1351 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352
Bram Moolenaar080457c2020-03-03 21:53:32 +01001353 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 if (vidx < 0)
1355 {
1356 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001357 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001358 return FAIL;
1359 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001362 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363}
1364
1365/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366 * Generate an ISN_UNLET instruction.
1367 */
1368 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001369generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001370{
1371 isn_T *isn;
1372
1373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001374 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001375 return FAIL;
1376 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1377 isn->isn_arg.unlet.ul_forceit = forceit;
1378
1379 return OK;
1380}
1381
1382/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001383 * Generate an ISN_LOCKCONST instruction.
1384 */
1385 static int
1386generate_LOCKCONST(cctx_T *cctx)
1387{
1388 isn_T *isn;
1389
1390 RETURN_OK_IF_SKIP(cctx);
1391 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1392 return FAIL;
1393 return OK;
1394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Generate an ISN_LOADS instruction.
1398 */
1399 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001402 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001404 int sid,
1405 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406{
1407 isn_T *isn;
1408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001410 if (isn_type == ISN_LOADS)
1411 isn = generate_instr_type(cctx, isn_type, type);
1412 else
1413 isn = generate_instr_drop(cctx, isn_type, 1);
1414 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001416 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1417 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 return OK;
1420}
1421
1422/*
1423 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1424 */
1425 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001426generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 cctx_T *cctx,
1428 isntype_T isn_type,
1429 int sid,
1430 int idx,
1431 type_T *type)
1432{
1433 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001434 scriptref_T *sref;
1435 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
Bram Moolenaar080457c2020-03-03 21:53:32 +01001437 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 if (isn_type == ISN_LOADSCRIPT)
1439 isn = generate_instr_type(cctx, isn_type, type);
1440 else
1441 isn = generate_instr_drop(cctx, isn_type, 1);
1442 if (isn == NULL)
1443 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001444
1445 // This requires three arguments, which doesn't fit in an instruction, thus
1446 // we need to allocate a struct for this.
1447 sref = ALLOC_ONE(scriptref_T);
1448 if (sref == NULL)
1449 return FAIL;
1450 isn->isn_arg.script.scriptref = sref;
1451 sref->sref_sid = sid;
1452 sref->sref_idx = idx;
1453 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001454 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 return OK;
1456}
1457
1458/*
1459 * Generate an ISN_NEWLIST instruction.
1460 */
1461 static int
1462generate_NEWLIST(cctx_T *cctx, int count)
1463{
1464 isn_T *isn;
1465 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 type_T *type;
1467 type_T *member;
1468
Bram Moolenaar080457c2020-03-03 21:53:32 +01001469 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1471 return FAIL;
1472 isn->isn_arg.number = count;
1473
Bram Moolenaar127542b2020-08-09 17:22:04 +02001474 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001475 if (count == 0)
1476 member = &t_void;
1477 else
1478 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001479 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1480 cctx->ctx_type_list);
1481 type = get_list_type(member, cctx->ctx_type_list);
1482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 // drop the value types
1484 stack->ga_len -= count;
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // add the list type to the type stack
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1490 ++stack->ga_len;
1491
1492 return OK;
1493}
1494
1495/*
1496 * Generate an ISN_NEWDICT instruction.
1497 */
1498 static int
1499generate_NEWDICT(cctx_T *cctx, int count)
1500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 type_T *type;
1504 type_T *member;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.number = count;
1510
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001511 if (count == 0)
1512 member = &t_void;
1513 else
1514 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001515 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1516 cctx->ctx_type_list);
1517 type = get_dict_type(member, cctx->ctx_type_list);
1518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 // drop the key and value types
1520 stack->ga_len -= 2 * count;
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // add the dict type to the type stack
1523 if (ga_grow(stack, 1) == FAIL)
1524 return FAIL;
1525 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1526 ++stack->ga_len;
1527
1528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_FUNCREF instruction.
1533 */
1534 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001535generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539
Bram Moolenaar080457c2020-03-03 21:53:32 +01001540 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1542 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001543 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001544 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001546 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001547 // the nested context, including this one.
1548 if (ufunc->uf_flags & FC_CLOSURE)
1549 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if (ga_grow(stack, 1) == FAIL)
1552 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001553 ((type_T **)stack->ga_data)[stack->ga_len] =
1554 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 ++stack->ga_len;
1556
1557 return OK;
1558}
1559
1560/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001561 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001562 * "lambda_name" and "func_name" must be in allocated memory and will be
1563 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 */
1565 static int
1566generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1567{
1568 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001569
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001570 if (cctx->ctx_skip == SKIP_YES)
1571 {
1572 vim_free(lambda_name);
1573 vim_free(func_name);
1574 return OK;
1575 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 {
1578 vim_free(lambda_name);
1579 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001580 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001581 }
1582 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 isn->isn_arg.newfunc.nf_global = func_name;
1584
1585 return OK;
1586}
1587
1588/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001589 * Generate an ISN_DEF instruction: list functions
1590 */
1591 static int
1592generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1593{
1594 isn_T *isn;
1595
1596 RETURN_OK_IF_SKIP(cctx);
1597 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1598 return FAIL;
1599 if (len > 0)
1600 {
1601 isn->isn_arg.string = vim_strnsave(name, len);
1602 if (isn->isn_arg.string == NULL)
1603 return FAIL;
1604 }
1605 return OK;
1606}
1607
1608/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 * Generate an ISN_JUMP instruction.
1610 */
1611 static int
1612generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1613{
1614 isn_T *isn;
1615 garray_T *stack = &cctx->ctx_type_stack;
1616
Bram Moolenaar080457c2020-03-03 21:53:32 +01001617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.jump.jump_when = when;
1621 isn->isn_arg.jump.jump_where = where;
1622
1623 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1624 --stack->ga_len;
1625
1626 return OK;
1627}
1628
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001629/*
1630 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1631 */
1632 static int
1633generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1634{
1635 isn_T *isn;
1636
1637 RETURN_OK_IF_SKIP(cctx);
1638 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1641 // jump_where is set later
1642 return OK;
1643}
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 static int
1646generate_FOR(cctx_T *cctx, int loop_idx)
1647{
1648 isn_T *isn;
1649 garray_T *stack = &cctx->ctx_type_stack;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.forloop.for_idx = loop_idx;
1655
1656 if (ga_grow(stack, 1) == FAIL)
1657 return FAIL;
1658 // type doesn't matter, will be stored next
1659 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1660 ++stack->ga_len;
1661
1662 return OK;
1663}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001664/*
1665 * Generate an ISN_TRYCONT instruction.
1666 */
1667 static int
1668generate_TRYCONT(cctx_T *cctx, int levels, int where)
1669{
1670 isn_T *isn;
1671
1672 RETURN_OK_IF_SKIP(cctx);
1673 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1674 return FAIL;
1675 isn->isn_arg.trycont.tct_levels = levels;
1676 isn->isn_arg.trycont.tct_where = where;
1677
1678 return OK;
1679}
1680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681
1682/*
1683 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 * Return FAIL if the number of arguments is wrong.
1686 */
1687 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001688generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689{
1690 isn_T *isn;
1691 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001692 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001693 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001694 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
Bram Moolenaar080457c2020-03-03 21:53:32 +01001696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 argoff = check_internal_func(func_idx, argcount);
1698 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 return FAIL;
1700
Bram Moolenaar389df252020-07-09 21:20:47 +02001701 if (method_call && argoff > 1)
1702 {
1703 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.shuffle.shfl_item = argcount;
1706 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1707 }
1708
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001709 if (argcount > 0)
1710 {
1711 // Check the types of the arguments.
1712 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001713 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1714 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001716 if (internal_func_is_map(func_idx))
1717 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001718 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1721 return FAIL;
1722 isn->isn_arg.bfunc.cbf_idx = func_idx;
1723 isn->isn_arg.bfunc.cbf_argcount = argcount;
1724
Bram Moolenaar94738d82020-10-21 14:25:07 +02001725 // Drop the argument types and push the return type.
1726 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 if (ga_grow(stack, 1) == FAIL)
1728 return FAIL;
1729 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001730 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001731 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733 if (maptype != NULL && maptype->tt_member != NULL
1734 && maptype->tt_member != &t_any)
1735 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001736 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 return OK;
1739}
1740
1741/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001742 * Generate an ISN_LISTAPPEND instruction. Works like add().
1743 * Argument count is already checked.
1744 */
1745 static int
1746generate_LISTAPPEND(cctx_T *cctx)
1747{
1748 garray_T *stack = &cctx->ctx_type_stack;
1749 type_T *list_type;
1750 type_T *item_type;
1751 type_T *expected;
1752
1753 // Caller already checked that list_type is a list.
1754 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1755 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1756 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001757 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001758 return FAIL;
1759
1760 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1761 return FAIL;
1762
1763 --stack->ga_len; // drop the argument
1764 return OK;
1765}
1766
1767/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001768 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1769 * Argument count is already checked.
1770 */
1771 static int
1772generate_BLOBAPPEND(cctx_T *cctx)
1773{
1774 garray_T *stack = &cctx->ctx_type_stack;
1775 type_T *item_type;
1776
1777 // Caller already checked that blob_type is a blob.
1778 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001779 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001780 return FAIL;
1781
1782 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1783 return FAIL;
1784
1785 --stack->ga_len; // drop the argument
1786 return OK;
1787}
1788
1789/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 * Return TRUE if "ufunc" should be compiled, taking into account whether
1791 * "profile" indicates profiling is to be done.
1792 */
1793 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001794func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001795{
1796 switch (ufunc->uf_def_status)
1797 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001798 case UF_TO_BE_COMPILED:
1799 return TRUE;
1800
Bram Moolenaarb2049902021-01-24 12:53:53 +01001801 case UF_COMPILED:
1802 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001803#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001804 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1805 + ufunc->uf_dfunc_idx;
1806
1807 return profile ? dfunc->df_instr_prof == NULL
1808 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001809#else
1810 break;
1811#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001812 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001813
1814 case UF_NOT_COMPILED:
1815 case UF_COMPILE_ERROR:
1816 case UF_COMPILING:
1817 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001818 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001819 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001820}
1821
1822/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 * Generate an ISN_DCALL or ISN_UCALL instruction.
1824 * Return FAIL if the number of arguments is wrong.
1825 */
1826 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001827generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
1831 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001832 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
Bram Moolenaar080457c2020-03-03 21:53:32 +01001834 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 if (argcount > regular_args && !has_varargs(ufunc))
1836 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001837 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 return FAIL;
1839 }
1840 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1841 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001842 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 return FAIL;
1844 }
1845
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001846 if (ufunc->uf_def_status != UF_NOT_COMPILED
1847 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001848 {
1849 int i;
1850
1851 for (i = 0; i < argcount; ++i)
1852 {
1853 type_T *expected;
1854 type_T *actual;
1855
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001856 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1857 if (actual == &t_special
1858 && i >= regular_args - ufunc->uf_def_args.ga_len)
1859 {
1860 // assume v:none used for default argument value
1861 continue;
1862 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001863 if (i < regular_args)
1864 {
1865 if (ufunc->uf_arg_types == NULL)
1866 continue;
1867 expected = ufunc->uf_arg_types[i];
1868 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001869 else if (ufunc->uf_va_type == NULL
1870 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001871 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001872 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 else
1874 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001875 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001876 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001877 {
1878 arg_type_mismatch(expected, actual, i + 1);
1879 return FAIL;
1880 }
1881 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001882 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001883 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001884 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001885 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001886 }
1887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001889 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001890 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001892 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 {
1894 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1895 isn->isn_arg.dfunc.cdf_argcount = argcount;
1896 }
1897 else
1898 {
1899 // A user function may be deleted and redefined later, can't use the
1900 // ufunc pointer, need to look it up again at runtime.
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903 }
1904
1905 stack->ga_len -= argcount; // drop the arguments
1906 if (ga_grow(stack, 1) == FAIL)
1907 return FAIL;
1908 // add return value
1909 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1910 ++stack->ga_len;
1911
1912 return OK;
1913}
1914
1915/*
1916 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1917 */
1918 static int
1919generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1920{
1921 isn_T *isn;
1922 garray_T *stack = &cctx->ctx_type_stack;
1923
Bram Moolenaar080457c2020-03-03 21:53:32 +01001924 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1926 return FAIL;
1927 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1928 isn->isn_arg.ufunc.cuf_argcount = argcount;
1929
1930 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001931 if (ga_grow(stack, 1) == FAIL)
1932 return FAIL;
1933 // add return value
1934 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1935 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
1937 return OK;
1938}
1939
1940/*
1941 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001942 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 */
1944 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001945generate_PCALL(
1946 cctx_T *cctx,
1947 int argcount,
1948 char_u *name,
1949 type_T *type,
1950 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951{
1952 isn_T *isn;
1953 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaar080457c2020-03-03 21:53:32 +01001956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001957
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001958 if (type->tt_type == VAR_ANY)
1959 ret_type = &t_any;
1960 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001961 {
1962 if (type->tt_argcount != -1)
1963 {
1964 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1965
1966 if (argcount < type->tt_min_argcount - varargs)
1967 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001968 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001969 return FAIL;
1970 }
1971 if (!varargs && argcount > type->tt_argcount)
1972 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001973 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 return FAIL;
1975 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001976 if (type->tt_args != NULL)
1977 {
1978 int i;
1979
1980 for (i = 0; i < argcount; ++i)
1981 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02001982 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001983 type_T *actual = ((type_T **)stack->ga_data)[
1984 stack->ga_len + offset];
1985 type_T *expected;
1986
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001987 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001988 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001989 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001990 else if (i >= type->tt_min_argcount
1991 && actual == &t_special)
1992 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001993 else
1994 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001995 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001996 cctx, TRUE, FALSE) == FAIL)
1997 {
1998 arg_type_mismatch(expected, actual, i + 1);
1999 return FAIL;
2000 }
2001 }
2002 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002003 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002004 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002005 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002006 else
2007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002008 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002009 return FAIL;
2010 }
2011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2013 return FAIL;
2014 isn->isn_arg.pfunc.cpf_top = at_top;
2015 isn->isn_arg.pfunc.cpf_argcount = argcount;
2016
2017 stack->ga_len -= argcount; // drop the arguments
2018
2019 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002022 // If partial is above the arguments it must be cleared and replaced with
2023 // the return value.
2024 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2025 return FAIL;
2026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 return OK;
2028}
2029
2030/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002031 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 */
2033 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002034generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035{
2036 isn_T *isn;
2037 garray_T *stack = &cctx->ctx_type_stack;
2038 type_T *type;
2039
Bram Moolenaar080457c2020-03-03 21:53:32 +01002040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002041 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002043 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002045 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002047 if (type->tt_type != VAR_DICT && type != &t_any)
2048 {
2049 emsg(_(e_dictreq));
2050 return FAIL;
2051 }
2052 // change dict type to dict member type
2053 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002054 {
2055 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2056 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058
2059 return OK;
2060}
2061
2062/*
2063 * Generate an ISN_ECHO instruction.
2064 */
2065 static int
2066generate_ECHO(cctx_T *cctx, int with_white, int count)
2067{
2068 isn_T *isn;
2069
Bram Moolenaar080457c2020-03-03 21:53:32 +01002070 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2072 return FAIL;
2073 isn->isn_arg.echo.echo_with_white = with_white;
2074 isn->isn_arg.echo.echo_count = count;
2075
2076 return OK;
2077}
2078
Bram Moolenaarad39c092020-02-26 18:23:43 +01002079/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002080 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 */
2082 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084{
2085 isn_T *isn;
2086
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002087 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002088 return FAIL;
2089 isn->isn_arg.number = count;
2090
2091 return OK;
2092}
2093
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002094/*
2095 * Generate an ISN_PUT instruction.
2096 */
2097 static int
2098generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2099{
2100 isn_T *isn;
2101
2102 RETURN_OK_IF_SKIP(cctx);
2103 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.put.put_regname = regname;
2106 isn->isn_arg.put.put_lnum = lnum;
2107 return OK;
2108}
2109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 static int
2111generate_EXEC(cctx_T *cctx, char_u *line)
2112{
2113 isn_T *isn;
2114
Bram Moolenaar080457c2020-03-03 21:53:32 +01002115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2117 return FAIL;
2118 isn->isn_arg.string = vim_strsave(line);
2119 return OK;
2120}
2121
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002122 static int
2123generate_EXECCONCAT(cctx_T *cctx, int count)
2124{
2125 isn_T *isn;
2126
2127 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2128 return FAIL;
2129 isn->isn_arg.number = count;
2130 return OK;
2131}
2132
Bram Moolenaar4c137212021-04-19 16:48:48 +02002133 static int
2134generate_substitute(char_u *cmd, int instr_start, cctx_T *cctx)
2135{
2136 isn_T *isn;
2137 isn_T *instr;
2138 int instr_count = cctx->ctx_instr.ga_len - instr_start;
2139
2140 instr = ALLOC_MULT(isn_T, instr_count + 1);
2141 if (instr == NULL)
2142 return FAIL;
2143 // Move the generated instructions into the ISN_SUBSTITUTE instructions,
2144 // then truncate the list of instructions, so they are used only once.
2145 mch_memmove(instr, ((isn_T *)cctx->ctx_instr.ga_data) + instr_start,
2146 instr_count * sizeof(isn_T));
2147 instr[instr_count].isn_type = ISN_FINISH;
2148 cctx->ctx_instr.ga_len = instr_start;
2149
2150 if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
2151 {
2152 vim_free(instr);
2153 return FAIL;
2154 }
2155 isn->isn_arg.subs.subs_cmd = vim_strsave(cmd);
2156 isn->isn_arg.subs.subs_instr = instr;
2157 return OK;
2158}
2159
Bram Moolenaar08597872020-12-10 19:43:40 +01002160/*
2161 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2162 */
2163 static int
2164generate_RANGE(cctx_T *cctx, char_u *range)
2165{
2166 isn_T *isn;
2167 garray_T *stack = &cctx->ctx_type_stack;
2168
2169 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2170 return FAIL;
2171 isn->isn_arg.string = range;
2172
2173 if (ga_grow(stack, 1) == FAIL)
2174 return FAIL;
2175 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2176 ++stack->ga_len;
2177 return OK;
2178}
2179
Bram Moolenaar792f7862020-11-23 08:31:18 +01002180 static int
2181generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2182{
2183 isn_T *isn;
2184
2185 RETURN_OK_IF_SKIP(cctx);
2186 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2187 return FAIL;
2188 isn->isn_arg.unpack.unp_count = var_count;
2189 isn->isn_arg.unpack.unp_semicolon = semicolon;
2190 return OK;
2191}
2192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002194 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002195 */
2196 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002197generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002198{
2199 isn_T *isn;
2200
Bram Moolenaarfa984412021-03-25 22:15:28 +01002201 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002202 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002203 cctx->ctx_has_cmdmod = TRUE;
2204
2205 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002206 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002207 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2208 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2209 return FAIL;
2210 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002211 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002212 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002213 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002214
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002215 return OK;
2216}
2217
2218 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002219generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002220{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002221 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2222 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002223 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002224 return OK;
2225}
2226
Bram Moolenaarfa984412021-03-25 22:15:28 +01002227 static int
2228misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002229{
2230 garray_T *instr = &cctx->ctx_instr;
2231
Bram Moolenaara91a7132021-03-25 21:12:15 +01002232 if (cctx->ctx_has_cmdmod
2233 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2234 == ISN_CMDMOD)
2235 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002236 emsg(_(e_misplaced_command_modifier));
2237 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002238 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002239 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002240}
2241
2242/*
2243 * Get the index of the current instruction.
2244 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2245 */
2246 static int
2247current_instr_idx(cctx_T *cctx)
2248{
2249 garray_T *instr = &cctx->ctx_instr;
2250 int idx = instr->ga_len;
2251
2252 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2253 .isn_type == ISN_CMDMOD)
2254 --idx;
2255#ifdef FEAT_PROFILE
2256 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2257 .isn_type == ISN_PROF_START)
2258 --idx;
2259#endif
2260 return idx;
2261}
2262
Bram Moolenaarf002a412021-01-24 13:34:18 +01002263#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002264 static void
2265may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2266{
2267 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002268 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002269}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002270#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002271
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002274 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002276 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002277reserve_local(
2278 cctx_T *cctx,
2279 char_u *name,
2280 size_t len,
2281 int isConst,
2282 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 lvar_T *lvar;
2285
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002286 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002288 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002289 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 }
2291
2292 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002293 return NULL;
2294 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002295 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002297 // Every local variable uses the next entry on the stack. We could re-use
2298 // the last ones when leaving a scope, but then variables used in a closure
2299 // might get overwritten. To keep things simple do not re-use stack
2300 // entries. This is less efficient, but memory is cheap these days.
2301 lvar->lv_idx = cctx->ctx_locals_count++;
2302
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002303 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 lvar->lv_const = isConst;
2305 lvar->lv_type = type;
2306
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002307 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308}
2309
2310/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002311 * Remove local variables above "new_top".
2312 */
2313 static void
2314unwind_locals(cctx_T *cctx, int new_top)
2315{
2316 if (cctx->ctx_locals.ga_len > new_top)
2317 {
2318 int idx;
2319 lvar_T *lvar;
2320
2321 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2322 {
2323 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2324 vim_free(lvar->lv_name);
2325 }
2326 }
2327 cctx->ctx_locals.ga_len = new_top;
2328}
2329
2330/*
2331 * Free all local variables.
2332 */
2333 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002334free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002335{
2336 unwind_locals(cctx, 0);
2337 ga_clear(&cctx->ctx_locals);
2338}
2339
2340/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002341 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2342 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2343 * error if the variable was defined with :const.
2344 */
2345 static int
2346check_item_writable(svar_T *sv, int check_writable, char_u *name)
2347{
2348 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2349 || (check_writable == ASSIGN_FINAL
2350 && sv->sv_const == ASSIGN_CONST))
2351 {
2352 semsg(_(e_readonlyvar), name);
2353 return FAIL;
2354 }
2355 return OK;
2356}
2357
2358/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002360 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 * Returns the index in "sn_var_vals" if found.
2362 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002363 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 */
2365 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002366get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367{
2368 hashtab_T *ht;
2369 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002370 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002371 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 int idx;
2373
Bram Moolenaare3d46852020-08-29 13:39:17 +02002374 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002376 if (sid == current_sctx.sc_sid)
2377 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002378 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002379
2380 if (sav == NULL)
2381 return -2;
2382 idx = sav->sav_var_vals_idx;
2383 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002384 if (check_item_writable(sv, check_writable, name) == FAIL)
2385 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002386 return idx;
2387 }
2388
2389 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 ht = &SCRIPT_VARS(sid);
2391 di = find_var_in_ht(ht, 0, name, TRUE);
2392 if (di == NULL)
2393 return -2;
2394
2395 // Now find the svar_T index in sn_var_vals.
2396 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2397 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002398 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 if (sv->sv_tv == &di->di_tv)
2400 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002401 if (check_item_writable(sv, check_writable, name) == FAIL)
2402 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 return idx;
2404 }
2405 }
2406 return -1;
2407}
2408
2409/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002410 * Find "name" in imported items of the current script or in "cctx" if not
2411 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 */
2413 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002414find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 int idx;
2417
Bram Moolenaare3d46852020-08-29 13:39:17 +02002418 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002419 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 if (cctx != NULL)
2421 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2422 {
2423 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2424 + idx;
2425
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002426 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2427 : STRLEN(import->imp_name) == len
2428 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 return import;
2430 }
2431
Bram Moolenaarefa94442020-08-08 22:16:00 +02002432 return find_imported_in_script(name, len, current_sctx.sc_sid);
2433}
2434
2435 imported_T *
2436find_imported_in_script(char_u *name, size_t len, int sid)
2437{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002438 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002439 int idx;
2440
Bram Moolenaare3d46852020-08-29 13:39:17 +02002441 if (!SCRIPT_ID_VALID(sid))
2442 return NULL;
2443 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2445 {
2446 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2447
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002448 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2449 : STRLEN(import->imp_name) == len
2450 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 return import;
2452 }
2453 return NULL;
2454}
2455
2456/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002457 * Free all imported variables.
2458 */
2459 static void
2460free_imported(cctx_T *cctx)
2461{
2462 int idx;
2463
2464 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2465 {
2466 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2467
2468 vim_free(import->imp_name);
2469 }
2470 ga_clear(&cctx->ctx_imports);
2471}
2472
2473/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002474 * Return a pointer to the next line that isn't empty or only contains a
2475 * comment. Skips over white space.
2476 * Returns NULL if there is none.
2477 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 char_u *
2479peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002480{
2481 int lnum = cctx->ctx_lnum;
2482
2483 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2484 {
2485 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002486 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002487
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002488 // ignore NULLs inserted for continuation lines
2489 if (line != NULL)
2490 {
2491 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002492 if (vim9_bad_comment(p))
2493 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002494 if (*p != NUL && !vim9_comment_start(p))
2495 return p;
2496 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497 }
2498 return NULL;
2499}
2500
2501/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002502 * Called when checking for a following operator at "arg". When the rest of
2503 * the line is empty or only a comment, peek the next line. If there is a next
2504 * line return a pointer to it and set "nextp".
2505 * Otherwise skip over white space.
2506 */
2507 static char_u *
2508may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2509{
2510 char_u *p = skipwhite(arg);
2511
2512 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002513 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002514 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002515 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002516 if (*nextp != NULL)
2517 return *nextp;
2518 }
2519 return p;
2520}
2521
2522/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002523 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002525 * Returns NULL when at the end.
2526 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002527 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002528next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002529{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002530 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002531
2532 do
2533 {
2534 ++cctx->ctx_lnum;
2535 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002536 {
2537 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002538 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002539 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002540 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002541 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002542 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002543 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002544 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002545 return line;
2546}
2547
2548/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002549 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002550 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002551 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002552 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2553 */
2554 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002555may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002556{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002557 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002558 if (vim9_bad_comment(*arg))
2559 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002560 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002561 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002562 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002563
2564 if (next == NULL)
2565 return FAIL;
2566 *arg = skipwhite(next);
2567 }
2568 return OK;
2569}
2570
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002571/*
2572 * Idem, and give an error when failed.
2573 */
2574 static int
2575may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2576{
2577 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2578 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002579 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002580 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002581 return FAIL;
2582 }
2583 return OK;
2584}
2585
2586
Bram Moolenaara5565e42020-05-09 15:44:01 +02002587// Structure passed between the compile_expr* functions to keep track of
2588// constants that have been parsed but for which no code was produced yet. If
2589// possible expressions on these constants are applied at compile time. If
2590// that is not possible, the code to push the constants needs to be generated
2591// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002592// Using 50 should be more than enough of 5 levels of ().
2593#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002594typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002595 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002596 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002597 int pp_is_const; // all generated code was constants, used for a
2598 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002599} ppconst_T;
2600
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002601static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002602static int compile_expr0(char_u **arg, cctx_T *cctx);
2603static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2604
Bram Moolenaara5565e42020-05-09 15:44:01 +02002605/*
2606 * Generate a PUSH instruction for "tv".
2607 * "tv" will be consumed or cleared.
2608 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2609 */
2610 static int
2611generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2612{
2613 if (tv != NULL)
2614 {
2615 switch (tv->v_type)
2616 {
2617 case VAR_UNKNOWN:
2618 break;
2619 case VAR_BOOL:
2620 generate_PUSHBOOL(cctx, tv->vval.v_number);
2621 break;
2622 case VAR_SPECIAL:
2623 generate_PUSHSPEC(cctx, tv->vval.v_number);
2624 break;
2625 case VAR_NUMBER:
2626 generate_PUSHNR(cctx, tv->vval.v_number);
2627 break;
2628#ifdef FEAT_FLOAT
2629 case VAR_FLOAT:
2630 generate_PUSHF(cctx, tv->vval.v_float);
2631 break;
2632#endif
2633 case VAR_BLOB:
2634 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2635 tv->vval.v_blob = NULL;
2636 break;
2637 case VAR_STRING:
2638 generate_PUSHS(cctx, tv->vval.v_string);
2639 tv->vval.v_string = NULL;
2640 break;
2641 default:
2642 iemsg("constant type not supported");
2643 clear_tv(tv);
2644 return FAIL;
2645 }
2646 tv->v_type = VAR_UNKNOWN;
2647 }
2648 return OK;
2649}
2650
2651/*
2652 * Generate code for any ppconst entries.
2653 */
2654 static int
2655generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2656{
2657 int i;
2658 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002659 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002660
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002661 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002662 for (i = 0; i < ppconst->pp_used; ++i)
2663 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2664 ret = FAIL;
2665 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002666 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002667 return ret;
2668}
2669
2670/*
2671 * Clear ppconst constants. Used when failing.
2672 */
2673 static void
2674clear_ppconst(ppconst_T *ppconst)
2675{
2676 int i;
2677
2678 for (i = 0; i < ppconst->pp_used; ++i)
2679 clear_tv(&ppconst->pp_tv[i]);
2680 ppconst->pp_used = 0;
2681}
2682
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002683/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002684 * Compile getting a member from a list/dict/string/blob. Stack has the
2685 * indexable value and the index.
2686 */
2687 static int
2688compile_member(int is_slice, cctx_T *cctx)
2689{
2690 type_T **typep;
2691 garray_T *stack = &cctx->ctx_type_stack;
2692 vartype_T vtype;
2693 type_T *valtype;
2694
2695 // We can index a list and a dict. If we don't know the type
2696 // we can use the index value type.
2697 // TODO: If we don't know use an instruction to figure it out at
2698 // runtime.
2699 typep = ((type_T **)stack->ga_data) + stack->ga_len
2700 - (is_slice ? 3 : 2);
2701 vtype = (*typep)->tt_type;
2702 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2703 // If the index is a string, the variable must be a Dict.
2704 if (*typep == &t_any && valtype == &t_string)
2705 vtype = VAR_DICT;
2706 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2707 {
2708 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2709 return FAIL;
2710 if (is_slice)
2711 {
2712 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2713 if (need_type(valtype, &t_number, -2, 0, cctx,
2714 FALSE, FALSE) == FAIL)
2715 return FAIL;
2716 }
2717 }
2718
2719 if (vtype == VAR_DICT)
2720 {
2721 if (is_slice)
2722 {
2723 emsg(_(e_cannot_slice_dictionary));
2724 return FAIL;
2725 }
2726 if ((*typep)->tt_type == VAR_DICT)
2727 {
2728 *typep = (*typep)->tt_member;
2729 if (*typep == &t_unknown)
2730 // empty dict was used
2731 *typep = &t_any;
2732 }
2733 else
2734 {
2735 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2736 FALSE, FALSE) == FAIL)
2737 return FAIL;
2738 *typep = &t_any;
2739 }
2740 if (may_generate_2STRING(-1, cctx) == FAIL)
2741 return FAIL;
2742 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2743 return FAIL;
2744 }
2745 else if (vtype == VAR_STRING)
2746 {
2747 *typep = &t_string;
2748 if ((is_slice
2749 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2750 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2751 return FAIL;
2752 }
2753 else if (vtype == VAR_BLOB)
2754 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002755 if (is_slice)
2756 {
2757 *typep = &t_blob;
2758 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2759 return FAIL;
2760 }
2761 else
2762 {
2763 *typep = &t_number;
2764 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2765 return FAIL;
2766 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002767 }
2768 else if (vtype == VAR_LIST || *typep == &t_any)
2769 {
2770 if (is_slice)
2771 {
2772 if (generate_instr_drop(cctx,
2773 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2774 2) == FAIL)
2775 return FAIL;
2776 }
2777 else
2778 {
2779 if ((*typep)->tt_type == VAR_LIST)
2780 {
2781 *typep = (*typep)->tt_member;
2782 if (*typep == &t_unknown)
2783 // empty list was used
2784 *typep = &t_any;
2785 }
2786 if (generate_instr_drop(cctx,
2787 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2788 return FAIL;
2789 }
2790 }
2791 else
2792 {
2793 emsg(_(e_string_list_dict_or_blob_required));
2794 return FAIL;
2795 }
2796 return OK;
2797}
2798
2799/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002800 * Generate an instruction to load script-local variable "name", without the
2801 * leading "s:".
2802 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 */
2804 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002805compile_load_scriptvar(
2806 cctx_T *cctx,
2807 char_u *name, // variable NUL terminated
2808 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002809 char_u **end, // end of variable
2810 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002812 scriptitem_T *si;
2813 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 imported_T *import;
2815
Bram Moolenaare3d46852020-08-29 13:39:17 +02002816 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2817 return FAIL;
2818 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002819 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002820 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002822 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002823 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2824 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 }
2826 if (idx >= 0)
2827 {
2828 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2829
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002830 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 current_sctx.sc_sid, idx, sv->sv_type);
2832 return OK;
2833 }
2834
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002835 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 if (import != NULL)
2837 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002838 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002839 {
2840 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002841 char_u *exp_name;
2842 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002843 ufunc_T *ufunc;
2844 type_T *type;
2845
2846 // Used "import * as Name", need to lookup the member.
2847 if (*p != '.')
2848 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002849 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002850 return FAIL;
2851 }
2852 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002853 if (VIM_ISWHITE(*p))
2854 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002855 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002856 return FAIL;
2857 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002858
Bram Moolenaar1c991142020-07-04 13:15:31 +02002859 // isolate one name
2860 exp_name = p;
2861 while (eval_isnamec(*p))
2862 ++p;
2863 cc = *p;
2864 *p = NUL;
2865
Bram Moolenaaredba7072021-03-13 21:14:18 +01002866 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2867 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002868 *p = cc;
2869 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002870 *end = p;
2871
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002872 if (idx < 0)
2873 {
2874 if (*p == '(' && ufunc != NULL)
2875 {
2876 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2877 return OK;
2878 }
2879 return FAIL;
2880 }
2881
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002882 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2883 import->imp_sid,
2884 idx,
2885 type);
2886 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002887 else if (import->imp_funcname != NULL)
2888 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002889 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002890 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2891 import->imp_sid,
2892 import->imp_var_vals_idx,
2893 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 return OK;
2895 }
2896
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002897 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002898 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 return FAIL;
2900}
2901
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002902 static int
2903generate_funcref(cctx_T *cctx, char_u *name)
2904{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002905 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002906
2907 if (ufunc == NULL)
2908 return FAIL;
2909
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002910 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002911 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2912 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002913 == FAIL)
2914 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002915 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002916}
2917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918/*
2919 * Compile a variable name into a load instruction.
2920 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002921 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 * When "error" is FALSE do not give an error when not found.
2923 */
2924 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002925compile_load(
2926 char_u **arg,
2927 char_u *end_arg,
2928 cctx_T *cctx,
2929 int is_expr,
2930 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931{
2932 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002933 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002934 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002936 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937
2938 if (*(*arg + 1) == ':')
2939 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002940 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002941 {
2942 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943
Bram Moolenaarfa596382021-04-07 21:58:16 +02002944 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002945 switch (**arg)
2946 {
2947 case 'g': isn_type = ISN_LOADGDICT; break;
2948 case 'w': isn_type = ISN_LOADWDICT; break;
2949 case 't': isn_type = ISN_LOADTDICT; break;
2950 case 'b': isn_type = ISN_LOADBDICT; break;
2951 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002952 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002953 goto theend;
2954 }
2955 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2956 goto theend;
2957 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 else
2960 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002961 isntype_T isn_type = ISN_DROP;
2962
Bram Moolenaarfa596382021-04-07 21:58:16 +02002963 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002964 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2965 if (name == NULL)
2966 return FAIL;
2967
2968 switch (**arg)
2969 {
2970 case 'v': res = generate_LOADV(cctx, name, error);
2971 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002972 case 's': if (is_expr && ASCII_ISUPPER(*name)
2973 && find_func(name, FALSE, cctx) != NULL)
2974 res = generate_funcref(cctx, name);
2975 else
2976 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002977 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002978 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002979 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002980 {
2981 if (is_expr && ASCII_ISUPPER(*name)
2982 && find_func(name, FALSE, cctx) != NULL)
2983 res = generate_funcref(cctx, name);
2984 else
2985 isn_type = ISN_LOADG;
2986 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002987 else
2988 {
2989 isn_type = ISN_LOADAUTO;
2990 vim_free(name);
2991 name = vim_strnsave(*arg, end - *arg);
2992 if (name == NULL)
2993 return FAIL;
2994 }
2995 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002996 case 'w': isn_type = ISN_LOADW; break;
2997 case 't': isn_type = ISN_LOADT; break;
2998 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002999 default: // cannot happen, just in case
3000 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003001 goto theend;
3002 }
3003 if (isn_type != ISN_DROP)
3004 {
3005 // Global, Buffer-local, Window-local and Tabpage-local
3006 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02003007 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003008 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
3009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 }
3011 }
3012 else
3013 {
3014 size_t len = end - *arg;
3015 int idx;
3016 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01003017 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018
3019 name = vim_strnsave(*arg, end - *arg);
3020 if (name == NULL)
3021 return FAIL;
3022
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003023 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003025 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02003026 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028 else
3029 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003030 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003031
Bram Moolenaar709664c2020-12-12 14:33:41 +01003032 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003034 type = lvar.lv_type;
3035 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003036 if (lvar.lv_from_outer != 0)
3037 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003038 else
3039 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
3041 else
3042 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003043 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003044 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003045 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003046 || find_imported(name, 0, cctx) != NULL)
3047 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003048
Bram Moolenaar0f769812020-09-12 18:32:34 +02003049 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003050 // uppercase letter it can be a user defined function.
3051 // generate_funcref() will fail if the function can't be found.
3052 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003053 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 }
3056 if (gen_load)
3057 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003058 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003059 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003060 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003061 cctx->ctx_outer_used = TRUE;
3062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 }
3064
3065 *arg = end;
3066
3067theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003068 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003069 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 vim_free(name);
3071 return res;
3072}
3073
3074/*
3075 * Compile the argument expressions.
3076 * "arg" points to just after the "(" and is advanced to after the ")"
3077 */
3078 static int
3079compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3080{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003081 char_u *p = *arg;
3082 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003083 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084
Bram Moolenaare6085c52020-04-12 20:19:16 +02003085 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003087 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3088 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003089 if (*p == ')')
3090 {
3091 *arg = p + 1;
3092 return OK;
3093 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003094 if (must_end)
3095 {
3096 semsg(_(e_missing_comma_before_argument_str), p);
3097 return FAIL;
3098 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003099
Bram Moolenaara5565e42020-05-09 15:44:01 +02003100 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 return FAIL;
3102 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003103
3104 if (*p != ',' && *skipwhite(p) == ',')
3105 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003106 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003107 p = skipwhite(p);
3108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003110 {
3111 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003112 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003113 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003114 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003115 else
3116 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003117 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003118 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003120failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003121 emsg(_(e_missing_close));
3122 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123}
3124
3125/*
3126 * Compile a function call: name(arg1, arg2)
3127 * "arg" points to "name", "arg + varlen" to the "(".
3128 * "argcount_init" is 1 for "value->method()"
3129 * Instructions:
3130 * EVAL arg1
3131 * EVAL arg2
3132 * BCALL / DCALL / UCALL
3133 */
3134 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003135compile_call(
3136 char_u **arg,
3137 size_t varlen,
3138 cctx_T *cctx,
3139 ppconst_T *ppconst,
3140 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141{
3142 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003143 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 int argcount = argcount_init;
3145 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003146 char_u fname_buf[FLEN_FIXED + 1];
3147 char_u *tofree = NULL;
3148 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003149 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003150 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003151 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152
Bram Moolenaara5565e42020-05-09 15:44:01 +02003153 // we can evaluate "has('name')" at compile time
3154 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3155 {
3156 char_u *s = skipwhite(*arg + varlen + 1);
3157 typval_T argvars[2];
3158
3159 argvars[0].v_type = VAR_UNKNOWN;
3160 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003161 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003162 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003163 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003164 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003165 if (*s == ')' && argvars[0].v_type == VAR_STRING
3166 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003167 {
3168 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3169
3170 *arg = s + 1;
3171 argvars[1].v_type = VAR_UNKNOWN;
3172 tv->v_type = VAR_NUMBER;
3173 tv->vval.v_number = 0;
3174 f_has(argvars, tv);
3175 clear_tv(&argvars[0]);
3176 ++ppconst->pp_used;
3177 return OK;
3178 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003179 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003180 }
3181
3182 if (generate_ppconst(cctx, ppconst) == FAIL)
3183 return FAIL;
3184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (varlen >= sizeof(namebuf))
3186 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003187 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 return FAIL;
3189 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003190 vim_strncpy(namebuf, *arg, varlen);
3191 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192
3193 *arg = skipwhite(*arg + varlen + 1);
3194 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003195 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196
Bram Moolenaar03290b82020-12-19 16:30:44 +01003197 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003198 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 {
3200 int idx;
3201
3202 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003203 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003205 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003206 if (STRCMP(name, "flatten") == 0)
3207 {
3208 emsg(_(e_cannot_use_flatten_in_vim9_script));
3209 goto theend;
3210 }
3211
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003212 if (STRCMP(name, "add") == 0 && argcount == 2)
3213 {
3214 garray_T *stack = &cctx->ctx_type_stack;
3215 type_T *type = ((type_T **)stack->ga_data)[
3216 stack->ga_len - 2];
3217
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003218 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003219 if (type->tt_type == VAR_LIST)
3220 {
3221 // inline "add(list, item)" so that the type can be checked
3222 res = generate_LISTAPPEND(cctx);
3223 idx = -1;
3224 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003225 else if (type->tt_type == VAR_BLOB)
3226 {
3227 // inline "add(blob, nr)" so that the type can be checked
3228 res = generate_BLOBAPPEND(cctx);
3229 idx = -1;
3230 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003231 }
3232
3233 if (idx >= 0)
3234 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3235 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003236 else
3237 semsg(_(e_unknownfunc), namebuf);
3238 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 }
3240
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003241 // An argument or local variable can be a function reference, this
3242 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003243 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003244 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003245 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003246 // If we can find the function by name generate the right call.
3247 // Skip global functions here, a local funcref takes precedence.
3248 ufunc = find_func(name, FALSE, cctx);
3249 if (ufunc != NULL && !func_is_global(ufunc))
3250 {
3251 res = generate_CALL(cctx, ufunc, argcount);
3252 goto theend;
3253 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255
3256 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003257 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003258 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003260 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003261 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003262 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003263 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003264 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003265
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003266 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003267 goto theend;
3268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
Bram Moolenaar0f769812020-09-12 18:32:34 +02003270 // If we can find a global function by name generate the right call.
3271 if (ufunc != NULL)
3272 {
3273 res = generate_CALL(cctx, ufunc, argcount);
3274 goto theend;
3275 }
3276
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003277 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003278 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003279 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003280 res = generate_UCALL(cctx, name, argcount);
3281 else
3282 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003283
3284theend:
3285 vim_free(tofree);
3286 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287}
3288
3289// like NAMESPACE_CHAR but with 'a' and 'l'.
3290#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3291
3292/*
3293 * Find the end of a variable or function name. Unlike find_name_end() this
3294 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003295 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 * Return a pointer to just after the name. Equal to "arg" if there is no
3297 * valid name.
3298 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003299 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003300to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301{
3302 char_u *p;
3303
3304 // Quick check for valid starting character.
3305 if (!eval_isnamec1(*arg))
3306 return arg;
3307
3308 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3309 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3310 // and can be used in slice "[n:]".
3311 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003312 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3314 break;
3315 return p;
3316}
3317
3318/*
3319 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003320 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 */
3322 char_u *
3323to_name_const_end(char_u *arg)
3324{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003325 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 typval_T rettv;
3327
3328 if (p == arg && *arg == '[')
3329 {
3330
3331 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003332 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 p = arg;
3334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 return p;
3336}
3337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338/*
3339 * parse a list: [expr, expr]
3340 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003341 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 */
3343 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003344compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345{
3346 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003347 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003349 int is_const;
3350 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003352 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003354 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003355 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003356 semsg(_(e_list_end), *arg);
3357 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003358 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003359 if (*p == ',')
3360 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003361 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003362 return FAIL;
3363 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003364 if (*p == ']')
3365 {
3366 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003367 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003368 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003369 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003370 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003371 if (!is_const)
3372 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 ++count;
3374 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003375 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003377 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3378 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003379 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003380 return FAIL;
3381 }
3382 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003383 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 p = skipwhite(p);
3385 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003386 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003388 ppconst->pp_is_const = is_all_const;
3389 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390}
3391
3392/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003393 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003394 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003395 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 */
3397 static int
3398compile_lambda(char_u **arg, cctx_T *cctx)
3399{
Bram Moolenaare462f522020-12-27 14:43:30 +01003400 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 typval_T rettv;
3402 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003403 evalarg_T evalarg;
3404
3405 CLEAR_FIELD(evalarg);
3406 evalarg.eval_flags = EVAL_EVALUATE;
3407 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408
3409 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003410 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3411 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003412 {
3413 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003414 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003415 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003416
Bram Moolenaar65c44152020-12-24 15:14:01 +01003417 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003419 ++ufunc->uf_refcount;
3420 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
Bram Moolenaar65c44152020-12-24 15:14:01 +01003422 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003423 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003425 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3426 // points into it. Point to the original line to avoid a dangling pointer.
3427 if (evalarg.eval_tofree_cmdline != NULL)
3428 {
3429 size_t off = *arg - evalarg.eval_tofree_cmdline;
3430
3431 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3432 + off;
3433 }
3434
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003435 clear_evalarg(&evalarg, NULL);
3436
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003437 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003438 {
3439 // The return type will now be known.
3440 set_function_type(ufunc);
3441
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003442 // The function reference count will be 1. When the ISN_FUNCREF
3443 // instruction is deleted the reference count is decremented and the
3444 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003445 return generate_FUNCREF(cctx, ufunc);
3446 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003447
3448 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 return FAIL;
3450}
3451
3452/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003453 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003455 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 */
3457 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003458compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459{
3460 garray_T *instr = &cctx->ctx_instr;
3461 int count = 0;
3462 dict_T *d = dict_alloc();
3463 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003464 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003465 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003466 int is_const;
3467 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468
3469 if (d == NULL)
3470 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003471 if (generate_ppconst(cctx, ppconst) == FAIL)
3472 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003473 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003475 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476
Bram Moolenaar23c55272020-06-21 16:58:13 +02003477 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003478 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003479 *arg = NULL;
3480 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003481 }
3482
3483 if (**arg == '}')
3484 break;
3485
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003486 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003488 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003490 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003491 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003492 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003495 if (isn->isn_type == ISN_PUSHNR)
3496 {
3497 char buf[NUMBUFLEN];
3498
3499 // Convert to string at compile time.
3500 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3501 isn->isn_type = ISN_PUSHS;
3502 isn->isn_arg.string = vim_strsave((char_u *)buf);
3503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (isn->isn_type == ISN_PUSHS)
3505 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003506 else if (may_generate_2STRING(-1, cctx) == FAIL)
3507 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003508 *arg = skipwhite(*arg);
3509 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003510 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003511 emsg(_(e_missing_matching_bracket_after_dict_key));
3512 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003513 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003514 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003516 else
3517 {
3518 // {"name": value},
3519 // {'name': value},
3520 // {name: value} use "name" as a literal key
3521 key = get_literal_key(arg);
3522 if (key == NULL)
3523 return FAIL;
3524 if (generate_PUSHS(cctx, key) == FAIL)
3525 return FAIL;
3526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527
3528 // Check for duplicate keys, if using string keys.
3529 if (key != NULL)
3530 {
3531 item = dict_find(d, key, -1);
3532 if (item != NULL)
3533 {
3534 semsg(_(e_duplicate_key), key);
3535 goto failret;
3536 }
3537 item = dictitem_alloc(key);
3538 if (item != NULL)
3539 {
3540 item->di_tv.v_type = VAR_UNKNOWN;
3541 item->di_tv.v_lock = 0;
3542 if (dict_add(d, item) == FAIL)
3543 dictitem_free(item);
3544 }
3545 }
3546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (**arg != ':')
3548 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003549 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003550 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003551 else
3552 semsg(_(e_missing_dict_colon), *arg);
3553 return FAIL;
3554 }
3555 whitep = *arg + 1;
3556 if (!IS_WHITE_OR_NUL(*whitep))
3557 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003558 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 return FAIL;
3560 }
3561
Bram Moolenaar23c55272020-06-21 16:58:13 +02003562 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003563 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003564 *arg = NULL;
3565 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003566 }
3567
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003570 if (!is_const)
3571 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 ++count;
3573
Bram Moolenaar2c330432020-04-13 14:41:35 +02003574 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003575 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003576 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003577 *arg = NULL;
3578 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 if (**arg == '}')
3581 break;
3582 if (**arg != ',')
3583 {
3584 semsg(_(e_missing_dict_comma), *arg);
3585 goto failret;
3586 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003587 if (IS_WHITE_OR_NUL(*whitep))
3588 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003589 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003590 return FAIL;
3591 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003592 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003593 if (!IS_WHITE_OR_NUL(*whitep))
3594 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003595 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003596 return FAIL;
3597 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003598 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 }
3600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 *arg = *arg + 1;
3602
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003603 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003604 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003605 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003606 *arg += STRLEN(*arg);
3607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003609 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 return generate_NEWDICT(cctx, count);
3611
3612failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003613 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003614 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003615 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003616 *arg = (char_u *)"";
3617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 dict_unref(d);
3619 return FAIL;
3620}
3621
3622/*
3623 * Compile "&option".
3624 */
3625 static int
3626compile_get_option(char_u **arg, cctx_T *cctx)
3627{
3628 typval_T rettv;
3629 char_u *start = *arg;
3630 int ret;
3631
3632 // parse the option and get the current value to get the type.
3633 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003634 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 if (ret == OK)
3636 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003637 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003638 char_u *name = vim_strnsave(start, *arg - start);
3639 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3640 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641
3642 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3643 vim_free(name);
3644 }
3645 clear_tv(&rettv);
3646
3647 return ret;
3648}
3649
3650/*
3651 * Compile "$VAR".
3652 */
3653 static int
3654compile_get_env(char_u **arg, cctx_T *cctx)
3655{
3656 char_u *start = *arg;
3657 int len;
3658 int ret;
3659 char_u *name;
3660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 ++*arg;
3662 len = get_env_len(arg);
3663 if (len == 0)
3664 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003665 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 return FAIL;
3667 }
3668
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003669 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 name = vim_strnsave(start, len + 1);
3671 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3672 vim_free(name);
3673 return ret;
3674}
3675
3676/*
3677 * Compile "@r".
3678 */
3679 static int
3680compile_get_register(char_u **arg, cctx_T *cctx)
3681{
3682 int ret;
3683
3684 ++*arg;
3685 if (**arg == NUL)
3686 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003687 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 return FAIL;
3689 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003690 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 {
3692 emsg_invreg(**arg);
3693 return FAIL;
3694 }
3695 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3696 ++*arg;
3697 return ret;
3698}
3699
3700/*
3701 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003702 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 */
3704 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003705apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003707 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708
3709 // this works from end to start
3710 while (p > start)
3711 {
3712 --p;
3713 if (*p == '-' || *p == '+')
3714 {
3715 // only '-' has an effect, for '+' we only check the type
3716#ifdef FEAT_FLOAT
3717 if (rettv->v_type == VAR_FLOAT)
3718 {
3719 if (*p == '-')
3720 rettv->vval.v_float = -rettv->vval.v_float;
3721 }
3722 else
3723#endif
3724 {
3725 varnumber_T val;
3726 int error = FALSE;
3727
3728 // tv_get_number_chk() accepts a string, but we don't want that
3729 // here
3730 if (check_not_string(rettv) == FAIL)
3731 return FAIL;
3732 val = tv_get_number_chk(rettv, &error);
3733 clear_tv(rettv);
3734 if (error)
3735 return FAIL;
3736 if (*p == '-')
3737 val = -val;
3738 rettv->v_type = VAR_NUMBER;
3739 rettv->vval.v_number = val;
3740 }
3741 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003742 else if (numeric_only)
3743 {
3744 ++p;
3745 break;
3746 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003747 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
3749 int v = tv2bool(rettv);
3750
3751 // '!' is permissive in the type.
3752 clear_tv(rettv);
3753 rettv->v_type = VAR_BOOL;
3754 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3755 }
3756 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003757 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 return OK;
3759}
3760
3761/*
3762 * Recognize v: variables that are constants and set "rettv".
3763 */
3764 static void
3765get_vim_constant(char_u **arg, typval_T *rettv)
3766{
3767 if (STRNCMP(*arg, "v:true", 6) == 0)
3768 {
3769 rettv->v_type = VAR_BOOL;
3770 rettv->vval.v_number = VVAL_TRUE;
3771 *arg += 6;
3772 }
3773 else if (STRNCMP(*arg, "v:false", 7) == 0)
3774 {
3775 rettv->v_type = VAR_BOOL;
3776 rettv->vval.v_number = VVAL_FALSE;
3777 *arg += 7;
3778 }
3779 else if (STRNCMP(*arg, "v:null", 6) == 0)
3780 {
3781 rettv->v_type = VAR_SPECIAL;
3782 rettv->vval.v_number = VVAL_NULL;
3783 *arg += 6;
3784 }
3785 else if (STRNCMP(*arg, "v:none", 6) == 0)
3786 {
3787 rettv->v_type = VAR_SPECIAL;
3788 rettv->vval.v_number = VVAL_NONE;
3789 *arg += 6;
3790 }
3791}
3792
Bram Moolenaar657137c2021-01-09 15:45:23 +01003793 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003794get_compare_type(char_u *p, int *len, int *type_is)
3795{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003796 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003797 int i;
3798
3799 switch (p[0])
3800 {
3801 case '=': if (p[1] == '=')
3802 type = EXPR_EQUAL;
3803 else if (p[1] == '~')
3804 type = EXPR_MATCH;
3805 break;
3806 case '!': if (p[1] == '=')
3807 type = EXPR_NEQUAL;
3808 else if (p[1] == '~')
3809 type = EXPR_NOMATCH;
3810 break;
3811 case '>': if (p[1] != '=')
3812 {
3813 type = EXPR_GREATER;
3814 *len = 1;
3815 }
3816 else
3817 type = EXPR_GEQUAL;
3818 break;
3819 case '<': if (p[1] != '=')
3820 {
3821 type = EXPR_SMALLER;
3822 *len = 1;
3823 }
3824 else
3825 type = EXPR_SEQUAL;
3826 break;
3827 case 'i': if (p[1] == 's')
3828 {
3829 // "is" and "isnot"; but not a prefix of a name
3830 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3831 *len = 5;
3832 i = p[*len];
3833 if (!isalnum(i) && i != '_')
3834 {
3835 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3836 *type_is = TRUE;
3837 }
3838 }
3839 break;
3840 }
3841 return type;
3842}
3843
3844/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003845 * Skip over an expression, ignoring most errors.
3846 */
3847 static void
3848skip_expr_cctx(char_u **arg, cctx_T *cctx)
3849{
3850 evalarg_T evalarg;
3851
3852 CLEAR_FIELD(evalarg);
3853 evalarg.eval_cctx = cctx;
3854 skip_expr(arg, &evalarg);
3855}
3856
3857/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003859 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 */
3861 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003862compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003864 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
3866 // this works from end to start
3867 while (p > start)
3868 {
3869 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003870 while (VIM_ISWHITE(*p))
3871 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 if (*p == '-' || *p == '+')
3873 {
3874 int negate = *p == '-';
3875 isn_T *isn;
3876
3877 // TODO: check type
3878 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3879 {
3880 --p;
3881 if (*p == '-')
3882 negate = !negate;
3883 }
3884 // only '-' has an effect, for '+' we only check the type
3885 if (negate)
3886 isn = generate_instr(cctx, ISN_NEGATENR);
3887 else
3888 isn = generate_instr(cctx, ISN_CHECKNR);
3889 if (isn == NULL)
3890 return FAIL;
3891 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003892 else if (numeric_only)
3893 {
3894 ++p;
3895 break;
3896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 else
3898 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003899 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003901 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003903 if (p[-1] == '!')
3904 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 }
3907 if (generate_2BOOL(cctx, invert) == FAIL)
3908 return FAIL;
3909 }
3910 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 return OK;
3913}
3914
3915/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003916 * Compile "(expression)": recursive!
3917 * Return FAIL/OK.
3918 */
3919 static int
3920compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3921{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003922 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003923 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003924
Bram Moolenaar24156692021-01-14 20:35:49 +01003925 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3926 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003927 if (ppconst->pp_used <= PPSIZE - 10)
3928 {
3929 ret = compile_expr1(arg, cctx, ppconst);
3930 }
3931 else
3932 {
3933 // Not enough space in ppconst, flush constants.
3934 if (generate_ppconst(cctx, ppconst) == FAIL)
3935 return FAIL;
3936 ret = compile_expr0(arg, cctx);
3937 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003938 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3939 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003940 if (**arg == ')')
3941 ++*arg;
3942 else if (ret == OK)
3943 {
3944 emsg(_(e_missing_close));
3945 ret = FAIL;
3946 }
3947 return ret;
3948}
3949
3950/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003952 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 */
3954 static int
3955compile_subscript(
3956 char_u **arg,
3957 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 char_u *start_leader,
3959 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003960 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003962 char_u *name_start = *end_leader;
3963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 for (;;)
3965 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003966 char_u *p = skipwhite(*arg);
3967
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003968 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003969 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003970 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003971
3972 // If a following line starts with "->{" or "->X" advance to that
3973 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003974 // Also if a following line starts with ".x".
3975 if (next != NULL &&
3976 ((next[0] == '-' && next[1] == '>'
3977 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003978 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003979 {
3980 next = next_line_from_context(cctx, TRUE);
3981 if (next == NULL)
3982 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003983 *arg = next;
3984 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003985 }
3986 }
3987
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003988 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003989 // not a function call.
3990 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003992 garray_T *stack = &cctx->ctx_type_stack;
3993 type_T *type;
3994 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003996 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003997 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003998 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02004001 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4002
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004003 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4005 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004006 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 return FAIL;
4008 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004009 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004011 char_u *pstart = p;
4012
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004013 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004014 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004015 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 // something->method()
4018 // Apply the '!', '-' and '+' first:
4019 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004020 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004023 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004024 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004025 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004026 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004027 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004028 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004029 garray_T *stack = &cctx->ctx_type_stack;
4030 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004031 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004032 int expr_isn_start = cctx->ctx_instr.ga_len;
4033 int expr_isn_end;
4034 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004035
4036 // Funcref call: list->(Refs[2])(arg)
4037 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004038 //
4039 // Fist compile the function expression.
4040 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004041 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004042
4043 // Remember the next instruction index, where the instructions
4044 // for arguments are being written.
4045 expr_isn_end = cctx->ctx_instr.ga_len;
4046
4047 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004048 if (**arg != '(')
4049 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004050 if (*skipwhite(*arg) == '(')
4051 emsg(_(e_nowhitespace));
4052 else
4053 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004054 return FAIL;
4055 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004056 *arg = skipwhite(*arg + 1);
4057 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4058 return FAIL;
4059
Bram Moolenaar2927c072021-04-05 19:41:21 +02004060 // Move the instructions for the arguments to before the
4061 // instructions of the expression and move the type of the
4062 // expression after the argument types. This is what ISN_PCALL
4063 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004064 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004065 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4066 if (arg_isn_count > 0)
4067 {
4068 int expr_isn_count = expr_isn_end - expr_isn_start;
4069 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4070
4071 if (isn == NULL)
4072 return FAIL;
4073 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4074 + expr_isn_start,
4075 sizeof(isn_T) * expr_isn_count);
4076 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4077 + expr_isn_start,
4078 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4079 sizeof(isn_T) * arg_isn_count);
4080 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4081 + expr_isn_start + arg_isn_count,
4082 isn, sizeof(isn_T) * expr_isn_count);
4083 vim_free(isn);
4084
4085 type = ((type_T **)stack->ga_data)[type_idx_start];
4086 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4087 ((type_T **)stack->ga_data) + type_idx_start + 1,
4088 sizeof(type_T *)
4089 * (stack->ga_len - type_idx_start - 1));
4090 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4091 }
4092
Bram Moolenaar7e368202020-12-25 21:56:57 +01004093 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4094 if (generate_PCALL(cctx, argcount,
4095 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 return FAIL;
4097 }
4098 else
4099 {
4100 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004101 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004102 if (!eval_isnamec1(*p))
4103 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004104 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004105 return FAIL;
4106 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004107 if (ASCII_ISALPHA(*p) && p[1] == ':')
4108 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004109 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 ;
4111 if (*p != '(')
4112 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004113 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 return FAIL;
4115 }
4116 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004117 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 return FAIL;
4119 }
4120 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004121 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004123 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004124
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004125 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004126 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004127 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004128 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004129 // TODO: more arguments
4130 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004131 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004132 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004133 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004134
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004135 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004136 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004137 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004138 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004139 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004140 // missing first index is equal to zero
4141 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004142 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004143 else
4144 {
4145 if (compile_expr0(arg, cctx) == FAIL)
4146 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004147 if (**arg == ':')
4148 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004149 semsg(_(e_white_space_required_before_and_after_str_at_str),
4150 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004151 return FAIL;
4152 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004153 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004154 return FAIL;
4155 *arg = skipwhite(*arg);
4156 }
4157 if (**arg == ':')
4158 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004159 is_slice = TRUE;
4160 ++*arg;
4161 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4162 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004163 semsg(_(e_white_space_required_before_and_after_str_at_str),
4164 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004165 return FAIL;
4166 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004167 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004168 return FAIL;
4169 if (**arg == ']')
4170 // missing second index is equal to end of string
4171 generate_PUSHNR(cctx, -1);
4172 else
4173 {
4174 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004175 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004176 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004177 return FAIL;
4178 *arg = skipwhite(*arg);
4179 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181
4182 if (**arg != ']')
4183 {
4184 emsg(_(e_missbrac));
4185 return FAIL;
4186 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004187 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
Bram Moolenaare42939a2021-04-05 17:11:17 +02004189 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004190 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004192 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004194 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004195 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004197 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004198
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004199 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004200 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004201 {
4202 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004203 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004204 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004205 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004206 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 while (eval_isnamec(*p))
4208 MB_PTR_ADV(p);
4209 if (p == *arg)
4210 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004211 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 return FAIL;
4213 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004214 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 return FAIL;
4216 *arg = p;
4217 }
4218 else
4219 break;
4220 }
4221
4222 // TODO - see handle_subscript():
4223 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4224 // Don't do this when "Func" is already a partial that was bound
4225 // explicitly (pt_auto is FALSE).
4226
4227 return OK;
4228}
4229
4230/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004231 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4232 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004234 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004235 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004236 *
4237 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 */
4239
4240/*
4241 * number number constant
4242 * 0zFFFFFFFF Blob constant
4243 * "string" string constant
4244 * 'string' literal string constant
4245 * &option-name option value
4246 * @r register contents
4247 * identifier variable value
4248 * function() function call
4249 * $VAR environment variable
4250 * (expression) nested expression
4251 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004252 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 *
4254 * Also handle:
4255 * ! in front logical NOT
4256 * - in front unary minus
4257 * + in front unary plus (ignored)
4258 * trailing (arg) funcref/partial call
4259 * trailing [] subscript in String or List
4260 * trailing .name entry in Dictionary
4261 * trailing ->name() method call
4262 */
4263 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004264compile_expr7(
4265 char_u **arg,
4266 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004267 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 char_u *start_leader, *end_leader;
4270 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004271 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004272 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004274 ppconst->pp_is_const = FALSE;
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 /*
4277 * Skip '!', '-' and '+' characters. They are handled later.
4278 */
4279 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004280 if (eval_leader(arg, TRUE) == FAIL)
4281 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 end_leader = *arg;
4283
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004284 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 switch (**arg)
4286 {
4287 /*
4288 * Number constant.
4289 */
4290 case '0': // also for blob starting with 0z
4291 case '1':
4292 case '2':
4293 case '3':
4294 case '4':
4295 case '5':
4296 case '6':
4297 case '7':
4298 case '8':
4299 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004300 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004302 // Apply "-" and "+" just before the number now, right to
4303 // left. Matters especially when "->" follows. Stops at
4304 // '!'.
4305 if (apply_leader(rettv, TRUE,
4306 start_leader, &end_leader) == FAIL)
4307 {
4308 clear_tv(rettv);
4309 return FAIL;
4310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 break;
4312
4313 /*
4314 * String constant: "string".
4315 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004316 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 return FAIL;
4318 break;
4319
4320 /*
4321 * Literal string constant: 'str''ing'.
4322 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004323 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325 break;
4326
4327 /*
4328 * Constant Vim variable.
4329 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004330 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 ret = NOTDONE;
4332 break;
4333
4334 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004335 * "true" constant
4336 */
4337 case 't': if (STRNCMP(*arg, "true", 4) == 0
4338 && !eval_isnamec((*arg)[4]))
4339 {
4340 *arg += 4;
4341 rettv->v_type = VAR_BOOL;
4342 rettv->vval.v_number = VVAL_TRUE;
4343 }
4344 else
4345 ret = NOTDONE;
4346 break;
4347
4348 /*
4349 * "false" constant
4350 */
4351 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4352 && !eval_isnamec((*arg)[5]))
4353 {
4354 *arg += 5;
4355 rettv->v_type = VAR_BOOL;
4356 rettv->vval.v_number = VVAL_FALSE;
4357 }
4358 else
4359 ret = NOTDONE;
4360 break;
4361
4362 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004363 * "null" constant
4364 */
4365 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004366 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004367 {
4368 *arg += 4;
4369 rettv->v_type = VAR_SPECIAL;
4370 rettv->vval.v_number = VVAL_NULL;
4371 }
4372 else
4373 ret = NOTDONE;
4374 break;
4375
4376 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 * List: [expr, expr]
4378 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004379 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4380 return FAIL;
4381 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 break;
4383
4384 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 * Dictionary: {'key': val, 'key': val}
4386 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004387 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4388 return FAIL;
4389 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 break;
4391
4392 /*
4393 * Option value: &name
4394 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004395 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4396 return FAIL;
4397 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 break;
4399
4400 /*
4401 * Environment variable: $VAR.
4402 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004403 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4404 return FAIL;
4405 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 break;
4407
4408 /*
4409 * Register contents: @r.
4410 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004411 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4412 return FAIL;
4413 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 break;
4415 /*
4416 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004417 * lambda: (arg, arg) => expr
4418 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004420 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4421 ret = compile_lambda(arg, cctx);
4422 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004423 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 break;
4425
4426 default: ret = NOTDONE;
4427 break;
4428 }
4429 if (ret == FAIL)
4430 return FAIL;
4431
Bram Moolenaar1c747212020-05-09 18:28:34 +02004432 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004434 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004435 clear_tv(rettv);
4436 else
4437 // A constant expression can possibly be handled compile time,
4438 // return the value instead of generating code.
4439 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 }
4441 else if (ret == NOTDONE)
4442 {
4443 char_u *p;
4444 int r;
4445
4446 if (!eval_isnamec1(**arg))
4447 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004448 if (!vim9_bad_comment(*arg))
4449 {
4450 if (ends_excmd(*skipwhite(*arg)))
4451 semsg(_(e_empty_expression_str), *arg);
4452 else
4453 semsg(_(e_name_expected_str), *arg);
4454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 return FAIL;
4456 }
4457
4458 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004459 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004460 if (p - *arg == (size_t)1 && **arg == '_')
4461 {
4462 emsg(_(e_cannot_use_underscore_here));
4463 return FAIL;
4464 }
4465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 {
4468 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4469 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 {
4472 if (generate_ppconst(cctx, ppconst) == FAIL)
4473 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004474 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 if (r == FAIL)
4477 return FAIL;
4478 }
4479
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004480 // Handle following "[]", ".member", etc.
4481 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004482 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483 ppconst) == FAIL)
4484 return FAIL;
4485 if (ppconst->pp_used > 0)
4486 {
4487 // apply the '!', '-' and '+' before the constant
4488 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004489 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490 return FAIL;
4491 return OK;
4492 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004493 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004495 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496}
4497
4498/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004499 * Give the "white on both sides" error, taking the operator from "p[len]".
4500 */
4501 void
4502error_white_both(char_u *op, int len)
4503{
4504 char_u buf[10];
4505
4506 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004507 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004508}
4509
4510/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004511 * <type>expr7: runtime type check / conversion
4512 */
4513 static int
4514compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4515{
4516 type_T *want_type = NULL;
4517
4518 // Recognize <type>
4519 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4520 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004521 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004522 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4523 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004524 return FAIL;
4525
4526 if (**arg != '>')
4527 {
4528 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004529 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004530 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004531 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004532 return FAIL;
4533 }
4534 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004535 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004536 return FAIL;
4537 }
4538
4539 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4540 return FAIL;
4541
4542 if (want_type != NULL)
4543 {
4544 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004545 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004546 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004547
Bram Moolenaard1103582020-08-14 22:44:25 +02004548 generate_ppconst(cctx, ppconst);
4549 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004550 where.wt_index = 0;
4551 where.wt_variable = FALSE;
4552 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004553 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004554 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004555 return FAIL;
4556 }
4557 }
4558
4559 return OK;
4560}
4561
4562/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 * * number multiplication
4564 * / number division
4565 * % number modulo
4566 */
4567 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004568compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569{
4570 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004571 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004572 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004574 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004575 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 return FAIL;
4577
4578 /*
4579 * Repeat computing, until no "*", "/" or "%" is following.
4580 */
4581 for (;;)
4582 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004583 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 if (*op != '*' && *op != '/' && *op != '%')
4585 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004586 if (next != NULL)
4587 {
4588 *arg = next_line_from_context(cctx, TRUE);
4589 op = skipwhite(*arg);
4590 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004591
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004592 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004594 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004597 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004598 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004600 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004601 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004603
4604 if (ppconst->pp_used == ppconst_used + 2
4605 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4606 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004607 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004608 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4609 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4610 varnumber_T res = 0;
4611 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004613 // both are numbers: compute the result
4614 switch (*op)
4615 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004616 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004617 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004618 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004619 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004620 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004621 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004622 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004623 break;
4624 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004625 if (failed)
4626 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004627 tv1->vval.v_number = res;
4628 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004629 }
4630 else
4631 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004632 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004633 generate_two_op(cctx, op);
4634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 }
4636
4637 return OK;
4638}
4639
4640/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004641 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 * - number subtraction
4643 * .. string concatenation
4644 */
4645 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647{
4648 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004649 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652
4653 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 return FAIL;
4656
4657 /*
4658 * Repeat computing, until no "+", "-" or ".." is following.
4659 */
4660 for (;;)
4661 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004662 op = may_peek_next_line(cctx, *arg, &next);
4663 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 break;
4665 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004666 if (next != NULL)
4667 {
4668 *arg = next_line_from_context(cctx, TRUE);
4669 op = skipwhite(*arg);
4670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004672 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004674 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004675 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 }
4677
Bram Moolenaare0de1712020-12-02 17:36:54 +01004678 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004679 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004681 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004682 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 return FAIL;
4684
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004686 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4688 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4689 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4690 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004692 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4693 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004694
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004695 // concat/subtract/add constant numbers
4696 if (*op == '+')
4697 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4698 else if (*op == '-')
4699 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4700 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004701 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004702 // concatenate constant strings
4703 char_u *s1 = tv1->vval.v_string;
4704 char_u *s2 = tv2->vval.v_string;
4705 size_t len1 = STRLEN(s1);
4706
4707 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4708 if (tv1->vval.v_string == NULL)
4709 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004711 return FAIL;
4712 }
4713 mch_memmove(tv1->vval.v_string, s1, len1);
4714 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004715 vim_free(s1);
4716 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004717 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 }
4720 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004721 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004723 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004724 if (*op == '.')
4725 {
4726 if (may_generate_2STRING(-2, cctx) == FAIL
4727 || may_generate_2STRING(-1, cctx) == FAIL)
4728 return FAIL;
4729 generate_instr_drop(cctx, ISN_CONCAT, 1);
4730 }
4731 else
4732 generate_two_op(cctx, op);
4733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 }
4735
4736 return OK;
4737}
4738
4739/*
4740 * expr5a == expr5b
4741 * expr5a =~ expr5b
4742 * expr5a != expr5b
4743 * expr5a !~ expr5b
4744 * expr5a > expr5b
4745 * expr5a >= expr5b
4746 * expr5a < expr5b
4747 * expr5a <= expr5b
4748 * expr5a is expr5b
4749 * expr5a isnot expr5b
4750 *
4751 * Produces instructions:
4752 * EVAL expr5a Push result of "expr5a"
4753 * EVAL expr5b Push result of "expr5b"
4754 * COMPARE one of the compare instructions
4755 */
4756 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004757compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004759 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004761 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004764 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765
4766 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 return FAIL;
4769
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004770 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004771 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004772
4773 /*
4774 * If there is a comparative operator, use it.
4775 */
4776 if (type != EXPR_UNKNOWN)
4777 {
4778 int ic = FALSE; // Default: do not ignore case
4779
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004780 if (next != NULL)
4781 {
4782 *arg = next_line_from_context(cctx, TRUE);
4783 p = skipwhite(*arg);
4784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 if (type_is && (p[len] == '?' || p[len] == '#'))
4786 {
4787 semsg(_(e_invexpr2), *arg);
4788 return FAIL;
4789 }
4790 // extra question mark appended: ignore case
4791 if (p[len] == '?')
4792 {
4793 ic = TRUE;
4794 ++len;
4795 }
4796 // extra '#' appended: match case (ignored)
4797 else if (p[len] == '#')
4798 ++len;
4799 // nothing appended: match case
4800
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004801 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004803 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004804 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 }
4806
4807 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004808 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004809 return FAIL;
4810
Bram Moolenaara5565e42020-05-09 15:44:01 +02004811 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 return FAIL;
4813
Bram Moolenaara5565e42020-05-09 15:44:01 +02004814 if (ppconst->pp_used == ppconst_used + 2)
4815 {
4816 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4817 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4818 int ret;
4819
4820 // Both sides are a constant, compute the result now.
4821 // First check for a valid combination of types, this is more
4822 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004823 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004824 ret = FAIL;
4825 else
4826 {
4827 ret = typval_compare(tv1, tv2, type, ic);
4828 tv1->v_type = VAR_BOOL;
4829 tv1->vval.v_number = tv1->vval.v_number
4830 ? VVAL_TRUE : VVAL_FALSE;
4831 clear_tv(tv2);
4832 --ppconst->pp_used;
4833 }
4834 return ret;
4835 }
4836
4837 generate_ppconst(cctx, ppconst);
4838 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839 }
4840
4841 return OK;
4842}
4843
Bram Moolenaar7f141552020-05-09 17:35:53 +02004844static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846/*
4847 * Compile || or &&.
4848 */
4849 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850compile_and_or(
4851 char_u **arg,
4852 cctx_T *cctx,
4853 char *op,
4854 ppconst_T *ppconst,
4855 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004857 char_u *next;
4858 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 int opchar = *op;
4860
4861 if (p[0] == opchar && p[1] == opchar)
4862 {
4863 garray_T *instr = &cctx->ctx_instr;
4864 garray_T end_ga;
4865
4866 /*
4867 * Repeat until there is no following "||" or "&&"
4868 */
4869 ga_init2(&end_ga, sizeof(int), 10);
4870 while (p[0] == opchar && p[1] == opchar)
4871 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004872 long start_lnum = SOURCING_LNUM;
4873 int start_ctx_lnum = cctx->ctx_lnum;
4874 int save_lnum;
4875
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004876 if (next != NULL)
4877 {
4878 *arg = next_line_from_context(cctx, TRUE);
4879 p = skipwhite(*arg);
4880 }
4881
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004882 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4883 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004884 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004885 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004886 return FAIL;
4887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004889 // TODO: use ppconst if the value is a constant and check
4890 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004891 generate_ppconst(cctx, ppconst);
4892
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004893 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004894 SOURCING_LNUM = start_lnum;
4895 save_lnum = cctx->ctx_lnum;
4896 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004897 if (bool_on_stack(cctx) == FAIL)
4898 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004899 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004900 ga_clear(&end_ga);
4901 return FAIL;
4902 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004903 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 if (ga_grow(&end_ga, 1) == FAIL)
4906 {
4907 ga_clear(&end_ga);
4908 return FAIL;
4909 }
4910 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4911 ++end_ga.ga_len;
4912 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004913 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914
4915 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004916 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004917 {
4918 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004919 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004920 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004921
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4923 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 {
4925 ga_clear(&end_ga);
4926 return FAIL;
4927 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004928
4929 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004933 // Every part must evaluate to a bool.
4934 if (bool_on_stack(cctx) == FAIL)
4935 {
4936 ga_clear(&end_ga);
4937 return FAIL;
4938 }
4939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 // Fill in the end label in all jumps.
4941 while (end_ga.ga_len > 0)
4942 {
4943 isn_T *isn;
4944
4945 --end_ga.ga_len;
4946 isn = ((isn_T *)instr->ga_data)
4947 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4948 isn->isn_arg.jump.jump_where = instr->ga_len;
4949 }
4950 ga_clear(&end_ga);
4951 }
4952
4953 return OK;
4954}
4955
4956/*
4957 * expr4a && expr4a && expr4a logical AND
4958 *
4959 * Produces instructions:
4960 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004961 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004962 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004964 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 * EVAL expr4c Push result of "expr4c"
4966 * end:
4967 */
4968 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971 int ppconst_used = ppconst->pp_used;
4972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 return FAIL;
4976
4977 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004978 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979}
4980
4981/*
4982 * expr3a || expr3b || expr3c logical OR
4983 *
4984 * Produces instructions:
4985 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004986 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004987 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004989 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 * EVAL expr3c Push result of "expr3c"
4991 * end:
4992 */
4993 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004994compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004996 int ppconst_used = ppconst->pp_used;
4997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004999 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 return FAIL;
5001
5002 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02005003 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004}
5005
5006/*
5007 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005009 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 * JUMP_IF_FALSE alt jump if false
5011 * EVAL expr1a
5012 * JUMP_ALWAYS end
5013 * alt: EVAL expr1b
5014 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005015 *
5016 * Toplevel expression: expr2 ?? expr1
5017 * Produces instructions:
5018 * EVAL expr2 Push result of "expr2"
5019 * JUMP_AND_KEEP_IF_TRUE end jump if true
5020 * EVAL expr1
5021 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 */
5023 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01005024compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025{
5026 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005028 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029
Bram Moolenaar3988f642020-08-27 22:43:03 +02005030 // Ignore all kinds of errors when not producing code.
5031 if (cctx->ctx_skip == SKIP_YES)
5032 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005033 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005034 return OK;
5035 }
5036
Bram Moolenaar61a89812020-05-07 16:58:17 +02005037 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 return FAIL;
5040
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005041 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 if (*p == '?')
5043 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005044 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 garray_T *instr = &cctx->ctx_instr;
5046 garray_T *stack = &cctx->ctx_type_stack;
5047 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005048 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005050 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005051 int has_const_expr = FALSE;
5052 int const_value = FALSE;
5053 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005055 if (next != NULL)
5056 {
5057 *arg = next_line_from_context(cctx, TRUE);
5058 p = skipwhite(*arg);
5059 }
5060
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005061 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005062 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005063 semsg(_(e_white_space_required_before_and_after_str_at_str),
5064 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005065 return FAIL;
5066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 if (ppconst->pp_used == ppconst_used + 1)
5069 {
5070 // the condition is a constant, we know whether the ? or the :
5071 // expression is to be evaluated.
5072 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005073 if (op_falsy)
5074 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5075 else
5076 {
5077 int error = FALSE;
5078
5079 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5080 &error);
5081 if (error)
5082 return FAIL;
5083 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005084 cctx->ctx_skip = save_skip == SKIP_YES ||
5085 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5086
5087 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5088 // "left ?? right" and "left" is truthy: produce "left"
5089 generate_ppconst(cctx, ppconst);
5090 else
5091 {
5092 clear_tv(&ppconst->pp_tv[ppconst_used]);
5093 --ppconst->pp_used;
5094 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005095 }
5096 else
5097 {
5098 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005099 if (op_falsy)
5100 end_idx = instr->ga_len;
5101 generate_JUMP(cctx, op_falsy
5102 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5103 if (op_falsy)
5104 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
5107 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005108 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005109 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005110 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005111 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaara5565e42020-05-09 15:44:01 +02005113 if (!has_const_expr)
5114 {
5115 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005117 if (!op_falsy)
5118 {
5119 // remember the type and drop it
5120 --stack->ga_len;
5121 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005123 end_idx = instr->ga_len;
5124 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005125
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005126 // jump here from JUMP_IF_FALSE
5127 isn = ((isn_T *)instr->ga_data) + alt_idx;
5128 isn->isn_arg.jump.jump_where = instr->ga_len;
5129 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005132 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005134 // Check for the ":".
5135 p = may_peek_next_line(cctx, *arg, &next);
5136 if (*p != ':')
5137 {
5138 emsg(_(e_missing_colon));
5139 return FAIL;
5140 }
5141 if (next != NULL)
5142 {
5143 *arg = next_line_from_context(cctx, TRUE);
5144 p = skipwhite(*arg);
5145 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005146
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005147 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5148 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005149 semsg(_(e_white_space_required_before_and_after_str_at_str),
5150 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005151 return FAIL;
5152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005154 // evaluate the third expression
5155 if (has_const_expr)
5156 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005157 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005158 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005159 return FAIL;
5160 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5161 return FAIL;
5162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163
Bram Moolenaara5565e42020-05-09 15:44:01 +02005164 if (!has_const_expr)
5165 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005166 type_T **typep;
5167
Bram Moolenaara5565e42020-05-09 15:44:01 +02005168 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169
Bram Moolenaara5565e42020-05-09 15:44:01 +02005170 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005171 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5172 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005173
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005174 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175 isn = ((isn_T *)instr->ga_data) + end_idx;
5176 isn->isn_arg.jump.jump_where = instr->ga_len;
5177 }
5178
5179 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 }
5181 return OK;
5182}
5183
5184/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005185 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005186 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5187 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005188 */
5189 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005190compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005191{
5192 ppconst_T ppconst;
5193
5194 CLEAR_FIELD(ppconst);
5195 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5196 {
5197 clear_ppconst(&ppconst);
5198 return FAIL;
5199 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005200 if (is_const != NULL)
5201 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 if (generate_ppconst(cctx, &ppconst) == FAIL)
5203 return FAIL;
5204 return OK;
5205}
5206
5207/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005208 * Toplevel expression.
5209 */
5210 static int
5211compile_expr0(char_u **arg, cctx_T *cctx)
5212{
5213 return compile_expr0_ext(arg, cctx, NULL);
5214}
5215
5216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 * compile "return [expr]"
5218 */
5219 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005220compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221{
5222 char_u *p = arg;
5223 garray_T *stack = &cctx->ctx_type_stack;
5224 type_T *stack_type;
5225
5226 if (*p != NUL && *p != '|' && *p != '\n')
5227 {
5228 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005229 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 return NULL;
5231
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005232 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005233 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005234 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005235 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005236 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5237 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005238 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005239 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005240 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005241 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005242 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005243 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5244 && stack_type->tt_type != VAR_VOID
5245 && stack_type->tt_type != VAR_UNKNOWN)
5246 {
5247 emsg(_(e_returning_value_in_function_without_return_type));
5248 return NULL;
5249 }
5250 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005251 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005252 return NULL;
5253 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255 }
5256 else
5257 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005258 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005259 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005260 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5261 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005263 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 return NULL;
5265 }
5266
5267 // No argument, return zero.
5268 generate_PUSHNR(cctx, 0);
5269 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005270
5271 // Undo any command modifiers.
5272 generate_undo_cmdmods(cctx);
5273
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005274 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 return NULL;
5276
5277 // "return val | endif" is possible
5278 return skipwhite(p);
5279}
5280
5281/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005282 * Get a line from the compilation context, compatible with exarg_T getline().
5283 * Return a pointer to the line in allocated memory.
5284 * Return NULL for end-of-file or some error.
5285 */
5286 static char_u *
5287exarg_getline(
5288 int c UNUSED,
5289 void *cookie,
5290 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005291 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005292{
5293 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005294 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005295
Bram Moolenaar66250c92020-08-20 15:02:42 +02005296 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005297 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005298 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005299 return NULL;
5300 ++cctx->ctx_lnum;
5301 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5302 // Comment lines result in NULL pointers, skip them.
5303 if (p != NULL)
5304 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005305 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005306}
5307
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005308 void
5309fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5310{
5311 eap->getline = exarg_getline;
5312 eap->cookie = cctx;
5313}
5314
Bram Moolenaar04b12692020-05-04 23:24:44 +02005315/*
5316 * Compile a nested :def command.
5317 */
5318 static char_u *
5319compile_nested_function(exarg_T *eap, cctx_T *cctx)
5320{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005321 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005322 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005323 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005324 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005325 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005326 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005327
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005328 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005329 {
5330 emsg(_(e_cannot_use_bang_with_nested_def));
5331 return NULL;
5332 }
5333
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005334 if (*name_start == '/')
5335 {
5336 name_end = skip_regexp(name_start + 1, '/', TRUE);
5337 if (*name_end == '/')
5338 ++name_end;
5339 eap->nextcmd = check_nextcmd(name_end);
5340 }
5341 if (name_end == name_start || *skipwhite(name_end) != '(')
5342 {
5343 if (!ends_excmd2(name_start, name_end))
5344 {
5345 semsg(_(e_invalid_command_str), eap->cmd);
5346 return NULL;
5347 }
5348
5349 // "def" or "def Name": list functions
5350 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5351 return NULL;
5352 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5353 }
5354
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005355 // Only g:Func() can use a namespace.
5356 if (name_start[1] == ':' && !is_global)
5357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005358 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005359 return NULL;
5360 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005361 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005362 return NULL;
5363
Bram Moolenaar04b12692020-05-04 23:24:44 +02005364 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005365 fill_exarg_from_cctx(eap, cctx);
5366
Bram Moolenaar04b12692020-05-04 23:24:44 +02005367 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005368 lambda_name = vim_strsave(get_lambda_name());
5369 if (lambda_name == NULL)
5370 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005371 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005372
Bram Moolenaar822ba242020-05-24 23:00:18 +02005373 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005374 {
5375 r = eap->skip ? OK : FAIL;
5376 goto theend;
5377 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005378
5379 // copy over the block scope IDs before compiling
5380 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5381 {
5382 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5383
5384 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5385 if (ufunc->uf_block_ids != NULL)
5386 {
5387 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5388 sizeof(int) * block_depth);
5389 ufunc->uf_block_depth = block_depth;
5390 }
5391 }
5392
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005393 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5394 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005395 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005396 {
5397 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005398 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005399 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005400
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005401 if (is_global)
5402 {
5403 char_u *func_name = vim_strnsave(name_start + 2,
5404 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005405
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005406 if (func_name == NULL)
5407 r = FAIL;
5408 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005409 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005410 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005411 lambda_name = NULL;
5412 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005413 }
5414 else
5415 {
5416 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005417 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005418 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005419
Bram Moolenaareef21022020-08-01 22:16:43 +02005420 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005421 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005422 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005423 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005424 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5425 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005426 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005427
5428theend:
5429 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005430 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005431}
5432
5433/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 * Return the length of an assignment operator, or zero if there isn't one.
5435 */
5436 int
5437assignment_len(char_u *p, int *heredoc)
5438{
5439 if (*p == '=')
5440 {
5441 if (p[1] == '<' && p[2] == '<')
5442 {
5443 *heredoc = TRUE;
5444 return 3;
5445 }
5446 return 1;
5447 }
5448 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5449 return 2;
5450 if (STRNCMP(p, "..=", 3) == 0)
5451 return 3;
5452 return 0;
5453}
5454
5455// words that cannot be used as a variable
5456static char *reserved[] = {
5457 "true",
5458 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005459 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460 NULL
5461};
5462
Bram Moolenaar752fc692021-01-04 21:57:11 +01005463// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005464typedef enum {
5465 dest_local,
5466 dest_option,
5467 dest_env,
5468 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005469 dest_buffer,
5470 dest_window,
5471 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005472 dest_vimvar,
5473 dest_script,
5474 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005475 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005476} assign_dest_T;
5477
Bram Moolenaar752fc692021-01-04 21:57:11 +01005478// Used by compile_lhs() to store information about the LHS of an assignment
5479// and one argument of ":unlet" with an index.
5480typedef struct {
5481 assign_dest_T lhs_dest; // type of destination
5482
5483 char_u *lhs_name; // allocated name including
5484 // "[expr]" or ".name".
5485 size_t lhs_varlen; // length of the variable without
5486 // "[expr]" or ".name"
5487 char_u *lhs_dest_end; // end of the destination, including
5488 // "[expr]" or ".name".
5489
5490 int lhs_has_index; // has "[expr]" or ".name"
5491
5492 int lhs_new_local; // create new local variable
5493 int lhs_opt_flags; // for when destination is an option
5494 int lhs_vimvaridx; // for when destination is a v:var
5495
5496 lvar_T lhs_local_lvar; // used for existing local destination
5497 lvar_T lhs_arg_lvar; // used for argument destination
5498 lvar_T *lhs_lvar; // points to destination lvar
5499 int lhs_scriptvar_sid;
5500 int lhs_scriptvar_idx;
5501
5502 int lhs_has_type; // type was specified
5503 type_T *lhs_type;
5504 type_T *lhs_member_type;
5505} lhs_T;
5506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005508 * Generate the load instruction for "name".
5509 */
5510 static void
5511generate_loadvar(
5512 cctx_T *cctx,
5513 assign_dest_T dest,
5514 char_u *name,
5515 lvar_T *lvar,
5516 type_T *type)
5517{
5518 switch (dest)
5519 {
5520 case dest_option:
5521 // TODO: check the option exists
5522 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5523 break;
5524 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005525 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5526 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5527 else
5528 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005529 break;
5530 case dest_buffer:
5531 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5532 break;
5533 case dest_window:
5534 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5535 break;
5536 case dest_tab:
5537 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5538 break;
5539 case dest_script:
5540 compile_load_scriptvar(cctx,
5541 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5542 break;
5543 case dest_env:
5544 // Include $ in the name here
5545 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5546 break;
5547 case dest_reg:
5548 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5549 break;
5550 case dest_vimvar:
5551 generate_LOADV(cctx, name + 2, TRUE);
5552 break;
5553 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005554 if (lvar->lv_from_outer > 0)
5555 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5556 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005557 else
5558 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5559 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005560 case dest_expr:
5561 // list or dict value should already be on the stack.
5562 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005563 }
5564}
5565
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005566/*
5567 * Skip over "[expr]" or ".member".
5568 * Does not check for any errors.
5569 */
5570 static char_u *
5571skip_index(char_u *start)
5572{
5573 char_u *p = start;
5574
5575 if (*p == '[')
5576 {
5577 p = skipwhite(p + 1);
5578 (void)skip_expr(&p, NULL);
5579 p = skipwhite(p);
5580 if (*p == ']')
5581 return p + 1;
5582 return p;
5583 }
5584 // if (*p == '.')
5585 return to_name_end(p + 1, TRUE);
5586}
5587
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005588 void
5589vim9_declare_error(char_u *name)
5590{
5591 char *scope = "";
5592
5593 switch (*name)
5594 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005595 case 'g': scope = _("global"); break;
5596 case 'b': scope = _("buffer"); break;
5597 case 'w': scope = _("window"); break;
5598 case 't': scope = _("tab"); break;
5599 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005600 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005601 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005602 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005603 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005604 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005605 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005606 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005607 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005608 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005609}
5610
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005611/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005612 * For one assignment figure out the type of destination. Return it in "dest".
5613 * When not recognized "dest" is not set.
5614 * For an option "opt_flags" is set.
5615 * For a v:var "vimvaridx" is set.
5616 * "type" is set to the destination type if known, unchanted otherwise.
5617 * Return FAIL if an error message was given.
5618 */
5619 static int
5620get_var_dest(
5621 char_u *name,
5622 assign_dest_T *dest,
5623 int cmdidx,
5624 int *opt_flags,
5625 int *vimvaridx,
5626 type_T **type,
5627 cctx_T *cctx)
5628{
5629 char_u *p;
5630
5631 if (*name == '&')
5632 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005633 int cc;
5634 long numval;
5635 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005636
5637 *dest = dest_option;
5638 if (cmdidx == CMD_final || cmdidx == CMD_const)
5639 {
5640 emsg(_(e_const_option));
5641 return FAIL;
5642 }
5643 p = name;
5644 p = find_option_end(&p, opt_flags);
5645 if (p == NULL)
5646 {
5647 // cannot happen?
5648 emsg(_(e_letunexp));
5649 return FAIL;
5650 }
5651 cc = *p;
5652 *p = NUL;
5653 opt_type = get_option_value(skip_option_env_lead(name),
5654 &numval, NULL, *opt_flags);
5655 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005656 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005657 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005658 case gov_unknown:
5659 semsg(_(e_unknown_option), name);
5660 return FAIL;
5661 case gov_string:
5662 case gov_hidden_string:
5663 *type = &t_string;
5664 break;
5665 case gov_bool:
5666 case gov_hidden_bool:
5667 *type = &t_bool;
5668 break;
5669 case gov_number:
5670 case gov_hidden_number:
5671 *type = &t_number;
5672 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005673 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005674 }
5675 else if (*name == '$')
5676 {
5677 *dest = dest_env;
5678 *type = &t_string;
5679 }
5680 else if (*name == '@')
5681 {
5682 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5683 {
5684 emsg_invreg(name[1]);
5685 return FAIL;
5686 }
5687 *dest = dest_reg;
5688 *type = &t_string;
5689 }
5690 else if (STRNCMP(name, "g:", 2) == 0)
5691 {
5692 *dest = dest_global;
5693 }
5694 else if (STRNCMP(name, "b:", 2) == 0)
5695 {
5696 *dest = dest_buffer;
5697 }
5698 else if (STRNCMP(name, "w:", 2) == 0)
5699 {
5700 *dest = dest_window;
5701 }
5702 else if (STRNCMP(name, "t:", 2) == 0)
5703 {
5704 *dest = dest_tab;
5705 }
5706 else if (STRNCMP(name, "v:", 2) == 0)
5707 {
5708 typval_T *vtv;
5709 int di_flags;
5710
5711 *vimvaridx = find_vim_var(name + 2, &di_flags);
5712 if (*vimvaridx < 0)
5713 {
5714 semsg(_(e_variable_not_found_str), name);
5715 return FAIL;
5716 }
5717 // We use the current value of "sandbox" here, is that OK?
5718 if (var_check_ro(di_flags, name, FALSE))
5719 return FAIL;
5720 *dest = dest_vimvar;
5721 vtv = get_vim_var_tv(*vimvaridx);
5722 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5723 }
5724 return OK;
5725}
5726
5727/*
5728 * Generate a STORE instruction for "dest", not being "dest_local".
5729 * Return FAIL when out of memory.
5730 */
5731 static int
5732generate_store_var(
5733 cctx_T *cctx,
5734 assign_dest_T dest,
5735 int opt_flags,
5736 int vimvaridx,
5737 int scriptvar_idx,
5738 int scriptvar_sid,
5739 type_T *type,
5740 char_u *name)
5741{
5742 switch (dest)
5743 {
5744 case dest_option:
5745 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5746 opt_flags);
5747 case dest_global:
5748 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005749 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5750 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005751 case dest_buffer:
5752 // include b: with the name, easier to execute that way
5753 return generate_STORE(cctx, ISN_STOREB, 0, name);
5754 case dest_window:
5755 // include w: with the name, easier to execute that way
5756 return generate_STORE(cctx, ISN_STOREW, 0, name);
5757 case dest_tab:
5758 // include t: with the name, easier to execute that way
5759 return generate_STORE(cctx, ISN_STORET, 0, name);
5760 case dest_env:
5761 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5762 case dest_reg:
5763 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5764 case dest_vimvar:
5765 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5766 case dest_script:
5767 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005768 // "s:" may be included in the name.
5769 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005770 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005771 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5772 scriptvar_sid, scriptvar_idx, type);
5773 case dest_local:
5774 case dest_expr:
5775 // cannot happen
5776 break;
5777 }
5778 return FAIL;
5779}
5780
Bram Moolenaar752fc692021-01-04 21:57:11 +01005781 static int
5782is_decl_command(int cmdidx)
5783{
5784 return cmdidx == CMD_let || cmdidx == CMD_var
5785 || cmdidx == CMD_final || cmdidx == CMD_const;
5786}
5787
5788/*
5789 * Figure out the LHS type and other properties for an assignment or one item
5790 * of ":unlet" with an index.
5791 * Returns OK or FAIL.
5792 */
5793 static int
5794compile_lhs(
5795 char_u *var_start,
5796 lhs_T *lhs,
5797 int cmdidx,
5798 int heredoc,
5799 int oplen,
5800 cctx_T *cctx)
5801{
5802 char_u *var_end;
5803 int is_decl = is_decl_command(cmdidx);
5804
5805 CLEAR_POINTER(lhs);
5806 lhs->lhs_dest = dest_local;
5807 lhs->lhs_vimvaridx = -1;
5808 lhs->lhs_scriptvar_idx = -1;
5809
5810 // "dest_end" is the end of the destination, including "[expr]" or
5811 // ".name".
5812 // "var_end" is the end of the variable/option/etc. name.
5813 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5814 if (*var_start == '@')
5815 var_end = var_start + 2;
5816 else
5817 {
5818 // skip over the leading "&", "&l:", "&g:" and "$"
5819 var_end = skip_option_env_lead(var_start);
5820 var_end = to_name_end(var_end, TRUE);
5821 }
5822
5823 // "a: type" is declaring variable "a" with a type, not dict "a:".
5824 if (is_decl && lhs->lhs_dest_end == var_start + 2
5825 && lhs->lhs_dest_end[-1] == ':')
5826 --lhs->lhs_dest_end;
5827 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5828 --var_end;
5829
5830 // compute the length of the destination without "[expr]" or ".name"
5831 lhs->lhs_varlen = var_end - var_start;
5832 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5833 if (lhs->lhs_name == NULL)
5834 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005835
5836 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5837 // Something follows after the variable: "var[idx]" or "var.key".
5838 lhs->lhs_has_index = TRUE;
5839
Bram Moolenaar752fc692021-01-04 21:57:11 +01005840 if (heredoc)
5841 lhs->lhs_type = &t_list_string;
5842 else
5843 lhs->lhs_type = &t_any;
5844
5845 if (cctx->ctx_skip != SKIP_YES)
5846 {
5847 int declare_error = FALSE;
5848
5849 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5850 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5851 &lhs->lhs_type, cctx) == FAIL)
5852 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005853 if (lhs->lhs_dest != dest_local
5854 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005855 {
5856 // Specific kind of variable recognized.
5857 declare_error = is_decl;
5858 }
5859 else
5860 {
5861 int idx;
5862
5863 // No specific kind of variable recognized, just a name.
5864 for (idx = 0; reserved[idx] != NULL; ++idx)
5865 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5866 {
5867 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5868 return FAIL;
5869 }
5870
5871
5872 if (lookup_local(var_start, lhs->lhs_varlen,
5873 &lhs->lhs_local_lvar, cctx) == OK)
5874 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5875 else
5876 {
5877 CLEAR_FIELD(lhs->lhs_arg_lvar);
5878 if (arg_exists(var_start, lhs->lhs_varlen,
5879 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5880 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5881 {
5882 if (is_decl)
5883 {
5884 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5885 return FAIL;
5886 }
5887 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5888 }
5889 }
5890 if (lhs->lhs_lvar != NULL)
5891 {
5892 if (is_decl)
5893 {
5894 semsg(_(e_variable_already_declared), lhs->lhs_name);
5895 return FAIL;
5896 }
5897 }
5898 else
5899 {
5900 int script_namespace = lhs->lhs_varlen > 1
5901 && STRNCMP(var_start, "s:", 2) == 0;
5902 int script_var = (script_namespace
5903 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005904 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005905 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005906 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005907 imported_T *import =
5908 find_imported(var_start, lhs->lhs_varlen, cctx);
5909
5910 if (script_namespace || script_var || import != NULL)
5911 {
5912 char_u *rawname = lhs->lhs_name
5913 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5914
5915 if (is_decl)
5916 {
5917 if (script_namespace)
5918 semsg(_(e_cannot_declare_script_variable_in_function),
5919 lhs->lhs_name);
5920 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005921 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005922 lhs->lhs_name);
5923 return FAIL;
5924 }
5925 else if (cctx->ctx_ufunc->uf_script_ctx_version
5926 == SCRIPT_VERSION_VIM9
5927 && script_namespace
5928 && !script_var && import == NULL)
5929 {
5930 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5931 return FAIL;
5932 }
5933
5934 lhs->lhs_dest = dest_script;
5935
5936 // existing script-local variables should have a type
5937 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5938 if (import != NULL)
5939 lhs->lhs_scriptvar_sid = import->imp_sid;
5940 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5941 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005942 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005943 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005944 lhs->lhs_scriptvar_sid, rawname,
5945 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5946 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005947 if (lhs->lhs_scriptvar_idx >= 0)
5948 {
5949 scriptitem_T *si = SCRIPT_ITEM(
5950 lhs->lhs_scriptvar_sid);
5951 svar_T *sv =
5952 ((svar_T *)si->sn_var_vals.ga_data)
5953 + lhs->lhs_scriptvar_idx;
5954 lhs->lhs_type = sv->sv_type;
5955 }
5956 }
5957 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005958 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005959 == FAIL)
5960 return FAIL;
5961 }
5962 }
5963
5964 if (declare_error)
5965 {
5966 vim9_declare_error(lhs->lhs_name);
5967 return FAIL;
5968 }
5969 }
5970
5971 // handle "a:name" as a name, not index "name" on "a"
5972 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5973 var_end = lhs->lhs_dest_end;
5974
5975 if (lhs->lhs_dest != dest_option)
5976 {
5977 if (is_decl && *var_end == ':')
5978 {
5979 char_u *p;
5980
5981 // parse optional type: "let var: type = expr"
5982 if (!VIM_ISWHITE(var_end[1]))
5983 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005984 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005985 return FAIL;
5986 }
5987 p = skipwhite(var_end + 1);
5988 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5989 if (lhs->lhs_type == NULL)
5990 return FAIL;
5991 lhs->lhs_has_type = TRUE;
5992 }
5993 else if (lhs->lhs_lvar != NULL)
5994 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5995 }
5996
Bram Moolenaare42939a2021-04-05 17:11:17 +02005997 if (oplen == 3 && !heredoc
5998 && lhs->lhs_dest != dest_global
5999 && !lhs->lhs_has_index
6000 && lhs->lhs_type->tt_type != VAR_STRING
6001 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006002 {
6003 emsg(_(e_can_only_concatenate_to_string));
6004 return FAIL;
6005 }
6006
6007 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
6008 && cctx->ctx_skip != SKIP_YES)
6009 {
6010 if (oplen > 1 && !heredoc)
6011 {
6012 // +=, /=, etc. require an existing variable
6013 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
6014 return FAIL;
6015 }
6016 if (!is_decl)
6017 {
6018 semsg(_(e_unknown_variable_str), lhs->lhs_name);
6019 return FAIL;
6020 }
6021
Bram Moolenaar3f327882021-03-17 20:56:38 +01006022 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006023 if ((lhs->lhs_type->tt_type == VAR_FUNC
6024 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006025 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01006026 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006027
6028 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006029 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6030 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6031 if (lhs->lhs_lvar == NULL)
6032 return FAIL;
6033 lhs->lhs_new_local = TRUE;
6034 }
6035
6036 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006037 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 {
6039 // Something follows after the variable: "var[idx]" or "var.key".
6040 // TODO: should we also handle "->func()" here?
6041 if (is_decl)
6042 {
6043 emsg(_(e_cannot_use_index_when_declaring_variable));
6044 return FAIL;
6045 }
6046
6047 if (var_start[lhs->lhs_varlen] == '['
6048 || var_start[lhs->lhs_varlen] == '.')
6049 {
6050 char_u *after = var_start + lhs->lhs_varlen;
6051 char_u *p;
6052
6053 // Only the last index is used below, if there are others
6054 // before it generate code for the expression. Thus for
6055 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6056 for (;;)
6057 {
6058 p = skip_index(after);
6059 if (*p != '[' && *p != '.')
6060 break;
6061 after = p;
6062 }
6063 if (after > var_start + lhs->lhs_varlen)
6064 {
6065 lhs->lhs_varlen = after - var_start;
6066 lhs->lhs_dest = dest_expr;
6067 // We don't know the type before evaluating the expression,
6068 // use "any" until then.
6069 lhs->lhs_type = &t_any;
6070 }
6071
Bram Moolenaar752fc692021-01-04 21:57:11 +01006072 if (lhs->lhs_type->tt_member == NULL)
6073 lhs->lhs_member_type = &t_any;
6074 else
6075 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6076 }
6077 else
6078 {
6079 semsg("Not supported yet: %s", var_start);
6080 return FAIL;
6081 }
6082 }
6083 return OK;
6084}
6085
6086/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006087 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6088 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006089 */
6090 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006091compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006092 char_u *var_start,
6093 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006094 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006095 cctx_T *cctx)
6096{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006097 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006098 char_u *p;
6099 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006100 int need_white_before = TRUE;
6101 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006102
Bram Moolenaar752fc692021-01-04 21:57:11 +01006103 p = var_start + varlen;
6104 if (*p == '[')
6105 {
6106 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006107 if (*p == ':')
6108 {
6109 // empty first index, push zero
6110 r = generate_PUSHNR(cctx, 0);
6111 need_white_before = FALSE;
6112 }
6113 else
6114 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006115
6116 if (r == OK && *skipwhite(p) == ':')
6117 {
6118 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006119 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006120 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006121 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006122 empty_second = *skipwhite(p + 1) == ']';
6123 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6124 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006125 {
6126 semsg(_(e_white_space_required_before_and_after_str_at_str),
6127 ":", p);
6128 return FAIL;
6129 }
6130 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006131 if (*p == ']')
6132 // empty second index, push "none"
6133 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6134 else
6135 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006136 }
6137
Bram Moolenaar752fc692021-01-04 21:57:11 +01006138 if (r == OK && *skipwhite(p) != ']')
6139 {
6140 // this should not happen
6141 emsg(_(e_missbrac));
6142 r = FAIL;
6143 }
6144 }
6145 else // if (*p == '.')
6146 {
6147 char_u *key_end = to_name_end(p + 1, TRUE);
6148 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6149
6150 r = generate_PUSHS(cctx, key);
6151 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006152 return r;
6153}
6154
6155/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006156 * For a LHS with an index, load the variable to be indexed.
6157 */
6158 static int
6159compile_load_lhs(
6160 lhs_T *lhs,
6161 char_u *var_start,
6162 type_T *rhs_type,
6163 cctx_T *cctx)
6164{
6165 if (lhs->lhs_dest == dest_expr)
6166 {
6167 size_t varlen = lhs->lhs_varlen;
6168 int c = var_start[varlen];
6169 char_u *p = var_start;
6170 garray_T *stack = &cctx->ctx_type_stack;
6171
6172 // Evaluate "ll[expr]" of "ll[expr][idx]"
6173 var_start[varlen] = NUL;
6174 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6175 {
6176 // this should not happen
6177 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006178 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006179 return FAIL;
6180 }
6181 var_start[varlen] = c;
6182
6183 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6184 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6185 // now we can properly check the type
6186 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6187 && rhs_type != &t_void
6188 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6189 FALSE, FALSE) == FAIL)
6190 return FAIL;
6191 }
6192 else
6193 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6194 lhs->lhs_lvar, lhs->lhs_type);
6195 return OK;
6196}
6197
6198/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006199 * Assignment to a list or dict member, or ":unlet" for the item, using the
6200 * information in "lhs".
6201 * Returns OK or FAIL.
6202 */
6203 static int
6204compile_assign_unlet(
6205 char_u *var_start,
6206 lhs_T *lhs,
6207 int is_assign,
6208 type_T *rhs_type,
6209 cctx_T *cctx)
6210{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006211 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006212 garray_T *stack = &cctx->ctx_type_stack;
6213 int range = FALSE;
6214
Bram Moolenaar68452172021-04-12 21:21:02 +02006215 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006216 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006217 if (is_assign && range && lhs->lhs_type != &t_blob
6218 && lhs->lhs_type != &t_any)
6219 {
6220 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6221 return FAIL;
6222 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006223
6224 if (lhs->lhs_type == &t_any)
6225 {
6226 // Index on variable of unknown type: check at runtime.
6227 dest_type = VAR_ANY;
6228 }
6229 else
6230 {
6231 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006232 if (dest_type == VAR_DICT && range)
6233 {
6234 emsg(e_cannot_use_range_with_dictionary);
6235 return FAIL;
6236 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6238 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006239 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006240 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02006241 type_T *type;
6242
6243 if (range)
6244 {
6245 type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6246 if (need_type(type, &t_number,
6247 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006248 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02006249 }
6250 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6251 if ((dest_type != VAR_BLOB || type != &t_special)
6252 && need_type(type, &t_number,
6253 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006254 return FAIL;
6255 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256 }
6257
6258 // Load the dict or list. On the stack we then have:
6259 // - value (for assignment, not for :unlet)
6260 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006261 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006262 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006263 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6264 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006265
Bram Moolenaar68452172021-04-12 21:21:02 +02006266 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6267 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006268 {
6269 if (is_assign)
6270 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006271 if (range)
6272 {
6273 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6274 return FAIL;
6275 }
6276 else
6277 {
6278 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279
Bram Moolenaar68452172021-04-12 21:21:02 +02006280 if (isn == NULL)
6281 return FAIL;
6282 isn->isn_arg.vartype = dest_type;
6283 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006285 else if (range)
6286 {
6287 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6288 return FAIL;
6289 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 else
6291 {
6292 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6293 return FAIL;
6294 }
6295 }
6296 else
6297 {
6298 emsg(_(e_indexable_type_required));
6299 return FAIL;
6300 }
6301
6302 return OK;
6303}
6304
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006305/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006307 * "let name"
6308 * "var name = expr"
6309 * "final name = expr"
6310 * "const name = expr"
6311 * "name = expr"
6312 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313 * Return NULL for an error.
6314 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 */
6316 static char_u *
6317compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6318{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006319 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006321 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 char_u *ret = NULL;
6323 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006324 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006327 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 int oplen = 0;
6330 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006331 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 int is_decl = is_decl_command(cmdidx);
6334 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006335 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006337 // Skip over the "var" or "[var, var]" to get to any "=".
6338 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6339 if (p == NULL)
6340 return *arg == '[' ? arg : NULL;
6341
6342 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006344 // TODO: should we allow this, and figure out type inference from list
6345 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006346 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 return NULL;
6348 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006349 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006351 sp = p;
6352 p = skipwhite(p);
6353 op = p;
6354 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006355
6356 if (var_count > 0 && oplen == 0)
6357 // can be something like "[1, 2]->func()"
6358 return arg;
6359
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006360 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006362 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006363 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006364 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 if (heredoc)
6367 {
6368 list_T *l;
6369 listitem_T *li;
6370
6371 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006372 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006374 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006375 if (l == NULL)
6376 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
Bram Moolenaar078269b2020-09-21 20:35:55 +02006378 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006380 // Push each line and the create the list.
6381 FOR_ALL_LIST_ITEMS(l, li)
6382 {
6383 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6384 li->li_tv.vval.v_string = NULL;
6385 }
6386 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006388 list_free(l);
6389 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006390 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006391 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006393 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006394 char_u *wp;
6395
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006396 // for "[var, var] = expr" evaluate the expression here, loop over the
6397 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006398 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006399
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006400 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006401 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006402 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006403 if (compile_expr0(&p, cctx) == FAIL)
6404 return NULL;
6405 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006407 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006409 type_T *stacktype;
6410
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006411 stacktype = stack->ga_len == 0 ? &t_void
6412 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006413 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006415 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006416 goto theend;
6417 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006418 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006419 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006420 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006421 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006422 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6423 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006424 if (stacktype->tt_member != NULL)
6425 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006426 }
6427 }
6428
6429 /*
6430 * Loop over variables in "[var, var] = expr".
6431 * For "var = expr" and "let var: type" this is done only once.
6432 */
6433 if (var_count > 0)
6434 var_start = skipwhite(arg + 1); // skip over the "["
6435 else
6436 var_start = arg;
6437 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6438 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006439 int instr_count = -1;
6440
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006441 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6442 {
6443 // Ignore underscore in "[a, _, b] = list".
6444 if (var_count > 0)
6445 {
6446 var_start = skipwhite(var_start + 2);
6447 continue;
6448 }
6449 emsg(_(e_cannot_use_underscore_here));
6450 goto theend;
6451 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 vim_free(lhs.lhs_name);
6453
6454 /*
6455 * Figure out the LHS type and other properties.
6456 */
6457 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6458 goto theend;
6459
6460 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006461 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006463 goto theend;
6464 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465 if (!is_decl && lhs.lhs_lvar != NULL
6466 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006467 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006468 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006469 goto theend;
6470 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006471
6472 if (!heredoc)
6473 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006474 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006475 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006476 if (oplen > 0 && var_count == 0)
6477 {
6478 // skip over the "=" and the expression
6479 p = skipwhite(op + oplen);
6480 compile_expr0(&p, cctx);
6481 }
6482 }
6483 else if (oplen > 0)
6484 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006485 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006486 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006487
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006488 // For "var = expr" evaluate the expression.
6489 if (var_count == 0)
6490 {
6491 int r;
6492
6493 // for "+=", "*=", "..=" etc. first load the current value
6494 if (*op != '=')
6495 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006496 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006497
Bram Moolenaar752fc692021-01-04 21:57:11 +01006498 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006499 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006500 int range = FALSE;
6501
6502 // Get member from list or dict. First compile the
6503 // index value.
6504 if (compile_assign_index(var_start, &lhs,
Bram Moolenaar68452172021-04-12 21:21:02 +02006505 &range, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02006506 goto theend;
Bram Moolenaar68452172021-04-12 21:21:02 +02006507 if (range)
6508 {
Bram Moolenaarb1419262021-04-14 20:54:07 +02006509 semsg(_(e_cannot_use_range_with_assignment_operator_str),
Bram Moolenaar68452172021-04-12 21:21:02 +02006510 var_start);
Bram Moolenaarf387f5d2021-04-15 13:42:21 +02006511 goto theend;
Bram Moolenaar68452172021-04-12 21:21:02 +02006512 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006513
6514 // Get the member.
6515 if (compile_member(FALSE, cctx) == FAIL)
6516 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006517 }
6518 }
6519
6520 // Compile the expression. Temporarily hide the new local
6521 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006523 --cctx->ctx_locals.ga_len;
6524 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006525 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006526 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006527 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006529 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006530 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006531 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006532 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006533 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006534 ++cctx->ctx_locals.ga_len;
6535 if (r == FAIL)
6536 goto theend;
6537 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006538 else if (semicolon && var_idx == var_count - 1)
6539 {
6540 // For "[var; var] = expr" get the rest of the list
6541 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6542 goto theend;
6543 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006544 else
6545 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006546 // For "[var, var] = expr" get the "var_idx" item from the
6547 // list.
6548 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006549 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006550 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006551
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006552 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006553 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006555 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006556 if ((rhs_type->tt_type == VAR_FUNC
6557 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006558 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006559 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006560 goto theend;
6561
Bram Moolenaar752fc692021-01-04 21:57:11 +01006562 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006563 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006564 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006566 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006567 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006568 }
6569 else
6570 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006571 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006572 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006573 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006574 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006575 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006577 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006578 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006579 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006580 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006581 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006582 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006583 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006584 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006585 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006586
Bram Moolenaar77709b12021-04-03 21:01:01 +02006587 // Without operator check type here, otherwise below.
6588 // Use the line number of the assignment.
6589 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006590 if (lhs.lhs_has_index)
6591 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006592 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006593 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006594 goto theend;
6595 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006596 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006597 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006598 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006601 else if (cmdidx == CMD_final)
6602 {
6603 emsg(_(e_final_requires_a_value));
6604 goto theend;
6605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006606 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006608 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006609 goto theend;
6610 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006613 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006614 goto theend;
6615 }
6616 else
6617 {
6618 // variables are always initialized
6619 if (ga_grow(instr, 1) == FAIL)
6620 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006621 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006622 {
6623 case VAR_BOOL:
6624 generate_PUSHBOOL(cctx, VVAL_FALSE);
6625 break;
6626 case VAR_FLOAT:
6627#ifdef FEAT_FLOAT
6628 generate_PUSHF(cctx, 0.0);
6629#endif
6630 break;
6631 case VAR_STRING:
6632 generate_PUSHS(cctx, NULL);
6633 break;
6634 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006635 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 break;
6637 case VAR_FUNC:
6638 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6639 break;
6640 case VAR_LIST:
6641 generate_NEWLIST(cctx, 0);
6642 break;
6643 case VAR_DICT:
6644 generate_NEWDICT(cctx, 0);
6645 break;
6646 case VAR_JOB:
6647 generate_PUSHJOB(cctx, NULL);
6648 break;
6649 case VAR_CHANNEL:
6650 generate_PUSHCHANNEL(cctx, NULL);
6651 break;
6652 case VAR_NUMBER:
6653 case VAR_UNKNOWN:
6654 case VAR_ANY:
6655 case VAR_PARTIAL:
6656 case VAR_VOID:
6657 case VAR_SPECIAL: // cannot happen
6658 generate_PUSHNR(cctx, 0);
6659 break;
6660 }
6661 }
6662 if (var_count == 0)
6663 end = p;
6664 }
6665
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006666 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006667 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006668 break;
6669
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006670 if (oplen > 0 && *op != '=')
6671 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006672 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673 type_T *stacktype;
6674
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006675 if (*op == '.')
6676 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006677 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006678 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006679 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006680 if (
6681#ifdef FEAT_FLOAT
6682 // If variable is float operation with number is OK.
6683 !(expected == &t_float && stacktype == &t_number) &&
6684#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006685 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006686 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006687 goto theend;
6688
6689 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006690 {
6691 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6692 goto theend;
6693 }
6694 else if (*op == '+')
6695 {
6696 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006697 operator_type(lhs.lhs_member_type, stacktype),
6698 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006699 goto theend;
6700 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006701 else if (generate_two_op(cctx, op) == FAIL)
6702 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006706 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006707 // Use the info in "lhs" to store the value at the index in the
6708 // list or dict.
6709 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6710 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006711 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006712 }
6713 else
6714 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006715 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006716 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006717 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006718 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006719 generate_LOCKCONST(cctx);
6720
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006721 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006722 && (lhs.lhs_type->tt_type == VAR_DICT
6723 || lhs.lhs_type->tt_type == VAR_LIST)
6724 && lhs.lhs_type->tt_member != NULL
6725 && lhs.lhs_type->tt_member != &t_any
6726 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006727 // Set the type in the list or dict, so that it can be checked,
6728 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006729 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006730
Bram Moolenaar752fc692021-01-04 21:57:11 +01006731 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006732 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006733 if (generate_store_var(cctx, lhs.lhs_dest,
6734 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6735 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6736 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006737 goto theend;
6738 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006739 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006740 {
6741 isn_T *isn = ((isn_T *)instr->ga_data)
6742 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006743
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006744 // optimization: turn "var = 123" from ISN_PUSHNR +
6745 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006746 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006747 && instr->ga_len == instr_count + 1
6748 && isn->isn_type == ISN_PUSHNR)
6749 {
6750 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006751
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006752 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006753 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006754 isn->isn_arg.storenr.stnr_val = val;
6755 if (stack->ga_len > 0)
6756 --stack->ga_len;
6757 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006758 else if (lhs.lhs_lvar->lv_from_outer > 0)
6759 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6760 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006761 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006762 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006763 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006764 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006765
6766 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006767 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006769
6770 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006771 if (var_count > 0 && !semicolon)
6772 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006773 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006774 goto theend;
6775 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006776
Bram Moolenaarb2097502020-07-19 17:17:02 +02006777 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
6779theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006780 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781 return ret;
6782}
6783
6784/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006785 * Check for an assignment at "eap->cmd", compile it if found.
6786 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6787 */
6788 static int
6789may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6790{
6791 char_u *pskip;
6792 char_u *p;
6793
6794 // Assuming the command starts with a variable or function name,
6795 // find what follows.
6796 // Skip over "var.member", "var[idx]" and the like.
6797 // Also "&opt = val", "$ENV = val" and "@r = val".
6798 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6799 ? eap->cmd + 1 : eap->cmd;
6800 p = to_name_end(pskip, TRUE);
6801 if (p > eap->cmd && *p != NUL)
6802 {
6803 char_u *var_end;
6804 int oplen;
6805 int heredoc;
6806
6807 if (eap->cmd[0] == '@')
6808 var_end = eap->cmd + 2;
6809 else
6810 var_end = find_name_end(pskip, NULL, NULL,
6811 FNE_CHECK_START | FNE_INCL_BR);
6812 oplen = assignment_len(skipwhite(var_end), &heredoc);
6813 if (oplen > 0)
6814 {
6815 size_t len = p - eap->cmd;
6816
6817 // Recognize an assignment if we recognize the variable
6818 // name:
6819 // "g:var = expr"
6820 // "local = expr" where "local" is a local var.
6821 // "script = expr" where "script" is a script-local var.
6822 // "import = expr" where "import" is an imported var
6823 // "&opt = expr"
6824 // "$ENV = expr"
6825 // "@r = expr"
6826 if (*eap->cmd == '&'
6827 || *eap->cmd == '$'
6828 || *eap->cmd == '@'
6829 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006830 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006831 {
6832 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6833 if (*line == NULL || *line == eap->cmd)
6834 return FAIL;
6835 return OK;
6836 }
6837 }
6838 }
6839
6840 if (*eap->cmd == '[')
6841 {
6842 // [var, var] = expr
6843 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6844 if (*line == NULL)
6845 return FAIL;
6846 if (*line != eap->cmd)
6847 return OK;
6848 }
6849 return NOTDONE;
6850}
6851
6852/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006853 * Check if "name" can be "unlet".
6854 */
6855 int
6856check_vim9_unlet(char_u *name)
6857{
6858 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6859 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006860 // "unlet s:var" is allowed in legacy script.
6861 if (*name == 's' && !script_is_vim9())
6862 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006863 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006864 return FAIL;
6865 }
6866 return OK;
6867}
6868
6869/*
6870 * Callback passed to ex_unletlock().
6871 */
6872 static int
6873compile_unlet(
6874 lval_T *lvp,
6875 char_u *name_end,
6876 exarg_T *eap,
6877 int deep UNUSED,
6878 void *coookie)
6879{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006880 cctx_T *cctx = coookie;
6881 char_u *p = lvp->ll_name;
6882 int cc = *name_end;
6883 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006884
Bram Moolenaar752fc692021-01-04 21:57:11 +01006885 if (cctx->ctx_skip == SKIP_YES)
6886 return OK;
6887
6888 *name_end = NUL;
6889 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006890 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006891 // :unlet $ENV_VAR
6892 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6893 }
6894 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6895 {
6896 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006897
Bram Moolenaar752fc692021-01-04 21:57:11 +01006898 // This is similar to assigning: lookup the list/dict, compile the
6899 // idx/key. Then instead of storing the value unlet the item.
6900 // unlet {list}[idx]
6901 // unlet {dict}[key] dict.key
6902 //
6903 // Figure out the LHS type and other properties.
6904 //
6905 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6906
6907 // : unlet an indexed item
6908 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006909 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006910 iemsg("called compile_lhs() without an index");
6911 ret = FAIL;
6912 }
6913 else
6914 {
6915 // Use the info in "lhs" to unlet the item at the index in the
6916 // list or dict.
6917 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006918 }
6919
Bram Moolenaar752fc692021-01-04 21:57:11 +01006920 vim_free(lhs.lhs_name);
6921 }
6922 else if (check_vim9_unlet(p) == FAIL)
6923 {
6924 ret = FAIL;
6925 }
6926 else
6927 {
6928 // Normal name. Only supports g:, w:, t: and b: namespaces.
6929 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006930 }
6931
Bram Moolenaar752fc692021-01-04 21:57:11 +01006932 *name_end = cc;
6933 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006934}
6935
6936/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006937 * Callback passed to ex_unletlock().
6938 */
6939 static int
6940compile_lock_unlock(
6941 lval_T *lvp,
6942 char_u *name_end,
6943 exarg_T *eap,
6944 int deep UNUSED,
6945 void *coookie)
6946{
6947 cctx_T *cctx = coookie;
6948 int cc = *name_end;
6949 char_u *p = lvp->ll_name;
6950 int ret = OK;
6951 size_t len;
6952 char_u *buf;
6953
6954 if (cctx->ctx_skip == SKIP_YES)
6955 return OK;
6956
6957 // Cannot use :lockvar and :unlockvar on local variables.
6958 if (p[1] != ':')
6959 {
6960 char_u *end = skip_var_one(p, FALSE);
6961
6962 if (lookup_local(p, end - p, NULL, cctx) == OK)
6963 {
6964 emsg(_(e_cannot_lock_unlock_local_variable));
6965 return FAIL;
6966 }
6967 }
6968
6969 // Checking is done at runtime.
6970 *name_end = NUL;
6971 len = name_end - p + 20;
6972 buf = alloc(len);
6973 if (buf == NULL)
6974 ret = FAIL;
6975 else
6976 {
6977 vim_snprintf((char *)buf, len, "%s %s",
6978 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6979 p);
6980 ret = generate_EXEC(cctx, buf);
6981
6982 vim_free(buf);
6983 *name_end = cc;
6984 }
6985 return ret;
6986}
6987
6988/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006989 * compile "unlet var", "lock var" and "unlock var"
6990 * "arg" points to "var".
6991 */
6992 static char_u *
6993compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6994{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006995 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6996 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6997 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006998 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6999}
7000
7001/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 * Compile an :import command.
7003 */
7004 static char_u *
7005compile_import(char_u *arg, cctx_T *cctx)
7006{
Bram Moolenaar1c991142020-07-04 13:15:31 +02007007 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007008}
7009
7010/*
7011 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
7012 */
7013 static int
7014compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
7015{
7016 garray_T *instr = &cctx->ctx_instr;
7017 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
7018
7019 if (endlabel == NULL)
7020 return FAIL;
7021 endlabel->el_next = *el;
7022 *el = endlabel;
7023 endlabel->el_end_label = instr->ga_len;
7024
7025 generate_JUMP(cctx, when, 0);
7026 return OK;
7027}
7028
7029 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007030compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031{
7032 garray_T *instr = &cctx->ctx_instr;
7033
7034 while (*el != NULL)
7035 {
7036 endlabel_T *cur = (*el);
7037 isn_T *isn;
7038
7039 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007040 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 *el = cur->el_next;
7042 vim_free(cur);
7043 }
7044}
7045
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007046 static void
7047compile_free_jump_to_end(endlabel_T **el)
7048{
7049 while (*el != NULL)
7050 {
7051 endlabel_T *cur = (*el);
7052
7053 *el = cur->el_next;
7054 vim_free(cur);
7055 }
7056}
7057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058/*
7059 * Create a new scope and set up the generic items.
7060 */
7061 static scope_T *
7062new_scope(cctx_T *cctx, scopetype_T type)
7063{
7064 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7065
7066 if (scope == NULL)
7067 return NULL;
7068 scope->se_outer = cctx->ctx_scope;
7069 cctx->ctx_scope = scope;
7070 scope->se_type = type;
7071 scope->se_local_count = cctx->ctx_locals.ga_len;
7072 return scope;
7073}
7074
7075/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007076 * Free the current scope and go back to the outer scope.
7077 */
7078 static void
7079drop_scope(cctx_T *cctx)
7080{
7081 scope_T *scope = cctx->ctx_scope;
7082
7083 if (scope == NULL)
7084 {
7085 iemsg("calling drop_scope() without a scope");
7086 return;
7087 }
7088 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007089 switch (scope->se_type)
7090 {
7091 case IF_SCOPE:
7092 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7093 case FOR_SCOPE:
7094 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7095 case WHILE_SCOPE:
7096 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7097 case TRY_SCOPE:
7098 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7099 case NO_SCOPE:
7100 case BLOCK_SCOPE:
7101 break;
7102 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007103 vim_free(scope);
7104}
7105
7106/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 * compile "if expr"
7108 *
7109 * "if expr" Produces instructions:
7110 * EVAL expr Push result of "expr"
7111 * JUMP_IF_FALSE end
7112 * ... body ...
7113 * end:
7114 *
7115 * "if expr | else" Produces instructions:
7116 * EVAL expr Push result of "expr"
7117 * JUMP_IF_FALSE else
7118 * ... body ...
7119 * JUMP_ALWAYS end
7120 * else:
7121 * ... body ...
7122 * end:
7123 *
7124 * "if expr1 | elseif expr2 | else" Produces instructions:
7125 * EVAL expr Push result of "expr"
7126 * JUMP_IF_FALSE elseif
7127 * ... body ...
7128 * JUMP_ALWAYS end
7129 * elseif:
7130 * EVAL expr Push result of "expr"
7131 * JUMP_IF_FALSE else
7132 * ... body ...
7133 * JUMP_ALWAYS end
7134 * else:
7135 * ... body ...
7136 * end:
7137 */
7138 static char_u *
7139compile_if(char_u *arg, cctx_T *cctx)
7140{
7141 char_u *p = arg;
7142 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007143 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007145 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007146 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147
Bram Moolenaara5565e42020-05-09 15:44:01 +02007148 CLEAR_FIELD(ppconst);
7149 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007150 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007151 clear_ppconst(&ppconst);
7152 return NULL;
7153 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007154 if (!ends_excmd2(arg, skipwhite(p)))
7155 {
7156 semsg(_(e_trailing_arg), p);
7157 return NULL;
7158 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007159 if (cctx->ctx_skip == SKIP_YES)
7160 clear_ppconst(&ppconst);
7161 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007162 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007163 int error = FALSE;
7164 int v;
7165
Bram Moolenaara5565e42020-05-09 15:44:01 +02007166 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007167 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007168 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007169 if (error)
7170 return NULL;
7171 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007172 }
7173 else
7174 {
7175 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007176 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007177 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007178 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007179 if (bool_on_stack(cctx) == FAIL)
7180 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007182
Bram Moolenaara91a7132021-03-25 21:12:15 +01007183 // CMDMOD_REV must come before the jump
7184 generate_undo_cmdmods(cctx);
7185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186 scope = new_scope(cctx, IF_SCOPE);
7187 if (scope == NULL)
7188 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007189 scope->se_skip_save = skip_save;
7190 // "is_had_return" will be reset if any block does not end in :return
7191 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007193 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007194 {
7195 // "where" is set when ":elseif", "else" or ":endif" is found
7196 scope->se_u.se_if.is_if_label = instr->ga_len;
7197 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7198 }
7199 else
7200 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201
Bram Moolenaarced68a02021-01-24 17:53:47 +01007202#ifdef FEAT_PROFILE
7203 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7204 && skip_save != SKIP_YES)
7205 {
7206 // generated a profile start, need to generate a profile end, since it
7207 // won't be done after returning
7208 cctx->ctx_skip = SKIP_NOT;
7209 generate_instr(cctx, ISN_PROF_END);
7210 cctx->ctx_skip = SKIP_YES;
7211 }
7212#endif
7213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214 return p;
7215}
7216
7217 static char_u *
7218compile_elseif(char_u *arg, cctx_T *cctx)
7219{
7220 char_u *p = arg;
7221 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007222 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007223 isn_T *isn;
7224 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007225 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007226 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227
7228 if (scope == NULL || scope->se_type != IF_SCOPE)
7229 {
7230 emsg(_(e_elseif_without_if));
7231 return NULL;
7232 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007233 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007234 if (!cctx->ctx_had_return)
7235 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236
Bram Moolenaarced68a02021-01-24 17:53:47 +01007237 if (cctx->ctx_skip == SKIP_NOT)
7238 {
7239 // previous block was executed, this one and following will not
7240 cctx->ctx_skip = SKIP_YES;
7241 scope->se_u.se_if.is_seen_skip_not = TRUE;
7242 }
7243 if (scope->se_u.se_if.is_seen_skip_not)
7244 {
7245 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007246 // Do not count the "elseif" for profiling and cmdmod
7247 instr->ga_len = current_instr_idx(cctx);
7248
Bram Moolenaarced68a02021-01-24 17:53:47 +01007249 skip_expr_cctx(&p, cctx);
7250 return p;
7251 }
7252
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007253 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007254 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007255 int moved_cmdmod = FALSE;
7256
7257 // Move any CMDMOD instruction to after the jump
7258 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7259 {
7260 if (ga_grow(instr, 1) == FAIL)
7261 return NULL;
7262 ((isn_T *)instr->ga_data)[instr->ga_len] =
7263 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7264 --instr->ga_len;
7265 moved_cmdmod = TRUE;
7266 }
7267
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007268 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007270 return NULL;
7271 // previous "if" or "elseif" jumps here
7272 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7273 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007274 if (moved_cmdmod)
7275 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007277
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007278 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007279 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007280 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007281 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007282 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007283#ifdef FEAT_PROFILE
7284 if (cctx->ctx_profiling)
7285 {
7286 // the previous block was skipped, need to profile this line
7287 generate_instr(cctx, ISN_PROF_START);
7288 instr_count = instr->ga_len;
7289 }
7290#endif
7291 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007292 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007293 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007294 clear_ppconst(&ppconst);
7295 return NULL;
7296 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007297 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007298 if (!ends_excmd2(arg, skipwhite(p)))
7299 {
7300 semsg(_(e_trailing_arg), p);
7301 return NULL;
7302 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007303 if (scope->se_skip_save == SKIP_YES)
7304 clear_ppconst(&ppconst);
7305 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007306 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007307 int error = FALSE;
7308 int v;
7309
Bram Moolenaar7f141552020-05-09 17:35:53 +02007310 // The expression results in a constant.
7311 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007312 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7313 if (error)
7314 return NULL;
7315 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007316 clear_ppconst(&ppconst);
7317 scope->se_u.se_if.is_if_label = -1;
7318 }
7319 else
7320 {
7321 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007322 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007323 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007324 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007325 if (bool_on_stack(cctx) == FAIL)
7326 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327
Bram Moolenaara91a7132021-03-25 21:12:15 +01007328 // CMDMOD_REV must come before the jump
7329 generate_undo_cmdmods(cctx);
7330
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007331 // "where" is set when ":elseif", "else" or ":endif" is found
7332 scope->se_u.se_if.is_if_label = instr->ga_len;
7333 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007335
7336 return p;
7337}
7338
7339 static char_u *
7340compile_else(char_u *arg, cctx_T *cctx)
7341{
7342 char_u *p = arg;
7343 garray_T *instr = &cctx->ctx_instr;
7344 isn_T *isn;
7345 scope_T *scope = cctx->ctx_scope;
7346
7347 if (scope == NULL || scope->se_type != IF_SCOPE)
7348 {
7349 emsg(_(e_else_without_if));
7350 return NULL;
7351 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007352 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007353 if (!cctx->ctx_had_return)
7354 scope->se_u.se_if.is_had_return = FALSE;
7355 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356
Bram Moolenaarced68a02021-01-24 17:53:47 +01007357#ifdef FEAT_PROFILE
7358 if (cctx->ctx_profiling)
7359 {
7360 if (cctx->ctx_skip == SKIP_NOT
7361 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7362 .isn_type == ISN_PROF_START)
7363 // the previous block was executed, do not count "else" for profiling
7364 --instr->ga_len;
7365 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7366 {
7367 // the previous block was not executed, this one will, do count the
7368 // "else" for profiling
7369 cctx->ctx_skip = SKIP_NOT;
7370 generate_instr(cctx, ISN_PROF_END);
7371 generate_instr(cctx, ISN_PROF_START);
7372 cctx->ctx_skip = SKIP_YES;
7373 }
7374 }
7375#endif
7376
7377 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007378 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007379 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007380 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007381 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007382 if (!cctx->ctx_had_return
7383 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7384 JUMP_ALWAYS, cctx) == FAIL)
7385 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007386 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007387
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007388 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007389 {
7390 if (scope->se_u.se_if.is_if_label >= 0)
7391 {
7392 // previous "if" or "elseif" jumps here
7393 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7394 isn->isn_arg.jump.jump_where = instr->ga_len;
7395 scope->se_u.se_if.is_if_label = -1;
7396 }
7397 }
7398
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007399 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007400 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7401 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402
7403 return p;
7404}
7405
7406 static char_u *
7407compile_endif(char_u *arg, cctx_T *cctx)
7408{
7409 scope_T *scope = cctx->ctx_scope;
7410 ifscope_T *ifscope;
7411 garray_T *instr = &cctx->ctx_instr;
7412 isn_T *isn;
7413
Bram Moolenaarfa984412021-03-25 22:15:28 +01007414 if (misplaced_cmdmod(cctx))
7415 return NULL;
7416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417 if (scope == NULL || scope->se_type != IF_SCOPE)
7418 {
7419 emsg(_(e_endif_without_if));
7420 return NULL;
7421 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007422 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007423 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007424 if (!cctx->ctx_had_return)
7425 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007427 if (scope->se_u.se_if.is_if_label >= 0)
7428 {
7429 // previous "if" or "elseif" jumps here
7430 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7431 isn->isn_arg.jump.jump_where = instr->ga_len;
7432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007434 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007435
7436#ifdef FEAT_PROFILE
7437 // even when skipping we count the endif as executed, unless the block it's
7438 // in is skipped
7439 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7440 && scope->se_skip_save != SKIP_YES)
7441 {
7442 cctx->ctx_skip = SKIP_NOT;
7443 generate_instr(cctx, ISN_PROF_START);
7444 }
7445#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007446 cctx->ctx_skip = scope->se_skip_save;
7447
7448 // If all the blocks end in :return and there is an :else then the
7449 // had_return flag is set.
7450 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007452 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 return arg;
7454}
7455
7456/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007457 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 *
7459 * Produces instructions:
7460 * PUSHNR -1
7461 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007462 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7464 * - if beyond end, jump to "end"
7465 * - otherwise get item from list and push it
7466 * STORE var Store item in "var"
7467 * ... body ...
7468 * JUMP top Jump back to repeat
7469 * end: DROP Drop the result of "expr"
7470 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007471 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7472 * UNPACK 2 Split item in 2
7473 * STORE var1 Store item in "var1"
7474 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 */
7476 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007477compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007479 char_u *arg;
7480 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007481 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007483 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007484 int var_count = 0;
7485 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 garray_T *stack = &cctx->ctx_type_stack;
7488 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007489 lvar_T *loop_lvar; // loop iteration variable
7490 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007492 type_T *item_type = &t_any;
7493 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
Bram Moolenaar792f7862020-11-23 08:31:18 +01007495 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007496 if (p == NULL)
7497 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007498 if (var_count == 0)
7499 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500
7501 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007502 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007503 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7504 return NULL;
7505 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 {
7507 emsg(_(e_missing_in));
7508 return NULL;
7509 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007510 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007511 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7512 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 scope = new_scope(cctx, FOR_SCOPE);
7515 if (scope == NULL)
7516 return NULL;
7517
Bram Moolenaar792f7862020-11-23 08:31:18 +01007518 // Reserve a variable to store the loop iteration counter and initialize it
7519 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007520 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7521 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007522 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007523 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007524 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007526 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007527 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528
7529 // compile "expr", it remains on the stack until "endfor"
7530 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007531 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007532 {
7533 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007535 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007536 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007537
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007538 // If we know the type of "var" and it is a not a supported type we can
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007539 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007540 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007541 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007542 && vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007544 semsg(_(e_for_loop_on_str_not_supported),
7545 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007546 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007547 return NULL;
7548 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007549
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007550 if (vartype->tt_type == VAR_STRING)
7551 item_type = &t_string;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007552 else if (vartype->tt_type == VAR_BLOB)
7553 item_type = &t_number;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007554 else if (vartype->tt_type == VAR_LIST
7555 && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007556 {
7557 if (var_count == 1)
7558 item_type = vartype->tt_member;
7559 else if (vartype->tt_member->tt_type == VAR_LIST
7560 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
Bram Moolenaard551d6c2021-04-18 13:15:58 +02007561 // TODO: should get the type for each lhs
Bram Moolenaar792f7862020-11-23 08:31:18 +01007562 item_type = vartype->tt_member->tt_member;
7563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564
Bram Moolenaara91a7132021-03-25 21:12:15 +01007565 // CMDMOD_REV must come before the FOR instruction
7566 generate_undo_cmdmods(cctx);
7567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007569 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007570 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
Bram Moolenaar792f7862020-11-23 08:31:18 +01007572 arg = arg_start;
7573 if (var_count > 1)
7574 {
7575 generate_UNPACK(cctx, var_count, semicolon);
7576 arg = skipwhite(arg + 1); // skip white after '['
7577
7578 // the list item is replaced by a number of items
7579 if (ga_grow(stack, var_count - 1) == FAIL)
7580 {
7581 drop_scope(cctx);
7582 return NULL;
7583 }
7584 --stack->ga_len;
7585 for (idx = 0; idx < var_count; ++idx)
7586 {
7587 ((type_T **)stack->ga_data)[stack->ga_len] =
7588 (semicolon && idx == 0) ? vartype : item_type;
7589 ++stack->ga_len;
7590 }
7591 }
7592
7593 for (idx = 0; idx < var_count; ++idx)
7594 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007595 assign_dest_T dest = dest_local;
7596 int opt_flags = 0;
7597 int vimvaridx = -1;
7598 type_T *type = &t_any;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007599 type_T *lhs_type = &t_any;
7600 where_T where;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007601
7602 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007603 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007604 name = vim_strnsave(arg, varlen);
7605 if (name == NULL)
7606 goto failed;
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007607 if (*p == ':')
7608 {
7609 p = skipwhite(p + 1);
7610 lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
7611 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007612
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007613 // TODO: script var not supported?
7614 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7615 &vimvaridx, &type, cctx) == FAIL)
7616 goto failed;
7617 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007618 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007619 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7620 0, 0, type, name) == FAIL)
7621 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007622 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007623 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007624 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007625 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007626 {
7627 semsg(_(e_variable_already_declared), arg);
7628 goto failed;
7629 }
7630
Bram Moolenaarea870692020-12-02 14:24:30 +01007631 if (STRNCMP(name, "s:", 2) == 0)
7632 {
7633 semsg(_(e_cannot_declare_script_variable_in_function), name);
7634 goto failed;
7635 }
7636
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007637 // Reserve a variable to store "var".
Bram Moolenaarfe090eb2021-04-15 21:48:32 +02007638 where.wt_index = var_count > 1 ? idx + 1 : 0;
7639 where.wt_variable = TRUE;
7640 if (lhs_type == &t_any)
7641 lhs_type = item_type;
7642 else if (item_type != &t_unknown
7643 && !(var_count > 1 && item_type == &t_any)
7644 && check_type(lhs_type, item_type, TRUE, where) == FAIL)
7645 goto failed;
7646 var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007647 if (var_lvar == NULL)
7648 // out of memory or used as an argument
7649 goto failed;
7650
7651 if (semicolon && idx == var_count - 1)
7652 var_lvar->lv_type = vartype;
7653 else
7654 var_lvar->lv_type = item_type;
7655 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7656 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007657
7658 if (*p == ',' || *p == ';')
7659 ++p;
7660 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007661 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007662 }
7663
7664 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007665
7666failed:
7667 vim_free(name);
7668 drop_scope(cctx);
7669 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670}
7671
7672/*
7673 * compile "endfor"
7674 */
7675 static char_u *
7676compile_endfor(char_u *arg, cctx_T *cctx)
7677{
7678 garray_T *instr = &cctx->ctx_instr;
7679 scope_T *scope = cctx->ctx_scope;
7680 forscope_T *forscope;
7681 isn_T *isn;
7682
Bram Moolenaarfa984412021-03-25 22:15:28 +01007683 if (misplaced_cmdmod(cctx))
7684 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 if (scope == NULL || scope->se_type != FOR_SCOPE)
7687 {
7688 emsg(_(e_for));
7689 return NULL;
7690 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007691 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007692 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007693 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
7695 // At end of ":for" scope jump back to the FOR instruction.
7696 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7697
7698 // Fill in the "end" label in the FOR statement so it can jump here
7699 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7700 isn->isn_arg.forloop.for_end = instr->ga_len;
7701
7702 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007703 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704
7705 // Below the ":for" scope drop the "expr" list from the stack.
7706 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7707 return NULL;
7708
7709 vim_free(scope);
7710
7711 return arg;
7712}
7713
7714/*
7715 * compile "while expr"
7716 *
7717 * Produces instructions:
7718 * top: EVAL expr Push result of "expr"
7719 * JUMP_IF_FALSE end jump if false
7720 * ... body ...
7721 * JUMP top Jump back to repeat
7722 * end:
7723 *
7724 */
7725 static char_u *
7726compile_while(char_u *arg, cctx_T *cctx)
7727{
7728 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 scope_T *scope;
7730
7731 scope = new_scope(cctx, WHILE_SCOPE);
7732 if (scope == NULL)
7733 return NULL;
7734
Bram Moolenaara91a7132021-03-25 21:12:15 +01007735 // "endwhile" jumps back here, one before when profiling or using cmdmods
7736 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737
7738 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007739 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007741 if (!ends_excmd2(arg, skipwhite(p)))
7742 {
7743 semsg(_(e_trailing_arg), p);
7744 return NULL;
7745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746
Bram Moolenaar13106602020-10-04 16:06:05 +02007747 if (bool_on_stack(cctx) == FAIL)
7748 return FAIL;
7749
Bram Moolenaara91a7132021-03-25 21:12:15 +01007750 // CMDMOD_REV must come before the jump
7751 generate_undo_cmdmods(cctx);
7752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007753 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007754 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007755 JUMP_IF_FALSE, cctx) == FAIL)
7756 return FAIL;
7757
7758 return p;
7759}
7760
7761/*
7762 * compile "endwhile"
7763 */
7764 static char_u *
7765compile_endwhile(char_u *arg, cctx_T *cctx)
7766{
7767 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007768 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769
Bram Moolenaarfa984412021-03-25 22:15:28 +01007770 if (misplaced_cmdmod(cctx))
7771 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007772 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7773 {
7774 emsg(_(e_while));
7775 return NULL;
7776 }
7777 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007778 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007779
Bram Moolenaarf002a412021-01-24 13:34:18 +01007780#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007781 // count the endwhile before jumping
7782 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007783#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007785 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007786 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787
7788 // Fill in the "end" label in the WHILE statement so it can jump here.
7789 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007790 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7791 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792
7793 vim_free(scope);
7794
7795 return arg;
7796}
7797
7798/*
7799 * compile "continue"
7800 */
7801 static char_u *
7802compile_continue(char_u *arg, cctx_T *cctx)
7803{
7804 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007805 int try_scopes = 0;
7806 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807
7808 for (;;)
7809 {
7810 if (scope == NULL)
7811 {
7812 emsg(_(e_continue));
7813 return NULL;
7814 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007815 if (scope->se_type == FOR_SCOPE)
7816 {
7817 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007819 }
7820 if (scope->se_type == WHILE_SCOPE)
7821 {
7822 loop_label = scope->se_u.se_while.ws_top_label;
7823 break;
7824 }
7825 if (scope->se_type == TRY_SCOPE)
7826 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 scope = scope->se_outer;
7828 }
7829
Bram Moolenaarc150c092021-02-13 15:02:46 +01007830 if (try_scopes > 0)
7831 // Inside one or more try/catch blocks we first need to jump to the
7832 // "finally" or "endtry" to cleanup.
7833 generate_TRYCONT(cctx, try_scopes, loop_label);
7834 else
7835 // Jump back to the FOR or WHILE instruction.
7836 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 return arg;
7839}
7840
7841/*
7842 * compile "break"
7843 */
7844 static char_u *
7845compile_break(char_u *arg, cctx_T *cctx)
7846{
7847 scope_T *scope = cctx->ctx_scope;
7848 endlabel_T **el;
7849
7850 for (;;)
7851 {
7852 if (scope == NULL)
7853 {
7854 emsg(_(e_break));
7855 return NULL;
7856 }
7857 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7858 break;
7859 scope = scope->se_outer;
7860 }
7861
7862 // Jump to the end of the FOR or WHILE loop.
7863 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007864 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007866 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7868 return FAIL;
7869
7870 return arg;
7871}
7872
7873/*
7874 * compile "{" start of block
7875 */
7876 static char_u *
7877compile_block(char_u *arg, cctx_T *cctx)
7878{
7879 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7880 return NULL;
7881 return skipwhite(arg + 1);
7882}
7883
7884/*
7885 * compile end of block: drop one scope
7886 */
7887 static void
7888compile_endblock(cctx_T *cctx)
7889{
7890 scope_T *scope = cctx->ctx_scope;
7891
7892 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007893 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 vim_free(scope);
7895}
7896
7897/*
7898 * compile "try"
7899 * Creates a new scope for the try-endtry, pointing to the first catch and
7900 * finally.
7901 * Creates another scope for the "try" block itself.
7902 * TRY instruction sets up exception handling at runtime.
7903 *
7904 * "try"
7905 * TRY -> catch1, -> finally push trystack entry
7906 * ... try block
7907 * "throw {exception}"
7908 * EVAL {exception}
7909 * THROW create exception
7910 * ... try block
7911 * " catch {expr}"
7912 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007913 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007914 * EVAL {expr}
7915 * MATCH
7916 * JUMP nomatch -> catch2
7917 * CATCH remove exception
7918 * ... catch block
7919 * " catch"
7920 * JUMP -> finally
7921 * catch2: CATCH remove exception
7922 * ... catch block
7923 * " finally"
7924 * finally:
7925 * ... finally block
7926 * " endtry"
7927 * ENDTRY pop trystack entry, may rethrow
7928 */
7929 static char_u *
7930compile_try(char_u *arg, cctx_T *cctx)
7931{
7932 garray_T *instr = &cctx->ctx_instr;
7933 scope_T *try_scope;
7934 scope_T *scope;
7935
Bram Moolenaarfa984412021-03-25 22:15:28 +01007936 if (misplaced_cmdmod(cctx))
7937 return NULL;
7938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939 // scope that holds the jumps that go to catch/finally/endtry
7940 try_scope = new_scope(cctx, TRY_SCOPE);
7941 if (try_scope == NULL)
7942 return NULL;
7943
Bram Moolenaar69f70502021-01-01 16:10:46 +01007944 if (cctx->ctx_skip != SKIP_YES)
7945 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007946 isn_T *isn;
7947
7948 // "try_catch" is set when the first ":catch" is found or when no catch
7949 // is found and ":finally" is found.
7950 // "try_finally" is set when ":finally" is found
7951 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007952 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007953 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7954 return NULL;
7955 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7956 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007957 return NULL;
7958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007959
7960 // scope for the try block itself
7961 scope = new_scope(cctx, BLOCK_SCOPE);
7962 if (scope == NULL)
7963 return NULL;
7964
7965 return arg;
7966}
7967
7968/*
7969 * compile "catch {expr}"
7970 */
7971 static char_u *
7972compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7973{
7974 scope_T *scope = cctx->ctx_scope;
7975 garray_T *instr = &cctx->ctx_instr;
7976 char_u *p;
7977 isn_T *isn;
7978
Bram Moolenaarfa984412021-03-25 22:15:28 +01007979 if (misplaced_cmdmod(cctx))
7980 return NULL;
7981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 // end block scope from :try or :catch
7983 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7984 compile_endblock(cctx);
7985 scope = cctx->ctx_scope;
7986
7987 // Error if not in a :try scope
7988 if (scope == NULL || scope->se_type != TRY_SCOPE)
7989 {
7990 emsg(_(e_catch));
7991 return NULL;
7992 }
7993
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007994 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007996 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007997 return NULL;
7998 }
7999
Bram Moolenaar69f70502021-01-01 16:10:46 +01008000 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008002#ifdef FEAT_PROFILE
8003 // the profile-start should be after the jump
8004 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8005 .isn_type == ISN_PROF_START)
8006 --instr->ga_len;
8007#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01008008 // Jump from end of previous block to :finally or :endtry
8009 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
8010 JUMP_ALWAYS, cctx) == FAIL)
8011 return NULL;
8012
8013 // End :try or :catch scope: set value in ISN_TRY instruction
8014 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008015 if (isn->isn_arg.try.try_ref->try_catch == 0)
8016 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008017 if (scope->se_u.se_try.ts_catch_label != 0)
8018 {
8019 // Previous catch without match jumps here
8020 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
8021 isn->isn_arg.jump.jump_where = instr->ga_len;
8022 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008023#ifdef FEAT_PROFILE
8024 if (cctx->ctx_profiling)
8025 {
8026 // a "throw" that jumps here needs to be counted
8027 generate_instr(cctx, ISN_PROF_END);
8028 // the "catch" is also counted
8029 generate_instr(cctx, ISN_PROF_START);
8030 }
8031#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 }
8033
8034 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02008035 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008037 scope->se_u.se_try.ts_caught_all = TRUE;
8038 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 }
8040 else
8041 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008042 char_u *end;
8043 char_u *pat;
8044 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008045 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008046 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048 // Push v:exception, push {expr} and MATCH
8049 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
8050
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008051 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008052 if (*end != *p)
8053 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008054 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008055 vim_free(tofree);
8056 return FAIL;
8057 }
8058 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008059 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008060 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008061 len = (int)(end - tofree);
8062 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008063 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008064 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008065 if (pat == NULL)
8066 return FAIL;
8067 if (generate_PUSHS(cctx, pat) == FAIL)
8068 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8071 return NULL;
8072
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008073 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8075 return NULL;
8076 }
8077
Bram Moolenaar69f70502021-01-01 16:10:46 +01008078 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 return NULL;
8080
8081 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8082 return NULL;
8083 return p;
8084}
8085
8086 static char_u *
8087compile_finally(char_u *arg, cctx_T *cctx)
8088{
8089 scope_T *scope = cctx->ctx_scope;
8090 garray_T *instr = &cctx->ctx_instr;
8091 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008092 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093
Bram Moolenaarfa984412021-03-25 22:15:28 +01008094 if (misplaced_cmdmod(cctx))
8095 return NULL;
8096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 // end block scope from :try or :catch
8098 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8099 compile_endblock(cctx);
8100 scope = cctx->ctx_scope;
8101
8102 // Error if not in a :try scope
8103 if (scope == NULL || scope->se_type != TRY_SCOPE)
8104 {
8105 emsg(_(e_finally));
8106 return NULL;
8107 }
8108
8109 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008110 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008111 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008112 {
8113 emsg(_(e_finally_dup));
8114 return NULL;
8115 }
8116
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008117 this_instr = instr->ga_len;
8118#ifdef FEAT_PROFILE
8119 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8120 .isn_type == ISN_PROF_START)
8121 // jump to the profile start of the "finally"
8122 --this_instr;
8123#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008124
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008125 // Fill in the "end" label in jumps at the end of the blocks.
8126 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8127 this_instr, cctx);
8128
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008129 // If there is no :catch then an exception jumps to :finally.
8130 if (isn->isn_arg.try.try_ref->try_catch == 0)
8131 isn->isn_arg.try.try_ref->try_catch = this_instr;
8132 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008133 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 {
8135 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008136 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008137 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008138 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008140 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8141 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 // TODO: set index in ts_finally_label jumps
8144
8145 return arg;
8146}
8147
8148 static char_u *
8149compile_endtry(char_u *arg, cctx_T *cctx)
8150{
8151 scope_T *scope = cctx->ctx_scope;
8152 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008153 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154
Bram Moolenaarfa984412021-03-25 22:15:28 +01008155 if (misplaced_cmdmod(cctx))
8156 return NULL;
8157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 // end block scope from :catch or :finally
8159 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8160 compile_endblock(cctx);
8161 scope = cctx->ctx_scope;
8162
8163 // Error if not in a :try scope
8164 if (scope == NULL || scope->se_type != TRY_SCOPE)
8165 {
8166 if (scope == NULL)
8167 emsg(_(e_no_endtry));
8168 else if (scope->se_type == WHILE_SCOPE)
8169 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008170 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 emsg(_(e_endfor));
8172 else
8173 emsg(_(e_endif));
8174 return NULL;
8175 }
8176
Bram Moolenaarc150c092021-02-13 15:02:46 +01008177 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008178 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008179 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008180 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8181 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008182 {
8183 emsg(_(e_missing_catch_or_finally));
8184 return NULL;
8185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008187#ifdef FEAT_PROFILE
8188 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8189 .isn_type == ISN_PROF_START)
8190 // move the profile start after "endtry" so that it's not counted when
8191 // the exception is rethrown.
8192 --instr->ga_len;
8193#endif
8194
Bram Moolenaar69f70502021-01-01 16:10:46 +01008195 // Fill in the "end" label in jumps at the end of the blocks, if not
8196 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008197 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8198 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199
Bram Moolenaar69f70502021-01-01 16:10:46 +01008200 if (scope->se_u.se_try.ts_catch_label != 0)
8201 {
8202 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008203 isn_T *isn = ((isn_T *)instr->ga_data)
8204 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008205 isn->isn_arg.jump.jump_where = instr->ga_len;
8206 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008207 }
8208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 compile_endblock(cctx);
8210
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008211 if (cctx->ctx_skip != SKIP_YES)
8212 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008213 // End :catch or :finally scope: set instruction index in ISN_TRY
8214 // instruction
8215 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008216 if (cctx->ctx_skip != SKIP_YES
8217 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8218 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008219#ifdef FEAT_PROFILE
8220 if (cctx->ctx_profiling)
8221 generate_instr(cctx, ISN_PROF_START);
8222#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 return arg;
8225}
8226
8227/*
8228 * compile "throw {expr}"
8229 */
8230 static char_u *
8231compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8232{
8233 char_u *p = skipwhite(arg);
8234
Bram Moolenaara5565e42020-05-09 15:44:01 +02008235 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008237 if (cctx->ctx_skip == SKIP_YES)
8238 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008239 if (may_generate_2STRING(-1, cctx) == FAIL)
8240 return NULL;
8241 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8242 return NULL;
8243
8244 return p;
8245}
8246
8247/*
8248 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008249 * compile "echomsg expr"
8250 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008251 * compile "execute expr"
8252 */
8253 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008254compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008255{
8256 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008257 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008258 int count = 0;
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008259 int start_ctx_lnum = cctx->ctx_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008260
8261 for (;;)
8262 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008263 if (ends_excmd2(prev, p))
8264 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008265 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008266 return NULL;
8267 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008268 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008269 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008270 }
8271
Bram Moolenaare4984292020-12-13 14:19:25 +01008272 if (count > 0)
8273 {
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008274 long save_lnum = cctx->ctx_lnum;
8275
8276 // Use the line number where the command started.
8277 cctx->ctx_lnum = start_ctx_lnum;
8278
Bram Moolenaare4984292020-12-13 14:19:25 +01008279 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8280 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8281 else if (cmdidx == CMD_execute)
8282 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8283 else if (cmdidx == CMD_echomsg)
8284 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8285 else
8286 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
Bram Moolenaarc70fe462021-04-17 17:59:19 +02008287
8288 cctx->ctx_lnum = save_lnum;
Bram Moolenaare4984292020-12-13 14:19:25 +01008289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008290 return p;
8291}
8292
8293/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008294 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008295 * instruction to compute it and return OK.
8296 * Otherwise return FAIL, the caller must deal with any range.
8297 */
8298 static int
8299compile_variable_range(exarg_T *eap, cctx_T *cctx)
8300{
8301 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8302 char_u *p = skipdigits(eap->cmd);
8303
8304 if (p == range_end)
8305 return FAIL;
8306 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8307}
8308
8309/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008310 * :put r
8311 * :put ={expr}
8312 */
8313 static char_u *
8314compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8315{
8316 char_u *line = arg;
8317 linenr_T lnum;
8318 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008319 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008320
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008321 eap->regname = *line;
8322
8323 if (eap->regname == '=')
8324 {
8325 char_u *p = line + 1;
8326
8327 if (compile_expr0(&p, cctx) == FAIL)
8328 return NULL;
8329 line = p;
8330 }
8331 else if (eap->regname != NUL)
8332 ++line;
8333
Bram Moolenaar08597872020-12-10 19:43:40 +01008334 if (compile_variable_range(eap, cctx) == OK)
8335 {
8336 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8337 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008338 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008339 {
8340 // Either no range or a number.
8341 // "errormsg" will not be set because the range is ADDR_LINES.
8342 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008343 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008344 return NULL;
8345 if (eap->addr_count == 0)
8346 lnum = -1;
8347 else
8348 lnum = eap->line2;
8349 if (above)
8350 --lnum;
8351 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008352
8353 generate_PUT(cctx, eap->regname, lnum);
8354 return line;
8355}
8356
8357/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008358 * A command that is not compiled, execute with legacy code.
8359 */
8360 static char_u *
8361compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8362{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008363 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008364 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008365 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008366
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008367 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008368 goto theend;
8369
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008370 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008371 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008372 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008373 int usefilter = FALSE;
8374
8375 has_expr = argt & (EX_XFILE | EX_EXPAND);
8376
8377 // If the command can be followed by a bar, find the bar and truncate
8378 // it, so that the following command can be compiled.
8379 // The '|' is overwritten with a NUL, it is put back below.
8380 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8381 && *eap->arg == '!')
8382 // :w !filter or :r !filter or :r! filter
8383 usefilter = TRUE;
8384 if ((argt & EX_TRLBAR) && !usefilter)
8385 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008386 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008387 separate_nextcmd(eap);
8388 if (eap->nextcmd != NULL)
8389 nextcmd = eap->nextcmd;
8390 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008391 else if (eap->cmdidx == CMD_wincmd)
8392 {
8393 p = eap->arg;
8394 if (*p != NUL)
8395 ++p;
8396 if (*p == 'g' || *p == Ctrl_G)
8397 ++p;
8398 p = skipwhite(p);
8399 if (*p == '|')
8400 {
8401 *p = NUL;
8402 nextcmd = p + 1;
8403 }
8404 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008405 }
8406
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008407 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8408 {
8409 // expand filename in "syntax include [@group] filename"
8410 has_expr = TRUE;
8411 eap->arg = skipwhite(eap->arg + 7);
8412 if (*eap->arg == '@')
8413 eap->arg = skiptowhite(eap->arg);
8414 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008415
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008416 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8417 && STRLEN(eap->arg) > 4)
8418 {
8419 int delim = *eap->arg;
8420
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008421 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008422 if (*p == delim)
8423 {
8424 eap->arg = p + 1;
8425 has_expr = TRUE;
8426 }
8427 }
8428
Bram Moolenaarecac5912021-01-05 19:23:28 +01008429 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8430 {
8431 // TODO: should only expand when appropriate for the command
8432 eap->arg = skiptowhite(eap->arg);
8433 has_expr = TRUE;
8434 }
8435
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008436 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008437 {
8438 int count = 0;
8439 char_u *start = skipwhite(line);
8440
8441 // :cmd xxx`=expr1`yyy`=expr2`zzz
8442 // PUSHS ":cmd xxx"
8443 // eval expr1
8444 // PUSHS "yyy"
8445 // eval expr2
8446 // PUSHS "zzz"
8447 // EXECCONCAT 5
8448 for (;;)
8449 {
8450 if (p > start)
8451 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008452 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008453 ++count;
8454 }
8455 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008456 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008457 return NULL;
8458 may_generate_2STRING(-1, cctx);
8459 ++count;
8460 p = skipwhite(p);
8461 if (*p != '`')
8462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008463 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008464 return NULL;
8465 }
8466 start = p + 1;
8467
8468 p = (char_u *)strstr((char *)start, "`=");
8469 if (p == NULL)
8470 {
8471 if (*skipwhite(start) != NUL)
8472 {
8473 generate_PUSHS(cctx, vim_strsave(start));
8474 ++count;
8475 }
8476 break;
8477 }
8478 }
8479 generate_EXECCONCAT(cctx, count);
8480 }
8481 else
8482 generate_EXEC(cctx, line);
8483
8484theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008485 if (*nextcmd != NUL)
8486 {
8487 // the parser expects a pointer to the bar, put it back
8488 --nextcmd;
8489 *nextcmd = '|';
8490 }
8491
8492 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008493}
8494
8495/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02008496 * :s/pat/repl/
8497 */
8498 static char_u *
8499compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
8500{
8501 char_u *cmd = eap->arg;
8502 char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
8503
8504 if (expr != NULL)
8505 {
8506 int delimiter = *cmd++;
8507
8508 // There is a \=expr, find it in the substitute part.
8509 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(),
8510 NULL, NULL, NULL);
8511 if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
8512 {
8513 int instr_count = cctx->ctx_instr.ga_len;
8514 char_u *end;
8515
8516 cmd += 3;
8517 end = skip_substitute(cmd, delimiter);
8518
8519 compile_expr0(&cmd, cctx);
8520 if (end[-1] == NUL)
8521 end[-1] = delimiter;
8522 cmd = skipwhite(cmd);
8523 if (*cmd != delimiter && *cmd != NUL)
8524 {
8525 semsg(_(e_trailing_arg), cmd);
8526 return NULL;
8527 }
8528
8529 if (generate_substitute(arg, instr_count, cctx) == FAIL)
8530 return NULL;
8531
8532 // skip over flags
8533 if (*end == '&')
8534 ++end;
8535 while (ASCII_ISALPHA(*end) || *end == '#')
8536 ++end;
8537 return end;
8538 }
8539 }
8540
8541 return compile_exec(arg, eap, cctx);
8542}
8543
8544/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008545 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008546 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008547 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008548 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008549add_def_function(ufunc_T *ufunc)
8550{
8551 dfunc_T *dfunc;
8552
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008553 if (def_functions.ga_len == 0)
8554 {
8555 // The first position is not used, so that a zero uf_dfunc_idx means it
8556 // wasn't set.
8557 if (ga_grow(&def_functions, 1) == FAIL)
8558 return FAIL;
8559 ++def_functions.ga_len;
8560 }
8561
Bram Moolenaar09689a02020-05-09 22:50:08 +02008562 // Add the function to "def_functions".
8563 if (ga_grow(&def_functions, 1) == FAIL)
8564 return FAIL;
8565 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8566 CLEAR_POINTER(dfunc);
8567 dfunc->df_idx = def_functions.ga_len;
8568 ufunc->uf_dfunc_idx = dfunc->df_idx;
8569 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008570 dfunc->df_name = vim_strsave(ufunc->uf_name);
8571 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008572 ++def_functions.ga_len;
8573 return OK;
8574}
8575
8576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008577 * After ex_function() has collected all the function lines: parse and compile
8578 * the lines into instructions.
8579 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008580 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8581 * the return statement (used for lambda). When uf_ret_type is already set
8582 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008583 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008584 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008585 * This can be used recursively through compile_lambda(), which may reallocate
8586 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008587 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008589 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008590compile_def_function(
8591 ufunc_T *ufunc,
8592 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008593 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008594 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008595{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 char_u *line = NULL;
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008597 char_u *line_to_free = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008598 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008600 cctx_T cctx;
8601 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008602 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008603 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008604 int ret = FAIL;
8605 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008606 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008607 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008608 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008609#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008610 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008611#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008613 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008614 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008615 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008617 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8618 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008619 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008621 else
8622 {
8623 if (add_def_function(ufunc) == FAIL)
8624 return FAIL;
8625 new_def_function = TRUE;
8626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008627
Bram Moolenaar985116a2020-07-12 17:31:09 +02008628 ufunc->uf_def_status = UF_COMPILING;
8629
Bram Moolenaara80faa82020-04-12 19:37:17 +02008630 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008631
Bram Moolenaarf002a412021-01-24 13:34:18 +01008632#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008633 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008634#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 cctx.ctx_ufunc = ufunc;
8636 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008637 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8639 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8640 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8641 cctx.ctx_type_list = &ufunc->uf_type_list;
8642 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8643 instr = &cctx.ctx_instr;
8644
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008645 // Set the context to the function, it may be compiled when called from
8646 // another script. Set the script version to the most modern one.
8647 // The line number will be set in next_line_from_context().
8648 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8650
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008651 // Make sure error messages are OK.
8652 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8653 if (do_estack_push)
8654 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008655 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008656
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008657 if (ufunc->uf_def_args.ga_len > 0)
8658 {
8659 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008660 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008661 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008662 int i;
8663 char_u *arg;
8664 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008665 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008666
8667 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008668 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008669 for (i = 0; i < count; ++i)
8670 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008671 garray_T *stack = &cctx.ctx_type_stack;
8672 type_T *val_type;
8673 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008674 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008675 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008676 int jump_instr_idx = instr->ga_len;
8677 isn_T *isn;
8678
8679 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8680 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8681 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008682
8683 // Make sure later arguments are not found.
8684 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008685
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008686 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008687 r = compile_expr0(&arg, &cctx);
8688
8689 ufunc->uf_args.ga_len = uf_args_len;
8690 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008691 goto erret;
8692
8693 // If no type specified use the type of the default value.
8694 // Otherwise check that the default value type matches the
8695 // specified type.
8696 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008697 where.wt_index = arg_idx + 1;
8698 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008699 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008700 {
8701 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008702 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008703 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008704 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008705 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008706 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008707
8708 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008709 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008710
8711 // set instruction index in JUMP_IF_ARG_SET to here
8712 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8713 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008714 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008715
8716 if (did_set_arg_type)
8717 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008718 }
8719
8720 /*
8721 * Loop over all the lines of the function and generate instructions.
8722 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 for (;;)
8724 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008725 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008726 int starts_with_colon = FALSE;
8727 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008728 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008729
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008730 // Bail out on the first error to avoid a flood of errors and report
8731 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008732 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008733 goto erret;
8734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 if (line != NULL && *line == '|')
8736 // the line continues after a '|'
8737 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008738 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008739 && !(*line == '#' && (line == cctx.ctx_line_start
8740 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008742 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743 goto erret;
8744 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008745 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8746 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 else
8748 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008749 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008750 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008751 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008752 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008753#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008754 if (cctx.ctx_skip != SKIP_YES)
8755 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008756#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008757 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008758 }
Bram Moolenaarf62d7392021-04-14 12:40:00 +02008759 // Make a copy, splitting off nextcmd and removing trailing spaces
8760 // may change it.
8761 if (line != NULL)
8762 {
8763 line = vim_strsave(line);
8764 vim_free(line_to_free);
8765 line_to_free = line;
8766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008767 }
8768
Bram Moolenaara80faa82020-04-12 19:37:17 +02008769 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 ea.cmdlinep = &line;
8771 ea.cmd = skipwhite(line);
8772
Bram Moolenaarb2049902021-01-24 12:53:53 +01008773 if (*ea.cmd == '#')
8774 {
8775 // "#" starts a comment
8776 line = (char_u *)"";
8777 continue;
8778 }
8779
Bram Moolenaarf002a412021-01-24 13:34:18 +01008780#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008781 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8782 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008783 {
8784 may_generate_prof_end(&cctx, prof_lnum);
8785
8786 prof_lnum = cctx.ctx_lnum;
8787 generate_instr(&cctx, ISN_PROF_START);
8788 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008789#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008790
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008791 // Some things can be recognized by the first character.
8792 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008794 case '}':
8795 {
8796 // "}" ends a block scope
8797 scopetype_T stype = cctx.ctx_scope == NULL
8798 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008800 if (stype == BLOCK_SCOPE)
8801 {
8802 compile_endblock(&cctx);
8803 line = ea.cmd;
8804 }
8805 else
8806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008807 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008808 goto erret;
8809 }
8810 if (line != NULL)
8811 line = skipwhite(ea.cmd + 1);
8812 continue;
8813 }
8814
8815 case '{':
8816 // "{" starts a block scope
8817 // "{'a': 1}->func() is something else
8818 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8819 {
8820 line = compile_block(ea.cmd, &cctx);
8821 continue;
8822 }
8823 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824 }
8825
8826 /*
8827 * COMMAND MODIFIERS
8828 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008829 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008830 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8831 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008832 {
8833 if (errormsg != NULL)
8834 goto erret;
8835 // empty line or comment
8836 line = (char_u *)"";
8837 continue;
8838 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008839 generate_cmdmods(&cctx, &local_cmdmod);
8840 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008842 // Check if there was a colon after the last command modifier or before
8843 // the current position.
8844 for (p = ea.cmd; p >= line; --p)
8845 {
8846 if (*p == ':')
8847 starts_with_colon = TRUE;
8848 if (p < ea.cmd && !VIM_ISWHITE(*p))
8849 break;
8850 }
8851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008853 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008854 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008855 {
8856 if (*ea.cmd == '(')
8857 // not for "call()"
8858 ea.cmd = p;
8859 else
8860 ea.cmd = skipwhite(ea.cmd);
8861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008862
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008863 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008864 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008865 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008866
Bram Moolenaar17126b12021-01-07 22:03:02 +01008867 // Check for assignment after command modifiers.
8868 assign = may_compile_assignment(&ea, &line, &cctx);
8869 if (assign == OK)
8870 goto nextline;
8871 if (assign == FAIL)
8872 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008873 }
8874
8875 /*
8876 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008877 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008879 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008880 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008881 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008882 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008883 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008884 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008885 if (!starts_with_colon)
8886 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008887 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008888 goto erret;
8889 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008890 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008891 if (ends_excmd2(line, ea.cmd))
8892 {
8893 // A range without a command: jump to the line.
8894 // TODO: compile to a more efficient command, possibly
8895 // calling parse_cmd_address().
8896 ea.cmdidx = CMD_SIZE;
8897 line = compile_exec(line, &ea, &cctx);
8898 goto nextline;
8899 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008900 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008901 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008902 p = find_ex_command(&ea, NULL, starts_with_colon
8903 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008905 if (p == NULL)
8906 {
8907 if (cctx.ctx_skip != SKIP_YES)
8908 emsg(_(e_ambiguous_use_of_user_defined_command));
8909 goto erret;
8910 }
8911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8913 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008914 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008915 {
8916 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008917 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008918 }
8919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008920 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008921 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008923 // CMD_var cannot happen, compile_assignment() above would be
8924 // used. Most likely an assignment to a non-existing variable.
8925 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008926 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008928 }
8929
Bram Moolenaar3988f642020-08-27 22:43:03 +02008930 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008931 && ea.cmdidx != CMD_elseif
8932 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008933 && ea.cmdidx != CMD_endif
8934 && ea.cmdidx != CMD_endfor
8935 && ea.cmdidx != CMD_endwhile
8936 && ea.cmdidx != CMD_catch
8937 && ea.cmdidx != CMD_finally
8938 && ea.cmdidx != CMD_endtry)
8939 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008940 emsg(_(e_unreachable_code_after_return));
8941 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008942 }
8943
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008944 p = skipwhite(p);
8945 if (ea.cmdidx != CMD_SIZE
8946 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8947 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008948 if (ea.cmdidx >= 0)
8949 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008950 if ((ea.argt & EX_BANG) && *p == '!')
8951 {
8952 ea.forceit = TRUE;
8953 p = skipwhite(p + 1);
8954 }
8955 }
8956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 switch (ea.cmdidx)
8958 {
8959 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008960 ea.arg = p;
8961 line = compile_nested_function(&ea, &cctx);
8962 break;
8963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008964 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008965 // TODO: should we allow this, e.g. to declare a global
8966 // function?
8967 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008968 goto erret;
8969
8970 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008971 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008972 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008973 break;
8974
8975 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008976 emsg(_(e_cannot_use_let_in_vim9_script));
8977 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008978 case CMD_var:
8979 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008980 case CMD_const:
8981 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008982 if (line == p)
8983 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008984 break;
8985
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008986 case CMD_unlet:
8987 case CMD_unlockvar:
8988 case CMD_lockvar:
8989 line = compile_unletlock(p, &ea, &cctx);
8990 break;
8991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008992 case CMD_import:
8993 line = compile_import(p, &cctx);
8994 break;
8995
8996 case CMD_if:
8997 line = compile_if(p, &cctx);
8998 break;
8999 case CMD_elseif:
9000 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009001 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009002 break;
9003 case CMD_else:
9004 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009005 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009006 break;
9007 case CMD_endif:
9008 line = compile_endif(p, &cctx);
9009 break;
9010
9011 case CMD_while:
9012 line = compile_while(p, &cctx);
9013 break;
9014 case CMD_endwhile:
9015 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009016 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009017 break;
9018
9019 case CMD_for:
9020 line = compile_for(p, &cctx);
9021 break;
9022 case CMD_endfor:
9023 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009024 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009025 break;
9026 case CMD_continue:
9027 line = compile_continue(p, &cctx);
9028 break;
9029 case CMD_break:
9030 line = compile_break(p, &cctx);
9031 break;
9032
9033 case CMD_try:
9034 line = compile_try(p, &cctx);
9035 break;
9036 case CMD_catch:
9037 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009038 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009039 break;
9040 case CMD_finally:
9041 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009042 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009043 break;
9044 case CMD_endtry:
9045 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02009046 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047 break;
9048 case CMD_throw:
9049 line = compile_throw(p, &cctx);
9050 break;
9051
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009052 case CMD_eval:
9053 if (compile_expr0(&p, &cctx) == FAIL)
9054 goto erret;
9055
Bram Moolenaar3988f642020-08-27 22:43:03 +02009056 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02009057 generate_instr_drop(&cctx, ISN_DROP, 1);
9058
9059 line = skipwhite(p);
9060 break;
9061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009062 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009063 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01009064 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009065 case CMD_echomsg:
9066 case CMD_echoerr:
9067 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01009068 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009069
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009070 case CMD_put:
9071 ea.cmd = cmd;
9072 line = compile_put(p, &ea, &cctx);
9073 break;
9074
Bram Moolenaar4c137212021-04-19 16:48:48 +02009075 case CMD_substitute:
9076 if (cctx.ctx_skip == SKIP_YES)
9077 line = (char_u *)"";
9078 else
9079 {
9080 ea.arg = p;
9081 line = compile_substitute(line, &ea, &cctx);
9082 }
9083 break;
9084
Bram Moolenaar3988f642020-08-27 22:43:03 +02009085 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02009086
Bram Moolenaarae616492020-07-28 20:07:27 +02009087 case CMD_append:
9088 case CMD_change:
9089 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01009090 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02009091 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02009092 case CMD_xit:
9093 not_in_vim9(&ea);
9094 goto erret;
9095
Bram Moolenaar002262f2020-07-08 17:47:57 +02009096 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02009097 if (cctx.ctx_skip != SKIP_YES)
9098 {
9099 semsg(_(e_invalid_command_str), ea.cmd);
9100 goto erret;
9101 }
9102 // We don't check for a next command here.
9103 line = (char_u *)"";
9104 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02009105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009106 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02009107 if (cctx.ctx_skip == SKIP_YES)
9108 {
9109 // We don't check for a next command here.
9110 line = (char_u *)"";
9111 }
9112 else
9113 {
9114 // Not recognized, execute with do_cmdline_cmd().
9115 ea.arg = p;
9116 line = compile_exec(line, &ea, &cctx);
9117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009118 break;
9119 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02009120nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009121 if (line == NULL)
9122 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02009123 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009124
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009125 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02009126 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009128 if (cctx.ctx_type_stack.ga_len < 0)
9129 {
9130 iemsg("Type stack underflow");
9131 goto erret;
9132 }
9133 }
9134
9135 if (cctx.ctx_scope != NULL)
9136 {
9137 if (cctx.ctx_scope->se_type == IF_SCOPE)
9138 emsg(_(e_endif));
9139 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9140 emsg(_(e_endwhile));
9141 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9142 emsg(_(e_endfor));
9143 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009144 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009145 goto erret;
9146 }
9147
Bram Moolenaarefd88552020-06-18 20:50:10 +02009148 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 {
9150 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9151 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009152 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009153 goto erret;
9154 }
9155
9156 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009157 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009158 }
9159
Bram Moolenaar599410c2021-04-10 14:03:43 +02009160 // When compiled with ":silent!" and there was an error don't consider the
9161 // function compiled.
9162 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009163 {
9164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9165 + ufunc->uf_dfunc_idx;
9166 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009167 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009168#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009169 if (cctx.ctx_profiling)
9170 {
9171 dfunc->df_instr_prof = instr->ga_data;
9172 dfunc->df_instr_prof_count = instr->ga_len;
9173 }
9174 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009175#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009176 {
9177 dfunc->df_instr = instr->ga_data;
9178 dfunc->df_instr_count = instr->ga_len;
9179 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009180 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009181 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009182 if (cctx.ctx_outer_used)
9183 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009184 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009186
9187 ret = OK;
9188
9189erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009190 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009191 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009192 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009193 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9194 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009195
9196 for (idx = 0; idx < instr->ga_len; ++idx)
9197 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009198 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009199 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009200
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009201 // If using the last entry in the table and it was added above, we
9202 // might as well remove it.
9203 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009204 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009205 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009206 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009207 ufunc->uf_dfunc_idx = 0;
9208 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009209 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009210
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009211 while (cctx.ctx_scope != NULL)
9212 drop_scope(&cctx);
9213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 if (errormsg != NULL)
9215 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009216 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009217 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009218 }
9219
9220 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009221 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009222 if (do_estack_push)
9223 estack_pop();
9224
Bram Moolenaarf62d7392021-04-14 12:40:00 +02009225 vim_free(line_to_free);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009226 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009227 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009229 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009230}
9231
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009232 void
9233set_function_type(ufunc_T *ufunc)
9234{
9235 int varargs = ufunc->uf_va_name != NULL;
9236 int argcount = ufunc->uf_args.ga_len;
9237
9238 // Create a type for the function, with the return type and any
9239 // argument types.
9240 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9241 // The type is included in "tt_args".
9242 if (argcount > 0 || varargs)
9243 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009244 if (ufunc->uf_type_list.ga_itemsize == 0)
9245 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009246 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9247 argcount, &ufunc->uf_type_list);
9248 // Add argument types to the function type.
9249 if (func_type_add_arg_types(ufunc->uf_func_type,
9250 argcount + varargs,
9251 &ufunc->uf_type_list) == FAIL)
9252 return;
9253 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9254 ufunc->uf_func_type->tt_min_argcount =
9255 argcount - ufunc->uf_def_args.ga_len;
9256 if (ufunc->uf_arg_types == NULL)
9257 {
9258 int i;
9259
9260 // lambda does not have argument types.
9261 for (i = 0; i < argcount; ++i)
9262 ufunc->uf_func_type->tt_args[i] = &t_any;
9263 }
9264 else
9265 mch_memmove(ufunc->uf_func_type->tt_args,
9266 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9267 if (varargs)
9268 {
9269 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009270 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009271 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9272 }
9273 }
9274 else
9275 // No arguments, can use a predefined type.
9276 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9277 argcount, &ufunc->uf_type_list);
9278}
9279
9280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281/*
9282 * Delete an instruction, free what it contains.
9283 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009284 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285delete_instr(isn_T *isn)
9286{
9287 switch (isn->isn_type)
9288 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009289 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009291 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009292 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009293 case ISN_LOADENV:
9294 case ISN_LOADG:
9295 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009296 case ISN_LOADT:
9297 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009298 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009299 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009300 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009301 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009302 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009303 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009304 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009305 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009306 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009307 case ISN_STOREW:
9308 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009309 vim_free(isn->isn_arg.string);
9310 break;
9311
Bram Moolenaar4c137212021-04-19 16:48:48 +02009312 case ISN_SUBSTITUTE:
9313 vim_free(isn->isn_arg.subs.subs_cmd);
9314 vim_free(isn->isn_arg.subs.subs_instr);
9315 break;
9316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009317 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009318 case ISN_STORES:
9319 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 break;
9321
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009322 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009323 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009324 vim_free(isn->isn_arg.unlet.ul_name);
9325 break;
9326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009327 case ISN_STOREOPT:
9328 vim_free(isn->isn_arg.storeopt.so_name);
9329 break;
9330
9331 case ISN_PUSHBLOB: // push blob isn_arg.blob
9332 blob_unref(isn->isn_arg.blob);
9333 break;
9334
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009335 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009336#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009337 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009338#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009339 break;
9340
9341 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009342#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009343 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009344#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009345 break;
9346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009347 case ISN_UCALL:
9348 vim_free(isn->isn_arg.ufunc.cuf_name);
9349 break;
9350
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009351 case ISN_FUNCREF:
9352 {
9353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9354 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009355 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009356
Bram Moolenaar077a4232020-12-22 18:33:27 +01009357 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9358 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009359 }
9360 break;
9361
9362 case ISN_DCALL:
9363 {
9364 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9365 + isn->isn_arg.dfunc.cdf_idx;
9366
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009367 if (dfunc->df_ufunc != NULL
9368 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009369 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009370 }
9371 break;
9372
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009373 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009374 {
9375 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9376 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9377
9378 if (ufunc != NULL)
9379 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009380 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009381 func_ptr_unref(ufunc);
9382 }
9383
9384 vim_free(lambda);
9385 vim_free(isn->isn_arg.newfunc.nf_global);
9386 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009387 break;
9388
Bram Moolenaar5e654232020-09-16 15:22:00 +02009389 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009390 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009391 free_type(isn->isn_arg.type.ct_type);
9392 break;
9393
Bram Moolenaar02194d22020-10-24 23:08:38 +02009394 case ISN_CMDMOD:
9395 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9396 ->cmod_filter_regmatch.regprog);
9397 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9398 break;
9399
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009400 case ISN_LOADSCRIPT:
9401 case ISN_STORESCRIPT:
9402 vim_free(isn->isn_arg.script.scriptref);
9403 break;
9404
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009405 case ISN_TRY:
9406 vim_free(isn->isn_arg.try.try_ref);
9407 break;
9408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009409 case ISN_2BOOL:
9410 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009411 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009412 case ISN_ADDBLOB:
9413 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009414 case ISN_ANYINDEX:
9415 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009416 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009417 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009418 case ISN_BLOBINDEX:
9419 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009420 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009421 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009422 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009423 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009424 case ISN_COMPAREANY:
9425 case ISN_COMPAREBLOB:
9426 case ISN_COMPAREBOOL:
9427 case ISN_COMPAREDICT:
9428 case ISN_COMPAREFLOAT:
9429 case ISN_COMPAREFUNC:
9430 case ISN_COMPARELIST:
9431 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009432 case ISN_COMPARESPECIAL:
9433 case ISN_COMPARESTRING:
9434 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009435 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009436 case ISN_DROP:
9437 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009438 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009439 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009440 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009441 case ISN_EXECCONCAT:
9442 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009443 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009444 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009445 case ISN_GETITEM:
9446 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009447 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009448 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009449 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009450 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009451 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009452 case ISN_LOADBDICT:
9453 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009454 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009455 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009456 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009457 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009458 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009459 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009460 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009461 case ISN_NEGATENR:
9462 case ISN_NEWDICT:
9463 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009464 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009465 case ISN_OPFLOAT:
9466 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009467 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009468 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009469 case ISN_PROF_END:
9470 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009471 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009472 case ISN_PUSHF:
9473 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009474 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009475 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009476 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009477 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009478 case ISN_SHUFFLE:
9479 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009480 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009481 case ISN_STOREINDEX:
Bram Moolenaar68452172021-04-12 21:21:02 +02009482 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009483 case ISN_STORENR:
9484 case ISN_STOREOUTER:
9485 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009486 case ISN_STOREV:
9487 case ISN_STRINDEX:
9488 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009489 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009490 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009491 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009492 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009493 case ISN_UNPACK:
Bram Moolenaar4c137212021-04-19 16:48:48 +02009494 case ISN_FINISH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009495 // nothing allocated
9496 break;
9497 }
9498}
9499
9500/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009501 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009502 */
9503 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009504delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009505{
9506 int idx;
9507
9508 ga_clear(&dfunc->df_def_args_isn);
9509
9510 if (dfunc->df_instr != NULL)
9511 {
9512 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9513 delete_instr(dfunc->df_instr + idx);
9514 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009515 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009516 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009517#ifdef FEAT_PROFILE
9518 if (dfunc->df_instr_prof != NULL)
9519 {
9520 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9521 delete_instr(dfunc->df_instr_prof + idx);
9522 VIM_CLEAR(dfunc->df_instr_prof);
9523 dfunc->df_instr_prof = NULL;
9524 }
9525#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009526
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009527 if (mark_deleted)
9528 dfunc->df_deleted = TRUE;
9529 if (dfunc->df_ufunc != NULL)
9530 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009531}
9532
9533/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009534 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009535 * function, unless another user function still uses it.
9536 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009537 */
9538 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009539unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009540{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009541 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009542 {
9543 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9544 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009545
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009546 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009547 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009548 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009549 ufunc->uf_dfunc_idx = 0;
9550 if (dfunc->df_ufunc == ufunc)
9551 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009552 }
9553}
9554
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009555/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009556 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009557 */
9558 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009559link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009560{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009561 if (ufunc->uf_dfunc_idx > 0)
9562 {
9563 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9564 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009565
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009566 ++dfunc->df_refcount;
9567 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009568}
9569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009570#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009571/*
9572 * Free all functions defined with ":def".
9573 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009574 void
9575free_def_functions(void)
9576{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009577 int idx;
9578
9579 for (idx = 0; idx < def_functions.ga_len; ++idx)
9580 {
9581 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9582
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009583 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009584 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009585 }
9586
9587 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009588}
9589#endif
9590
9591
9592#endif // FEAT_EVAL