blob: 4a55f1973baaf785a565516f7caa76a5ec9517dd [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 Moolenaar08597872020-12-10 19:43:40 +01002133/*
2134 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2135 */
2136 static int
2137generate_RANGE(cctx_T *cctx, char_u *range)
2138{
2139 isn_T *isn;
2140 garray_T *stack = &cctx->ctx_type_stack;
2141
2142 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2143 return FAIL;
2144 isn->isn_arg.string = range;
2145
2146 if (ga_grow(stack, 1) == FAIL)
2147 return FAIL;
2148 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2149 ++stack->ga_len;
2150 return OK;
2151}
2152
Bram Moolenaar792f7862020-11-23 08:31:18 +01002153 static int
2154generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2155{
2156 isn_T *isn;
2157
2158 RETURN_OK_IF_SKIP(cctx);
2159 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2160 return FAIL;
2161 isn->isn_arg.unpack.unp_count = var_count;
2162 isn->isn_arg.unpack.unp_semicolon = semicolon;
2163 return OK;
2164}
2165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002167 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 */
2169 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002170generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171{
2172 isn_T *isn;
2173
Bram Moolenaarfa984412021-03-25 22:15:28 +01002174 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 cctx->ctx_has_cmdmod = TRUE;
2177
2178 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002179 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002180 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2181 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2182 return FAIL;
2183 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002184 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002185 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002186 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002187
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002188 return OK;
2189}
2190
2191 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002192generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002194 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2195 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002196 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002197 return OK;
2198}
2199
Bram Moolenaarfa984412021-03-25 22:15:28 +01002200 static int
2201misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002202{
2203 garray_T *instr = &cctx->ctx_instr;
2204
Bram Moolenaara91a7132021-03-25 21:12:15 +01002205 if (cctx->ctx_has_cmdmod
2206 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2207 == ISN_CMDMOD)
2208 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002209 emsg(_(e_misplaced_command_modifier));
2210 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002211 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002212 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002213}
2214
2215/*
2216 * Get the index of the current instruction.
2217 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2218 */
2219 static int
2220current_instr_idx(cctx_T *cctx)
2221{
2222 garray_T *instr = &cctx->ctx_instr;
2223 int idx = instr->ga_len;
2224
2225 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_CMDMOD)
2227 --idx;
2228#ifdef FEAT_PROFILE
2229 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2230 .isn_type == ISN_PROF_START)
2231 --idx;
2232#endif
2233 return idx;
2234}
2235
Bram Moolenaarf002a412021-01-24 13:34:18 +01002236#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 static void
2238may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2239{
2240 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002241 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002242}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002243#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002244
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002247 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002249 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002250reserve_local(
2251 cctx_T *cctx,
2252 char_u *name,
2253 size_t len,
2254 int isConst,
2255 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 lvar_T *lvar;
2258
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002259 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002261 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 }
2264
2265 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 return NULL;
2267 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002268 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002270 // Every local variable uses the next entry on the stack. We could re-use
2271 // the last ones when leaving a scope, but then variables used in a closure
2272 // might get overwritten. To keep things simple do not re-use stack
2273 // entries. This is less efficient, but memory is cheap these days.
2274 lvar->lv_idx = cctx->ctx_locals_count++;
2275
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002276 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 lvar->lv_const = isConst;
2278 lvar->lv_type = type;
2279
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002280 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281}
2282
2283/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002284 * Remove local variables above "new_top".
2285 */
2286 static void
2287unwind_locals(cctx_T *cctx, int new_top)
2288{
2289 if (cctx->ctx_locals.ga_len > new_top)
2290 {
2291 int idx;
2292 lvar_T *lvar;
2293
2294 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2295 {
2296 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2297 vim_free(lvar->lv_name);
2298 }
2299 }
2300 cctx->ctx_locals.ga_len = new_top;
2301}
2302
2303/*
2304 * Free all local variables.
2305 */
2306 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002307free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002308{
2309 unwind_locals(cctx, 0);
2310 ga_clear(&cctx->ctx_locals);
2311}
2312
2313/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002314 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2315 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2316 * error if the variable was defined with :const.
2317 */
2318 static int
2319check_item_writable(svar_T *sv, int check_writable, char_u *name)
2320{
2321 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2322 || (check_writable == ASSIGN_FINAL
2323 && sv->sv_const == ASSIGN_CONST))
2324 {
2325 semsg(_(e_readonlyvar), name);
2326 return FAIL;
2327 }
2328 return OK;
2329}
2330
2331/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002333 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 * Returns the index in "sn_var_vals" if found.
2335 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002336 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 */
2338 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002339get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340{
2341 hashtab_T *ht;
2342 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002343 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002344 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 int idx;
2346
Bram Moolenaare3d46852020-08-29 13:39:17 +02002347 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002349 if (sid == current_sctx.sc_sid)
2350 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002351 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002352
2353 if (sav == NULL)
2354 return -2;
2355 idx = sav->sav_var_vals_idx;
2356 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002357 if (check_item_writable(sv, check_writable, name) == FAIL)
2358 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002359 return idx;
2360 }
2361
2362 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 ht = &SCRIPT_VARS(sid);
2364 di = find_var_in_ht(ht, 0, name, TRUE);
2365 if (di == NULL)
2366 return -2;
2367
2368 // Now find the svar_T index in sn_var_vals.
2369 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2370 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002371 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (sv->sv_tv == &di->di_tv)
2373 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002374 if (check_item_writable(sv, check_writable, name) == FAIL)
2375 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 return idx;
2377 }
2378 }
2379 return -1;
2380}
2381
2382/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002383 * Find "name" in imported items of the current script or in "cctx" if not
2384 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 */
2386 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002387find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 int idx;
2390
Bram Moolenaare3d46852020-08-29 13:39:17 +02002391 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002392 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 if (cctx != NULL)
2394 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2395 {
2396 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2397 + idx;
2398
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002399 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2400 : STRLEN(import->imp_name) == len
2401 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return import;
2403 }
2404
Bram Moolenaarefa94442020-08-08 22:16:00 +02002405 return find_imported_in_script(name, len, current_sctx.sc_sid);
2406}
2407
2408 imported_T *
2409find_imported_in_script(char_u *name, size_t len, int sid)
2410{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002411 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002412 int idx;
2413
Bram Moolenaare3d46852020-08-29 13:39:17 +02002414 if (!SCRIPT_ID_VALID(sid))
2415 return NULL;
2416 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2418 {
2419 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2420
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002421 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2422 : STRLEN(import->imp_name) == len
2423 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 return import;
2425 }
2426 return NULL;
2427}
2428
2429/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002430 * Free all imported variables.
2431 */
2432 static void
2433free_imported(cctx_T *cctx)
2434{
2435 int idx;
2436
2437 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2438 {
2439 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2440
2441 vim_free(import->imp_name);
2442 }
2443 ga_clear(&cctx->ctx_imports);
2444}
2445
2446/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002447 * Return a pointer to the next line that isn't empty or only contains a
2448 * comment. Skips over white space.
2449 * Returns NULL if there is none.
2450 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002451 char_u *
2452peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002453{
2454 int lnum = cctx->ctx_lnum;
2455
2456 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2457 {
2458 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002459 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002460
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002461 // ignore NULLs inserted for continuation lines
2462 if (line != NULL)
2463 {
2464 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002465 if (vim9_bad_comment(p))
2466 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002467 if (*p != NUL && !vim9_comment_start(p))
2468 return p;
2469 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002470 }
2471 return NULL;
2472}
2473
2474/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002475 * Called when checking for a following operator at "arg". When the rest of
2476 * the line is empty or only a comment, peek the next line. If there is a next
2477 * line return a pointer to it and set "nextp".
2478 * Otherwise skip over white space.
2479 */
2480 static char_u *
2481may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2482{
2483 char_u *p = skipwhite(arg);
2484
2485 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002486 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002487 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002488 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002489 if (*nextp != NULL)
2490 return *nextp;
2491 }
2492 return p;
2493}
2494
2495/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498 * Returns NULL when at the end.
2499 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002500 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002501next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002502{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002503 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002504
2505 do
2506 {
2507 ++cctx->ctx_lnum;
2508 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002509 {
2510 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002511 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002512 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002513 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002514 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002515 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002516 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002517 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002518 return line;
2519}
2520
2521/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002522 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002523 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2526 */
2527 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002529{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002530 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002531 if (vim9_bad_comment(*arg))
2532 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002533 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002536
2537 if (next == NULL)
2538 return FAIL;
2539 *arg = skipwhite(next);
2540 }
2541 return OK;
2542}
2543
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002544/*
2545 * Idem, and give an error when failed.
2546 */
2547 static int
2548may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2549{
2550 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2551 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002552 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002553 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002554 return FAIL;
2555 }
2556 return OK;
2557}
2558
2559
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560// Structure passed between the compile_expr* functions to keep track of
2561// constants that have been parsed but for which no code was produced yet. If
2562// possible expressions on these constants are applied at compile time. If
2563// that is not possible, the code to push the constants needs to be generated
2564// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002565// Using 50 should be more than enough of 5 levels of ().
2566#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002567typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002568 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002569 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570 int pp_is_const; // all generated code was constants, used for a
2571 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002572} ppconst_T;
2573
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002574static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002575static int compile_expr0(char_u **arg, cctx_T *cctx);
2576static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578/*
2579 * Generate a PUSH instruction for "tv".
2580 * "tv" will be consumed or cleared.
2581 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2582 */
2583 static int
2584generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2585{
2586 if (tv != NULL)
2587 {
2588 switch (tv->v_type)
2589 {
2590 case VAR_UNKNOWN:
2591 break;
2592 case VAR_BOOL:
2593 generate_PUSHBOOL(cctx, tv->vval.v_number);
2594 break;
2595 case VAR_SPECIAL:
2596 generate_PUSHSPEC(cctx, tv->vval.v_number);
2597 break;
2598 case VAR_NUMBER:
2599 generate_PUSHNR(cctx, tv->vval.v_number);
2600 break;
2601#ifdef FEAT_FLOAT
2602 case VAR_FLOAT:
2603 generate_PUSHF(cctx, tv->vval.v_float);
2604 break;
2605#endif
2606 case VAR_BLOB:
2607 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2608 tv->vval.v_blob = NULL;
2609 break;
2610 case VAR_STRING:
2611 generate_PUSHS(cctx, tv->vval.v_string);
2612 tv->vval.v_string = NULL;
2613 break;
2614 default:
2615 iemsg("constant type not supported");
2616 clear_tv(tv);
2617 return FAIL;
2618 }
2619 tv->v_type = VAR_UNKNOWN;
2620 }
2621 return OK;
2622}
2623
2624/*
2625 * Generate code for any ppconst entries.
2626 */
2627 static int
2628generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2629{
2630 int i;
2631 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002632 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002634 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635 for (i = 0; i < ppconst->pp_used; ++i)
2636 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2637 ret = FAIL;
2638 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002639 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640 return ret;
2641}
2642
2643/*
2644 * Clear ppconst constants. Used when failing.
2645 */
2646 static void
2647clear_ppconst(ppconst_T *ppconst)
2648{
2649 int i;
2650
2651 for (i = 0; i < ppconst->pp_used; ++i)
2652 clear_tv(&ppconst->pp_tv[i]);
2653 ppconst->pp_used = 0;
2654}
2655
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002656/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002657 * Compile getting a member from a list/dict/string/blob. Stack has the
2658 * indexable value and the index.
2659 */
2660 static int
2661compile_member(int is_slice, cctx_T *cctx)
2662{
2663 type_T **typep;
2664 garray_T *stack = &cctx->ctx_type_stack;
2665 vartype_T vtype;
2666 type_T *valtype;
2667
2668 // We can index a list and a dict. If we don't know the type
2669 // we can use the index value type.
2670 // TODO: If we don't know use an instruction to figure it out at
2671 // runtime.
2672 typep = ((type_T **)stack->ga_data) + stack->ga_len
2673 - (is_slice ? 3 : 2);
2674 vtype = (*typep)->tt_type;
2675 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2676 // If the index is a string, the variable must be a Dict.
2677 if (*typep == &t_any && valtype == &t_string)
2678 vtype = VAR_DICT;
2679 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2680 {
2681 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2682 return FAIL;
2683 if (is_slice)
2684 {
2685 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2686 if (need_type(valtype, &t_number, -2, 0, cctx,
2687 FALSE, FALSE) == FAIL)
2688 return FAIL;
2689 }
2690 }
2691
2692 if (vtype == VAR_DICT)
2693 {
2694 if (is_slice)
2695 {
2696 emsg(_(e_cannot_slice_dictionary));
2697 return FAIL;
2698 }
2699 if ((*typep)->tt_type == VAR_DICT)
2700 {
2701 *typep = (*typep)->tt_member;
2702 if (*typep == &t_unknown)
2703 // empty dict was used
2704 *typep = &t_any;
2705 }
2706 else
2707 {
2708 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2709 FALSE, FALSE) == FAIL)
2710 return FAIL;
2711 *typep = &t_any;
2712 }
2713 if (may_generate_2STRING(-1, cctx) == FAIL)
2714 return FAIL;
2715 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2716 return FAIL;
2717 }
2718 else if (vtype == VAR_STRING)
2719 {
2720 *typep = &t_string;
2721 if ((is_slice
2722 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2723 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2724 return FAIL;
2725 }
2726 else if (vtype == VAR_BLOB)
2727 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02002728 if (is_slice)
2729 {
2730 *typep = &t_blob;
2731 if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
2732 return FAIL;
2733 }
2734 else
2735 {
2736 *typep = &t_number;
2737 if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
2738 return FAIL;
2739 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002740 }
2741 else if (vtype == VAR_LIST || *typep == &t_any)
2742 {
2743 if (is_slice)
2744 {
2745 if (generate_instr_drop(cctx,
2746 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2747 2) == FAIL)
2748 return FAIL;
2749 }
2750 else
2751 {
2752 if ((*typep)->tt_type == VAR_LIST)
2753 {
2754 *typep = (*typep)->tt_member;
2755 if (*typep == &t_unknown)
2756 // empty list was used
2757 *typep = &t_any;
2758 }
2759 if (generate_instr_drop(cctx,
2760 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2761 return FAIL;
2762 }
2763 }
2764 else
2765 {
2766 emsg(_(e_string_list_dict_or_blob_required));
2767 return FAIL;
2768 }
2769 return OK;
2770}
2771
2772/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002773 * Generate an instruction to load script-local variable "name", without the
2774 * leading "s:".
2775 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 */
2777 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002778compile_load_scriptvar(
2779 cctx_T *cctx,
2780 char_u *name, // variable NUL terminated
2781 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002782 char_u **end, // end of variable
2783 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002785 scriptitem_T *si;
2786 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 imported_T *import;
2788
Bram Moolenaare3d46852020-08-29 13:39:17 +02002789 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2790 return FAIL;
2791 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002792 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002793 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002795 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002796 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2797 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 }
2799 if (idx >= 0)
2800 {
2801 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2802
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002803 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 current_sctx.sc_sid, idx, sv->sv_type);
2805 return OK;
2806 }
2807
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002808 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 if (import != NULL)
2810 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002811 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002812 {
2813 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002814 char_u *exp_name;
2815 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002816 ufunc_T *ufunc;
2817 type_T *type;
2818
2819 // Used "import * as Name", need to lookup the member.
2820 if (*p != '.')
2821 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002822 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002823 return FAIL;
2824 }
2825 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002826 if (VIM_ISWHITE(*p))
2827 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002828 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002829 return FAIL;
2830 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002831
Bram Moolenaar1c991142020-07-04 13:15:31 +02002832 // isolate one name
2833 exp_name = p;
2834 while (eval_isnamec(*p))
2835 ++p;
2836 cc = *p;
2837 *p = NUL;
2838
Bram Moolenaaredba7072021-03-13 21:14:18 +01002839 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2840 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002841 *p = cc;
2842 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002843 *end = p;
2844
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002845 if (idx < 0)
2846 {
2847 if (*p == '(' && ufunc != NULL)
2848 {
2849 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2850 return OK;
2851 }
2852 return FAIL;
2853 }
2854
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002855 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2856 import->imp_sid,
2857 idx,
2858 type);
2859 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002860 else if (import->imp_funcname != NULL)
2861 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002862 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002863 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2864 import->imp_sid,
2865 import->imp_var_vals_idx,
2866 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 return OK;
2868 }
2869
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002870 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002871 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 return FAIL;
2873}
2874
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002875 static int
2876generate_funcref(cctx_T *cctx, char_u *name)
2877{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002878 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002879
2880 if (ufunc == NULL)
2881 return FAIL;
2882
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002883 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002884 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2885 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002886 == FAIL)
2887 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002888 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002889}
2890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891/*
2892 * Compile a variable name into a load instruction.
2893 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002894 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 * When "error" is FALSE do not give an error when not found.
2896 */
2897 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002898compile_load(
2899 char_u **arg,
2900 char_u *end_arg,
2901 cctx_T *cctx,
2902 int is_expr,
2903 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904{
2905 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002906 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002907 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002909 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910
2911 if (*(*arg + 1) == ':')
2912 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002913 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002914 {
2915 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916
Bram Moolenaarfa596382021-04-07 21:58:16 +02002917 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002918 switch (**arg)
2919 {
2920 case 'g': isn_type = ISN_LOADGDICT; break;
2921 case 'w': isn_type = ISN_LOADWDICT; break;
2922 case 't': isn_type = ISN_LOADTDICT; break;
2923 case 'b': isn_type = ISN_LOADBDICT; break;
2924 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002925 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002926 goto theend;
2927 }
2928 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2929 goto theend;
2930 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 else
2933 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002934 isntype_T isn_type = ISN_DROP;
2935
Bram Moolenaarfa596382021-04-07 21:58:16 +02002936 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2938 if (name == NULL)
2939 return FAIL;
2940
2941 switch (**arg)
2942 {
2943 case 'v': res = generate_LOADV(cctx, name, error);
2944 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002945 case 's': if (is_expr && ASCII_ISUPPER(*name)
2946 && find_func(name, FALSE, cctx) != NULL)
2947 res = generate_funcref(cctx, name);
2948 else
2949 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002950 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002951 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002952 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002953 {
2954 if (is_expr && ASCII_ISUPPER(*name)
2955 && find_func(name, FALSE, cctx) != NULL)
2956 res = generate_funcref(cctx, name);
2957 else
2958 isn_type = ISN_LOADG;
2959 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002960 else
2961 {
2962 isn_type = ISN_LOADAUTO;
2963 vim_free(name);
2964 name = vim_strnsave(*arg, end - *arg);
2965 if (name == NULL)
2966 return FAIL;
2967 }
2968 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002969 case 'w': isn_type = ISN_LOADW; break;
2970 case 't': isn_type = ISN_LOADT; break;
2971 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002972 default: // cannot happen, just in case
2973 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002974 goto theend;
2975 }
2976 if (isn_type != ISN_DROP)
2977 {
2978 // Global, Buffer-local, Window-local and Tabpage-local
2979 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002980 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002981 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 }
2984 }
2985 else
2986 {
2987 size_t len = end - *arg;
2988 int idx;
2989 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002990 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991
2992 name = vim_strnsave(*arg, end - *arg);
2993 if (name == NULL)
2994 return FAIL;
2995
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002996 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002998 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002999 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 }
3001 else
3002 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003003 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003004
Bram Moolenaar709664c2020-12-12 14:33:41 +01003005 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01003007 type = lvar.lv_type;
3008 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01003009 if (lvar.lv_from_outer != 0)
3010 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003011 else
3012 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 }
3014 else
3015 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003016 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003017 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003018 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003019 || find_imported(name, 0, cctx) != NULL)
3020 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003021
Bram Moolenaar0f769812020-09-12 18:32:34 +02003022 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003023 // uppercase letter it can be a user defined function.
3024 // generate_funcref() will fail if the function can't be found.
3025 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003026 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028 }
3029 if (gen_load)
3030 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003031 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003032 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003033 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003034 cctx->ctx_outer_used = TRUE;
3035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 }
3037
3038 *arg = end;
3039
3040theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003041 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003042 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 vim_free(name);
3044 return res;
3045}
3046
3047/*
3048 * Compile the argument expressions.
3049 * "arg" points to just after the "(" and is advanced to after the ")"
3050 */
3051 static int
3052compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3053{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003054 char_u *p = *arg;
3055 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003056 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057
Bram Moolenaare6085c52020-04-12 20:19:16 +02003058 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003060 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3061 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003062 if (*p == ')')
3063 {
3064 *arg = p + 1;
3065 return OK;
3066 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003067 if (must_end)
3068 {
3069 semsg(_(e_missing_comma_before_argument_str), p);
3070 return FAIL;
3071 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003072
Bram Moolenaara5565e42020-05-09 15:44:01 +02003073 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 return FAIL;
3075 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003076
3077 if (*p != ',' && *skipwhite(p) == ',')
3078 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003079 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003080 p = skipwhite(p);
3081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003083 {
3084 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003085 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003086 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003087 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003088 else
3089 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003090 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003091 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003093failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003094 emsg(_(e_missing_close));
3095 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096}
3097
3098/*
3099 * Compile a function call: name(arg1, arg2)
3100 * "arg" points to "name", "arg + varlen" to the "(".
3101 * "argcount_init" is 1 for "value->method()"
3102 * Instructions:
3103 * EVAL arg1
3104 * EVAL arg2
3105 * BCALL / DCALL / UCALL
3106 */
3107 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003108compile_call(
3109 char_u **arg,
3110 size_t varlen,
3111 cctx_T *cctx,
3112 ppconst_T *ppconst,
3113 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114{
3115 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003116 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 int argcount = argcount_init;
3118 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003119 char_u fname_buf[FLEN_FIXED + 1];
3120 char_u *tofree = NULL;
3121 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003122 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003123 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003124 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
Bram Moolenaara5565e42020-05-09 15:44:01 +02003126 // we can evaluate "has('name')" at compile time
3127 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3128 {
3129 char_u *s = skipwhite(*arg + varlen + 1);
3130 typval_T argvars[2];
3131
3132 argvars[0].v_type = VAR_UNKNOWN;
3133 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003134 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003135 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003136 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003137 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003138 if (*s == ')' && argvars[0].v_type == VAR_STRING
3139 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003140 {
3141 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3142
3143 *arg = s + 1;
3144 argvars[1].v_type = VAR_UNKNOWN;
3145 tv->v_type = VAR_NUMBER;
3146 tv->vval.v_number = 0;
3147 f_has(argvars, tv);
3148 clear_tv(&argvars[0]);
3149 ++ppconst->pp_used;
3150 return OK;
3151 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003152 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003153 }
3154
3155 if (generate_ppconst(cctx, ppconst) == FAIL)
3156 return FAIL;
3157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 if (varlen >= sizeof(namebuf))
3159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003160 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 return FAIL;
3162 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003163 vim_strncpy(namebuf, *arg, varlen);
3164 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
3166 *arg = skipwhite(*arg + varlen + 1);
3167 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003168 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169
Bram Moolenaar03290b82020-12-19 16:30:44 +01003170 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003171 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 {
3173 int idx;
3174
3175 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003176 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003178 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003179 if (STRCMP(name, "flatten") == 0)
3180 {
3181 emsg(_(e_cannot_use_flatten_in_vim9_script));
3182 goto theend;
3183 }
3184
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003185 if (STRCMP(name, "add") == 0 && argcount == 2)
3186 {
3187 garray_T *stack = &cctx->ctx_type_stack;
3188 type_T *type = ((type_T **)stack->ga_data)[
3189 stack->ga_len - 2];
3190
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003191 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003192 if (type->tt_type == VAR_LIST)
3193 {
3194 // inline "add(list, item)" so that the type can be checked
3195 res = generate_LISTAPPEND(cctx);
3196 idx = -1;
3197 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003198 else if (type->tt_type == VAR_BLOB)
3199 {
3200 // inline "add(blob, nr)" so that the type can be checked
3201 res = generate_BLOBAPPEND(cctx);
3202 idx = -1;
3203 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003204 }
3205
3206 if (idx >= 0)
3207 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3208 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003209 else
3210 semsg(_(e_unknownfunc), namebuf);
3211 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003214 // An argument or local variable can be a function reference, this
3215 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003216 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003217 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003218 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003219 // If we can find the function by name generate the right call.
3220 // Skip global functions here, a local funcref takes precedence.
3221 ufunc = find_func(name, FALSE, cctx);
3222 if (ufunc != NULL && !func_is_global(ufunc))
3223 {
3224 res = generate_CALL(cctx, ufunc, argcount);
3225 goto theend;
3226 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228
3229 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003230 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003231 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003233 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003234 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003235 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003236 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003237 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003238
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003239 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003240 goto theend;
3241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242
Bram Moolenaar0f769812020-09-12 18:32:34 +02003243 // If we can find a global function by name generate the right call.
3244 if (ufunc != NULL)
3245 {
3246 res = generate_CALL(cctx, ufunc, argcount);
3247 goto theend;
3248 }
3249
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003250 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003251 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003252 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003253 res = generate_UCALL(cctx, name, argcount);
3254 else
3255 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003256
3257theend:
3258 vim_free(tofree);
3259 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260}
3261
3262// like NAMESPACE_CHAR but with 'a' and 'l'.
3263#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3264
3265/*
3266 * Find the end of a variable or function name. Unlike find_name_end() this
3267 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003268 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 * Return a pointer to just after the name. Equal to "arg" if there is no
3270 * valid name.
3271 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003272 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003273to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274{
3275 char_u *p;
3276
3277 // Quick check for valid starting character.
3278 if (!eval_isnamec1(*arg))
3279 return arg;
3280
3281 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3282 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3283 // and can be used in slice "[n:]".
3284 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003285 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3287 break;
3288 return p;
3289}
3290
3291/*
3292 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003293 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 */
3295 char_u *
3296to_name_const_end(char_u *arg)
3297{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003298 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 typval_T rettv;
3300
3301 if (p == arg && *arg == '[')
3302 {
3303
3304 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003305 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 p = arg;
3307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 return p;
3309}
3310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311/*
3312 * parse a list: [expr, expr]
3313 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003314 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 */
3316 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003317compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318{
3319 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003320 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003322 int is_const;
3323 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003325 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003327 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003328 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003329 semsg(_(e_list_end), *arg);
3330 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003331 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003332 if (*p == ',')
3333 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003334 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003335 return FAIL;
3336 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003337 if (*p == ']')
3338 {
3339 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003340 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003341 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003342 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003343 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003344 if (!is_const)
3345 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 ++count;
3347 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003348 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003350 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3351 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003352 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003353 return FAIL;
3354 }
3355 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003356 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 p = skipwhite(p);
3358 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003359 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003361 ppconst->pp_is_const = is_all_const;
3362 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363}
3364
3365/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003366 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003367 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003368 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 */
3370 static int
3371compile_lambda(char_u **arg, cctx_T *cctx)
3372{
Bram Moolenaare462f522020-12-27 14:43:30 +01003373 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 typval_T rettv;
3375 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003376 evalarg_T evalarg;
3377
3378 CLEAR_FIELD(evalarg);
3379 evalarg.eval_flags = EVAL_EVALUATE;
3380 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381
3382 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003383 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3384 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003385 {
3386 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003387 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003388 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003389
Bram Moolenaar65c44152020-12-24 15:14:01 +01003390 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003392 ++ufunc->uf_refcount;
3393 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394
Bram Moolenaar65c44152020-12-24 15:14:01 +01003395 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003396 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003398 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3399 // points into it. Point to the original line to avoid a dangling pointer.
3400 if (evalarg.eval_tofree_cmdline != NULL)
3401 {
3402 size_t off = *arg - evalarg.eval_tofree_cmdline;
3403
3404 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3405 + off;
3406 }
3407
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003408 clear_evalarg(&evalarg, NULL);
3409
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003410 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003411 {
3412 // The return type will now be known.
3413 set_function_type(ufunc);
3414
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003415 // The function reference count will be 1. When the ISN_FUNCREF
3416 // instruction is deleted the reference count is decremented and the
3417 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003418 return generate_FUNCREF(cctx, ufunc);
3419 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003420
3421 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return FAIL;
3423}
3424
3425/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003426 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003428 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 */
3430 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003431compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432{
3433 garray_T *instr = &cctx->ctx_instr;
3434 int count = 0;
3435 dict_T *d = dict_alloc();
3436 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003437 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003438 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003439 int is_const;
3440 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441
3442 if (d == NULL)
3443 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003444 if (generate_ppconst(cctx, ppconst) == FAIL)
3445 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003446 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003448 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449
Bram Moolenaar23c55272020-06-21 16:58:13 +02003450 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003451 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003452 *arg = NULL;
3453 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003454 }
3455
3456 if (**arg == '}')
3457 break;
3458
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003459 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003461 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003463 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003464 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003465 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003468 if (isn->isn_type == ISN_PUSHNR)
3469 {
3470 char buf[NUMBUFLEN];
3471
3472 // Convert to string at compile time.
3473 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3474 isn->isn_type = ISN_PUSHS;
3475 isn->isn_arg.string = vim_strsave((char_u *)buf);
3476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 if (isn->isn_type == ISN_PUSHS)
3478 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003479 else if (may_generate_2STRING(-1, cctx) == FAIL)
3480 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003481 *arg = skipwhite(*arg);
3482 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003483 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003484 emsg(_(e_missing_matching_bracket_after_dict_key));
3485 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003486 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003487 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003489 else
3490 {
3491 // {"name": value},
3492 // {'name': value},
3493 // {name: value} use "name" as a literal key
3494 key = get_literal_key(arg);
3495 if (key == NULL)
3496 return FAIL;
3497 if (generate_PUSHS(cctx, key) == FAIL)
3498 return FAIL;
3499 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500
3501 // Check for duplicate keys, if using string keys.
3502 if (key != NULL)
3503 {
3504 item = dict_find(d, key, -1);
3505 if (item != NULL)
3506 {
3507 semsg(_(e_duplicate_key), key);
3508 goto failret;
3509 }
3510 item = dictitem_alloc(key);
3511 if (item != NULL)
3512 {
3513 item->di_tv.v_type = VAR_UNKNOWN;
3514 item->di_tv.v_lock = 0;
3515 if (dict_add(d, item) == FAIL)
3516 dictitem_free(item);
3517 }
3518 }
3519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 if (**arg != ':')
3521 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003522 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003523 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003524 else
3525 semsg(_(e_missing_dict_colon), *arg);
3526 return FAIL;
3527 }
3528 whitep = *arg + 1;
3529 if (!IS_WHITE_OR_NUL(*whitep))
3530 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003531 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return FAIL;
3533 }
3534
Bram Moolenaar23c55272020-06-21 16:58:13 +02003535 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003536 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003537 *arg = NULL;
3538 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003539 }
3540
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003541 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003543 if (!is_const)
3544 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 ++count;
3546
Bram Moolenaar2c330432020-04-13 14:41:35 +02003547 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003548 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003549 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003550 *arg = NULL;
3551 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 if (**arg == '}')
3554 break;
3555 if (**arg != ',')
3556 {
3557 semsg(_(e_missing_dict_comma), *arg);
3558 goto failret;
3559 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003560 if (IS_WHITE_OR_NUL(*whitep))
3561 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003562 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003563 return FAIL;
3564 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003565 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003566 if (!IS_WHITE_OR_NUL(*whitep))
3567 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003568 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003569 return FAIL;
3570 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003571 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 }
3573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 *arg = *arg + 1;
3575
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003576 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003577 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003578 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003579 *arg += STRLEN(*arg);
3580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003582 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 return generate_NEWDICT(cctx, count);
3584
3585failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003586 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003587 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003588 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003589 *arg = (char_u *)"";
3590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 dict_unref(d);
3592 return FAIL;
3593}
3594
3595/*
3596 * Compile "&option".
3597 */
3598 static int
3599compile_get_option(char_u **arg, cctx_T *cctx)
3600{
3601 typval_T rettv;
3602 char_u *start = *arg;
3603 int ret;
3604
3605 // parse the option and get the current value to get the type.
3606 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003607 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 if (ret == OK)
3609 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003610 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003611 char_u *name = vim_strnsave(start, *arg - start);
3612 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3613 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614
3615 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3616 vim_free(name);
3617 }
3618 clear_tv(&rettv);
3619
3620 return ret;
3621}
3622
3623/*
3624 * Compile "$VAR".
3625 */
3626 static int
3627compile_get_env(char_u **arg, cctx_T *cctx)
3628{
3629 char_u *start = *arg;
3630 int len;
3631 int ret;
3632 char_u *name;
3633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 ++*arg;
3635 len = get_env_len(arg);
3636 if (len == 0)
3637 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003638 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 return FAIL;
3640 }
3641
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003642 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 name = vim_strnsave(start, len + 1);
3644 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3645 vim_free(name);
3646 return ret;
3647}
3648
3649/*
3650 * Compile "@r".
3651 */
3652 static int
3653compile_get_register(char_u **arg, cctx_T *cctx)
3654{
3655 int ret;
3656
3657 ++*arg;
3658 if (**arg == NUL)
3659 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003660 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 return FAIL;
3662 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003663 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 {
3665 emsg_invreg(**arg);
3666 return FAIL;
3667 }
3668 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3669 ++*arg;
3670 return ret;
3671}
3672
3673/*
3674 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003675 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 */
3677 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003678apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003680 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681
3682 // this works from end to start
3683 while (p > start)
3684 {
3685 --p;
3686 if (*p == '-' || *p == '+')
3687 {
3688 // only '-' has an effect, for '+' we only check the type
3689#ifdef FEAT_FLOAT
3690 if (rettv->v_type == VAR_FLOAT)
3691 {
3692 if (*p == '-')
3693 rettv->vval.v_float = -rettv->vval.v_float;
3694 }
3695 else
3696#endif
3697 {
3698 varnumber_T val;
3699 int error = FALSE;
3700
3701 // tv_get_number_chk() accepts a string, but we don't want that
3702 // here
3703 if (check_not_string(rettv) == FAIL)
3704 return FAIL;
3705 val = tv_get_number_chk(rettv, &error);
3706 clear_tv(rettv);
3707 if (error)
3708 return FAIL;
3709 if (*p == '-')
3710 val = -val;
3711 rettv->v_type = VAR_NUMBER;
3712 rettv->vval.v_number = val;
3713 }
3714 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003715 else if (numeric_only)
3716 {
3717 ++p;
3718 break;
3719 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003720 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 {
3722 int v = tv2bool(rettv);
3723
3724 // '!' is permissive in the type.
3725 clear_tv(rettv);
3726 rettv->v_type = VAR_BOOL;
3727 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3728 }
3729 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003730 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 return OK;
3732}
3733
3734/*
3735 * Recognize v: variables that are constants and set "rettv".
3736 */
3737 static void
3738get_vim_constant(char_u **arg, typval_T *rettv)
3739{
3740 if (STRNCMP(*arg, "v:true", 6) == 0)
3741 {
3742 rettv->v_type = VAR_BOOL;
3743 rettv->vval.v_number = VVAL_TRUE;
3744 *arg += 6;
3745 }
3746 else if (STRNCMP(*arg, "v:false", 7) == 0)
3747 {
3748 rettv->v_type = VAR_BOOL;
3749 rettv->vval.v_number = VVAL_FALSE;
3750 *arg += 7;
3751 }
3752 else if (STRNCMP(*arg, "v:null", 6) == 0)
3753 {
3754 rettv->v_type = VAR_SPECIAL;
3755 rettv->vval.v_number = VVAL_NULL;
3756 *arg += 6;
3757 }
3758 else if (STRNCMP(*arg, "v:none", 6) == 0)
3759 {
3760 rettv->v_type = VAR_SPECIAL;
3761 rettv->vval.v_number = VVAL_NONE;
3762 *arg += 6;
3763 }
3764}
3765
Bram Moolenaar657137c2021-01-09 15:45:23 +01003766 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003767get_compare_type(char_u *p, int *len, int *type_is)
3768{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003769 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003770 int i;
3771
3772 switch (p[0])
3773 {
3774 case '=': if (p[1] == '=')
3775 type = EXPR_EQUAL;
3776 else if (p[1] == '~')
3777 type = EXPR_MATCH;
3778 break;
3779 case '!': if (p[1] == '=')
3780 type = EXPR_NEQUAL;
3781 else if (p[1] == '~')
3782 type = EXPR_NOMATCH;
3783 break;
3784 case '>': if (p[1] != '=')
3785 {
3786 type = EXPR_GREATER;
3787 *len = 1;
3788 }
3789 else
3790 type = EXPR_GEQUAL;
3791 break;
3792 case '<': if (p[1] != '=')
3793 {
3794 type = EXPR_SMALLER;
3795 *len = 1;
3796 }
3797 else
3798 type = EXPR_SEQUAL;
3799 break;
3800 case 'i': if (p[1] == 's')
3801 {
3802 // "is" and "isnot"; but not a prefix of a name
3803 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3804 *len = 5;
3805 i = p[*len];
3806 if (!isalnum(i) && i != '_')
3807 {
3808 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3809 *type_is = TRUE;
3810 }
3811 }
3812 break;
3813 }
3814 return type;
3815}
3816
3817/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003818 * Skip over an expression, ignoring most errors.
3819 */
3820 static void
3821skip_expr_cctx(char_u **arg, cctx_T *cctx)
3822{
3823 evalarg_T evalarg;
3824
3825 CLEAR_FIELD(evalarg);
3826 evalarg.eval_cctx = cctx;
3827 skip_expr(arg, &evalarg);
3828}
3829
3830/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003832 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 */
3834 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003835compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003837 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
3839 // this works from end to start
3840 while (p > start)
3841 {
3842 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003843 while (VIM_ISWHITE(*p))
3844 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 if (*p == '-' || *p == '+')
3846 {
3847 int negate = *p == '-';
3848 isn_T *isn;
3849
3850 // TODO: check type
3851 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3852 {
3853 --p;
3854 if (*p == '-')
3855 negate = !negate;
3856 }
3857 // only '-' has an effect, for '+' we only check the type
3858 if (negate)
3859 isn = generate_instr(cctx, ISN_NEGATENR);
3860 else
3861 isn = generate_instr(cctx, ISN_CHECKNR);
3862 if (isn == NULL)
3863 return FAIL;
3864 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003865 else if (numeric_only)
3866 {
3867 ++p;
3868 break;
3869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 else
3871 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003872 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003874 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003876 if (p[-1] == '!')
3877 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
3880 if (generate_2BOOL(cctx, invert) == FAIL)
3881 return FAIL;
3882 }
3883 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003884 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 return OK;
3886}
3887
3888/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003889 * Compile "(expression)": recursive!
3890 * Return FAIL/OK.
3891 */
3892 static int
3893compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3894{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003895 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003896 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003897
Bram Moolenaar24156692021-01-14 20:35:49 +01003898 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3899 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003900 if (ppconst->pp_used <= PPSIZE - 10)
3901 {
3902 ret = compile_expr1(arg, cctx, ppconst);
3903 }
3904 else
3905 {
3906 // Not enough space in ppconst, flush constants.
3907 if (generate_ppconst(cctx, ppconst) == FAIL)
3908 return FAIL;
3909 ret = compile_expr0(arg, cctx);
3910 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003911 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3912 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003913 if (**arg == ')')
3914 ++*arg;
3915 else if (ret == OK)
3916 {
3917 emsg(_(e_missing_close));
3918 ret = FAIL;
3919 }
3920 return ret;
3921}
3922
3923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003925 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 */
3927 static int
3928compile_subscript(
3929 char_u **arg,
3930 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003931 char_u *start_leader,
3932 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003933 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003935 char_u *name_start = *end_leader;
3936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 for (;;)
3938 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003939 char_u *p = skipwhite(*arg);
3940
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003941 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003942 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003943 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003944
3945 // If a following line starts with "->{" or "->X" advance to that
3946 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003947 // Also if a following line starts with ".x".
3948 if (next != NULL &&
3949 ((next[0] == '-' && next[1] == '>'
3950 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003951 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003952 {
3953 next = next_line_from_context(cctx, TRUE);
3954 if (next == NULL)
3955 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003956 *arg = next;
3957 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003958 }
3959 }
3960
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003961 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003962 // not a function call.
3963 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003965 garray_T *stack = &cctx->ctx_type_stack;
3966 type_T *type;
3967 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003969 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003970 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003971 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003974 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3975
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003976 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3978 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003979 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 return FAIL;
3981 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003982 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003984 char_u *pstart = p;
3985
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003986 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003987 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003988 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 // something->method()
3991 // Apply the '!', '-' and '+' first:
3992 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003993 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003996 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003997 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003998 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003999 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01004000 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004001 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004002 garray_T *stack = &cctx->ctx_type_stack;
4003 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004004 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004005 int expr_isn_start = cctx->ctx_instr.ga_len;
4006 int expr_isn_end;
4007 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01004008
4009 // Funcref call: list->(Refs[2])(arg)
4010 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004011 //
4012 // Fist compile the function expression.
4013 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004014 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004015
4016 // Remember the next instruction index, where the instructions
4017 // for arguments are being written.
4018 expr_isn_end = cctx->ctx_instr.ga_len;
4019
4020 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004021 if (**arg != '(')
4022 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004023 if (*skipwhite(*arg) == '(')
4024 emsg(_(e_nowhitespace));
4025 else
4026 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004027 return FAIL;
4028 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004029 *arg = skipwhite(*arg + 1);
4030 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4031 return FAIL;
4032
Bram Moolenaar2927c072021-04-05 19:41:21 +02004033 // Move the instructions for the arguments to before the
4034 // instructions of the expression and move the type of the
4035 // expression after the argument types. This is what ISN_PCALL
4036 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004037 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004038 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4039 if (arg_isn_count > 0)
4040 {
4041 int expr_isn_count = expr_isn_end - expr_isn_start;
4042 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4043
4044 if (isn == NULL)
4045 return FAIL;
4046 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4047 + expr_isn_start,
4048 sizeof(isn_T) * expr_isn_count);
4049 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4050 + expr_isn_start,
4051 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4052 sizeof(isn_T) * arg_isn_count);
4053 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4054 + expr_isn_start + arg_isn_count,
4055 isn, sizeof(isn_T) * expr_isn_count);
4056 vim_free(isn);
4057
4058 type = ((type_T **)stack->ga_data)[type_idx_start];
4059 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4060 ((type_T **)stack->ga_data) + type_idx_start + 1,
4061 sizeof(type_T *)
4062 * (stack->ga_len - type_idx_start - 1));
4063 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4064 }
4065
Bram Moolenaar7e368202020-12-25 21:56:57 +01004066 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4067 if (generate_PCALL(cctx, argcount,
4068 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 return FAIL;
4070 }
4071 else
4072 {
4073 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004074 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004075 if (!eval_isnamec1(*p))
4076 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004077 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004078 return FAIL;
4079 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004080 if (ASCII_ISALPHA(*p) && p[1] == ':')
4081 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004082 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 ;
4084 if (*p != '(')
4085 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004086 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 return FAIL;
4088 }
4089 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004090 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 return FAIL;
4092 }
4093 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004094 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004096 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004097
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004098 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004099 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004100 // string index: text[123]
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004101 // blob index: blob[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004102 // TODO: more arguments
4103 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004104 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004105 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004106 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004107
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004108 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004109 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004110 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004111 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004112 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004113 // missing first index is equal to zero
4114 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004115 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004116 else
4117 {
4118 if (compile_expr0(arg, cctx) == FAIL)
4119 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004120 if (**arg == ':')
4121 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004122 semsg(_(e_white_space_required_before_and_after_str_at_str),
4123 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004124 return FAIL;
4125 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004126 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004127 return FAIL;
4128 *arg = skipwhite(*arg);
4129 }
4130 if (**arg == ':')
4131 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004132 is_slice = TRUE;
4133 ++*arg;
4134 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4135 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004136 semsg(_(e_white_space_required_before_and_after_str_at_str),
4137 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004138 return FAIL;
4139 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004140 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004141 return FAIL;
4142 if (**arg == ']')
4143 // missing second index is equal to end of string
4144 generate_PUSHNR(cctx, -1);
4145 else
4146 {
4147 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004148 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004149 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004150 return FAIL;
4151 *arg = skipwhite(*arg);
4152 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154
4155 if (**arg != ']')
4156 {
4157 emsg(_(e_missbrac));
4158 return FAIL;
4159 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004160 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaare42939a2021-04-05 17:11:17 +02004162 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004163 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004165 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004167 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004168 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004169 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004170 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004171
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004172 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004173 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004174 {
4175 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004176 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004177 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004178 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004179 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 while (eval_isnamec(*p))
4181 MB_PTR_ADV(p);
4182 if (p == *arg)
4183 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004184 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 return FAIL;
4186 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004187 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 return FAIL;
4189 *arg = p;
4190 }
4191 else
4192 break;
4193 }
4194
4195 // TODO - see handle_subscript():
4196 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4197 // Don't do this when "Func" is already a partial that was bound
4198 // explicitly (pt_auto is FALSE).
4199
4200 return OK;
4201}
4202
4203/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004204 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4205 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004207 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004208 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 *
4210 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 */
4212
4213/*
4214 * number number constant
4215 * 0zFFFFFFFF Blob constant
4216 * "string" string constant
4217 * 'string' literal string constant
4218 * &option-name option value
4219 * @r register contents
4220 * identifier variable value
4221 * function() function call
4222 * $VAR environment variable
4223 * (expression) nested expression
4224 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004225 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 *
4227 * Also handle:
4228 * ! in front logical NOT
4229 * - in front unary minus
4230 * + in front unary plus (ignored)
4231 * trailing (arg) funcref/partial call
4232 * trailing [] subscript in String or List
4233 * trailing .name entry in Dictionary
4234 * trailing ->name() method call
4235 */
4236 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004237compile_expr7(
4238 char_u **arg,
4239 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004240 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 char_u *start_leader, *end_leader;
4243 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004244 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004245 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004247 ppconst->pp_is_const = FALSE;
4248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 /*
4250 * Skip '!', '-' and '+' characters. They are handled later.
4251 */
4252 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004253 if (eval_leader(arg, TRUE) == FAIL)
4254 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 end_leader = *arg;
4256
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004257 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 switch (**arg)
4259 {
4260 /*
4261 * Number constant.
4262 */
4263 case '0': // also for blob starting with 0z
4264 case '1':
4265 case '2':
4266 case '3':
4267 case '4':
4268 case '5':
4269 case '6':
4270 case '7':
4271 case '8':
4272 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004273 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004275 // Apply "-" and "+" just before the number now, right to
4276 // left. Matters especially when "->" follows. Stops at
4277 // '!'.
4278 if (apply_leader(rettv, TRUE,
4279 start_leader, &end_leader) == FAIL)
4280 {
4281 clear_tv(rettv);
4282 return FAIL;
4283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 break;
4285
4286 /*
4287 * String constant: "string".
4288 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004289 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 return FAIL;
4291 break;
4292
4293 /*
4294 * Literal string constant: 'str''ing'.
4295 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004296 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 return FAIL;
4298 break;
4299
4300 /*
4301 * Constant Vim variable.
4302 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004303 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 ret = NOTDONE;
4305 break;
4306
4307 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004308 * "true" constant
4309 */
4310 case 't': if (STRNCMP(*arg, "true", 4) == 0
4311 && !eval_isnamec((*arg)[4]))
4312 {
4313 *arg += 4;
4314 rettv->v_type = VAR_BOOL;
4315 rettv->vval.v_number = VVAL_TRUE;
4316 }
4317 else
4318 ret = NOTDONE;
4319 break;
4320
4321 /*
4322 * "false" constant
4323 */
4324 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4325 && !eval_isnamec((*arg)[5]))
4326 {
4327 *arg += 5;
4328 rettv->v_type = VAR_BOOL;
4329 rettv->vval.v_number = VVAL_FALSE;
4330 }
4331 else
4332 ret = NOTDONE;
4333 break;
4334
4335 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004336 * "null" constant
4337 */
4338 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004339 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004340 {
4341 *arg += 4;
4342 rettv->v_type = VAR_SPECIAL;
4343 rettv->vval.v_number = VVAL_NULL;
4344 }
4345 else
4346 ret = NOTDONE;
4347 break;
4348
4349 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 * List: [expr, expr]
4351 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004352 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4353 return FAIL;
4354 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 break;
4356
4357 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 * Dictionary: {'key': val, 'key': val}
4359 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004360 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4361 return FAIL;
4362 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 break;
4364
4365 /*
4366 * Option value: &name
4367 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004368 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4369 return FAIL;
4370 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 break;
4372
4373 /*
4374 * Environment variable: $VAR.
4375 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004376 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4377 return FAIL;
4378 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 break;
4380
4381 /*
4382 * Register contents: @r.
4383 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004384 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4385 return FAIL;
4386 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 break;
4388 /*
4389 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004390 * lambda: (arg, arg) => expr
4391 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004393 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4394 ret = compile_lambda(arg, cctx);
4395 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004396 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 break;
4398
4399 default: ret = NOTDONE;
4400 break;
4401 }
4402 if (ret == FAIL)
4403 return FAIL;
4404
Bram Moolenaar1c747212020-05-09 18:28:34 +02004405 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004407 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004408 clear_tv(rettv);
4409 else
4410 // A constant expression can possibly be handled compile time,
4411 // return the value instead of generating code.
4412 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 }
4414 else if (ret == NOTDONE)
4415 {
4416 char_u *p;
4417 int r;
4418
4419 if (!eval_isnamec1(**arg))
4420 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004421 if (!vim9_bad_comment(*arg))
4422 {
4423 if (ends_excmd(*skipwhite(*arg)))
4424 semsg(_(e_empty_expression_str), *arg);
4425 else
4426 semsg(_(e_name_expected_str), *arg);
4427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 return FAIL;
4429 }
4430
4431 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004432 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004433 if (p - *arg == (size_t)1 && **arg == '_')
4434 {
4435 emsg(_(e_cannot_use_underscore_here));
4436 return FAIL;
4437 }
4438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 {
4441 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 {
4445 if (generate_ppconst(cctx, ppconst) == FAIL)
4446 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004447 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 if (r == FAIL)
4450 return FAIL;
4451 }
4452
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004453 // Handle following "[]", ".member", etc.
4454 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004455 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 ppconst) == FAIL)
4457 return FAIL;
4458 if (ppconst->pp_used > 0)
4459 {
4460 // apply the '!', '-' and '+' before the constant
4461 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004462 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 return FAIL;
4464 return OK;
4465 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004466 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004468 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469}
4470
4471/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004472 * Give the "white on both sides" error, taking the operator from "p[len]".
4473 */
4474 void
4475error_white_both(char_u *op, int len)
4476{
4477 char_u buf[10];
4478
4479 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004480 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004481}
4482
4483/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004484 * <type>expr7: runtime type check / conversion
4485 */
4486 static int
4487compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4488{
4489 type_T *want_type = NULL;
4490
4491 // Recognize <type>
4492 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4493 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004494 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004495 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4496 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004497 return FAIL;
4498
4499 if (**arg != '>')
4500 {
4501 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004502 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004503 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004504 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004505 return FAIL;
4506 }
4507 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004508 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004509 return FAIL;
4510 }
4511
4512 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4513 return FAIL;
4514
4515 if (want_type != NULL)
4516 {
4517 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004518 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004519 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004520
Bram Moolenaard1103582020-08-14 22:44:25 +02004521 generate_ppconst(cctx, ppconst);
4522 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004523 where.wt_index = 0;
4524 where.wt_variable = FALSE;
4525 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004526 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004527 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004528 return FAIL;
4529 }
4530 }
4531
4532 return OK;
4533}
4534
4535/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 * * number multiplication
4537 * / number division
4538 * % number modulo
4539 */
4540 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004541compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542{
4543 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004544 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004545 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004547 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004548 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 return FAIL;
4550
4551 /*
4552 * Repeat computing, until no "*", "/" or "%" is following.
4553 */
4554 for (;;)
4555 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004556 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 if (*op != '*' && *op != '/' && *op != '%')
4558 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004559 if (next != NULL)
4560 {
4561 *arg = next_line_from_context(cctx, TRUE);
4562 op = skipwhite(*arg);
4563 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004564
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004565 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004567 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004568 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004570 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004571 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004573 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004574 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004576
4577 if (ppconst->pp_used == ppconst_used + 2
4578 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4579 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004581 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4582 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4583 varnumber_T res = 0;
4584 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004586 // both are numbers: compute the result
4587 switch (*op)
4588 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004589 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004590 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004591 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004592 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004593 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004594 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004595 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004596 break;
4597 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004598 if (failed)
4599 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004600 tv1->vval.v_number = res;
4601 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004602 }
4603 else
4604 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004605 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004606 generate_two_op(cctx, op);
4607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
4609
4610 return OK;
4611}
4612
4613/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004614 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 * - number subtraction
4616 * .. string concatenation
4617 */
4618 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004619compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620{
4621 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004622 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004624 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625
4626 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 return FAIL;
4629
4630 /*
4631 * Repeat computing, until no "+", "-" or ".." is following.
4632 */
4633 for (;;)
4634 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004635 op = may_peek_next_line(cctx, *arg, &next);
4636 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 break;
4638 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004639 if (next != NULL)
4640 {
4641 *arg = next_line_from_context(cctx, TRUE);
4642 op = skipwhite(*arg);
4643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004645 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004647 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004648 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 }
4650
Bram Moolenaare0de1712020-12-02 17:36:54 +01004651 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004652 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004654 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 return FAIL;
4657
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004659 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4661 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4662 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4663 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4666 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004667
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004668 // concat/subtract/add constant numbers
4669 if (*op == '+')
4670 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4671 else if (*op == '-')
4672 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4673 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004674 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004675 // concatenate constant strings
4676 char_u *s1 = tv1->vval.v_string;
4677 char_u *s2 = tv2->vval.v_string;
4678 size_t len1 = STRLEN(s1);
4679
4680 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4681 if (tv1->vval.v_string == NULL)
4682 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004684 return FAIL;
4685 }
4686 mch_memmove(tv1->vval.v_string, s1, len1);
4687 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004688 vim_free(s1);
4689 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004690 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 }
4693 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004694 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004696 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004697 if (*op == '.')
4698 {
4699 if (may_generate_2STRING(-2, cctx) == FAIL
4700 || may_generate_2STRING(-1, cctx) == FAIL)
4701 return FAIL;
4702 generate_instr_drop(cctx, ISN_CONCAT, 1);
4703 }
4704 else
4705 generate_two_op(cctx, op);
4706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 }
4708
4709 return OK;
4710}
4711
4712/*
4713 * expr5a == expr5b
4714 * expr5a =~ expr5b
4715 * expr5a != expr5b
4716 * expr5a !~ expr5b
4717 * expr5a > expr5b
4718 * expr5a >= expr5b
4719 * expr5a < expr5b
4720 * expr5a <= expr5b
4721 * expr5a is expr5b
4722 * expr5a isnot expr5b
4723 *
4724 * Produces instructions:
4725 * EVAL expr5a Push result of "expr5a"
4726 * EVAL expr5b Push result of "expr5b"
4727 * COMPARE one of the compare instructions
4728 */
4729 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004732 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004734 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004737 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
4739 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 return FAIL;
4742
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004743 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004744 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745
4746 /*
4747 * If there is a comparative operator, use it.
4748 */
4749 if (type != EXPR_UNKNOWN)
4750 {
4751 int ic = FALSE; // Default: do not ignore case
4752
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004753 if (next != NULL)
4754 {
4755 *arg = next_line_from_context(cctx, TRUE);
4756 p = skipwhite(*arg);
4757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 if (type_is && (p[len] == '?' || p[len] == '#'))
4759 {
4760 semsg(_(e_invexpr2), *arg);
4761 return FAIL;
4762 }
4763 // extra question mark appended: ignore case
4764 if (p[len] == '?')
4765 {
4766 ic = TRUE;
4767 ++len;
4768 }
4769 // extra '#' appended: match case (ignored)
4770 else if (p[len] == '#')
4771 ++len;
4772 // nothing appended: match case
4773
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004774 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004776 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 }
4779
4780 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004781 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004782 return FAIL;
4783
Bram Moolenaara5565e42020-05-09 15:44:01 +02004784 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 return FAIL;
4786
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787 if (ppconst->pp_used == ppconst_used + 2)
4788 {
4789 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4790 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4791 int ret;
4792
4793 // Both sides are a constant, compute the result now.
4794 // First check for a valid combination of types, this is more
4795 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004796 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797 ret = FAIL;
4798 else
4799 {
4800 ret = typval_compare(tv1, tv2, type, ic);
4801 tv1->v_type = VAR_BOOL;
4802 tv1->vval.v_number = tv1->vval.v_number
4803 ? VVAL_TRUE : VVAL_FALSE;
4804 clear_tv(tv2);
4805 --ppconst->pp_used;
4806 }
4807 return ret;
4808 }
4809
4810 generate_ppconst(cctx, ppconst);
4811 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 }
4813
4814 return OK;
4815}
4816
Bram Moolenaar7f141552020-05-09 17:35:53 +02004817static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819/*
4820 * Compile || or &&.
4821 */
4822 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004823compile_and_or(
4824 char_u **arg,
4825 cctx_T *cctx,
4826 char *op,
4827 ppconst_T *ppconst,
4828 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004830 char_u *next;
4831 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 int opchar = *op;
4833
4834 if (p[0] == opchar && p[1] == opchar)
4835 {
4836 garray_T *instr = &cctx->ctx_instr;
4837 garray_T end_ga;
4838
4839 /*
4840 * Repeat until there is no following "||" or "&&"
4841 */
4842 ga_init2(&end_ga, sizeof(int), 10);
4843 while (p[0] == opchar && p[1] == opchar)
4844 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004845 long start_lnum = SOURCING_LNUM;
4846 int start_ctx_lnum = cctx->ctx_lnum;
4847 int save_lnum;
4848
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004849 if (next != NULL)
4850 {
4851 *arg = next_line_from_context(cctx, TRUE);
4852 p = skipwhite(*arg);
4853 }
4854
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004855 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4856 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004857 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004858 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004859 return FAIL;
4860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004862 // TODO: use ppconst if the value is a constant and check
4863 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864 generate_ppconst(cctx, ppconst);
4865
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004866 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004867 SOURCING_LNUM = start_lnum;
4868 save_lnum = cctx->ctx_lnum;
4869 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004870 if (bool_on_stack(cctx) == FAIL)
4871 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004872 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004873 ga_clear(&end_ga);
4874 return FAIL;
4875 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004876 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 if (ga_grow(&end_ga, 1) == FAIL)
4879 {
4880 ga_clear(&end_ga);
4881 return FAIL;
4882 }
4883 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4884 ++end_ga.ga_len;
4885 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004886 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
4888 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004889 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004890 {
4891 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004892 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004893 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004894
Bram Moolenaara5565e42020-05-09 15:44:01 +02004895 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4896 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
4898 ga_clear(&end_ga);
4899 return FAIL;
4900 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004901
4902 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004904 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004906 // Every part must evaluate to a bool.
4907 if (bool_on_stack(cctx) == FAIL)
4908 {
4909 ga_clear(&end_ga);
4910 return FAIL;
4911 }
4912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 // Fill in the end label in all jumps.
4914 while (end_ga.ga_len > 0)
4915 {
4916 isn_T *isn;
4917
4918 --end_ga.ga_len;
4919 isn = ((isn_T *)instr->ga_data)
4920 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4921 isn->isn_arg.jump.jump_where = instr->ga_len;
4922 }
4923 ga_clear(&end_ga);
4924 }
4925
4926 return OK;
4927}
4928
4929/*
4930 * expr4a && expr4a && expr4a logical AND
4931 *
4932 * Produces instructions:
4933 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004934 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004935 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004937 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 * EVAL expr4c Push result of "expr4c"
4939 * end:
4940 */
4941 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004942compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004944 int ppconst_used = ppconst->pp_used;
4945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 return FAIL;
4949
4950 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004951 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952}
4953
4954/*
4955 * expr3a || expr3b || expr3c logical OR
4956 *
4957 * Produces instructions:
4958 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004959 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004960 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004962 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 * EVAL expr3c Push result of "expr3c"
4964 * end:
4965 */
4966 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004969 int ppconst_used = ppconst->pp_used;
4970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004972 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 return FAIL;
4974
4975 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004976 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977}
4978
4979/*
4980 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004982 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 * JUMP_IF_FALSE alt jump if false
4984 * EVAL expr1a
4985 * JUMP_ALWAYS end
4986 * alt: EVAL expr1b
4987 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004988 *
4989 * Toplevel expression: expr2 ?? expr1
4990 * Produces instructions:
4991 * EVAL expr2 Push result of "expr2"
4992 * JUMP_AND_KEEP_IF_TRUE end jump if true
4993 * EVAL expr1
4994 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 */
4996 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004997compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998{
4999 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005000 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005001 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002
Bram Moolenaar3988f642020-08-27 22:43:03 +02005003 // Ignore all kinds of errors when not producing code.
5004 if (cctx->ctx_skip == SKIP_YES)
5005 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01005006 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02005007 return OK;
5008 }
5009
Bram Moolenaar61a89812020-05-07 16:58:17 +02005010 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005011 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 return FAIL;
5013
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005014 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 if (*p == '?')
5016 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005017 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 garray_T *instr = &cctx->ctx_instr;
5019 garray_T *stack = &cctx->ctx_type_stack;
5020 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005021 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005023 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005024 int has_const_expr = FALSE;
5025 int const_value = FALSE;
5026 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005028 if (next != NULL)
5029 {
5030 *arg = next_line_from_context(cctx, TRUE);
5031 p = skipwhite(*arg);
5032 }
5033
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005034 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005035 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005036 semsg(_(e_white_space_required_before_and_after_str_at_str),
5037 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005038 return FAIL;
5039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040
Bram Moolenaara5565e42020-05-09 15:44:01 +02005041 if (ppconst->pp_used == ppconst_used + 1)
5042 {
5043 // the condition is a constant, we know whether the ? or the :
5044 // expression is to be evaluated.
5045 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005046 if (op_falsy)
5047 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5048 else
5049 {
5050 int error = FALSE;
5051
5052 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5053 &error);
5054 if (error)
5055 return FAIL;
5056 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005057 cctx->ctx_skip = save_skip == SKIP_YES ||
5058 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5059
5060 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5061 // "left ?? right" and "left" is truthy: produce "left"
5062 generate_ppconst(cctx, ppconst);
5063 else
5064 {
5065 clear_tv(&ppconst->pp_tv[ppconst_used]);
5066 --ppconst->pp_used;
5067 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 }
5069 else
5070 {
5071 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005072 if (op_falsy)
5073 end_idx = instr->ga_len;
5074 generate_JUMP(cctx, op_falsy
5075 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5076 if (op_falsy)
5077 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079
5080 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005081 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005082 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005083 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005084 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085
Bram Moolenaara5565e42020-05-09 15:44:01 +02005086 if (!has_const_expr)
5087 {
5088 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005090 if (!op_falsy)
5091 {
5092 // remember the type and drop it
5093 --stack->ga_len;
5094 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005096 end_idx = instr->ga_len;
5097 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005098
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005099 // jump here from JUMP_IF_FALSE
5100 isn = ((isn_T *)instr->ga_data) + alt_idx;
5101 isn->isn_arg.jump.jump_where = instr->ga_len;
5102 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005105 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005107 // Check for the ":".
5108 p = may_peek_next_line(cctx, *arg, &next);
5109 if (*p != ':')
5110 {
5111 emsg(_(e_missing_colon));
5112 return FAIL;
5113 }
5114 if (next != NULL)
5115 {
5116 *arg = next_line_from_context(cctx, TRUE);
5117 p = skipwhite(*arg);
5118 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005119
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005120 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5121 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005122 semsg(_(e_white_space_required_before_and_after_str_at_str),
5123 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005124 return FAIL;
5125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005127 // evaluate the third expression
5128 if (has_const_expr)
5129 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005130 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005131 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005132 return FAIL;
5133 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5134 return FAIL;
5135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136
Bram Moolenaara5565e42020-05-09 15:44:01 +02005137 if (!has_const_expr)
5138 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005139 type_T **typep;
5140
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005142
Bram Moolenaara5565e42020-05-09 15:44:01 +02005143 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005144 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5145 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005146
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005147 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148 isn = ((isn_T *)instr->ga_data) + end_idx;
5149 isn->isn_arg.jump.jump_where = instr->ga_len;
5150 }
5151
5152 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 }
5154 return OK;
5155}
5156
5157/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005158 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005159 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5160 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005161 */
5162 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005163compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005164{
5165 ppconst_T ppconst;
5166
5167 CLEAR_FIELD(ppconst);
5168 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5169 {
5170 clear_ppconst(&ppconst);
5171 return FAIL;
5172 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005173 if (is_const != NULL)
5174 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175 if (generate_ppconst(cctx, &ppconst) == FAIL)
5176 return FAIL;
5177 return OK;
5178}
5179
5180/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005181 * Toplevel expression.
5182 */
5183 static int
5184compile_expr0(char_u **arg, cctx_T *cctx)
5185{
5186 return compile_expr0_ext(arg, cctx, NULL);
5187}
5188
5189/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 * compile "return [expr]"
5191 */
5192 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005193compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194{
5195 char_u *p = arg;
5196 garray_T *stack = &cctx->ctx_type_stack;
5197 type_T *stack_type;
5198
5199 if (*p != NUL && *p != '|' && *p != '\n')
5200 {
5201 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005202 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 return NULL;
5204
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005205 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005206 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005207 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005208 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005209 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5210 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005211 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005212 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005213 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005214 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005215 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005216 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5217 && stack_type->tt_type != VAR_VOID
5218 && stack_type->tt_type != VAR_UNKNOWN)
5219 {
5220 emsg(_(e_returning_value_in_function_without_return_type));
5221 return NULL;
5222 }
5223 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005224 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005225 return NULL;
5226 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 }
5229 else
5230 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005231 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005232 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005233 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5234 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005236 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 return NULL;
5238 }
5239
5240 // No argument, return zero.
5241 generate_PUSHNR(cctx, 0);
5242 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005243
5244 // Undo any command modifiers.
5245 generate_undo_cmdmods(cctx);
5246
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005247 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 return NULL;
5249
5250 // "return val | endif" is possible
5251 return skipwhite(p);
5252}
5253
5254/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005255 * Get a line from the compilation context, compatible with exarg_T getline().
5256 * Return a pointer to the line in allocated memory.
5257 * Return NULL for end-of-file or some error.
5258 */
5259 static char_u *
5260exarg_getline(
5261 int c UNUSED,
5262 void *cookie,
5263 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005264 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005265{
5266 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005267 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268
Bram Moolenaar66250c92020-08-20 15:02:42 +02005269 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005270 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005271 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005272 return NULL;
5273 ++cctx->ctx_lnum;
5274 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5275 // Comment lines result in NULL pointers, skip them.
5276 if (p != NULL)
5277 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005278 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005279}
5280
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005281 void
5282fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5283{
5284 eap->getline = exarg_getline;
5285 eap->cookie = cctx;
5286}
5287
Bram Moolenaar04b12692020-05-04 23:24:44 +02005288/*
5289 * Compile a nested :def command.
5290 */
5291 static char_u *
5292compile_nested_function(exarg_T *eap, cctx_T *cctx)
5293{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005294 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005295 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005296 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005297 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005298 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005299 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005300
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005301 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005302 {
5303 emsg(_(e_cannot_use_bang_with_nested_def));
5304 return NULL;
5305 }
5306
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005307 if (*name_start == '/')
5308 {
5309 name_end = skip_regexp(name_start + 1, '/', TRUE);
5310 if (*name_end == '/')
5311 ++name_end;
5312 eap->nextcmd = check_nextcmd(name_end);
5313 }
5314 if (name_end == name_start || *skipwhite(name_end) != '(')
5315 {
5316 if (!ends_excmd2(name_start, name_end))
5317 {
5318 semsg(_(e_invalid_command_str), eap->cmd);
5319 return NULL;
5320 }
5321
5322 // "def" or "def Name": list functions
5323 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5324 return NULL;
5325 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5326 }
5327
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005328 // Only g:Func() can use a namespace.
5329 if (name_start[1] == ':' && !is_global)
5330 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005331 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005332 return NULL;
5333 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005334 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005335 return NULL;
5336
Bram Moolenaar04b12692020-05-04 23:24:44 +02005337 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005338 fill_exarg_from_cctx(eap, cctx);
5339
Bram Moolenaar04b12692020-05-04 23:24:44 +02005340 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005341 lambda_name = vim_strsave(get_lambda_name());
5342 if (lambda_name == NULL)
5343 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005344 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005345
Bram Moolenaar822ba242020-05-24 23:00:18 +02005346 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005347 {
5348 r = eap->skip ? OK : FAIL;
5349 goto theend;
5350 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005351
5352 // copy over the block scope IDs before compiling
5353 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5354 {
5355 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5356
5357 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5358 if (ufunc->uf_block_ids != NULL)
5359 {
5360 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5361 sizeof(int) * block_depth);
5362 ufunc->uf_block_depth = block_depth;
5363 }
5364 }
5365
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005366 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5367 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005368 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005369 {
5370 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005371 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005372 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005373
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005374 if (is_global)
5375 {
5376 char_u *func_name = vim_strnsave(name_start + 2,
5377 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005378
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005379 if (func_name == NULL)
5380 r = FAIL;
5381 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005382 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005383 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005384 lambda_name = NULL;
5385 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005386 }
5387 else
5388 {
5389 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005390 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005391 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005392
Bram Moolenaareef21022020-08-01 22:16:43 +02005393 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005394 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005395 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005396 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005397 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5398 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005399 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005400
5401theend:
5402 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005403 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005404}
5405
5406/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 * Return the length of an assignment operator, or zero if there isn't one.
5408 */
5409 int
5410assignment_len(char_u *p, int *heredoc)
5411{
5412 if (*p == '=')
5413 {
5414 if (p[1] == '<' && p[2] == '<')
5415 {
5416 *heredoc = TRUE;
5417 return 3;
5418 }
5419 return 1;
5420 }
5421 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5422 return 2;
5423 if (STRNCMP(p, "..=", 3) == 0)
5424 return 3;
5425 return 0;
5426}
5427
5428// words that cannot be used as a variable
5429static char *reserved[] = {
5430 "true",
5431 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005432 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 NULL
5434};
5435
Bram Moolenaar752fc692021-01-04 21:57:11 +01005436// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005437typedef enum {
5438 dest_local,
5439 dest_option,
5440 dest_env,
5441 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005442 dest_buffer,
5443 dest_window,
5444 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005445 dest_vimvar,
5446 dest_script,
5447 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005448 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005449} assign_dest_T;
5450
Bram Moolenaar752fc692021-01-04 21:57:11 +01005451// Used by compile_lhs() to store information about the LHS of an assignment
5452// and one argument of ":unlet" with an index.
5453typedef struct {
5454 assign_dest_T lhs_dest; // type of destination
5455
5456 char_u *lhs_name; // allocated name including
5457 // "[expr]" or ".name".
5458 size_t lhs_varlen; // length of the variable without
5459 // "[expr]" or ".name"
5460 char_u *lhs_dest_end; // end of the destination, including
5461 // "[expr]" or ".name".
5462
5463 int lhs_has_index; // has "[expr]" or ".name"
5464
5465 int lhs_new_local; // create new local variable
5466 int lhs_opt_flags; // for when destination is an option
5467 int lhs_vimvaridx; // for when destination is a v:var
5468
5469 lvar_T lhs_local_lvar; // used for existing local destination
5470 lvar_T lhs_arg_lvar; // used for argument destination
5471 lvar_T *lhs_lvar; // points to destination lvar
5472 int lhs_scriptvar_sid;
5473 int lhs_scriptvar_idx;
5474
5475 int lhs_has_type; // type was specified
5476 type_T *lhs_type;
5477 type_T *lhs_member_type;
5478} lhs_T;
5479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005481 * Generate the load instruction for "name".
5482 */
5483 static void
5484generate_loadvar(
5485 cctx_T *cctx,
5486 assign_dest_T dest,
5487 char_u *name,
5488 lvar_T *lvar,
5489 type_T *type)
5490{
5491 switch (dest)
5492 {
5493 case dest_option:
5494 // TODO: check the option exists
5495 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5496 break;
5497 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005498 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5499 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5500 else
5501 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005502 break;
5503 case dest_buffer:
5504 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5505 break;
5506 case dest_window:
5507 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5508 break;
5509 case dest_tab:
5510 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5511 break;
5512 case dest_script:
5513 compile_load_scriptvar(cctx,
5514 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5515 break;
5516 case dest_env:
5517 // Include $ in the name here
5518 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5519 break;
5520 case dest_reg:
5521 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5522 break;
5523 case dest_vimvar:
5524 generate_LOADV(cctx, name + 2, TRUE);
5525 break;
5526 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005527 if (lvar->lv_from_outer > 0)
5528 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5529 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005530 else
5531 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5532 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005533 case dest_expr:
5534 // list or dict value should already be on the stack.
5535 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005536 }
5537}
5538
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005539/*
5540 * Skip over "[expr]" or ".member".
5541 * Does not check for any errors.
5542 */
5543 static char_u *
5544skip_index(char_u *start)
5545{
5546 char_u *p = start;
5547
5548 if (*p == '[')
5549 {
5550 p = skipwhite(p + 1);
5551 (void)skip_expr(&p, NULL);
5552 p = skipwhite(p);
5553 if (*p == ']')
5554 return p + 1;
5555 return p;
5556 }
5557 // if (*p == '.')
5558 return to_name_end(p + 1, TRUE);
5559}
5560
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005561 void
5562vim9_declare_error(char_u *name)
5563{
5564 char *scope = "";
5565
5566 switch (*name)
5567 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005568 case 'g': scope = _("global"); break;
5569 case 'b': scope = _("buffer"); break;
5570 case 'w': scope = _("window"); break;
5571 case 't': scope = _("tab"); break;
5572 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005573 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005574 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005575 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005576 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005577 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005578 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005579 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005580 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005581 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005582}
5583
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005584/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005585 * For one assignment figure out the type of destination. Return it in "dest".
5586 * When not recognized "dest" is not set.
5587 * For an option "opt_flags" is set.
5588 * For a v:var "vimvaridx" is set.
5589 * "type" is set to the destination type if known, unchanted otherwise.
5590 * Return FAIL if an error message was given.
5591 */
5592 static int
5593get_var_dest(
5594 char_u *name,
5595 assign_dest_T *dest,
5596 int cmdidx,
5597 int *opt_flags,
5598 int *vimvaridx,
5599 type_T **type,
5600 cctx_T *cctx)
5601{
5602 char_u *p;
5603
5604 if (*name == '&')
5605 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005606 int cc;
5607 long numval;
5608 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005609
5610 *dest = dest_option;
5611 if (cmdidx == CMD_final || cmdidx == CMD_const)
5612 {
5613 emsg(_(e_const_option));
5614 return FAIL;
5615 }
5616 p = name;
5617 p = find_option_end(&p, opt_flags);
5618 if (p == NULL)
5619 {
5620 // cannot happen?
5621 emsg(_(e_letunexp));
5622 return FAIL;
5623 }
5624 cc = *p;
5625 *p = NUL;
5626 opt_type = get_option_value(skip_option_env_lead(name),
5627 &numval, NULL, *opt_flags);
5628 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005629 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005630 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005631 case gov_unknown:
5632 semsg(_(e_unknown_option), name);
5633 return FAIL;
5634 case gov_string:
5635 case gov_hidden_string:
5636 *type = &t_string;
5637 break;
5638 case gov_bool:
5639 case gov_hidden_bool:
5640 *type = &t_bool;
5641 break;
5642 case gov_number:
5643 case gov_hidden_number:
5644 *type = &t_number;
5645 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005646 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005647 }
5648 else if (*name == '$')
5649 {
5650 *dest = dest_env;
5651 *type = &t_string;
5652 }
5653 else if (*name == '@')
5654 {
5655 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5656 {
5657 emsg_invreg(name[1]);
5658 return FAIL;
5659 }
5660 *dest = dest_reg;
5661 *type = &t_string;
5662 }
5663 else if (STRNCMP(name, "g:", 2) == 0)
5664 {
5665 *dest = dest_global;
5666 }
5667 else if (STRNCMP(name, "b:", 2) == 0)
5668 {
5669 *dest = dest_buffer;
5670 }
5671 else if (STRNCMP(name, "w:", 2) == 0)
5672 {
5673 *dest = dest_window;
5674 }
5675 else if (STRNCMP(name, "t:", 2) == 0)
5676 {
5677 *dest = dest_tab;
5678 }
5679 else if (STRNCMP(name, "v:", 2) == 0)
5680 {
5681 typval_T *vtv;
5682 int di_flags;
5683
5684 *vimvaridx = find_vim_var(name + 2, &di_flags);
5685 if (*vimvaridx < 0)
5686 {
5687 semsg(_(e_variable_not_found_str), name);
5688 return FAIL;
5689 }
5690 // We use the current value of "sandbox" here, is that OK?
5691 if (var_check_ro(di_flags, name, FALSE))
5692 return FAIL;
5693 *dest = dest_vimvar;
5694 vtv = get_vim_var_tv(*vimvaridx);
5695 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5696 }
5697 return OK;
5698}
5699
5700/*
5701 * Generate a STORE instruction for "dest", not being "dest_local".
5702 * Return FAIL when out of memory.
5703 */
5704 static int
5705generate_store_var(
5706 cctx_T *cctx,
5707 assign_dest_T dest,
5708 int opt_flags,
5709 int vimvaridx,
5710 int scriptvar_idx,
5711 int scriptvar_sid,
5712 type_T *type,
5713 char_u *name)
5714{
5715 switch (dest)
5716 {
5717 case dest_option:
5718 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5719 opt_flags);
5720 case dest_global:
5721 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005722 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5723 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005724 case dest_buffer:
5725 // include b: with the name, easier to execute that way
5726 return generate_STORE(cctx, ISN_STOREB, 0, name);
5727 case dest_window:
5728 // include w: with the name, easier to execute that way
5729 return generate_STORE(cctx, ISN_STOREW, 0, name);
5730 case dest_tab:
5731 // include t: with the name, easier to execute that way
5732 return generate_STORE(cctx, ISN_STORET, 0, name);
5733 case dest_env:
5734 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5735 case dest_reg:
5736 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5737 case dest_vimvar:
5738 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5739 case dest_script:
5740 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005741 // "s:" may be included in the name.
5742 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005743 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005744 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5745 scriptvar_sid, scriptvar_idx, type);
5746 case dest_local:
5747 case dest_expr:
5748 // cannot happen
5749 break;
5750 }
5751 return FAIL;
5752}
5753
Bram Moolenaar752fc692021-01-04 21:57:11 +01005754 static int
5755is_decl_command(int cmdidx)
5756{
5757 return cmdidx == CMD_let || cmdidx == CMD_var
5758 || cmdidx == CMD_final || cmdidx == CMD_const;
5759}
5760
5761/*
5762 * Figure out the LHS type and other properties for an assignment or one item
5763 * of ":unlet" with an index.
5764 * Returns OK or FAIL.
5765 */
5766 static int
5767compile_lhs(
5768 char_u *var_start,
5769 lhs_T *lhs,
5770 int cmdidx,
5771 int heredoc,
5772 int oplen,
5773 cctx_T *cctx)
5774{
5775 char_u *var_end;
5776 int is_decl = is_decl_command(cmdidx);
5777
5778 CLEAR_POINTER(lhs);
5779 lhs->lhs_dest = dest_local;
5780 lhs->lhs_vimvaridx = -1;
5781 lhs->lhs_scriptvar_idx = -1;
5782
5783 // "dest_end" is the end of the destination, including "[expr]" or
5784 // ".name".
5785 // "var_end" is the end of the variable/option/etc. name.
5786 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5787 if (*var_start == '@')
5788 var_end = var_start + 2;
5789 else
5790 {
5791 // skip over the leading "&", "&l:", "&g:" and "$"
5792 var_end = skip_option_env_lead(var_start);
5793 var_end = to_name_end(var_end, TRUE);
5794 }
5795
5796 // "a: type" is declaring variable "a" with a type, not dict "a:".
5797 if (is_decl && lhs->lhs_dest_end == var_start + 2
5798 && lhs->lhs_dest_end[-1] == ':')
5799 --lhs->lhs_dest_end;
5800 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5801 --var_end;
5802
5803 // compute the length of the destination without "[expr]" or ".name"
5804 lhs->lhs_varlen = var_end - var_start;
5805 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5806 if (lhs->lhs_name == NULL)
5807 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005808
5809 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5810 // Something follows after the variable: "var[idx]" or "var.key".
5811 lhs->lhs_has_index = TRUE;
5812
Bram Moolenaar752fc692021-01-04 21:57:11 +01005813 if (heredoc)
5814 lhs->lhs_type = &t_list_string;
5815 else
5816 lhs->lhs_type = &t_any;
5817
5818 if (cctx->ctx_skip != SKIP_YES)
5819 {
5820 int declare_error = FALSE;
5821
5822 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5823 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5824 &lhs->lhs_type, cctx) == FAIL)
5825 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005826 if (lhs->lhs_dest != dest_local
5827 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005828 {
5829 // Specific kind of variable recognized.
5830 declare_error = is_decl;
5831 }
5832 else
5833 {
5834 int idx;
5835
5836 // No specific kind of variable recognized, just a name.
5837 for (idx = 0; reserved[idx] != NULL; ++idx)
5838 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5839 {
5840 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5841 return FAIL;
5842 }
5843
5844
5845 if (lookup_local(var_start, lhs->lhs_varlen,
5846 &lhs->lhs_local_lvar, cctx) == OK)
5847 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5848 else
5849 {
5850 CLEAR_FIELD(lhs->lhs_arg_lvar);
5851 if (arg_exists(var_start, lhs->lhs_varlen,
5852 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5853 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5854 {
5855 if (is_decl)
5856 {
5857 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5858 return FAIL;
5859 }
5860 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5861 }
5862 }
5863 if (lhs->lhs_lvar != NULL)
5864 {
5865 if (is_decl)
5866 {
5867 semsg(_(e_variable_already_declared), lhs->lhs_name);
5868 return FAIL;
5869 }
5870 }
5871 else
5872 {
5873 int script_namespace = lhs->lhs_varlen > 1
5874 && STRNCMP(var_start, "s:", 2) == 0;
5875 int script_var = (script_namespace
5876 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005877 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005878 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005879 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005880 imported_T *import =
5881 find_imported(var_start, lhs->lhs_varlen, cctx);
5882
5883 if (script_namespace || script_var || import != NULL)
5884 {
5885 char_u *rawname = lhs->lhs_name
5886 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5887
5888 if (is_decl)
5889 {
5890 if (script_namespace)
5891 semsg(_(e_cannot_declare_script_variable_in_function),
5892 lhs->lhs_name);
5893 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005894 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005895 lhs->lhs_name);
5896 return FAIL;
5897 }
5898 else if (cctx->ctx_ufunc->uf_script_ctx_version
5899 == SCRIPT_VERSION_VIM9
5900 && script_namespace
5901 && !script_var && import == NULL)
5902 {
5903 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5904 return FAIL;
5905 }
5906
5907 lhs->lhs_dest = dest_script;
5908
5909 // existing script-local variables should have a type
5910 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5911 if (import != NULL)
5912 lhs->lhs_scriptvar_sid = import->imp_sid;
5913 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5914 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005915 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005916 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005917 lhs->lhs_scriptvar_sid, rawname,
5918 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5919 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005920 if (lhs->lhs_scriptvar_idx >= 0)
5921 {
5922 scriptitem_T *si = SCRIPT_ITEM(
5923 lhs->lhs_scriptvar_sid);
5924 svar_T *sv =
5925 ((svar_T *)si->sn_var_vals.ga_data)
5926 + lhs->lhs_scriptvar_idx;
5927 lhs->lhs_type = sv->sv_type;
5928 }
5929 }
5930 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005931 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 == FAIL)
5933 return FAIL;
5934 }
5935 }
5936
5937 if (declare_error)
5938 {
5939 vim9_declare_error(lhs->lhs_name);
5940 return FAIL;
5941 }
5942 }
5943
5944 // handle "a:name" as a name, not index "name" on "a"
5945 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5946 var_end = lhs->lhs_dest_end;
5947
5948 if (lhs->lhs_dest != dest_option)
5949 {
5950 if (is_decl && *var_end == ':')
5951 {
5952 char_u *p;
5953
5954 // parse optional type: "let var: type = expr"
5955 if (!VIM_ISWHITE(var_end[1]))
5956 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005957 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005958 return FAIL;
5959 }
5960 p = skipwhite(var_end + 1);
5961 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5962 if (lhs->lhs_type == NULL)
5963 return FAIL;
5964 lhs->lhs_has_type = TRUE;
5965 }
5966 else if (lhs->lhs_lvar != NULL)
5967 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5968 }
5969
Bram Moolenaare42939a2021-04-05 17:11:17 +02005970 if (oplen == 3 && !heredoc
5971 && lhs->lhs_dest != dest_global
5972 && !lhs->lhs_has_index
5973 && lhs->lhs_type->tt_type != VAR_STRING
5974 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005975 {
5976 emsg(_(e_can_only_concatenate_to_string));
5977 return FAIL;
5978 }
5979
5980 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5981 && cctx->ctx_skip != SKIP_YES)
5982 {
5983 if (oplen > 1 && !heredoc)
5984 {
5985 // +=, /=, etc. require an existing variable
5986 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5987 return FAIL;
5988 }
5989 if (!is_decl)
5990 {
5991 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5992 return FAIL;
5993 }
5994
Bram Moolenaar3f327882021-03-17 20:56:38 +01005995 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005996 if ((lhs->lhs_type->tt_type == VAR_FUNC
5997 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005998 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005999 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01006000
6001 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006002 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
6003 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
6004 if (lhs->lhs_lvar == NULL)
6005 return FAIL;
6006 lhs->lhs_new_local = TRUE;
6007 }
6008
6009 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006010 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006011 {
6012 // Something follows after the variable: "var[idx]" or "var.key".
6013 // TODO: should we also handle "->func()" here?
6014 if (is_decl)
6015 {
6016 emsg(_(e_cannot_use_index_when_declaring_variable));
6017 return FAIL;
6018 }
6019
6020 if (var_start[lhs->lhs_varlen] == '['
6021 || var_start[lhs->lhs_varlen] == '.')
6022 {
6023 char_u *after = var_start + lhs->lhs_varlen;
6024 char_u *p;
6025
6026 // Only the last index is used below, if there are others
6027 // before it generate code for the expression. Thus for
6028 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6029 for (;;)
6030 {
6031 p = skip_index(after);
6032 if (*p != '[' && *p != '.')
6033 break;
6034 after = p;
6035 }
6036 if (after > var_start + lhs->lhs_varlen)
6037 {
6038 lhs->lhs_varlen = after - var_start;
6039 lhs->lhs_dest = dest_expr;
6040 // We don't know the type before evaluating the expression,
6041 // use "any" until then.
6042 lhs->lhs_type = &t_any;
6043 }
6044
Bram Moolenaar752fc692021-01-04 21:57:11 +01006045 if (lhs->lhs_type->tt_member == NULL)
6046 lhs->lhs_member_type = &t_any;
6047 else
6048 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6049 }
6050 else
6051 {
6052 semsg("Not supported yet: %s", var_start);
6053 return FAIL;
6054 }
6055 }
6056 return OK;
6057}
6058
6059/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006060 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6061 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006062 */
6063 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006064compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 char_u *var_start,
6066 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006067 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006068 cctx_T *cctx)
6069{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006070 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006071 char_u *p;
6072 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02006073 int need_white_before = TRUE;
6074 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006075
Bram Moolenaar752fc692021-01-04 21:57:11 +01006076 p = var_start + varlen;
6077 if (*p == '[')
6078 {
6079 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006080 if (*p == ':')
6081 {
6082 // empty first index, push zero
6083 r = generate_PUSHNR(cctx, 0);
6084 need_white_before = FALSE;
6085 }
6086 else
6087 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006088
6089 if (r == OK && *skipwhite(p) == ':')
6090 {
6091 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02006092 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02006093 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006094 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02006095 empty_second = *skipwhite(p + 1) == ']';
6096 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
6097 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006098 {
6099 semsg(_(e_white_space_required_before_and_after_str_at_str),
6100 ":", p);
6101 return FAIL;
6102 }
6103 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02006104 if (*p == ']')
6105 // empty second index, push "none"
6106 r = generate_PUSHSPEC(cctx, VVAL_NONE);
6107 else
6108 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006109 }
6110
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 if (r == OK && *skipwhite(p) != ']')
6112 {
6113 // this should not happen
6114 emsg(_(e_missbrac));
6115 r = FAIL;
6116 }
6117 }
6118 else // if (*p == '.')
6119 {
6120 char_u *key_end = to_name_end(p + 1, TRUE);
6121 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6122
6123 r = generate_PUSHS(cctx, key);
6124 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006125 return r;
6126}
6127
6128/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006129 * For a LHS with an index, load the variable to be indexed.
6130 */
6131 static int
6132compile_load_lhs(
6133 lhs_T *lhs,
6134 char_u *var_start,
6135 type_T *rhs_type,
6136 cctx_T *cctx)
6137{
6138 if (lhs->lhs_dest == dest_expr)
6139 {
6140 size_t varlen = lhs->lhs_varlen;
6141 int c = var_start[varlen];
6142 char_u *p = var_start;
6143 garray_T *stack = &cctx->ctx_type_stack;
6144
6145 // Evaluate "ll[expr]" of "ll[expr][idx]"
6146 var_start[varlen] = NUL;
6147 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6148 {
6149 // this should not happen
6150 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006151 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006152 return FAIL;
6153 }
6154 var_start[varlen] = c;
6155
6156 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6157 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6158 // now we can properly check the type
6159 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6160 && rhs_type != &t_void
6161 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6162 FALSE, FALSE) == FAIL)
6163 return FAIL;
6164 }
6165 else
6166 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6167 lhs->lhs_lvar, lhs->lhs_type);
6168 return OK;
6169}
6170
6171/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006172 * Assignment to a list or dict member, or ":unlet" for the item, using the
6173 * information in "lhs".
6174 * Returns OK or FAIL.
6175 */
6176 static int
6177compile_assign_unlet(
6178 char_u *var_start,
6179 lhs_T *lhs,
6180 int is_assign,
6181 type_T *rhs_type,
6182 cctx_T *cctx)
6183{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006184 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006185 garray_T *stack = &cctx->ctx_type_stack;
6186 int range = FALSE;
6187
Bram Moolenaar68452172021-04-12 21:21:02 +02006188 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189 return FAIL;
Bram Moolenaar68452172021-04-12 21:21:02 +02006190 if (is_assign && range && lhs->lhs_type != &t_blob
6191 && lhs->lhs_type != &t_any)
6192 {
6193 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
6194 return FAIL;
6195 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196
6197 if (lhs->lhs_type == &t_any)
6198 {
6199 // Index on variable of unknown type: check at runtime.
6200 dest_type = VAR_ANY;
6201 }
6202 else
6203 {
6204 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006205 if (dest_type == VAR_DICT && range)
6206 {
6207 emsg(e_cannot_use_range_with_dictionary);
6208 return FAIL;
6209 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006210 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6211 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006212 if (dest_type == VAR_LIST)
6213 {
6214 if (range
6215 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006216 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006217 return FAIL;
6218 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6219 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6220 return FAIL;
6221 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006222 }
6223
6224 // Load the dict or list. On the stack we then have:
6225 // - value (for assignment, not for :unlet)
6226 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006227 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006229 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6230 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006231
Bram Moolenaar68452172021-04-12 21:21:02 +02006232 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6233 || dest_type == VAR_BLOB || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006234 {
6235 if (is_assign)
6236 {
Bram Moolenaar68452172021-04-12 21:21:02 +02006237 if (range)
6238 {
6239 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
6240 return FAIL;
6241 }
6242 else
6243 {
6244 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006245
Bram Moolenaar68452172021-04-12 21:21:02 +02006246 if (isn == NULL)
6247 return FAIL;
6248 isn->isn_arg.vartype = dest_type;
6249 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006250 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006251 else if (range)
6252 {
6253 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6254 return FAIL;
6255 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006256 else
6257 {
6258 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6259 return FAIL;
6260 }
6261 }
6262 else
6263 {
6264 emsg(_(e_indexable_type_required));
6265 return FAIL;
6266 }
6267
6268 return OK;
6269}
6270
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006271/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006272 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006273 * "let name"
6274 * "var name = expr"
6275 * "final name = expr"
6276 * "const name = expr"
6277 * "name = expr"
6278 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006279 * Return NULL for an error.
6280 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 */
6282 static char_u *
6283compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6284{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006285 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006287 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 char_u *ret = NULL;
6289 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006290 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006293 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 int oplen = 0;
6296 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006297 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006299 int is_decl = is_decl_command(cmdidx);
6300 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006301 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006303 // Skip over the "var" or "[var, var]" to get to any "=".
6304 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6305 if (p == NULL)
6306 return *arg == '[' ? arg : NULL;
6307
6308 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006310 // TODO: should we allow this, and figure out type inference from list
6311 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006312 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 return NULL;
6314 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006317 sp = p;
6318 p = skipwhite(p);
6319 op = p;
6320 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006321
6322 if (var_count > 0 && oplen == 0)
6323 // can be something like "[1, 2]->func()"
6324 return arg;
6325
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006326 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006328 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006329 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332 if (heredoc)
6333 {
6334 list_T *l;
6335 listitem_T *li;
6336
6337 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006338 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006340 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006341 if (l == NULL)
6342 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006343
Bram Moolenaar078269b2020-09-21 20:35:55 +02006344 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006346 // Push each line and the create the list.
6347 FOR_ALL_LIST_ITEMS(l, li)
6348 {
6349 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6350 li->li_tv.vval.v_string = NULL;
6351 }
6352 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006353 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 list_free(l);
6355 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006356 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006358 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006359 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006360 char_u *wp;
6361
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006362 // for "[var, var] = expr" evaluate the expression here, loop over the
6363 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006364 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006365
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006366 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006367 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006368 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006369 if (compile_expr0(&p, cctx) == FAIL)
6370 return NULL;
6371 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006372
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006373 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006374 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006375 type_T *stacktype;
6376
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006377 stacktype = stack->ga_len == 0 ? &t_void
6378 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006379 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006381 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006382 goto theend;
6383 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006384 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006385 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006386 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006387 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006388 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6389 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006390 if (stacktype->tt_member != NULL)
6391 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392 }
6393 }
6394
6395 /*
6396 * Loop over variables in "[var, var] = expr".
6397 * For "var = expr" and "let var: type" this is done only once.
6398 */
6399 if (var_count > 0)
6400 var_start = skipwhite(arg + 1); // skip over the "["
6401 else
6402 var_start = arg;
6403 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6404 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006405 int instr_count = -1;
6406
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006407 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6408 {
6409 // Ignore underscore in "[a, _, b] = list".
6410 if (var_count > 0)
6411 {
6412 var_start = skipwhite(var_start + 2);
6413 continue;
6414 }
6415 emsg(_(e_cannot_use_underscore_here));
6416 goto theend;
6417 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006418 vim_free(lhs.lhs_name);
6419
6420 /*
6421 * Figure out the LHS type and other properties.
6422 */
6423 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6424 goto theend;
6425
6426 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006427 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006428 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006429 goto theend;
6430 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006431 if (!is_decl && lhs.lhs_lvar != NULL
6432 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006433 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006434 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006435 goto theend;
6436 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006437
6438 if (!heredoc)
6439 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006440 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006441 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006442 if (oplen > 0 && var_count == 0)
6443 {
6444 // skip over the "=" and the expression
6445 p = skipwhite(op + oplen);
6446 compile_expr0(&p, cctx);
6447 }
6448 }
6449 else if (oplen > 0)
6450 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006451 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006452 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006453
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454 // For "var = expr" evaluate the expression.
6455 if (var_count == 0)
6456 {
6457 int r;
6458
6459 // for "+=", "*=", "..=" etc. first load the current value
6460 if (*op != '=')
6461 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006462 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006463
Bram Moolenaar752fc692021-01-04 21:57:11 +01006464 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006465 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006466 int range = FALSE;
6467
6468 // Get member from list or dict. First compile the
6469 // index value.
6470 if (compile_assign_index(var_start, &lhs,
Bram Moolenaar68452172021-04-12 21:21:02 +02006471 &range, cctx) == FAIL)
Bram Moolenaare42939a2021-04-05 17:11:17 +02006472 goto theend;
Bram Moolenaar68452172021-04-12 21:21:02 +02006473 if (range)
6474 {
6475 semsg(_(e_cannot_use_range_with_assignment_str),
6476 var_start);
6477 return FAIL;
6478 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006479
6480 // Get the member.
6481 if (compile_member(FALSE, cctx) == FAIL)
6482 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006483 }
6484 }
6485
6486 // Compile the expression. Temporarily hide the new local
6487 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006489 --cctx->ctx_locals.ga_len;
6490 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006491 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006492 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006493 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006495 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006496 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006497 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006498 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006500 ++cctx->ctx_locals.ga_len;
6501 if (r == FAIL)
6502 goto theend;
6503 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006504 else if (semicolon && var_idx == var_count - 1)
6505 {
6506 // For "[var; var] = expr" get the rest of the list
6507 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6508 goto theend;
6509 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006510 else
6511 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006512 // For "[var, var] = expr" get the "var_idx" item from the
6513 // list.
6514 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006515 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006516 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006517
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006518 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006519 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006520 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006521 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006522 if ((rhs_type->tt_type == VAR_FUNC
6523 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006524 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006525 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006526 goto theend;
6527
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006529 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006530 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006531 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006532 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006533 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006534 }
6535 else
6536 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006537 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006538 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006539 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006541 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006542 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006543 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006544 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006545 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006546 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006547 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006548 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006549 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006550 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006551 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006552
Bram Moolenaar77709b12021-04-03 21:01:01 +02006553 // Without operator check type here, otherwise below.
6554 // Use the line number of the assignment.
6555 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006556 if (lhs.lhs_has_index)
6557 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006558 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006559 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006560 goto theend;
6561 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006562 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006563 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006564 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006567 else if (cmdidx == CMD_final)
6568 {
6569 emsg(_(e_final_requires_a_value));
6570 goto theend;
6571 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006572 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006574 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006575 goto theend;
6576 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006577 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006578 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006579 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006580 goto theend;
6581 }
6582 else
6583 {
6584 // variables are always initialized
6585 if (ga_grow(instr, 1) == FAIL)
6586 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006587 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588 {
6589 case VAR_BOOL:
6590 generate_PUSHBOOL(cctx, VVAL_FALSE);
6591 break;
6592 case VAR_FLOAT:
6593#ifdef FEAT_FLOAT
6594 generate_PUSHF(cctx, 0.0);
6595#endif
6596 break;
6597 case VAR_STRING:
6598 generate_PUSHS(cctx, NULL);
6599 break;
6600 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006601 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006602 break;
6603 case VAR_FUNC:
6604 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6605 break;
6606 case VAR_LIST:
6607 generate_NEWLIST(cctx, 0);
6608 break;
6609 case VAR_DICT:
6610 generate_NEWDICT(cctx, 0);
6611 break;
6612 case VAR_JOB:
6613 generate_PUSHJOB(cctx, NULL);
6614 break;
6615 case VAR_CHANNEL:
6616 generate_PUSHCHANNEL(cctx, NULL);
6617 break;
6618 case VAR_NUMBER:
6619 case VAR_UNKNOWN:
6620 case VAR_ANY:
6621 case VAR_PARTIAL:
6622 case VAR_VOID:
6623 case VAR_SPECIAL: // cannot happen
6624 generate_PUSHNR(cctx, 0);
6625 break;
6626 }
6627 }
6628 if (var_count == 0)
6629 end = p;
6630 }
6631
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006632 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006633 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006634 break;
6635
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 if (oplen > 0 && *op != '=')
6637 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006638 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006639 type_T *stacktype;
6640
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006641 if (*op == '.')
6642 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006643 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006644 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006645 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006646 if (
6647#ifdef FEAT_FLOAT
6648 // If variable is float operation with number is OK.
6649 !(expected == &t_float && stacktype == &t_number) &&
6650#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006651 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006652 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006653 goto theend;
6654
6655 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006656 {
6657 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6658 goto theend;
6659 }
6660 else if (*op == '+')
6661 {
6662 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006663 operator_type(lhs.lhs_member_type, stacktype),
6664 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006665 goto theend;
6666 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006667 else if (generate_two_op(cctx, op) == FAIL)
6668 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006670
Bram Moolenaar752fc692021-01-04 21:57:11 +01006671 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006672 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006673 // Use the info in "lhs" to store the value at the index in the
6674 // list or dict.
6675 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6676 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006677 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006678 }
6679 else
6680 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006682 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006683 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006684 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006685 generate_LOCKCONST(cctx);
6686
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006687 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006688 && (lhs.lhs_type->tt_type == VAR_DICT
6689 || lhs.lhs_type->tt_type == VAR_LIST)
6690 && lhs.lhs_type->tt_member != NULL
6691 && lhs.lhs_type->tt_member != &t_any
6692 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006693 // Set the type in the list or dict, so that it can be checked,
6694 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006696
Bram Moolenaar752fc692021-01-04 21:57:11 +01006697 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006698 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006699 if (generate_store_var(cctx, lhs.lhs_dest,
6700 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6701 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6702 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006703 goto theend;
6704 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006706 {
6707 isn_T *isn = ((isn_T *)instr->ga_data)
6708 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006709
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006710 // optimization: turn "var = 123" from ISN_PUSHNR +
6711 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006712 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006713 && instr->ga_len == instr_count + 1
6714 && isn->isn_type == ISN_PUSHNR)
6715 {
6716 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006717
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006718 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006719 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006720 isn->isn_arg.storenr.stnr_val = val;
6721 if (stack->ga_len > 0)
6722 --stack->ga_len;
6723 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006724 else if (lhs.lhs_lvar->lv_from_outer > 0)
6725 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6726 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006727 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006728 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006729 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006730 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006731
6732 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006733 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006735
6736 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006737 if (var_count > 0 && !semicolon)
6738 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006739 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006740 goto theend;
6741 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006742
Bram Moolenaarb2097502020-07-19 17:17:02 +02006743 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
6745theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006746 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 return ret;
6748}
6749
6750/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006751 * Check for an assignment at "eap->cmd", compile it if found.
6752 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6753 */
6754 static int
6755may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6756{
6757 char_u *pskip;
6758 char_u *p;
6759
6760 // Assuming the command starts with a variable or function name,
6761 // find what follows.
6762 // Skip over "var.member", "var[idx]" and the like.
6763 // Also "&opt = val", "$ENV = val" and "@r = val".
6764 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6765 ? eap->cmd + 1 : eap->cmd;
6766 p = to_name_end(pskip, TRUE);
6767 if (p > eap->cmd && *p != NUL)
6768 {
6769 char_u *var_end;
6770 int oplen;
6771 int heredoc;
6772
6773 if (eap->cmd[0] == '@')
6774 var_end = eap->cmd + 2;
6775 else
6776 var_end = find_name_end(pskip, NULL, NULL,
6777 FNE_CHECK_START | FNE_INCL_BR);
6778 oplen = assignment_len(skipwhite(var_end), &heredoc);
6779 if (oplen > 0)
6780 {
6781 size_t len = p - eap->cmd;
6782
6783 // Recognize an assignment if we recognize the variable
6784 // name:
6785 // "g:var = expr"
6786 // "local = expr" where "local" is a local var.
6787 // "script = expr" where "script" is a script-local var.
6788 // "import = expr" where "import" is an imported var
6789 // "&opt = expr"
6790 // "$ENV = expr"
6791 // "@r = expr"
6792 if (*eap->cmd == '&'
6793 || *eap->cmd == '$'
6794 || *eap->cmd == '@'
6795 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006796 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006797 {
6798 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6799 if (*line == NULL || *line == eap->cmd)
6800 return FAIL;
6801 return OK;
6802 }
6803 }
6804 }
6805
6806 if (*eap->cmd == '[')
6807 {
6808 // [var, var] = expr
6809 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6810 if (*line == NULL)
6811 return FAIL;
6812 if (*line != eap->cmd)
6813 return OK;
6814 }
6815 return NOTDONE;
6816}
6817
6818/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006819 * Check if "name" can be "unlet".
6820 */
6821 int
6822check_vim9_unlet(char_u *name)
6823{
6824 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6825 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006826 // "unlet s:var" is allowed in legacy script.
6827 if (*name == 's' && !script_is_vim9())
6828 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006829 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006830 return FAIL;
6831 }
6832 return OK;
6833}
6834
6835/*
6836 * Callback passed to ex_unletlock().
6837 */
6838 static int
6839compile_unlet(
6840 lval_T *lvp,
6841 char_u *name_end,
6842 exarg_T *eap,
6843 int deep UNUSED,
6844 void *coookie)
6845{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006846 cctx_T *cctx = coookie;
6847 char_u *p = lvp->ll_name;
6848 int cc = *name_end;
6849 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006850
Bram Moolenaar752fc692021-01-04 21:57:11 +01006851 if (cctx->ctx_skip == SKIP_YES)
6852 return OK;
6853
6854 *name_end = NUL;
6855 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006856 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 // :unlet $ENV_VAR
6858 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6859 }
6860 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6861 {
6862 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006863
Bram Moolenaar752fc692021-01-04 21:57:11 +01006864 // This is similar to assigning: lookup the list/dict, compile the
6865 // idx/key. Then instead of storing the value unlet the item.
6866 // unlet {list}[idx]
6867 // unlet {dict}[key] dict.key
6868 //
6869 // Figure out the LHS type and other properties.
6870 //
6871 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6872
6873 // : unlet an indexed item
6874 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006875 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006876 iemsg("called compile_lhs() without an index");
6877 ret = FAIL;
6878 }
6879 else
6880 {
6881 // Use the info in "lhs" to unlet the item at the index in the
6882 // list or dict.
6883 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006884 }
6885
Bram Moolenaar752fc692021-01-04 21:57:11 +01006886 vim_free(lhs.lhs_name);
6887 }
6888 else if (check_vim9_unlet(p) == FAIL)
6889 {
6890 ret = FAIL;
6891 }
6892 else
6893 {
6894 // Normal name. Only supports g:, w:, t: and b: namespaces.
6895 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006896 }
6897
Bram Moolenaar752fc692021-01-04 21:57:11 +01006898 *name_end = cc;
6899 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006900}
6901
6902/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006903 * Callback passed to ex_unletlock().
6904 */
6905 static int
6906compile_lock_unlock(
6907 lval_T *lvp,
6908 char_u *name_end,
6909 exarg_T *eap,
6910 int deep UNUSED,
6911 void *coookie)
6912{
6913 cctx_T *cctx = coookie;
6914 int cc = *name_end;
6915 char_u *p = lvp->ll_name;
6916 int ret = OK;
6917 size_t len;
6918 char_u *buf;
6919
6920 if (cctx->ctx_skip == SKIP_YES)
6921 return OK;
6922
6923 // Cannot use :lockvar and :unlockvar on local variables.
6924 if (p[1] != ':')
6925 {
6926 char_u *end = skip_var_one(p, FALSE);
6927
6928 if (lookup_local(p, end - p, NULL, cctx) == OK)
6929 {
6930 emsg(_(e_cannot_lock_unlock_local_variable));
6931 return FAIL;
6932 }
6933 }
6934
6935 // Checking is done at runtime.
6936 *name_end = NUL;
6937 len = name_end - p + 20;
6938 buf = alloc(len);
6939 if (buf == NULL)
6940 ret = FAIL;
6941 else
6942 {
6943 vim_snprintf((char *)buf, len, "%s %s",
6944 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6945 p);
6946 ret = generate_EXEC(cctx, buf);
6947
6948 vim_free(buf);
6949 *name_end = cc;
6950 }
6951 return ret;
6952}
6953
6954/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006955 * compile "unlet var", "lock var" and "unlock var"
6956 * "arg" points to "var".
6957 */
6958 static char_u *
6959compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6960{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006961 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6962 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6963 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006964 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6965}
6966
6967/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006968 * Compile an :import command.
6969 */
6970 static char_u *
6971compile_import(char_u *arg, cctx_T *cctx)
6972{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006973 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006974}
6975
6976/*
6977 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6978 */
6979 static int
6980compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6981{
6982 garray_T *instr = &cctx->ctx_instr;
6983 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6984
6985 if (endlabel == NULL)
6986 return FAIL;
6987 endlabel->el_next = *el;
6988 *el = endlabel;
6989 endlabel->el_end_label = instr->ga_len;
6990
6991 generate_JUMP(cctx, when, 0);
6992 return OK;
6993}
6994
6995 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006996compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997{
6998 garray_T *instr = &cctx->ctx_instr;
6999
7000 while (*el != NULL)
7001 {
7002 endlabel_T *cur = (*el);
7003 isn_T *isn;
7004
7005 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007006 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 *el = cur->el_next;
7008 vim_free(cur);
7009 }
7010}
7011
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007012 static void
7013compile_free_jump_to_end(endlabel_T **el)
7014{
7015 while (*el != NULL)
7016 {
7017 endlabel_T *cur = (*el);
7018
7019 *el = cur->el_next;
7020 vim_free(cur);
7021 }
7022}
7023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024/*
7025 * Create a new scope and set up the generic items.
7026 */
7027 static scope_T *
7028new_scope(cctx_T *cctx, scopetype_T type)
7029{
7030 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
7031
7032 if (scope == NULL)
7033 return NULL;
7034 scope->se_outer = cctx->ctx_scope;
7035 cctx->ctx_scope = scope;
7036 scope->se_type = type;
7037 scope->se_local_count = cctx->ctx_locals.ga_len;
7038 return scope;
7039}
7040
7041/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007042 * Free the current scope and go back to the outer scope.
7043 */
7044 static void
7045drop_scope(cctx_T *cctx)
7046{
7047 scope_T *scope = cctx->ctx_scope;
7048
7049 if (scope == NULL)
7050 {
7051 iemsg("calling drop_scope() without a scope");
7052 return;
7053 }
7054 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007055 switch (scope->se_type)
7056 {
7057 case IF_SCOPE:
7058 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7059 case FOR_SCOPE:
7060 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7061 case WHILE_SCOPE:
7062 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7063 case TRY_SCOPE:
7064 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7065 case NO_SCOPE:
7066 case BLOCK_SCOPE:
7067 break;
7068 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007069 vim_free(scope);
7070}
7071
7072/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 * compile "if expr"
7074 *
7075 * "if expr" Produces instructions:
7076 * EVAL expr Push result of "expr"
7077 * JUMP_IF_FALSE end
7078 * ... body ...
7079 * end:
7080 *
7081 * "if expr | else" Produces instructions:
7082 * EVAL expr Push result of "expr"
7083 * JUMP_IF_FALSE else
7084 * ... body ...
7085 * JUMP_ALWAYS end
7086 * else:
7087 * ... body ...
7088 * end:
7089 *
7090 * "if expr1 | elseif expr2 | else" Produces instructions:
7091 * EVAL expr Push result of "expr"
7092 * JUMP_IF_FALSE elseif
7093 * ... body ...
7094 * JUMP_ALWAYS end
7095 * elseif:
7096 * EVAL expr Push result of "expr"
7097 * JUMP_IF_FALSE else
7098 * ... body ...
7099 * JUMP_ALWAYS end
7100 * else:
7101 * ... body ...
7102 * end:
7103 */
7104 static char_u *
7105compile_if(char_u *arg, cctx_T *cctx)
7106{
7107 char_u *p = arg;
7108 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007109 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007111 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007112 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007113
Bram Moolenaara5565e42020-05-09 15:44:01 +02007114 CLEAR_FIELD(ppconst);
7115 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007116 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007117 clear_ppconst(&ppconst);
7118 return NULL;
7119 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007120 if (!ends_excmd2(arg, skipwhite(p)))
7121 {
7122 semsg(_(e_trailing_arg), p);
7123 return NULL;
7124 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007125 if (cctx->ctx_skip == SKIP_YES)
7126 clear_ppconst(&ppconst);
7127 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007128 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007129 int error = FALSE;
7130 int v;
7131
Bram Moolenaara5565e42020-05-09 15:44:01 +02007132 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007133 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007134 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007135 if (error)
7136 return NULL;
7137 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007138 }
7139 else
7140 {
7141 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007142 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007143 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007144 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007145 if (bool_on_stack(cctx) == FAIL)
7146 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148
Bram Moolenaara91a7132021-03-25 21:12:15 +01007149 // CMDMOD_REV must come before the jump
7150 generate_undo_cmdmods(cctx);
7151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 scope = new_scope(cctx, IF_SCOPE);
7153 if (scope == NULL)
7154 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007155 scope->se_skip_save = skip_save;
7156 // "is_had_return" will be reset if any block does not end in :return
7157 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007159 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007160 {
7161 // "where" is set when ":elseif", "else" or ":endif" is found
7162 scope->se_u.se_if.is_if_label = instr->ga_len;
7163 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7164 }
7165 else
7166 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167
Bram Moolenaarced68a02021-01-24 17:53:47 +01007168#ifdef FEAT_PROFILE
7169 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7170 && skip_save != SKIP_YES)
7171 {
7172 // generated a profile start, need to generate a profile end, since it
7173 // won't be done after returning
7174 cctx->ctx_skip = SKIP_NOT;
7175 generate_instr(cctx, ISN_PROF_END);
7176 cctx->ctx_skip = SKIP_YES;
7177 }
7178#endif
7179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180 return p;
7181}
7182
7183 static char_u *
7184compile_elseif(char_u *arg, cctx_T *cctx)
7185{
7186 char_u *p = arg;
7187 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007188 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189 isn_T *isn;
7190 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007191 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007192 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007193
7194 if (scope == NULL || scope->se_type != IF_SCOPE)
7195 {
7196 emsg(_(e_elseif_without_if));
7197 return NULL;
7198 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007199 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007200 if (!cctx->ctx_had_return)
7201 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202
Bram Moolenaarced68a02021-01-24 17:53:47 +01007203 if (cctx->ctx_skip == SKIP_NOT)
7204 {
7205 // previous block was executed, this one and following will not
7206 cctx->ctx_skip = SKIP_YES;
7207 scope->se_u.se_if.is_seen_skip_not = TRUE;
7208 }
7209 if (scope->se_u.se_if.is_seen_skip_not)
7210 {
7211 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007212 // Do not count the "elseif" for profiling and cmdmod
7213 instr->ga_len = current_instr_idx(cctx);
7214
Bram Moolenaarced68a02021-01-24 17:53:47 +01007215 skip_expr_cctx(&p, cctx);
7216 return p;
7217 }
7218
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007219 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007220 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007221 int moved_cmdmod = FALSE;
7222
7223 // Move any CMDMOD instruction to after the jump
7224 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7225 {
7226 if (ga_grow(instr, 1) == FAIL)
7227 return NULL;
7228 ((isn_T *)instr->ga_data)[instr->ga_len] =
7229 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7230 --instr->ga_len;
7231 moved_cmdmod = TRUE;
7232 }
7233
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007234 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007236 return NULL;
7237 // previous "if" or "elseif" jumps here
7238 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7239 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007240 if (moved_cmdmod)
7241 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007244 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007245 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007246 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007247 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007248 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007249#ifdef FEAT_PROFILE
7250 if (cctx->ctx_profiling)
7251 {
7252 // the previous block was skipped, need to profile this line
7253 generate_instr(cctx, ISN_PROF_START);
7254 instr_count = instr->ga_len;
7255 }
7256#endif
7257 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007258 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007259 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007260 clear_ppconst(&ppconst);
7261 return NULL;
7262 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007263 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007264 if (!ends_excmd2(arg, skipwhite(p)))
7265 {
7266 semsg(_(e_trailing_arg), p);
7267 return NULL;
7268 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007269 if (scope->se_skip_save == SKIP_YES)
7270 clear_ppconst(&ppconst);
7271 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007272 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007273 int error = FALSE;
7274 int v;
7275
Bram Moolenaar7f141552020-05-09 17:35:53 +02007276 // The expression results in a constant.
7277 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007278 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7279 if (error)
7280 return NULL;
7281 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007282 clear_ppconst(&ppconst);
7283 scope->se_u.se_if.is_if_label = -1;
7284 }
7285 else
7286 {
7287 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007288 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007289 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007290 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007291 if (bool_on_stack(cctx) == FAIL)
7292 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007293
Bram Moolenaara91a7132021-03-25 21:12:15 +01007294 // CMDMOD_REV must come before the jump
7295 generate_undo_cmdmods(cctx);
7296
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007297 // "where" is set when ":elseif", "else" or ":endif" is found
7298 scope->se_u.se_if.is_if_label = instr->ga_len;
7299 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301
7302 return p;
7303}
7304
7305 static char_u *
7306compile_else(char_u *arg, cctx_T *cctx)
7307{
7308 char_u *p = arg;
7309 garray_T *instr = &cctx->ctx_instr;
7310 isn_T *isn;
7311 scope_T *scope = cctx->ctx_scope;
7312
7313 if (scope == NULL || scope->se_type != IF_SCOPE)
7314 {
7315 emsg(_(e_else_without_if));
7316 return NULL;
7317 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007318 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007319 if (!cctx->ctx_had_return)
7320 scope->se_u.se_if.is_had_return = FALSE;
7321 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322
Bram Moolenaarced68a02021-01-24 17:53:47 +01007323#ifdef FEAT_PROFILE
7324 if (cctx->ctx_profiling)
7325 {
7326 if (cctx->ctx_skip == SKIP_NOT
7327 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7328 .isn_type == ISN_PROF_START)
7329 // the previous block was executed, do not count "else" for profiling
7330 --instr->ga_len;
7331 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7332 {
7333 // the previous block was not executed, this one will, do count the
7334 // "else" for profiling
7335 cctx->ctx_skip = SKIP_NOT;
7336 generate_instr(cctx, ISN_PROF_END);
7337 generate_instr(cctx, ISN_PROF_START);
7338 cctx->ctx_skip = SKIP_YES;
7339 }
7340 }
7341#endif
7342
7343 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007344 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007345 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007346 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007347 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007348 if (!cctx->ctx_had_return
7349 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7350 JUMP_ALWAYS, cctx) == FAIL)
7351 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007352 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007353
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007354 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007355 {
7356 if (scope->se_u.se_if.is_if_label >= 0)
7357 {
7358 // previous "if" or "elseif" jumps here
7359 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7360 isn->isn_arg.jump.jump_where = instr->ga_len;
7361 scope->se_u.se_if.is_if_label = -1;
7362 }
7363 }
7364
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007365 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007366 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
7369 return p;
7370}
7371
7372 static char_u *
7373compile_endif(char_u *arg, cctx_T *cctx)
7374{
7375 scope_T *scope = cctx->ctx_scope;
7376 ifscope_T *ifscope;
7377 garray_T *instr = &cctx->ctx_instr;
7378 isn_T *isn;
7379
Bram Moolenaarfa984412021-03-25 22:15:28 +01007380 if (misplaced_cmdmod(cctx))
7381 return NULL;
7382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 if (scope == NULL || scope->se_type != IF_SCOPE)
7384 {
7385 emsg(_(e_endif_without_if));
7386 return NULL;
7387 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007388 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007389 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007390 if (!cctx->ctx_had_return)
7391 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007393 if (scope->se_u.se_if.is_if_label >= 0)
7394 {
7395 // previous "if" or "elseif" jumps here
7396 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7397 isn->isn_arg.jump.jump_where = instr->ga_len;
7398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007400 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007401
7402#ifdef FEAT_PROFILE
7403 // even when skipping we count the endif as executed, unless the block it's
7404 // in is skipped
7405 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7406 && scope->se_skip_save != SKIP_YES)
7407 {
7408 cctx->ctx_skip = SKIP_NOT;
7409 generate_instr(cctx, ISN_PROF_START);
7410 }
7411#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007412 cctx->ctx_skip = scope->se_skip_save;
7413
7414 // If all the blocks end in :return and there is an :else then the
7415 // had_return flag is set.
7416 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007418 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419 return arg;
7420}
7421
7422/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007423 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 *
7425 * Produces instructions:
7426 * PUSHNR -1
7427 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007428 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7430 * - if beyond end, jump to "end"
7431 * - otherwise get item from list and push it
7432 * STORE var Store item in "var"
7433 * ... body ...
7434 * JUMP top Jump back to repeat
7435 * end: DROP Drop the result of "expr"
7436 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007437 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7438 * UNPACK 2 Split item in 2
7439 * STORE var1 Store item in "var1"
7440 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 */
7442 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007443compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007445 char_u *arg;
7446 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007447 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007449 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007450 int var_count = 0;
7451 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 garray_T *stack = &cctx->ctx_type_stack;
7454 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007455 lvar_T *loop_lvar; // loop iteration variable
7456 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007458 type_T *item_type = &t_any;
7459 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460
Bram Moolenaar792f7862020-11-23 08:31:18 +01007461 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007462 if (p == NULL)
7463 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007464 if (var_count == 0)
7465 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007466
7467 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007468 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007469 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7470 return NULL;
7471 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 {
7473 emsg(_(e_missing_in));
7474 return NULL;
7475 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007476 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007477 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7478 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 scope = new_scope(cctx, FOR_SCOPE);
7481 if (scope == NULL)
7482 return NULL;
7483
Bram Moolenaar792f7862020-11-23 08:31:18 +01007484 // Reserve a variable to store the loop iteration counter and initialize it
7485 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007486 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7487 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007488 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007489 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007490 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007492 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007493 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
7495 // compile "expr", it remains on the stack until "endfor"
7496 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007497 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007498 {
7499 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007500 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007501 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007502 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007504 // If we know the type of "var" and it is a not a list or string we can
7505 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007507 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7508 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007509 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007510 // TODO: support Blob
7511 semsg(_(e_for_loop_on_str_not_supported),
7512 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007513 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 return NULL;
7515 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007516
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007517 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007518 {
7519 if (var_count == 1)
7520 item_type = vartype->tt_member;
7521 else if (vartype->tt_member->tt_type == VAR_LIST
7522 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7523 item_type = vartype->tt_member->tt_member;
7524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525
Bram Moolenaara91a7132021-03-25 21:12:15 +01007526 // CMDMOD_REV must come before the FOR instruction
7527 generate_undo_cmdmods(cctx);
7528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007530 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007531 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007532
Bram Moolenaar792f7862020-11-23 08:31:18 +01007533 arg = arg_start;
7534 if (var_count > 1)
7535 {
7536 generate_UNPACK(cctx, var_count, semicolon);
7537 arg = skipwhite(arg + 1); // skip white after '['
7538
7539 // the list item is replaced by a number of items
7540 if (ga_grow(stack, var_count - 1) == FAIL)
7541 {
7542 drop_scope(cctx);
7543 return NULL;
7544 }
7545 --stack->ga_len;
7546 for (idx = 0; idx < var_count; ++idx)
7547 {
7548 ((type_T **)stack->ga_data)[stack->ga_len] =
7549 (semicolon && idx == 0) ? vartype : item_type;
7550 ++stack->ga_len;
7551 }
7552 }
7553
7554 for (idx = 0; idx < var_count; ++idx)
7555 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007556 assign_dest_T dest = dest_local;
7557 int opt_flags = 0;
7558 int vimvaridx = -1;
7559 type_T *type = &t_any;
7560
7561 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007562 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007563 name = vim_strnsave(arg, varlen);
7564 if (name == NULL)
7565 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007566
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007567 // TODO: script var not supported?
7568 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7569 &vimvaridx, &type, cctx) == FAIL)
7570 goto failed;
7571 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007572 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007573 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7574 0, 0, type, name) == FAIL)
7575 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007576 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007577 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007578 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007579 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007580 {
7581 semsg(_(e_variable_already_declared), arg);
7582 goto failed;
7583 }
7584
Bram Moolenaarea870692020-12-02 14:24:30 +01007585 if (STRNCMP(name, "s:", 2) == 0)
7586 {
7587 semsg(_(e_cannot_declare_script_variable_in_function), name);
7588 goto failed;
7589 }
7590
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007591 // Reserve a variable to store "var".
7592 // TODO: check for type
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02007593 var_lvar = reserve_local(cctx, arg, varlen, TRUE, &t_any);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007594 if (var_lvar == NULL)
7595 // out of memory or used as an argument
7596 goto failed;
7597
7598 if (semicolon && idx == var_count - 1)
7599 var_lvar->lv_type = vartype;
7600 else
7601 var_lvar->lv_type = item_type;
7602 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7603 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007604
Bram Moolenaar036d0712021-01-17 20:23:38 +01007605 if (*p == ':')
7606 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007607 if (*p == ',' || *p == ';')
7608 ++p;
7609 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007610 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007611 }
7612
7613 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007614
7615failed:
7616 vim_free(name);
7617 drop_scope(cctx);
7618 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619}
7620
7621/*
7622 * compile "endfor"
7623 */
7624 static char_u *
7625compile_endfor(char_u *arg, cctx_T *cctx)
7626{
7627 garray_T *instr = &cctx->ctx_instr;
7628 scope_T *scope = cctx->ctx_scope;
7629 forscope_T *forscope;
7630 isn_T *isn;
7631
Bram Moolenaarfa984412021-03-25 22:15:28 +01007632 if (misplaced_cmdmod(cctx))
7633 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 if (scope == NULL || scope->se_type != FOR_SCOPE)
7636 {
7637 emsg(_(e_for));
7638 return NULL;
7639 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007640 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007642 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643
7644 // At end of ":for" scope jump back to the FOR instruction.
7645 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7646
7647 // Fill in the "end" label in the FOR statement so it can jump here
7648 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7649 isn->isn_arg.forloop.for_end = instr->ga_len;
7650
7651 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007652 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653
7654 // Below the ":for" scope drop the "expr" list from the stack.
7655 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7656 return NULL;
7657
7658 vim_free(scope);
7659
7660 return arg;
7661}
7662
7663/*
7664 * compile "while expr"
7665 *
7666 * Produces instructions:
7667 * top: EVAL expr Push result of "expr"
7668 * JUMP_IF_FALSE end jump if false
7669 * ... body ...
7670 * JUMP top Jump back to repeat
7671 * end:
7672 *
7673 */
7674 static char_u *
7675compile_while(char_u *arg, cctx_T *cctx)
7676{
7677 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 scope_T *scope;
7679
7680 scope = new_scope(cctx, WHILE_SCOPE);
7681 if (scope == NULL)
7682 return NULL;
7683
Bram Moolenaara91a7132021-03-25 21:12:15 +01007684 // "endwhile" jumps back here, one before when profiling or using cmdmods
7685 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686
7687 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007688 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007690 if (!ends_excmd2(arg, skipwhite(p)))
7691 {
7692 semsg(_(e_trailing_arg), p);
7693 return NULL;
7694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695
Bram Moolenaar13106602020-10-04 16:06:05 +02007696 if (bool_on_stack(cctx) == FAIL)
7697 return FAIL;
7698
Bram Moolenaara91a7132021-03-25 21:12:15 +01007699 // CMDMOD_REV must come before the jump
7700 generate_undo_cmdmods(cctx);
7701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007702 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007703 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704 JUMP_IF_FALSE, cctx) == FAIL)
7705 return FAIL;
7706
7707 return p;
7708}
7709
7710/*
7711 * compile "endwhile"
7712 */
7713 static char_u *
7714compile_endwhile(char_u *arg, cctx_T *cctx)
7715{
7716 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007717 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718
Bram Moolenaarfa984412021-03-25 22:15:28 +01007719 if (misplaced_cmdmod(cctx))
7720 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007721 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7722 {
7723 emsg(_(e_while));
7724 return NULL;
7725 }
7726 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007727 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007728
Bram Moolenaarf002a412021-01-24 13:34:18 +01007729#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007730 // count the endwhile before jumping
7731 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007732#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007735 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736
7737 // Fill in the "end" label in the WHILE statement so it can jump here.
7738 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007739 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7740 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007741
7742 vim_free(scope);
7743
7744 return arg;
7745}
7746
7747/*
7748 * compile "continue"
7749 */
7750 static char_u *
7751compile_continue(char_u *arg, cctx_T *cctx)
7752{
7753 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007754 int try_scopes = 0;
7755 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007756
7757 for (;;)
7758 {
7759 if (scope == NULL)
7760 {
7761 emsg(_(e_continue));
7762 return NULL;
7763 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007764 if (scope->se_type == FOR_SCOPE)
7765 {
7766 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007768 }
7769 if (scope->se_type == WHILE_SCOPE)
7770 {
7771 loop_label = scope->se_u.se_while.ws_top_label;
7772 break;
7773 }
7774 if (scope->se_type == TRY_SCOPE)
7775 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007776 scope = scope->se_outer;
7777 }
7778
Bram Moolenaarc150c092021-02-13 15:02:46 +01007779 if (try_scopes > 0)
7780 // Inside one or more try/catch blocks we first need to jump to the
7781 // "finally" or "endtry" to cleanup.
7782 generate_TRYCONT(cctx, try_scopes, loop_label);
7783 else
7784 // Jump back to the FOR or WHILE instruction.
7785 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 return arg;
7788}
7789
7790/*
7791 * compile "break"
7792 */
7793 static char_u *
7794compile_break(char_u *arg, cctx_T *cctx)
7795{
7796 scope_T *scope = cctx->ctx_scope;
7797 endlabel_T **el;
7798
7799 for (;;)
7800 {
7801 if (scope == NULL)
7802 {
7803 emsg(_(e_break));
7804 return NULL;
7805 }
7806 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7807 break;
7808 scope = scope->se_outer;
7809 }
7810
7811 // Jump to the end of the FOR or WHILE loop.
7812 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007813 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007814 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007815 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7817 return FAIL;
7818
7819 return arg;
7820}
7821
7822/*
7823 * compile "{" start of block
7824 */
7825 static char_u *
7826compile_block(char_u *arg, cctx_T *cctx)
7827{
7828 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7829 return NULL;
7830 return skipwhite(arg + 1);
7831}
7832
7833/*
7834 * compile end of block: drop one scope
7835 */
7836 static void
7837compile_endblock(cctx_T *cctx)
7838{
7839 scope_T *scope = cctx->ctx_scope;
7840
7841 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007842 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007843 vim_free(scope);
7844}
7845
7846/*
7847 * compile "try"
7848 * Creates a new scope for the try-endtry, pointing to the first catch and
7849 * finally.
7850 * Creates another scope for the "try" block itself.
7851 * TRY instruction sets up exception handling at runtime.
7852 *
7853 * "try"
7854 * TRY -> catch1, -> finally push trystack entry
7855 * ... try block
7856 * "throw {exception}"
7857 * EVAL {exception}
7858 * THROW create exception
7859 * ... try block
7860 * " catch {expr}"
7861 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007862 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 * EVAL {expr}
7864 * MATCH
7865 * JUMP nomatch -> catch2
7866 * CATCH remove exception
7867 * ... catch block
7868 * " catch"
7869 * JUMP -> finally
7870 * catch2: CATCH remove exception
7871 * ... catch block
7872 * " finally"
7873 * finally:
7874 * ... finally block
7875 * " endtry"
7876 * ENDTRY pop trystack entry, may rethrow
7877 */
7878 static char_u *
7879compile_try(char_u *arg, cctx_T *cctx)
7880{
7881 garray_T *instr = &cctx->ctx_instr;
7882 scope_T *try_scope;
7883 scope_T *scope;
7884
Bram Moolenaarfa984412021-03-25 22:15:28 +01007885 if (misplaced_cmdmod(cctx))
7886 return NULL;
7887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 // scope that holds the jumps that go to catch/finally/endtry
7889 try_scope = new_scope(cctx, TRY_SCOPE);
7890 if (try_scope == NULL)
7891 return NULL;
7892
Bram Moolenaar69f70502021-01-01 16:10:46 +01007893 if (cctx->ctx_skip != SKIP_YES)
7894 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007895 isn_T *isn;
7896
7897 // "try_catch" is set when the first ":catch" is found or when no catch
7898 // is found and ":finally" is found.
7899 // "try_finally" is set when ":finally" is found
7900 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007901 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007902 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7903 return NULL;
7904 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7905 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007906 return NULL;
7907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908
7909 // scope for the try block itself
7910 scope = new_scope(cctx, BLOCK_SCOPE);
7911 if (scope == NULL)
7912 return NULL;
7913
7914 return arg;
7915}
7916
7917/*
7918 * compile "catch {expr}"
7919 */
7920 static char_u *
7921compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7922{
7923 scope_T *scope = cctx->ctx_scope;
7924 garray_T *instr = &cctx->ctx_instr;
7925 char_u *p;
7926 isn_T *isn;
7927
Bram Moolenaarfa984412021-03-25 22:15:28 +01007928 if (misplaced_cmdmod(cctx))
7929 return NULL;
7930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007931 // end block scope from :try or :catch
7932 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7933 compile_endblock(cctx);
7934 scope = cctx->ctx_scope;
7935
7936 // Error if not in a :try scope
7937 if (scope == NULL || scope->se_type != TRY_SCOPE)
7938 {
7939 emsg(_(e_catch));
7940 return NULL;
7941 }
7942
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007943 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007945 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 return NULL;
7947 }
7948
Bram Moolenaar69f70502021-01-01 16:10:46 +01007949 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007951#ifdef FEAT_PROFILE
7952 // the profile-start should be after the jump
7953 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7954 .isn_type == ISN_PROF_START)
7955 --instr->ga_len;
7956#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007957 // Jump from end of previous block to :finally or :endtry
7958 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7959 JUMP_ALWAYS, cctx) == FAIL)
7960 return NULL;
7961
7962 // End :try or :catch scope: set value in ISN_TRY instruction
7963 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007964 if (isn->isn_arg.try.try_ref->try_catch == 0)
7965 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007966 if (scope->se_u.se_try.ts_catch_label != 0)
7967 {
7968 // Previous catch without match jumps here
7969 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7970 isn->isn_arg.jump.jump_where = instr->ga_len;
7971 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007972#ifdef FEAT_PROFILE
7973 if (cctx->ctx_profiling)
7974 {
7975 // a "throw" that jumps here needs to be counted
7976 generate_instr(cctx, ISN_PROF_END);
7977 // the "catch" is also counted
7978 generate_instr(cctx, ISN_PROF_START);
7979 }
7980#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 }
7982
7983 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007984 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007986 scope->se_u.se_try.ts_caught_all = TRUE;
7987 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988 }
7989 else
7990 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007991 char_u *end;
7992 char_u *pat;
7993 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007994 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007995 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007997 // Push v:exception, push {expr} and MATCH
7998 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7999
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008000 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008001 if (*end != *p)
8002 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02008003 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008004 vim_free(tofree);
8005 return FAIL;
8006 }
8007 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01008008 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008009 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008010 len = (int)(end - tofree);
8011 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008012 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008013 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01008014 if (pat == NULL)
8015 return FAIL;
8016 if (generate_PUSHS(cctx, pat) == FAIL)
8017 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
8020 return NULL;
8021
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008022 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
8024 return NULL;
8025 }
8026
Bram Moolenaar69f70502021-01-01 16:10:46 +01008027 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 return NULL;
8029
8030 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
8031 return NULL;
8032 return p;
8033}
8034
8035 static char_u *
8036compile_finally(char_u *arg, cctx_T *cctx)
8037{
8038 scope_T *scope = cctx->ctx_scope;
8039 garray_T *instr = &cctx->ctx_instr;
8040 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008041 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042
Bram Moolenaarfa984412021-03-25 22:15:28 +01008043 if (misplaced_cmdmod(cctx))
8044 return NULL;
8045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046 // end block scope from :try or :catch
8047 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8048 compile_endblock(cctx);
8049 scope = cctx->ctx_scope;
8050
8051 // Error if not in a :try scope
8052 if (scope == NULL || scope->se_type != TRY_SCOPE)
8053 {
8054 emsg(_(e_finally));
8055 return NULL;
8056 }
8057
8058 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008059 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008060 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061 {
8062 emsg(_(e_finally_dup));
8063 return NULL;
8064 }
8065
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008066 this_instr = instr->ga_len;
8067#ifdef FEAT_PROFILE
8068 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8069 .isn_type == ISN_PROF_START)
8070 // jump to the profile start of the "finally"
8071 --this_instr;
8072#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008074 // Fill in the "end" label in jumps at the end of the blocks.
8075 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8076 this_instr, cctx);
8077
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008078 // If there is no :catch then an exception jumps to :finally.
8079 if (isn->isn_arg.try.try_ref->try_catch == 0)
8080 isn->isn_arg.try.try_ref->try_catch = this_instr;
8081 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008082 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008083 {
8084 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008085 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008086 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008087 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008089 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8090 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092 // TODO: set index in ts_finally_label jumps
8093
8094 return arg;
8095}
8096
8097 static char_u *
8098compile_endtry(char_u *arg, cctx_T *cctx)
8099{
8100 scope_T *scope = cctx->ctx_scope;
8101 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008102 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103
Bram Moolenaarfa984412021-03-25 22:15:28 +01008104 if (misplaced_cmdmod(cctx))
8105 return NULL;
8106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 // end block scope from :catch or :finally
8108 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8109 compile_endblock(cctx);
8110 scope = cctx->ctx_scope;
8111
8112 // Error if not in a :try scope
8113 if (scope == NULL || scope->se_type != TRY_SCOPE)
8114 {
8115 if (scope == NULL)
8116 emsg(_(e_no_endtry));
8117 else if (scope->se_type == WHILE_SCOPE)
8118 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008119 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 emsg(_(e_endfor));
8121 else
8122 emsg(_(e_endif));
8123 return NULL;
8124 }
8125
Bram Moolenaarc150c092021-02-13 15:02:46 +01008126 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008127 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008128 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008129 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8130 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008131 {
8132 emsg(_(e_missing_catch_or_finally));
8133 return NULL;
8134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008136#ifdef FEAT_PROFILE
8137 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8138 .isn_type == ISN_PROF_START)
8139 // move the profile start after "endtry" so that it's not counted when
8140 // the exception is rethrown.
8141 --instr->ga_len;
8142#endif
8143
Bram Moolenaar69f70502021-01-01 16:10:46 +01008144 // Fill in the "end" label in jumps at the end of the blocks, if not
8145 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008146 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8147 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148
Bram Moolenaar69f70502021-01-01 16:10:46 +01008149 if (scope->se_u.se_try.ts_catch_label != 0)
8150 {
8151 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008152 isn_T *isn = ((isn_T *)instr->ga_data)
8153 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008154 isn->isn_arg.jump.jump_where = instr->ga_len;
8155 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008156 }
8157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 compile_endblock(cctx);
8159
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008160 if (cctx->ctx_skip != SKIP_YES)
8161 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008162 // End :catch or :finally scope: set instruction index in ISN_TRY
8163 // instruction
8164 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008165 if (cctx->ctx_skip != SKIP_YES
8166 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8167 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008168#ifdef FEAT_PROFILE
8169 if (cctx->ctx_profiling)
8170 generate_instr(cctx, ISN_PROF_START);
8171#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008173 return arg;
8174}
8175
8176/*
8177 * compile "throw {expr}"
8178 */
8179 static char_u *
8180compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8181{
8182 char_u *p = skipwhite(arg);
8183
Bram Moolenaara5565e42020-05-09 15:44:01 +02008184 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008185 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008186 if (cctx->ctx_skip == SKIP_YES)
8187 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 if (may_generate_2STRING(-1, cctx) == FAIL)
8189 return NULL;
8190 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8191 return NULL;
8192
8193 return p;
8194}
8195
8196/*
8197 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008198 * compile "echomsg expr"
8199 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008200 * compile "execute expr"
8201 */
8202 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008203compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008204{
8205 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008206 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008207 int count = 0;
8208
8209 for (;;)
8210 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008211 if (ends_excmd2(prev, p))
8212 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008213 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008214 return NULL;
8215 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008216 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008217 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008218 }
8219
Bram Moolenaare4984292020-12-13 14:19:25 +01008220 if (count > 0)
8221 {
8222 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8223 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8224 else if (cmdidx == CMD_execute)
8225 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8226 else if (cmdidx == CMD_echomsg)
8227 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8228 else
8229 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 return p;
8232}
8233
8234/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008235 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008236 * instruction to compute it and return OK.
8237 * Otherwise return FAIL, the caller must deal with any range.
8238 */
8239 static int
8240compile_variable_range(exarg_T *eap, cctx_T *cctx)
8241{
8242 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8243 char_u *p = skipdigits(eap->cmd);
8244
8245 if (p == range_end)
8246 return FAIL;
8247 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8248}
8249
8250/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008251 * :put r
8252 * :put ={expr}
8253 */
8254 static char_u *
8255compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8256{
8257 char_u *line = arg;
8258 linenr_T lnum;
8259 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008260 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008261
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008262 eap->regname = *line;
8263
8264 if (eap->regname == '=')
8265 {
8266 char_u *p = line + 1;
8267
8268 if (compile_expr0(&p, cctx) == FAIL)
8269 return NULL;
8270 line = p;
8271 }
8272 else if (eap->regname != NUL)
8273 ++line;
8274
Bram Moolenaar08597872020-12-10 19:43:40 +01008275 if (compile_variable_range(eap, cctx) == OK)
8276 {
8277 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8278 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008279 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008280 {
8281 // Either no range or a number.
8282 // "errormsg" will not be set because the range is ADDR_LINES.
8283 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008284 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008285 return NULL;
8286 if (eap->addr_count == 0)
8287 lnum = -1;
8288 else
8289 lnum = eap->line2;
8290 if (above)
8291 --lnum;
8292 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008293
8294 generate_PUT(cctx, eap->regname, lnum);
8295 return line;
8296}
8297
8298/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008299 * A command that is not compiled, execute with legacy code.
8300 */
8301 static char_u *
8302compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8303{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008304 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008305 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008306 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008307
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008308 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008309 goto theend;
8310
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008311 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008312 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008313 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008314 int usefilter = FALSE;
8315
8316 has_expr = argt & (EX_XFILE | EX_EXPAND);
8317
8318 // If the command can be followed by a bar, find the bar and truncate
8319 // it, so that the following command can be compiled.
8320 // The '|' is overwritten with a NUL, it is put back below.
8321 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8322 && *eap->arg == '!')
8323 // :w !filter or :r !filter or :r! filter
8324 usefilter = TRUE;
8325 if ((argt & EX_TRLBAR) && !usefilter)
8326 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008327 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008328 separate_nextcmd(eap);
8329 if (eap->nextcmd != NULL)
8330 nextcmd = eap->nextcmd;
8331 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008332 else if (eap->cmdidx == CMD_wincmd)
8333 {
8334 p = eap->arg;
8335 if (*p != NUL)
8336 ++p;
8337 if (*p == 'g' || *p == Ctrl_G)
8338 ++p;
8339 p = skipwhite(p);
8340 if (*p == '|')
8341 {
8342 *p = NUL;
8343 nextcmd = p + 1;
8344 }
8345 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008346 }
8347
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008348 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8349 {
8350 // expand filename in "syntax include [@group] filename"
8351 has_expr = TRUE;
8352 eap->arg = skipwhite(eap->arg + 7);
8353 if (*eap->arg == '@')
8354 eap->arg = skiptowhite(eap->arg);
8355 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008356
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008357 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8358 && STRLEN(eap->arg) > 4)
8359 {
8360 int delim = *eap->arg;
8361
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008362 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008363 if (*p == delim)
8364 {
8365 eap->arg = p + 1;
8366 has_expr = TRUE;
8367 }
8368 }
8369
Bram Moolenaarecac5912021-01-05 19:23:28 +01008370 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8371 {
8372 // TODO: should only expand when appropriate for the command
8373 eap->arg = skiptowhite(eap->arg);
8374 has_expr = TRUE;
8375 }
8376
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008377 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008378 {
8379 int count = 0;
8380 char_u *start = skipwhite(line);
8381
8382 // :cmd xxx`=expr1`yyy`=expr2`zzz
8383 // PUSHS ":cmd xxx"
8384 // eval expr1
8385 // PUSHS "yyy"
8386 // eval expr2
8387 // PUSHS "zzz"
8388 // EXECCONCAT 5
8389 for (;;)
8390 {
8391 if (p > start)
8392 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008393 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008394 ++count;
8395 }
8396 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008397 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008398 return NULL;
8399 may_generate_2STRING(-1, cctx);
8400 ++count;
8401 p = skipwhite(p);
8402 if (*p != '`')
8403 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008404 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008405 return NULL;
8406 }
8407 start = p + 1;
8408
8409 p = (char_u *)strstr((char *)start, "`=");
8410 if (p == NULL)
8411 {
8412 if (*skipwhite(start) != NUL)
8413 {
8414 generate_PUSHS(cctx, vim_strsave(start));
8415 ++count;
8416 }
8417 break;
8418 }
8419 }
8420 generate_EXECCONCAT(cctx, count);
8421 }
8422 else
8423 generate_EXEC(cctx, line);
8424
8425theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008426 if (*nextcmd != NUL)
8427 {
8428 // the parser expects a pointer to the bar, put it back
8429 --nextcmd;
8430 *nextcmd = '|';
8431 }
8432
8433 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008434}
8435
8436/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008437 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008438 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008439 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008440 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008441add_def_function(ufunc_T *ufunc)
8442{
8443 dfunc_T *dfunc;
8444
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008445 if (def_functions.ga_len == 0)
8446 {
8447 // The first position is not used, so that a zero uf_dfunc_idx means it
8448 // wasn't set.
8449 if (ga_grow(&def_functions, 1) == FAIL)
8450 return FAIL;
8451 ++def_functions.ga_len;
8452 }
8453
Bram Moolenaar09689a02020-05-09 22:50:08 +02008454 // Add the function to "def_functions".
8455 if (ga_grow(&def_functions, 1) == FAIL)
8456 return FAIL;
8457 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8458 CLEAR_POINTER(dfunc);
8459 dfunc->df_idx = def_functions.ga_len;
8460 ufunc->uf_dfunc_idx = dfunc->df_idx;
8461 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008462 dfunc->df_name = vim_strsave(ufunc->uf_name);
8463 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008464 ++def_functions.ga_len;
8465 return OK;
8466}
8467
8468/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008469 * After ex_function() has collected all the function lines: parse and compile
8470 * the lines into instructions.
8471 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008472 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8473 * the return statement (used for lambda). When uf_ret_type is already set
8474 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008475 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008476 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008477 * This can be used recursively through compile_lambda(), which may reallocate
8478 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008479 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008480 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008481 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008482compile_def_function(
8483 ufunc_T *ufunc,
8484 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008485 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008486 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 char_u *line = NULL;
8489 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008491 cctx_T cctx;
8492 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008493 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008494 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495 int ret = FAIL;
8496 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008497 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008498 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008499 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008500#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008501 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008502#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008504 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008505 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008506 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008507 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008508 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8509 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008510 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008512 else
8513 {
8514 if (add_def_function(ufunc) == FAIL)
8515 return FAIL;
8516 new_def_function = TRUE;
8517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008518
Bram Moolenaar985116a2020-07-12 17:31:09 +02008519 ufunc->uf_def_status = UF_COMPILING;
8520
Bram Moolenaara80faa82020-04-12 19:37:17 +02008521 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008522
Bram Moolenaarf002a412021-01-24 13:34:18 +01008523#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008524 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008525#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008526 cctx.ctx_ufunc = ufunc;
8527 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008528 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8530 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8531 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8532 cctx.ctx_type_list = &ufunc->uf_type_list;
8533 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8534 instr = &cctx.ctx_instr;
8535
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008536 // Set the context to the function, it may be compiled when called from
8537 // another script. Set the script version to the most modern one.
8538 // The line number will be set in next_line_from_context().
8539 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8541
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008542 // Make sure error messages are OK.
8543 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8544 if (do_estack_push)
8545 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008546 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008547
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008548 if (ufunc->uf_def_args.ga_len > 0)
8549 {
8550 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008551 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008552 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008553 int i;
8554 char_u *arg;
8555 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008556 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008557
8558 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008559 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008560 for (i = 0; i < count; ++i)
8561 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008562 garray_T *stack = &cctx.ctx_type_stack;
8563 type_T *val_type;
8564 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008565 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008566 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008567 int jump_instr_idx = instr->ga_len;
8568 isn_T *isn;
8569
8570 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8571 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8572 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008573
8574 // Make sure later arguments are not found.
8575 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008576
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008577 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008578 r = compile_expr0(&arg, &cctx);
8579
8580 ufunc->uf_args.ga_len = uf_args_len;
8581 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008582 goto erret;
8583
8584 // If no type specified use the type of the default value.
8585 // Otherwise check that the default value type matches the
8586 // specified type.
8587 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008588 where.wt_index = arg_idx + 1;
8589 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008590 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008591 {
8592 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008593 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008594 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008595 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008596 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008597 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008598
8599 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008600 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008601
8602 // set instruction index in JUMP_IF_ARG_SET to here
8603 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8604 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008605 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008606
8607 if (did_set_arg_type)
8608 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008609 }
8610
8611 /*
8612 * Loop over all the lines of the function and generate instructions.
8613 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614 for (;;)
8615 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008616 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008617 int starts_with_colon = FALSE;
8618 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008619 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008620
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008621 // Bail out on the first error to avoid a flood of errors and report
8622 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008623 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008624 goto erret;
8625
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008626 if (line != NULL && *line == '|')
8627 // the line continues after a '|'
8628 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008629 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008630 && !(*line == '#' && (line == cctx.ctx_line_start
8631 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008632 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008633 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 goto erret;
8635 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008636 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8637 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 else
8639 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008640 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008641 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008642 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008643 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008644#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008645 if (cctx.ctx_skip != SKIP_YES)
8646 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008647#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008649 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 }
8651
Bram Moolenaara80faa82020-04-12 19:37:17 +02008652 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653 ea.cmdlinep = &line;
8654 ea.cmd = skipwhite(line);
8655
Bram Moolenaarb2049902021-01-24 12:53:53 +01008656 if (*ea.cmd == '#')
8657 {
8658 // "#" starts a comment
8659 line = (char_u *)"";
8660 continue;
8661 }
8662
Bram Moolenaarf002a412021-01-24 13:34:18 +01008663#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008664 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8665 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008666 {
8667 may_generate_prof_end(&cctx, prof_lnum);
8668
8669 prof_lnum = cctx.ctx_lnum;
8670 generate_instr(&cctx, ISN_PROF_START);
8671 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008672#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008673
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008674 // Some things can be recognized by the first character.
8675 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008677 case '}':
8678 {
8679 // "}" ends a block scope
8680 scopetype_T stype = cctx.ctx_scope == NULL
8681 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008682
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008683 if (stype == BLOCK_SCOPE)
8684 {
8685 compile_endblock(&cctx);
8686 line = ea.cmd;
8687 }
8688 else
8689 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008690 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008691 goto erret;
8692 }
8693 if (line != NULL)
8694 line = skipwhite(ea.cmd + 1);
8695 continue;
8696 }
8697
8698 case '{':
8699 // "{" starts a block scope
8700 // "{'a': 1}->func() is something else
8701 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8702 {
8703 line = compile_block(ea.cmd, &cctx);
8704 continue;
8705 }
8706 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008707 }
8708
8709 /*
8710 * COMMAND MODIFIERS
8711 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008712 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008713 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8714 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 {
8716 if (errormsg != NULL)
8717 goto erret;
8718 // empty line or comment
8719 line = (char_u *)"";
8720 continue;
8721 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008722 generate_cmdmods(&cctx, &local_cmdmod);
8723 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008724
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008725 // Check if there was a colon after the last command modifier or before
8726 // the current position.
8727 for (p = ea.cmd; p >= line; --p)
8728 {
8729 if (*p == ':')
8730 starts_with_colon = TRUE;
8731 if (p < ea.cmd && !VIM_ISWHITE(*p))
8732 break;
8733 }
8734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008736 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008737 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008738 {
8739 if (*ea.cmd == '(')
8740 // not for "call()"
8741 ea.cmd = p;
8742 else
8743 ea.cmd = skipwhite(ea.cmd);
8744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008745
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008746 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008748 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008749
Bram Moolenaar17126b12021-01-07 22:03:02 +01008750 // Check for assignment after command modifiers.
8751 assign = may_compile_assignment(&ea, &line, &cctx);
8752 if (assign == OK)
8753 goto nextline;
8754 if (assign == FAIL)
8755 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008756 }
8757
8758 /*
8759 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008760 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008761 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008762 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008763 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008764 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008765 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008766 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008767 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008768 if (!starts_with_colon)
8769 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008770 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008771 goto erret;
8772 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008773 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008774 if (ends_excmd2(line, ea.cmd))
8775 {
8776 // A range without a command: jump to the line.
8777 // TODO: compile to a more efficient command, possibly
8778 // calling parse_cmd_address().
8779 ea.cmdidx = CMD_SIZE;
8780 line = compile_exec(line, &ea, &cctx);
8781 goto nextline;
8782 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008783 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008784 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008785 p = find_ex_command(&ea, NULL, starts_with_colon
8786 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008788 if (p == NULL)
8789 {
8790 if (cctx.ctx_skip != SKIP_YES)
8791 emsg(_(e_ambiguous_use_of_user_defined_command));
8792 goto erret;
8793 }
8794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008795 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8796 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008797 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008798 {
8799 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008800 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008801 }
8802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008803 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008804 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008806 // CMD_var cannot happen, compile_assignment() above would be
8807 // used. Most likely an assignment to a non-existing variable.
8808 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008809 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008811 }
8812
Bram Moolenaar3988f642020-08-27 22:43:03 +02008813 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008814 && ea.cmdidx != CMD_elseif
8815 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008816 && ea.cmdidx != CMD_endif
8817 && ea.cmdidx != CMD_endfor
8818 && ea.cmdidx != CMD_endwhile
8819 && ea.cmdidx != CMD_catch
8820 && ea.cmdidx != CMD_finally
8821 && ea.cmdidx != CMD_endtry)
8822 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008823 emsg(_(e_unreachable_code_after_return));
8824 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008825 }
8826
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008827 p = skipwhite(p);
8828 if (ea.cmdidx != CMD_SIZE
8829 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8830 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008831 if (ea.cmdidx >= 0)
8832 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008833 if ((ea.argt & EX_BANG) && *p == '!')
8834 {
8835 ea.forceit = TRUE;
8836 p = skipwhite(p + 1);
8837 }
8838 }
8839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008840 switch (ea.cmdidx)
8841 {
8842 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008843 ea.arg = p;
8844 line = compile_nested_function(&ea, &cctx);
8845 break;
8846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008847 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008848 // TODO: should we allow this, e.g. to declare a global
8849 // function?
8850 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 goto erret;
8852
8853 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008854 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008855 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856 break;
8857
8858 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008859 emsg(_(e_cannot_use_let_in_vim9_script));
8860 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008861 case CMD_var:
8862 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008863 case CMD_const:
8864 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008865 if (line == p)
8866 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008867 break;
8868
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008869 case CMD_unlet:
8870 case CMD_unlockvar:
8871 case CMD_lockvar:
8872 line = compile_unletlock(p, &ea, &cctx);
8873 break;
8874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875 case CMD_import:
8876 line = compile_import(p, &cctx);
8877 break;
8878
8879 case CMD_if:
8880 line = compile_if(p, &cctx);
8881 break;
8882 case CMD_elseif:
8883 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008884 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008885 break;
8886 case CMD_else:
8887 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008888 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889 break;
8890 case CMD_endif:
8891 line = compile_endif(p, &cctx);
8892 break;
8893
8894 case CMD_while:
8895 line = compile_while(p, &cctx);
8896 break;
8897 case CMD_endwhile:
8898 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008899 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008900 break;
8901
8902 case CMD_for:
8903 line = compile_for(p, &cctx);
8904 break;
8905 case CMD_endfor:
8906 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008907 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008908 break;
8909 case CMD_continue:
8910 line = compile_continue(p, &cctx);
8911 break;
8912 case CMD_break:
8913 line = compile_break(p, &cctx);
8914 break;
8915
8916 case CMD_try:
8917 line = compile_try(p, &cctx);
8918 break;
8919 case CMD_catch:
8920 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008921 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922 break;
8923 case CMD_finally:
8924 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008925 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008926 break;
8927 case CMD_endtry:
8928 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008929 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930 break;
8931 case CMD_throw:
8932 line = compile_throw(p, &cctx);
8933 break;
8934
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008935 case CMD_eval:
8936 if (compile_expr0(&p, &cctx) == FAIL)
8937 goto erret;
8938
Bram Moolenaar3988f642020-08-27 22:43:03 +02008939 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008940 generate_instr_drop(&cctx, ISN_DROP, 1);
8941
8942 line = skipwhite(p);
8943 break;
8944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008945 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008946 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008947 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008948 case CMD_echomsg:
8949 case CMD_echoerr:
8950 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008951 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008953 case CMD_put:
8954 ea.cmd = cmd;
8955 line = compile_put(p, &ea, &cctx);
8956 break;
8957
Bram Moolenaar3988f642020-08-27 22:43:03 +02008958 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008959
Bram Moolenaarae616492020-07-28 20:07:27 +02008960 case CMD_append:
8961 case CMD_change:
8962 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008963 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008964 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008965 case CMD_xit:
8966 not_in_vim9(&ea);
8967 goto erret;
8968
Bram Moolenaar002262f2020-07-08 17:47:57 +02008969 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008970 if (cctx.ctx_skip != SKIP_YES)
8971 {
8972 semsg(_(e_invalid_command_str), ea.cmd);
8973 goto erret;
8974 }
8975 // We don't check for a next command here.
8976 line = (char_u *)"";
8977 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008979 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008980 if (cctx.ctx_skip == SKIP_YES)
8981 {
8982 // We don't check for a next command here.
8983 line = (char_u *)"";
8984 }
8985 else
8986 {
8987 // Not recognized, execute with do_cmdline_cmd().
8988 ea.arg = p;
8989 line = compile_exec(line, &ea, &cctx);
8990 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008991 break;
8992 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008993nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008994 if (line == NULL)
8995 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008996 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008997
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008998 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008999 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02009000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009001 if (cctx.ctx_type_stack.ga_len < 0)
9002 {
9003 iemsg("Type stack underflow");
9004 goto erret;
9005 }
9006 }
9007
9008 if (cctx.ctx_scope != NULL)
9009 {
9010 if (cctx.ctx_scope->se_type == IF_SCOPE)
9011 emsg(_(e_endif));
9012 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
9013 emsg(_(e_endwhile));
9014 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
9015 emsg(_(e_endfor));
9016 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009017 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018 goto erret;
9019 }
9020
Bram Moolenaarefd88552020-06-18 20:50:10 +02009021 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022 {
9023 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
9024 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02009025 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009026 goto erret;
9027 }
9028
9029 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01009030 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009031 }
9032
Bram Moolenaar599410c2021-04-10 14:03:43 +02009033 // When compiled with ":silent!" and there was an error don't consider the
9034 // function compiled.
9035 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009036 {
9037 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9038 + ufunc->uf_dfunc_idx;
9039 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009040 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009041#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009042 if (cctx.ctx_profiling)
9043 {
9044 dfunc->df_instr_prof = instr->ga_data;
9045 dfunc->df_instr_prof_count = instr->ga_len;
9046 }
9047 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009048#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009049 {
9050 dfunc->df_instr = instr->ga_data;
9051 dfunc->df_instr_count = instr->ga_len;
9052 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009053 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009054 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009055 if (cctx.ctx_outer_used)
9056 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009057 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059
9060 ret = OK;
9061
9062erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009063 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009064 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009065 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009066 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9067 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009068
9069 for (idx = 0; idx < instr->ga_len; ++idx)
9070 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009071 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009072 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009073
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009074 // If using the last entry in the table and it was added above, we
9075 // might as well remove it.
9076 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009077 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009078 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009079 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009080 ufunc->uf_dfunc_idx = 0;
9081 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009082 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009083
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009084 while (cctx.ctx_scope != NULL)
9085 drop_scope(&cctx);
9086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087 if (errormsg != NULL)
9088 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009089 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009090 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091 }
9092
9093 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009094 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009095 if (do_estack_push)
9096 estack_pop();
9097
Bram Moolenaar20431c92020-03-20 18:39:46 +01009098 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009099 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009100 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009101 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009102}
9103
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009104 void
9105set_function_type(ufunc_T *ufunc)
9106{
9107 int varargs = ufunc->uf_va_name != NULL;
9108 int argcount = ufunc->uf_args.ga_len;
9109
9110 // Create a type for the function, with the return type and any
9111 // argument types.
9112 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9113 // The type is included in "tt_args".
9114 if (argcount > 0 || varargs)
9115 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009116 if (ufunc->uf_type_list.ga_itemsize == 0)
9117 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009118 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9119 argcount, &ufunc->uf_type_list);
9120 // Add argument types to the function type.
9121 if (func_type_add_arg_types(ufunc->uf_func_type,
9122 argcount + varargs,
9123 &ufunc->uf_type_list) == FAIL)
9124 return;
9125 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9126 ufunc->uf_func_type->tt_min_argcount =
9127 argcount - ufunc->uf_def_args.ga_len;
9128 if (ufunc->uf_arg_types == NULL)
9129 {
9130 int i;
9131
9132 // lambda does not have argument types.
9133 for (i = 0; i < argcount; ++i)
9134 ufunc->uf_func_type->tt_args[i] = &t_any;
9135 }
9136 else
9137 mch_memmove(ufunc->uf_func_type->tt_args,
9138 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9139 if (varargs)
9140 {
9141 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009142 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009143 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9144 }
9145 }
9146 else
9147 // No arguments, can use a predefined type.
9148 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9149 argcount, &ufunc->uf_type_list);
9150}
9151
9152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009153/*
9154 * Delete an instruction, free what it contains.
9155 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009156 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009157delete_instr(isn_T *isn)
9158{
9159 switch (isn->isn_type)
9160 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009161 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009162 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009163 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009164 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009165 case ISN_LOADENV:
9166 case ISN_LOADG:
9167 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009168 case ISN_LOADT:
9169 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009170 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009171 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009172 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009173 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009174 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009175 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009176 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009177 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009178 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009179 case ISN_STOREW:
9180 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009181 vim_free(isn->isn_arg.string);
9182 break;
9183
9184 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009185 case ISN_STORES:
9186 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009187 break;
9188
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009189 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009190 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009191 vim_free(isn->isn_arg.unlet.ul_name);
9192 break;
9193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009194 case ISN_STOREOPT:
9195 vim_free(isn->isn_arg.storeopt.so_name);
9196 break;
9197
9198 case ISN_PUSHBLOB: // push blob isn_arg.blob
9199 blob_unref(isn->isn_arg.blob);
9200 break;
9201
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009202 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009203#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009204 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009205#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009206 break;
9207
9208 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009209#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009210 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009211#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009212 break;
9213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 case ISN_UCALL:
9215 vim_free(isn->isn_arg.ufunc.cuf_name);
9216 break;
9217
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009218 case ISN_FUNCREF:
9219 {
9220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9221 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009222 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009223
Bram Moolenaar077a4232020-12-22 18:33:27 +01009224 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9225 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009226 }
9227 break;
9228
9229 case ISN_DCALL:
9230 {
9231 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9232 + isn->isn_arg.dfunc.cdf_idx;
9233
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009234 if (dfunc->df_ufunc != NULL
9235 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009236 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009237 }
9238 break;
9239
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009240 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009241 {
9242 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9243 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9244
9245 if (ufunc != NULL)
9246 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009247 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009248 func_ptr_unref(ufunc);
9249 }
9250
9251 vim_free(lambda);
9252 vim_free(isn->isn_arg.newfunc.nf_global);
9253 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009254 break;
9255
Bram Moolenaar5e654232020-09-16 15:22:00 +02009256 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009257 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009258 free_type(isn->isn_arg.type.ct_type);
9259 break;
9260
Bram Moolenaar02194d22020-10-24 23:08:38 +02009261 case ISN_CMDMOD:
9262 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9263 ->cmod_filter_regmatch.regprog);
9264 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9265 break;
9266
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009267 case ISN_LOADSCRIPT:
9268 case ISN_STORESCRIPT:
9269 vim_free(isn->isn_arg.script.scriptref);
9270 break;
9271
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009272 case ISN_TRY:
9273 vim_free(isn->isn_arg.try.try_ref);
9274 break;
9275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 case ISN_2BOOL:
9277 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009278 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case ISN_ADDBLOB:
9280 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009281 case ISN_ANYINDEX:
9282 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009283 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009284 case ISN_BLOBAPPEND:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02009285 case ISN_BLOBINDEX:
9286 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009287 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009288 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009290 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009291 case ISN_COMPAREANY:
9292 case ISN_COMPAREBLOB:
9293 case ISN_COMPAREBOOL:
9294 case ISN_COMPAREDICT:
9295 case ISN_COMPAREFLOAT:
9296 case ISN_COMPAREFUNC:
9297 case ISN_COMPARELIST:
9298 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009299 case ISN_COMPARESPECIAL:
9300 case ISN_COMPARESTRING:
9301 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009302 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009303 case ISN_DROP:
9304 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009305 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009306 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009307 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009308 case ISN_EXECCONCAT:
9309 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009310 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009311 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009312 case ISN_GETITEM:
9313 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009314 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009315 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009316 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009317 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009318 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009319 case ISN_LOADBDICT:
9320 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009321 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009322 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009323 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009324 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009325 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009326 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009327 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009328 case ISN_NEGATENR:
9329 case ISN_NEWDICT:
9330 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009331 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009332 case ISN_OPFLOAT:
9333 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009335 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009336 case ISN_PROF_END:
9337 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009338 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009339 case ISN_PUSHF:
9340 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009341 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009342 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009343 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009344 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009345 case ISN_SHUFFLE:
9346 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009347 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009348 case ISN_STOREINDEX:
Bram Moolenaar68452172021-04-12 21:21:02 +02009349 case ISN_STORERANGE:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009350 case ISN_STORENR:
9351 case ISN_STOREOUTER:
9352 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009353 case ISN_STOREV:
9354 case ISN_STRINDEX:
9355 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009356 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009357 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009358 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009359 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009360 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361 // nothing allocated
9362 break;
9363 }
9364}
9365
9366/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009367 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009368 */
9369 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009370delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009371{
9372 int idx;
9373
9374 ga_clear(&dfunc->df_def_args_isn);
9375
9376 if (dfunc->df_instr != NULL)
9377 {
9378 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9379 delete_instr(dfunc->df_instr + idx);
9380 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009381 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009382 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009383#ifdef FEAT_PROFILE
9384 if (dfunc->df_instr_prof != NULL)
9385 {
9386 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9387 delete_instr(dfunc->df_instr_prof + idx);
9388 VIM_CLEAR(dfunc->df_instr_prof);
9389 dfunc->df_instr_prof = NULL;
9390 }
9391#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009392
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009393 if (mark_deleted)
9394 dfunc->df_deleted = TRUE;
9395 if (dfunc->df_ufunc != NULL)
9396 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009397}
9398
9399/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009400 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009401 * function, unless another user function still uses it.
9402 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009403 */
9404 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009405unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009406{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009407 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009408 {
9409 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9410 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009411
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009412 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009413 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009414 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009415 ufunc->uf_dfunc_idx = 0;
9416 if (dfunc->df_ufunc == ufunc)
9417 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009418 }
9419}
9420
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009421/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009422 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009423 */
9424 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009425link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009426{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009427 if (ufunc->uf_dfunc_idx > 0)
9428 {
9429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9430 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009431
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009432 ++dfunc->df_refcount;
9433 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009434}
9435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009436#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009437/*
9438 * Free all functions defined with ":def".
9439 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009440 void
9441free_def_functions(void)
9442{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009443 int idx;
9444
9445 for (idx = 0; idx < def_functions.ga_len; ++idx)
9446 {
9447 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9448
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009449 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009450 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009451 }
9452
9453 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009454}
9455#endif
9456
9457
9458#endif // FEAT_EVAL