blob: 00a8f56469a29610934b3e1f06cfeb317175fe1b [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 Moolenaar15e5e532021-04-07 21:21:13 +0200425 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100426 {
427 if (is_arg)
428 semsg(_(e_argument_already_declared_in_script_str), p);
429 else
430 semsg(_(e_variable_already_declared_in_script_str), p);
431 return FAIL;
432 }
433
Bram Moolenaarad486a02020-08-01 23:22:18 +0200434 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100435 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100436 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200437 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200439 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200441 // A local or script-local function can shadow a global function.
442 if (ufunc == NULL || !func_is_global(ufunc)
443 || (p[0] == 'g' && p[1] == ':'))
444 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 if (is_arg)
446 semsg(_(e_argument_name_shadows_existing_variable_str), p);
447 else
448 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200449 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200450 return FAIL;
451 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100452 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200453 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100454 return OK;
455}
456
Bram Moolenaar65b95452020-07-19 14:03:09 +0200457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458/////////////////////////////////////////////////////////////////////
459// Following generate_ functions expect the caller to call ga_grow().
460
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200461#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
462#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464/*
465 * Generate an instruction without arguments.
466 * Returns a pointer to the new instruction, NULL if failed.
467 */
468 static isn_T *
469generate_instr(cctx_T *cctx, isntype_T isn_type)
470{
471 garray_T *instr = &cctx->ctx_instr;
472 isn_T *isn;
473
Bram Moolenaar080457c2020-03-03 21:53:32 +0100474 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 if (ga_grow(instr, 1) == FAIL)
476 return NULL;
477 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
478 isn->isn_type = isn_type;
479 isn->isn_lnum = cctx->ctx_lnum + 1;
480 ++instr->ga_len;
481
482 return isn;
483}
484
485/*
486 * Generate an instruction without arguments.
487 * "drop" will be removed from the stack.
488 * Returns a pointer to the new instruction, NULL if failed.
489 */
490 static isn_T *
491generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
492{
493 garray_T *stack = &cctx->ctx_type_stack;
494
Bram Moolenaar080457c2020-03-03 21:53:32 +0100495 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 stack->ga_len -= drop;
497 return generate_instr(cctx, isn_type);
498}
499
500/*
501 * Generate instruction "isn_type" and put "type" on the type stack.
502 */
503 static isn_T *
504generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
505{
506 isn_T *isn;
507 garray_T *stack = &cctx->ctx_type_stack;
508
509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
510 return NULL;
511
512 if (ga_grow(stack, 1) == FAIL)
513 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200514 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 ++stack->ga_len;
516
517 return isn;
518}
519
520/*
521 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200522 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 */
524 static int
525may_generate_2STRING(int offset, cctx_T *cctx)
526{
527 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200528 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100530 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100532 RETURN_OK_IF_SKIP(cctx);
533 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200534 switch ((*type)->tt_type)
535 {
536 // nothing to be done
537 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200539 // conversion possible
540 case VAR_SPECIAL:
541 case VAR_BOOL:
542 case VAR_NUMBER:
543 case VAR_FLOAT:
544 break;
545
546 // conversion possible (with runtime check)
547 case VAR_ANY:
548 case VAR_UNKNOWN:
549 isntype = ISN_2STRING_ANY;
550 break;
551
552 // conversion not possible
553 case VAR_VOID:
554 case VAR_BLOB:
555 case VAR_FUNC:
556 case VAR_PARTIAL:
557 case VAR_LIST:
558 case VAR_DICT:
559 case VAR_JOB:
560 case VAR_CHANNEL:
561 to_string_error((*type)->tt_type);
562 return FAIL;
563 }
564
565 *type = &t_string;
566 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 return FAIL;
568 isn->isn_arg.number = offset;
569
570 return OK;
571}
572
573 static int
574check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
575{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200578 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 {
580 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200581 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200583 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200589 static int
590generate_add_instr(
591 cctx_T *cctx,
592 vartype_T vartype,
593 type_T *type1,
594 type_T *type2)
595{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100596 garray_T *stack = &cctx->ctx_type_stack;
597 isn_T *isn = generate_instr_drop(cctx,
598 vartype == VAR_NUMBER ? ISN_OPNR
599 : vartype == VAR_LIST ? ISN_ADDLIST
600 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200601#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100602 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200603#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100604 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605
606 if (vartype != VAR_LIST && vartype != VAR_BLOB
607 && type1->tt_type != VAR_ANY
608 && type2->tt_type != VAR_ANY
609 && check_number_or_float(
610 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
611 return FAIL;
612
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100615
616 // When concatenating two lists with different member types the member type
617 // becomes "any".
618 if (vartype == VAR_LIST
619 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
620 && type1->tt_member != type2->tt_member)
621 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
622
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200623 return isn == NULL ? FAIL : OK;
624}
625
626/*
627 * Get the type to use for an instruction for an operation on "type1" and
628 * "type2". If they are matching use a type-specific instruction. Otherwise
629 * fall back to runtime type checking.
630 */
631 static vartype_T
632operator_type(type_T *type1, type_T *type2)
633{
634 if (type1->tt_type == type2->tt_type
635 && (type1->tt_type == VAR_NUMBER
636 || type1->tt_type == VAR_LIST
637#ifdef FEAT_FLOAT
638 || type1->tt_type == VAR_FLOAT
639#endif
640 || type1->tt_type == VAR_BLOB))
641 return type1->tt_type;
642 return VAR_ANY;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Generate an instruction with two arguments. The instruction depends on the
647 * type of the arguments.
648 */
649 static int
650generate_two_op(cctx_T *cctx, char_u *op)
651{
652 garray_T *stack = &cctx->ctx_type_stack;
653 type_T *type1;
654 type_T *type2;
655 vartype_T vartype;
656 isn_T *isn;
657
Bram Moolenaar080457c2020-03-03 21:53:32 +0100658 RETURN_OK_IF_SKIP(cctx);
659
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
662 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 switch (*op)
666 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 case '+':
668 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 break;
671
672 case '-':
673 case '*':
674 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
675 op) == FAIL)
676 return FAIL;
677 if (vartype == VAR_NUMBER)
678 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
679#ifdef FEAT_FLOAT
680 else if (vartype == VAR_FLOAT)
681 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
682#endif
683 else
684 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
685 if (isn != NULL)
686 isn->isn_arg.op.op_type = *op == '*'
687 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
688 break;
689
Bram Moolenaar4c683752020-04-05 21:38:23 +0200690 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200692 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 && type2->tt_type != VAR_NUMBER))
694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200695 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 return FAIL;
697 }
698 isn = generate_instr_drop(cctx,
699 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_REM;
702 break;
703 }
704
705 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200706 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 type_T *type = &t_any;
709
710#ifdef FEAT_FLOAT
711 // float+number and number+float results in float
712 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
713 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
714 type = &t_float;
715#endif
716 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
717 }
718
719 return OK;
720}
721
722/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 * Get the instruction to use for comparing "type1" with "type2"
724 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200726 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100727get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728{
729 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730
Bram Moolenaar4c683752020-04-05 21:38:23 +0200731 if (type1 == VAR_UNKNOWN)
732 type1 = VAR_ANY;
733 if (type2 == VAR_UNKNOWN)
734 type2 = VAR_ANY;
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if (type1 == type2)
737 {
738 switch (type1)
739 {
740 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
741 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
742 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
743 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
744 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
745 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
746 case VAR_LIST: isntype = ISN_COMPARELIST; break;
747 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
748 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 default: isntype = ISN_COMPAREANY; break;
750 }
751 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200752 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100754 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 isntype = ISN_COMPAREANY;
756
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 && (isntype == ISN_COMPAREBOOL
759 || isntype == ISN_COMPARESPECIAL
760 || isntype == ISN_COMPARENR
761 || isntype == ISN_COMPAREFLOAT))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200765 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 }
767 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
770 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
772 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BLOB || type2 == VAR_BLOB
774 || type1 == VAR_LIST || type2 == VAR_LIST))))
775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200776 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200778 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 return isntype;
781}
782
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200783 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200785{
786 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
787 return FAIL;
788 return OK;
789}
790
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791/*
792 * Generate an ISN_COMPARE* instruction with a boolean result.
793 */
794 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200796{
797 isntype_T isntype;
798 isn_T *isn;
799 garray_T *stack = &cctx->ctx_type_stack;
800 vartype_T type1;
801 vartype_T type2;
802
803 RETURN_OK_IF_SKIP(cctx);
804
805 // Get the known type of the two items on the stack. If they are matching
806 // use a type-specific instruction. Otherwise fall back to runtime type
807 // checking.
808 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
809 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100810 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 if (isntype == ISN_DROP)
812 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 if ((isn = generate_instr(cctx, isntype)) == NULL)
815 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100816 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 isn->isn_arg.op.op_ic = ic;
818
819 // takes two arguments, puts one bool back
820 if (stack->ga_len >= 2)
821 {
822 --stack->ga_len;
823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
824 }
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_2BOOL instruction.
831 */
832 static int
833generate_2BOOL(cctx_T *cctx, int invert)
834{
835 isn_T *isn;
836 garray_T *stack = &cctx->ctx_type_stack;
837
Bram Moolenaar080457c2020-03-03 21:53:32 +0100838 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
840 return FAIL;
841 isn->isn_arg.number = invert;
842
843 // type becomes bool
844 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
845
846 return OK;
847}
848
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200849/*
850 * Generate an ISN_COND2BOOL instruction.
851 */
852 static int
853generate_COND2BOOL(cctx_T *cctx)
854{
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857
858 RETURN_OK_IF_SKIP(cctx);
859 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
860 return FAIL;
861
862 // type becomes bool
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864
865 return OK;
866}
867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200869generate_TYPECHECK(
870 cctx_T *cctx,
871 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100872 int offset,
873 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
880 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200881 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100882 isn->isn_arg.type.ct_off = (int8_T)offset;
883 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 // type becomes expected
886 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 return OK;
889}
890
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100891 static int
892generate_SETTYPE(
893 cctx_T *cctx,
894 type_T *expected)
895{
896 isn_T *isn;
897
898 RETURN_OK_IF_SKIP(cctx);
899 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
900 return FAIL;
901 isn->isn_arg.type.ct_type = alloc_type(expected);
902 return OK;
903}
904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200906 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
907 * used. Return FALSE if the types will never match.
908 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100909 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910use_typecheck(type_T *actual, type_T *expected)
911{
912 if (actual->tt_type == VAR_ANY
913 || actual->tt_type == VAR_UNKNOWN
914 || (actual->tt_type == VAR_FUNC
915 && (expected->tt_type == VAR_FUNC
916 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100917 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
918 && ((actual->tt_member == &t_void)
919 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200920 return TRUE;
921 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
922 && actual->tt_type == expected->tt_type)
923 // This takes care of a nested list or dict.
924 return use_typecheck(actual->tt_member, expected->tt_member);
925 return FALSE;
926}
927
928/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200929 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200930 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 * - "actual" is a type that can be "expected" type: add a runtime check; or
932 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200933 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
934 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100936 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200937need_type(
938 type_T *actual,
939 type_T *expected,
940 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100941 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200942 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200943 int silent,
944 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200945{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100946 where_T where;
947
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200948 if (expected == &t_bool && actual != &t_bool
949 && (actual->tt_flags & TTFLAG_BOOL_OK))
950 {
951 // Using "0", "1" or the result of an expression with "&&" or "||" as a
952 // boolean is OK but requires a conversion.
953 generate_2BOOL(cctx, FALSE);
954 return OK;
955 }
956
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100957 where.wt_index = arg_idx;
958 where.wt_variable = FALSE;
959 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200960 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200961
962 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 // If it's a constant a runtime check makes no sense.
964 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100966 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200967 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200968 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200969
970 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100971 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200972 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200973}
974
975/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100976 * Check that the top of the type stack has a type that can be used as a
977 * condition. Give an error and return FAIL if not.
978 */
979 static int
980bool_on_stack(cctx_T *cctx)
981{
982 garray_T *stack = &cctx->ctx_type_stack;
983 type_T *type;
984
985 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
986 if (type == &t_bool)
987 return OK;
988
989 if (type == &t_any || type == &t_number)
990 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
991 // This requires a runtime type check.
992 return generate_COND2BOOL(cctx);
993
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Generate an ISN_PUSHNR instruction.
999 */
1000 static int
1001generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001004 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001012 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001013 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHBOOL instruction.
1019 */
1020 static int
1021generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1022{
1023 isn_T *isn;
1024
Bram Moolenaar080457c2020-03-03 21:53:32 +01001025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.number = number;
1029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_PUSHSPEC instruction.
1035 */
1036 static int
1037generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.number = number;
1045
1046 return OK;
1047}
1048
1049#ifdef FEAT_FLOAT
1050/*
1051 * Generate an ISN_PUSHF instruction.
1052 */
1053 static int
1054generate_PUSHF(cctx_T *cctx, float_T fnumber)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.fnumber = fnumber;
1062
1063 return OK;
1064}
1065#endif
1066
1067/*
1068 * Generate an ISN_PUSHS instruction.
1069 * Consumes "str".
1070 */
1071 static int
1072generate_PUSHS(cctx_T *cctx, char_u *str)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar508b5612021-01-02 18:17:26 +01001076 if (cctx->ctx_skip == SKIP_YES)
1077 {
1078 vim_free(str);
1079 return OK;
1080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.string = str;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001089 * Generate an ISN_PUSHCHANNEL instruction.
1090 * Consumes "channel".
1091 */
1092 static int
1093generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1094{
1095 isn_T *isn;
1096
Bram Moolenaar080457c2020-03-03 21:53:32 +01001097 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001098 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1099 return FAIL;
1100 isn->isn_arg.channel = channel;
1101
1102 return OK;
1103}
1104
1105/*
1106 * Generate an ISN_PUSHJOB instruction.
1107 * Consumes "job".
1108 */
1109 static int
1110generate_PUSHJOB(cctx_T *cctx, job_T *job)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 return FAIL;
1117 isn->isn_arg.job = job;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Generate an ISN_PUSHBLOB instruction.
1124 * Consumes "blob".
1125 */
1126 static int
1127generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.blob = blob;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001140 * Generate an ISN_PUSHFUNC instruction with name "name".
1141 * Consumes "name".
1142 */
1143 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001144generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001149 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001150 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001151 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152
1153 return OK;
1154}
1155
1156/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001157 * Generate an ISN_GETITEM instruction with "index".
1158 */
1159 static int
1160generate_GETITEM(cctx_T *cctx, int index)
1161{
1162 isn_T *isn;
1163 garray_T *stack = &cctx->ctx_type_stack;
1164 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1165 type_T *item_type = &t_any;
1166
1167 RETURN_OK_IF_SKIP(cctx);
1168
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001169 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001170 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001171 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001172 emsg(_(e_listreq));
1173 return FAIL;
1174 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.number = index;
1179
1180 // add the item type to the type stack
1181 if (ga_grow(stack, 1) == FAIL)
1182 return FAIL;
1183 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1184 ++stack->ga_len;
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001189 * Generate an ISN_SLICE instruction with "count".
1190 */
1191 static int
1192generate_SLICE(cctx_T *cctx, int count)
1193{
1194 isn_T *isn;
1195
1196 RETURN_OK_IF_SKIP(cctx);
1197 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1198 return FAIL;
1199 isn->isn_arg.number = count;
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_CHECKLEN instruction with "min_len".
1205 */
1206 static int
1207generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1208{
1209 isn_T *isn;
1210
1211 RETURN_OK_IF_SKIP(cctx);
1212
1213 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.checklen.cl_min_len = min_len;
1216 isn->isn_arg.checklen.cl_more_OK = more_OK;
1217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 * Generate an ISN_STORE instruction.
1223 */
1224 static int
1225generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1226{
1227 isn_T *isn;
1228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1231 return FAIL;
1232 if (name != NULL)
1233 isn->isn_arg.string = vim_strsave(name);
1234 else
1235 isn->isn_arg.number = idx;
1236
1237 return OK;
1238}
1239
1240/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001241 * Generate an ISN_STOREOUTER instruction.
1242 */
1243 static int
1244generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1245{
1246 isn_T *isn;
1247
1248 RETURN_OK_IF_SKIP(cctx);
1249 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.outer.outer_idx = idx;
1252 isn->isn_arg.outer.outer_depth = level;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1259 */
1260 static int
1261generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1262{
1263 isn_T *isn;
1264
Bram Moolenaar080457c2020-03-03 21:53:32 +01001265 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1267 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001268 isn->isn_arg.storenr.stnr_idx = idx;
1269 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 return OK;
1272}
1273
1274/*
1275 * Generate an ISN_STOREOPT instruction
1276 */
1277 static int
1278generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1279{
1280 isn_T *isn;
1281
Bram Moolenaar080457c2020-03-03 21:53:32 +01001282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001283 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 return FAIL;
1285 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1286 isn->isn_arg.storeopt.so_flags = opt_flags;
1287
1288 return OK;
1289}
1290
1291/*
1292 * Generate an ISN_LOAD or similar instruction.
1293 */
1294 static int
1295generate_LOAD(
1296 cctx_T *cctx,
1297 isntype_T isn_type,
1298 int idx,
1299 char_u *name,
1300 type_T *type)
1301{
1302 isn_T *isn;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1306 return FAIL;
1307 if (name != NULL)
1308 isn->isn_arg.string = vim_strsave(name);
1309 else
1310 isn->isn_arg.number = idx;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001316 * Generate an ISN_LOADOUTER instruction
1317 */
1318 static int
1319generate_LOADOUTER(
1320 cctx_T *cctx,
1321 int idx,
1322 int nesting,
1323 type_T *type)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.outer.outer_idx = idx;
1331 isn->isn_arg.outer.outer_depth = nesting;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001353 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001379 * Generate an ISN_LOCKCONST instruction.
1380 */
1381 static int
1382generate_LOCKCONST(cctx_T *cctx)
1383{
1384 isn_T *isn;
1385
1386 RETURN_OK_IF_SKIP(cctx);
1387 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1388 return FAIL;
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 * Generate an ISN_LOADS instruction.
1394 */
1395 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400 int sid,
1401 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406 if (isn_type == ISN_LOADS)
1407 isn = generate_instr_type(cctx, isn_type, type);
1408 else
1409 isn = generate_instr_drop(cctx, isn_type, 1);
1410 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1413 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414
1415 return OK;
1416}
1417
1418/*
1419 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1420 */
1421 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 cctx_T *cctx,
1424 isntype_T isn_type,
1425 int sid,
1426 int idx,
1427 type_T *type)
1428{
1429 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001430 scriptref_T *sref;
1431 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if (isn_type == ISN_LOADSCRIPT)
1435 isn = generate_instr_type(cctx, isn_type, type);
1436 else
1437 isn = generate_instr_drop(cctx, isn_type, 1);
1438 if (isn == NULL)
1439 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001440
1441 // This requires three arguments, which doesn't fit in an instruction, thus
1442 // we need to allocate a struct for this.
1443 sref = ALLOC_ONE(scriptref_T);
1444 if (sref == NULL)
1445 return FAIL;
1446 isn->isn_arg.script.scriptref = sref;
1447 sref->sref_sid = sid;
1448 sref->sref_idx = idx;
1449 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001450 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_NEWLIST instruction.
1456 */
1457 static int
1458generate_NEWLIST(cctx_T *cctx, int count)
1459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 type_T *type;
1463 type_T *member;
1464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1467 return FAIL;
1468 isn->isn_arg.number = count;
1469
Bram Moolenaar127542b2020-08-09 17:22:04 +02001470 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001471 if (count == 0)
1472 member = &t_void;
1473 else
1474 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001475 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1476 cctx->ctx_type_list);
1477 type = get_list_type(member, cctx->ctx_type_list);
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // drop the value types
1480 stack->ga_len -= count;
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 // add the list type to the type stack
1483 if (ga_grow(stack, 1) == FAIL)
1484 return FAIL;
1485 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1486 ++stack->ga_len;
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_NEWDICT instruction.
1493 */
1494 static int
1495generate_NEWDICT(cctx_T *cctx, int count)
1496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 type_T *type;
1500 type_T *member;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1504 return FAIL;
1505 isn->isn_arg.number = count;
1506
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001507 if (count == 0)
1508 member = &t_void;
1509 else
1510 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001511 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1512 cctx->ctx_type_list);
1513 type = get_dict_type(member, cctx->ctx_type_list);
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 // drop the key and value types
1516 stack->ga_len -= 2 * count;
1517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // add the dict type to the type stack
1519 if (ga_grow(stack, 1) == FAIL)
1520 return FAIL;
1521 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1522 ++stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Generate an ISN_FUNCREF instruction.
1529 */
1530 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001531generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
Bram Moolenaar080457c2020-03-03 21:53:32 +01001536 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1538 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001539 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001540 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001542 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001543 // the nested context, including this one.
1544 if (ufunc->uf_flags & FC_CLOSURE)
1545 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if (ga_grow(stack, 1) == FAIL)
1548 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001549 ((type_T **)stack->ga_data)[stack->ga_len] =
1550 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 ++stack->ga_len;
1552
1553 return OK;
1554}
1555
1556/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001558 * "lambda_name" and "func_name" must be in allocated memory and will be
1559 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001560 */
1561 static int
1562generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1563{
1564 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001565
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001566 if (cctx->ctx_skip == SKIP_YES)
1567 {
1568 vim_free(lambda_name);
1569 vim_free(func_name);
1570 return OK;
1571 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 {
1574 vim_free(lambda_name);
1575 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 }
1578 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 isn->isn_arg.newfunc.nf_global = func_name;
1580
1581 return OK;
1582}
1583
1584/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001585 * Generate an ISN_DEF instruction: list functions
1586 */
1587 static int
1588generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1589{
1590 isn_T *isn;
1591
1592 RETURN_OK_IF_SKIP(cctx);
1593 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1594 return FAIL;
1595 if (len > 0)
1596 {
1597 isn->isn_arg.string = vim_strnsave(name, len);
1598 if (isn->isn_arg.string == NULL)
1599 return FAIL;
1600 }
1601 return OK;
1602}
1603
1604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 * Generate an ISN_JUMP instruction.
1606 */
1607 static int
1608generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1609{
1610 isn_T *isn;
1611 garray_T *stack = &cctx->ctx_type_stack;
1612
Bram Moolenaar080457c2020-03-03 21:53:32 +01001613 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.jump.jump_when = when;
1617 isn->isn_arg.jump.jump_where = where;
1618
1619 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1620 --stack->ga_len;
1621
1622 return OK;
1623}
1624
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001625/*
1626 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1627 */
1628 static int
1629generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1630{
1631 isn_T *isn;
1632
1633 RETURN_OK_IF_SKIP(cctx);
1634 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1637 // jump_where is set later
1638 return OK;
1639}
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 static int
1642generate_FOR(cctx_T *cctx, int loop_idx)
1643{
1644 isn_T *isn;
1645 garray_T *stack = &cctx->ctx_type_stack;
1646
Bram Moolenaar080457c2020-03-03 21:53:32 +01001647 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.forloop.for_idx = loop_idx;
1651
1652 if (ga_grow(stack, 1) == FAIL)
1653 return FAIL;
1654 // type doesn't matter, will be stored next
1655 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1656 ++stack->ga_len;
1657
1658 return OK;
1659}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001660/*
1661 * Generate an ISN_TRYCONT instruction.
1662 */
1663 static int
1664generate_TRYCONT(cctx_T *cctx, int levels, int where)
1665{
1666 isn_T *isn;
1667
1668 RETURN_OK_IF_SKIP(cctx);
1669 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.trycont.tct_levels = levels;
1672 isn->isn_arg.trycont.tct_where = where;
1673
1674 return OK;
1675}
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
1678/*
1679 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001680 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 * Return FAIL if the number of arguments is wrong.
1682 */
1683 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001684generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685{
1686 isn_T *isn;
1687 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001689 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001690 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar080457c2020-03-03 21:53:32 +01001692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001693 argoff = check_internal_func(func_idx, argcount);
1694 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 return FAIL;
1696
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 if (method_call && argoff > 1)
1698 {
1699 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.shuffle.shfl_item = argcount;
1702 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1703 }
1704
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 if (argcount > 0)
1706 {
1707 // Check the types of the arguments.
1708 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001709 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1710 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001711 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001712 if (internal_func_is_map(func_idx))
1713 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001714 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1717 return FAIL;
1718 isn->isn_arg.bfunc.cbf_idx = func_idx;
1719 isn->isn_arg.bfunc.cbf_argcount = argcount;
1720
Bram Moolenaar94738d82020-10-21 14:25:07 +02001721 // Drop the argument types and push the return type.
1722 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if (ga_grow(stack, 1) == FAIL)
1724 return FAIL;
1725 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001726 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001727 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001729 if (maptype != NULL && maptype->tt_member != NULL
1730 && maptype->tt_member != &t_any)
1731 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001732 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 return OK;
1735}
1736
1737/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001738 * Generate an ISN_LISTAPPEND instruction. Works like add().
1739 * Argument count is already checked.
1740 */
1741 static int
1742generate_LISTAPPEND(cctx_T *cctx)
1743{
1744 garray_T *stack = &cctx->ctx_type_stack;
1745 type_T *list_type;
1746 type_T *item_type;
1747 type_T *expected;
1748
1749 // Caller already checked that list_type is a list.
1750 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1751 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1752 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001753 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001754 return FAIL;
1755
1756 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1757 return FAIL;
1758
1759 --stack->ga_len; // drop the argument
1760 return OK;
1761}
1762
1763/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001764 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1765 * Argument count is already checked.
1766 */
1767 static int
1768generate_BLOBAPPEND(cctx_T *cctx)
1769{
1770 garray_T *stack = &cctx->ctx_type_stack;
1771 type_T *item_type;
1772
1773 // Caller already checked that blob_type is a blob.
1774 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001775 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001776 return FAIL;
1777
1778 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1779 return FAIL;
1780
1781 --stack->ga_len; // drop the argument
1782 return OK;
1783}
1784
1785/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 * Return TRUE if "ufunc" should be compiled, taking into account whether
1787 * "profile" indicates profiling is to be done.
1788 */
1789 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001790func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001791{
1792 switch (ufunc->uf_def_status)
1793 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001794 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001795 case UF_TO_BE_COMPILED: return TRUE;
1796 case UF_COMPILED:
1797 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001798#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001799 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1800 + ufunc->uf_dfunc_idx;
1801
1802 return profile ? dfunc->df_instr_prof == NULL
1803 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001804#else
1805 break;
1806#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001807 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001808 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001809 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001810 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001811}
1812
1813/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 * Generate an ISN_DCALL or ISN_UCALL instruction.
1815 * Return FAIL if the number of arguments is wrong.
1816 */
1817 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001818generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819{
1820 isn_T *isn;
1821 garray_T *stack = &cctx->ctx_type_stack;
1822 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001823 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if (argcount > regular_args && !has_varargs(ufunc))
1827 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001828 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return FAIL;
1830 }
1831 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1832 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001833 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 return FAIL;
1835 }
1836
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001837 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001838 {
1839 int i;
1840
1841 for (i = 0; i < argcount; ++i)
1842 {
1843 type_T *expected;
1844 type_T *actual;
1845
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001846 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1847 if (actual == &t_special
1848 && i >= regular_args - ufunc->uf_def_args.ga_len)
1849 {
1850 // assume v:none used for default argument value
1851 continue;
1852 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001853 if (i < regular_args)
1854 {
1855 if (ufunc->uf_arg_types == NULL)
1856 continue;
1857 expected = ufunc->uf_arg_types[i];
1858 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001859 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1860 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001861 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001862 else
1863 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001864 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001865 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001866 {
1867 arg_type_mismatch(expected, actual, i + 1);
1868 return FAIL;
1869 }
1870 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001871 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001872 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001873 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001874 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001875 }
1876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001878 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001879 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001881 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 {
1883 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1884 isn->isn_arg.dfunc.cdf_argcount = argcount;
1885 }
1886 else
1887 {
1888 // A user function may be deleted and redefined later, can't use the
1889 // ufunc pointer, need to look it up again at runtime.
1890 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1891 isn->isn_arg.ufunc.cuf_argcount = argcount;
1892 }
1893
1894 stack->ga_len -= argcount; // drop the arguments
1895 if (ga_grow(stack, 1) == FAIL)
1896 return FAIL;
1897 // add return value
1898 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1899 ++stack->ga_len;
1900
1901 return OK;
1902}
1903
1904/*
1905 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1906 */
1907 static int
1908generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1909{
1910 isn_T *isn;
1911 garray_T *stack = &cctx->ctx_type_stack;
1912
Bram Moolenaar080457c2020-03-03 21:53:32 +01001913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1915 return FAIL;
1916 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1917 isn->isn_arg.ufunc.cuf_argcount = argcount;
1918
1919 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001920 if (ga_grow(stack, 1) == FAIL)
1921 return FAIL;
1922 // add return value
1923 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1924 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925
1926 return OK;
1927}
1928
1929/*
1930 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001931 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 */
1933 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001934generate_PCALL(
1935 cctx_T *cctx,
1936 int argcount,
1937 char_u *name,
1938 type_T *type,
1939 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940{
1941 isn_T *isn;
1942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001943 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944
Bram Moolenaar080457c2020-03-03 21:53:32 +01001945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001946
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001947 if (type->tt_type == VAR_ANY)
1948 ret_type = &t_any;
1949 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001950 {
1951 if (type->tt_argcount != -1)
1952 {
1953 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1954
1955 if (argcount < type->tt_min_argcount - varargs)
1956 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001957 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001958 return FAIL;
1959 }
1960 if (!varargs && argcount > type->tt_argcount)
1961 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001962 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001963 return FAIL;
1964 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001965 if (type->tt_args != NULL)
1966 {
1967 int i;
1968
1969 for (i = 0; i < argcount; ++i)
1970 {
1971 int offset = -argcount + i - 1;
1972 type_T *actual = ((type_T **)stack->ga_data)[
1973 stack->ga_len + offset];
1974 type_T *expected;
1975
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001976 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001977 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001978 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001979 else if (i >= type->tt_min_argcount
1980 && actual == &t_special)
1981 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001982 else
1983 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001984 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001985 cctx, TRUE, FALSE) == FAIL)
1986 {
1987 arg_type_mismatch(expected, actual, i + 1);
1988 return FAIL;
1989 }
1990 }
1991 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001992 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001993 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001994 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001995 else
1996 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001997 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001998 return FAIL;
1999 }
2000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2002 return FAIL;
2003 isn->isn_arg.pfunc.cpf_top = at_top;
2004 isn->isn_arg.pfunc.cpf_argcount = argcount;
2005
2006 stack->ga_len -= argcount; // drop the arguments
2007
2008 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002009 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002011 // If partial is above the arguments it must be cleared and replaced with
2012 // the return value.
2013 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2014 return FAIL;
2015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 return OK;
2017}
2018
2019/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002020 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 */
2022 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002023generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024{
2025 isn_T *isn;
2026 garray_T *stack = &cctx->ctx_type_stack;
2027 type_T *type;
2028
Bram Moolenaar080457c2020-03-03 21:53:32 +01002029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002032 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002034 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002036 if (type->tt_type != VAR_DICT && type != &t_any)
2037 {
2038 emsg(_(e_dictreq));
2039 return FAIL;
2040 }
2041 // change dict type to dict member type
2042 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002043 {
2044 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2045 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047
2048 return OK;
2049}
2050
2051/*
2052 * Generate an ISN_ECHO instruction.
2053 */
2054 static int
2055generate_ECHO(cctx_T *cctx, int with_white, int count)
2056{
2057 isn_T *isn;
2058
Bram Moolenaar080457c2020-03-03 21:53:32 +01002059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2061 return FAIL;
2062 isn->isn_arg.echo.echo_with_white = with_white;
2063 isn->isn_arg.echo.echo_count = count;
2064
2065 return OK;
2066}
2067
Bram Moolenaarad39c092020-02-26 18:23:43 +01002068/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002069 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002070 */
2071 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002072generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002073{
2074 isn_T *isn;
2075
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077 return FAIL;
2078 isn->isn_arg.number = count;
2079
2080 return OK;
2081}
2082
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002083/*
2084 * Generate an ISN_PUT instruction.
2085 */
2086 static int
2087generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2088{
2089 isn_T *isn;
2090
2091 RETURN_OK_IF_SKIP(cctx);
2092 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2093 return FAIL;
2094 isn->isn_arg.put.put_regname = regname;
2095 isn->isn_arg.put.put_lnum = lnum;
2096 return OK;
2097}
2098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 static int
2100generate_EXEC(cctx_T *cctx, char_u *line)
2101{
2102 isn_T *isn;
2103
Bram Moolenaar080457c2020-03-03 21:53:32 +01002104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.string = vim_strsave(line);
2108 return OK;
2109}
2110
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002111 static int
2112generate_EXECCONCAT(cctx_T *cctx, int count)
2113{
2114 isn_T *isn;
2115
2116 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2117 return FAIL;
2118 isn->isn_arg.number = count;
2119 return OK;
2120}
2121
Bram Moolenaar08597872020-12-10 19:43:40 +01002122/*
2123 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2124 */
2125 static int
2126generate_RANGE(cctx_T *cctx, char_u *range)
2127{
2128 isn_T *isn;
2129 garray_T *stack = &cctx->ctx_type_stack;
2130
2131 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2132 return FAIL;
2133 isn->isn_arg.string = range;
2134
2135 if (ga_grow(stack, 1) == FAIL)
2136 return FAIL;
2137 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2138 ++stack->ga_len;
2139 return OK;
2140}
2141
Bram Moolenaar792f7862020-11-23 08:31:18 +01002142 static int
2143generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2144{
2145 isn_T *isn;
2146
2147 RETURN_OK_IF_SKIP(cctx);
2148 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2149 return FAIL;
2150 isn->isn_arg.unpack.unp_count = var_count;
2151 isn->isn_arg.unpack.unp_semicolon = semicolon;
2152 return OK;
2153}
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002156 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002157 */
2158 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002159generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002160{
2161 isn_T *isn;
2162
Bram Moolenaarfa984412021-03-25 22:15:28 +01002163 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002165 cctx->ctx_has_cmdmod = TRUE;
2166
2167 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002169 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2170 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2171 return FAIL;
2172 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002173 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002174 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002177 return OK;
2178}
2179
2180 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002181generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002182{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002183 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2184 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002185 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002186 return OK;
2187}
2188
Bram Moolenaarfa984412021-03-25 22:15:28 +01002189 static int
2190misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002191{
2192 garray_T *instr = &cctx->ctx_instr;
2193
Bram Moolenaara91a7132021-03-25 21:12:15 +01002194 if (cctx->ctx_has_cmdmod
2195 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2196 == ISN_CMDMOD)
2197 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002198 emsg(_(e_misplaced_command_modifier));
2199 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002200 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002201 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002202}
2203
2204/*
2205 * Get the index of the current instruction.
2206 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2207 */
2208 static int
2209current_instr_idx(cctx_T *cctx)
2210{
2211 garray_T *instr = &cctx->ctx_instr;
2212 int idx = instr->ga_len;
2213
2214 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2215 .isn_type == ISN_CMDMOD)
2216 --idx;
2217#ifdef FEAT_PROFILE
2218 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2219 .isn_type == ISN_PROF_START)
2220 --idx;
2221#endif
2222 return idx;
2223}
2224
Bram Moolenaarf002a412021-01-24 13:34:18 +01002225#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002226 static void
2227may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2228{
2229 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002230 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002231}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002232#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002233
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002236 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002238 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002239reserve_local(
2240 cctx_T *cctx,
2241 char_u *name,
2242 size_t len,
2243 int isConst,
2244 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 lvar_T *lvar;
2247
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002248 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002250 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002251 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
2253
2254 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002255 return NULL;
2256 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002257 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002259 // Every local variable uses the next entry on the stack. We could re-use
2260 // the last ones when leaving a scope, but then variables used in a closure
2261 // might get overwritten. To keep things simple do not re-use stack
2262 // entries. This is less efficient, but memory is cheap these days.
2263 lvar->lv_idx = cctx->ctx_locals_count++;
2264
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002265 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 lvar->lv_const = isConst;
2267 lvar->lv_type = type;
2268
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002269 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270}
2271
2272/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002273 * Remove local variables above "new_top".
2274 */
2275 static void
2276unwind_locals(cctx_T *cctx, int new_top)
2277{
2278 if (cctx->ctx_locals.ga_len > new_top)
2279 {
2280 int idx;
2281 lvar_T *lvar;
2282
2283 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2284 {
2285 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2286 vim_free(lvar->lv_name);
2287 }
2288 }
2289 cctx->ctx_locals.ga_len = new_top;
2290}
2291
2292/*
2293 * Free all local variables.
2294 */
2295 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002296free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002297{
2298 unwind_locals(cctx, 0);
2299 ga_clear(&cctx->ctx_locals);
2300}
2301
2302/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002303 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2304 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2305 * error if the variable was defined with :const.
2306 */
2307 static int
2308check_item_writable(svar_T *sv, int check_writable, char_u *name)
2309{
2310 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2311 || (check_writable == ASSIGN_FINAL
2312 && sv->sv_const == ASSIGN_CONST))
2313 {
2314 semsg(_(e_readonlyvar), name);
2315 return FAIL;
2316 }
2317 return OK;
2318}
2319
2320/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002322 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 * Returns the index in "sn_var_vals" if found.
2324 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002325 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 */
2327 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002328get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329{
2330 hashtab_T *ht;
2331 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002332 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002333 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 int idx;
2335
Bram Moolenaare3d46852020-08-29 13:39:17 +02002336 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002338 if (sid == current_sctx.sc_sid)
2339 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002340 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002341
2342 if (sav == NULL)
2343 return -2;
2344 idx = sav->sav_var_vals_idx;
2345 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002346 if (check_item_writable(sv, check_writable, name) == FAIL)
2347 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348 return idx;
2349 }
2350
2351 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 ht = &SCRIPT_VARS(sid);
2353 di = find_var_in_ht(ht, 0, name, TRUE);
2354 if (di == NULL)
2355 return -2;
2356
2357 // Now find the svar_T index in sn_var_vals.
2358 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2359 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002360 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 if (sv->sv_tv == &di->di_tv)
2362 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002363 if (check_item_writable(sv, check_writable, name) == FAIL)
2364 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 return idx;
2366 }
2367 }
2368 return -1;
2369}
2370
2371/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002372 * Find "name" in imported items of the current script or in "cctx" if not
2373 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 */
2375 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002376find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 int idx;
2379
Bram Moolenaare3d46852020-08-29 13:39:17 +02002380 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002381 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 if (cctx != NULL)
2383 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2384 {
2385 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2386 + idx;
2387
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002388 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2389 : STRLEN(import->imp_name) == len
2390 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 return import;
2392 }
2393
Bram Moolenaarefa94442020-08-08 22:16:00 +02002394 return find_imported_in_script(name, len, current_sctx.sc_sid);
2395}
2396
2397 imported_T *
2398find_imported_in_script(char_u *name, size_t len, int sid)
2399{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002400 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002401 int idx;
2402
Bram Moolenaare3d46852020-08-29 13:39:17 +02002403 if (!SCRIPT_ID_VALID(sid))
2404 return NULL;
2405 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2407 {
2408 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2409
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002410 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2411 : STRLEN(import->imp_name) == len
2412 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 return import;
2414 }
2415 return NULL;
2416}
2417
2418/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002419 * Free all imported variables.
2420 */
2421 static void
2422free_imported(cctx_T *cctx)
2423{
2424 int idx;
2425
2426 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2427 {
2428 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2429
2430 vim_free(import->imp_name);
2431 }
2432 ga_clear(&cctx->ctx_imports);
2433}
2434
2435/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002436 * Return a pointer to the next line that isn't empty or only contains a
2437 * comment. Skips over white space.
2438 * Returns NULL if there is none.
2439 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002440 char_u *
2441peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002442{
2443 int lnum = cctx->ctx_lnum;
2444
2445 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2446 {
2447 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002448 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002450 // ignore NULLs inserted for continuation lines
2451 if (line != NULL)
2452 {
2453 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002454 if (vim9_bad_comment(p))
2455 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002456 if (*p != NUL && !vim9_comment_start(p))
2457 return p;
2458 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002459 }
2460 return NULL;
2461}
2462
2463/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002464 * Called when checking for a following operator at "arg". When the rest of
2465 * the line is empty or only a comment, peek the next line. If there is a next
2466 * line return a pointer to it and set "nextp".
2467 * Otherwise skip over white space.
2468 */
2469 static char_u *
2470may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2471{
2472 char_u *p = skipwhite(arg);
2473
2474 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002475 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002476 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002477 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002478 if (*nextp != NULL)
2479 return *nextp;
2480 }
2481 return p;
2482}
2483
2484/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002485 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002486 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002487 * Returns NULL when at the end.
2488 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002489 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002490next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002491{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002492 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002493
2494 do
2495 {
2496 ++cctx->ctx_lnum;
2497 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002498 {
2499 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002500 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002501 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002502 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002503 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002504 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002505 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002506 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002507 return line;
2508}
2509
2510/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002511 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002512 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002513 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002514 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2515 */
2516 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002517may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002518{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002519 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002520 if (vim9_bad_comment(*arg))
2521 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002522 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002523 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525
2526 if (next == NULL)
2527 return FAIL;
2528 *arg = skipwhite(next);
2529 }
2530 return OK;
2531}
2532
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002533/*
2534 * Idem, and give an error when failed.
2535 */
2536 static int
2537may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2538{
2539 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2540 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002541 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002542 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002543 return FAIL;
2544 }
2545 return OK;
2546}
2547
2548
Bram Moolenaara5565e42020-05-09 15:44:01 +02002549// Structure passed between the compile_expr* functions to keep track of
2550// constants that have been parsed but for which no code was produced yet. If
2551// possible expressions on these constants are applied at compile time. If
2552// that is not possible, the code to push the constants needs to be generated
2553// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002554// Using 50 should be more than enough of 5 levels of ().
2555#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002557 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002558 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002559 int pp_is_const; // all generated code was constants, used for a
2560 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002561} ppconst_T;
2562
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002563static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002564static int compile_expr0(char_u **arg, cctx_T *cctx);
2565static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2566
Bram Moolenaara5565e42020-05-09 15:44:01 +02002567/*
2568 * Generate a PUSH instruction for "tv".
2569 * "tv" will be consumed or cleared.
2570 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2571 */
2572 static int
2573generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2574{
2575 if (tv != NULL)
2576 {
2577 switch (tv->v_type)
2578 {
2579 case VAR_UNKNOWN:
2580 break;
2581 case VAR_BOOL:
2582 generate_PUSHBOOL(cctx, tv->vval.v_number);
2583 break;
2584 case VAR_SPECIAL:
2585 generate_PUSHSPEC(cctx, tv->vval.v_number);
2586 break;
2587 case VAR_NUMBER:
2588 generate_PUSHNR(cctx, tv->vval.v_number);
2589 break;
2590#ifdef FEAT_FLOAT
2591 case VAR_FLOAT:
2592 generate_PUSHF(cctx, tv->vval.v_float);
2593 break;
2594#endif
2595 case VAR_BLOB:
2596 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2597 tv->vval.v_blob = NULL;
2598 break;
2599 case VAR_STRING:
2600 generate_PUSHS(cctx, tv->vval.v_string);
2601 tv->vval.v_string = NULL;
2602 break;
2603 default:
2604 iemsg("constant type not supported");
2605 clear_tv(tv);
2606 return FAIL;
2607 }
2608 tv->v_type = VAR_UNKNOWN;
2609 }
2610 return OK;
2611}
2612
2613/*
2614 * Generate code for any ppconst entries.
2615 */
2616 static int
2617generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2618{
2619 int i;
2620 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002621 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002622
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002623 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002624 for (i = 0; i < ppconst->pp_used; ++i)
2625 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2626 ret = FAIL;
2627 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002628 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002629 return ret;
2630}
2631
2632/*
2633 * Clear ppconst constants. Used when failing.
2634 */
2635 static void
2636clear_ppconst(ppconst_T *ppconst)
2637{
2638 int i;
2639
2640 for (i = 0; i < ppconst->pp_used; ++i)
2641 clear_tv(&ppconst->pp_tv[i]);
2642 ppconst->pp_used = 0;
2643}
2644
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002645/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002646 * Compile getting a member from a list/dict/string/blob. Stack has the
2647 * indexable value and the index.
2648 */
2649 static int
2650compile_member(int is_slice, cctx_T *cctx)
2651{
2652 type_T **typep;
2653 garray_T *stack = &cctx->ctx_type_stack;
2654 vartype_T vtype;
2655 type_T *valtype;
2656
2657 // We can index a list and a dict. If we don't know the type
2658 // we can use the index value type.
2659 // TODO: If we don't know use an instruction to figure it out at
2660 // runtime.
2661 typep = ((type_T **)stack->ga_data) + stack->ga_len
2662 - (is_slice ? 3 : 2);
2663 vtype = (*typep)->tt_type;
2664 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2665 // If the index is a string, the variable must be a Dict.
2666 if (*typep == &t_any && valtype == &t_string)
2667 vtype = VAR_DICT;
2668 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2669 {
2670 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2671 return FAIL;
2672 if (is_slice)
2673 {
2674 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2675 if (need_type(valtype, &t_number, -2, 0, cctx,
2676 FALSE, FALSE) == FAIL)
2677 return FAIL;
2678 }
2679 }
2680
2681 if (vtype == VAR_DICT)
2682 {
2683 if (is_slice)
2684 {
2685 emsg(_(e_cannot_slice_dictionary));
2686 return FAIL;
2687 }
2688 if ((*typep)->tt_type == VAR_DICT)
2689 {
2690 *typep = (*typep)->tt_member;
2691 if (*typep == &t_unknown)
2692 // empty dict was used
2693 *typep = &t_any;
2694 }
2695 else
2696 {
2697 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2698 FALSE, FALSE) == FAIL)
2699 return FAIL;
2700 *typep = &t_any;
2701 }
2702 if (may_generate_2STRING(-1, cctx) == FAIL)
2703 return FAIL;
2704 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2705 return FAIL;
2706 }
2707 else if (vtype == VAR_STRING)
2708 {
2709 *typep = &t_string;
2710 if ((is_slice
2711 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2712 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2713 return FAIL;
2714 }
2715 else if (vtype == VAR_BLOB)
2716 {
2717 emsg("Sorry, blob index and slice not implemented yet");
2718 return FAIL;
2719 }
2720 else if (vtype == VAR_LIST || *typep == &t_any)
2721 {
2722 if (is_slice)
2723 {
2724 if (generate_instr_drop(cctx,
2725 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2726 2) == FAIL)
2727 return FAIL;
2728 }
2729 else
2730 {
2731 if ((*typep)->tt_type == VAR_LIST)
2732 {
2733 *typep = (*typep)->tt_member;
2734 if (*typep == &t_unknown)
2735 // empty list was used
2736 *typep = &t_any;
2737 }
2738 if (generate_instr_drop(cctx,
2739 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2740 return FAIL;
2741 }
2742 }
2743 else
2744 {
2745 emsg(_(e_string_list_dict_or_blob_required));
2746 return FAIL;
2747 }
2748 return OK;
2749}
2750
2751/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002752 * Generate an instruction to load script-local variable "name", without the
2753 * leading "s:".
2754 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 */
2756 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002757compile_load_scriptvar(
2758 cctx_T *cctx,
2759 char_u *name, // variable NUL terminated
2760 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002761 char_u **end, // end of variable
2762 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002764 scriptitem_T *si;
2765 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 imported_T *import;
2767
Bram Moolenaare3d46852020-08-29 13:39:17 +02002768 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2769 return FAIL;
2770 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002771 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002772 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002774 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002775 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2776 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 }
2778 if (idx >= 0)
2779 {
2780 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2781
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002782 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 current_sctx.sc_sid, idx, sv->sv_type);
2784 return OK;
2785 }
2786
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002787 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 if (import != NULL)
2789 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002790 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002791 {
2792 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002793 char_u *exp_name;
2794 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002795 ufunc_T *ufunc;
2796 type_T *type;
2797
2798 // Used "import * as Name", need to lookup the member.
2799 if (*p != '.')
2800 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002801 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 return FAIL;
2803 }
2804 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002805 if (VIM_ISWHITE(*p))
2806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002807 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002808 return FAIL;
2809 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002810
Bram Moolenaar1c991142020-07-04 13:15:31 +02002811 // isolate one name
2812 exp_name = p;
2813 while (eval_isnamec(*p))
2814 ++p;
2815 cc = *p;
2816 *p = NUL;
2817
Bram Moolenaaredba7072021-03-13 21:14:18 +01002818 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2819 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002820 *p = cc;
2821 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002822 *end = p;
2823
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002824 if (idx < 0)
2825 {
2826 if (*p == '(' && ufunc != NULL)
2827 {
2828 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2829 return OK;
2830 }
2831 return FAIL;
2832 }
2833
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002834 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2835 import->imp_sid,
2836 idx,
2837 type);
2838 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002839 else if (import->imp_funcname != NULL)
2840 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002842 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2843 import->imp_sid,
2844 import->imp_var_vals_idx,
2845 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 return OK;
2847 }
2848
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002849 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002850 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return FAIL;
2852}
2853
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002854 static int
2855generate_funcref(cctx_T *cctx, char_u *name)
2856{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002857 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002858
2859 if (ufunc == NULL)
2860 return FAIL;
2861
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002862 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002863 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2864 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002865 == FAIL)
2866 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002867 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002868}
2869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870/*
2871 * Compile a variable name into a load instruction.
2872 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002873 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 * When "error" is FALSE do not give an error when not found.
2875 */
2876 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002877compile_load(
2878 char_u **arg,
2879 char_u *end_arg,
2880 cctx_T *cctx,
2881 int is_expr,
2882 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883{
2884 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002885 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002886 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002888 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 if (*(*arg + 1) == ':')
2891 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002892 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002893 {
2894 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895
Bram Moolenaarfa596382021-04-07 21:58:16 +02002896 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002897 switch (**arg)
2898 {
2899 case 'g': isn_type = ISN_LOADGDICT; break;
2900 case 'w': isn_type = ISN_LOADWDICT; break;
2901 case 't': isn_type = ISN_LOADTDICT; break;
2902 case 'b': isn_type = ISN_LOADBDICT; break;
2903 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002904 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002905 goto theend;
2906 }
2907 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2908 goto theend;
2909 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 else
2912 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002913 isntype_T isn_type = ISN_DROP;
2914
Bram Moolenaarfa596382021-04-07 21:58:16 +02002915 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002916 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2917 if (name == NULL)
2918 return FAIL;
2919
2920 switch (**arg)
2921 {
2922 case 'v': res = generate_LOADV(cctx, name, error);
2923 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002924 case 's': if (is_expr && ASCII_ISUPPER(*name)
2925 && find_func(name, FALSE, cctx) != NULL)
2926 res = generate_funcref(cctx, name);
2927 else
2928 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002929 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002930 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002931 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002932 {
2933 if (is_expr && ASCII_ISUPPER(*name)
2934 && find_func(name, FALSE, cctx) != NULL)
2935 res = generate_funcref(cctx, name);
2936 else
2937 isn_type = ISN_LOADG;
2938 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002939 else
2940 {
2941 isn_type = ISN_LOADAUTO;
2942 vim_free(name);
2943 name = vim_strnsave(*arg, end - *arg);
2944 if (name == NULL)
2945 return FAIL;
2946 }
2947 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002948 case 'w': isn_type = ISN_LOADW; break;
2949 case 't': isn_type = ISN_LOADT; break;
2950 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002951 default: // cannot happen, just in case
2952 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002953 goto theend;
2954 }
2955 if (isn_type != ISN_DROP)
2956 {
2957 // Global, Buffer-local, Window-local and Tabpage-local
2958 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002959 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2961 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 }
2963 }
2964 else
2965 {
2966 size_t len = end - *arg;
2967 int idx;
2968 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002969 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
2971 name = vim_strnsave(*arg, end - *arg);
2972 if (name == NULL)
2973 return FAIL;
2974
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002975 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002977 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002978 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
2980 else
2981 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002982 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002983
Bram Moolenaar709664c2020-12-12 14:33:41 +01002984 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002986 type = lvar.lv_type;
2987 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002988 if (lvar.lv_from_outer != 0)
2989 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002990 else
2991 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 }
2993 else
2994 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002995 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002996 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02002997 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002998 || find_imported(name, 0, cctx) != NULL)
2999 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003000
Bram Moolenaar0f769812020-09-12 18:32:34 +02003001 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003002 // uppercase letter it can be a user defined function.
3003 // generate_funcref() will fail if the function can't be found.
3004 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003005 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 }
3007 }
3008 if (gen_load)
3009 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003010 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003011 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003012 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003013 cctx->ctx_outer_used = TRUE;
3014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 }
3016
3017 *arg = end;
3018
3019theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003020 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003021 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 vim_free(name);
3023 return res;
3024}
3025
3026/*
3027 * Compile the argument expressions.
3028 * "arg" points to just after the "(" and is advanced to after the ")"
3029 */
3030 static int
3031compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3032{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003033 char_u *p = *arg;
3034 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003035 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036
Bram Moolenaare6085c52020-04-12 20:19:16 +02003037 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003039 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3040 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003041 if (*p == ')')
3042 {
3043 *arg = p + 1;
3044 return OK;
3045 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003046 if (must_end)
3047 {
3048 semsg(_(e_missing_comma_before_argument_str), p);
3049 return FAIL;
3050 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003051
Bram Moolenaara5565e42020-05-09 15:44:01 +02003052 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 return FAIL;
3054 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003055
3056 if (*p != ',' && *skipwhite(p) == ',')
3057 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003058 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003059 p = skipwhite(p);
3060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003062 {
3063 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003064 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003065 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003066 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003067 else
3068 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003069 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003070 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003072failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003073 emsg(_(e_missing_close));
3074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075}
3076
3077/*
3078 * Compile a function call: name(arg1, arg2)
3079 * "arg" points to "name", "arg + varlen" to the "(".
3080 * "argcount_init" is 1 for "value->method()"
3081 * Instructions:
3082 * EVAL arg1
3083 * EVAL arg2
3084 * BCALL / DCALL / UCALL
3085 */
3086 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003087compile_call(
3088 char_u **arg,
3089 size_t varlen,
3090 cctx_T *cctx,
3091 ppconst_T *ppconst,
3092 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093{
3094 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003095 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 int argcount = argcount_init;
3097 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003098 char_u fname_buf[FLEN_FIXED + 1];
3099 char_u *tofree = NULL;
3100 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003101 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003102 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003103 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
Bram Moolenaara5565e42020-05-09 15:44:01 +02003105 // we can evaluate "has('name')" at compile time
3106 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3107 {
3108 char_u *s = skipwhite(*arg + varlen + 1);
3109 typval_T argvars[2];
3110
3111 argvars[0].v_type = VAR_UNKNOWN;
3112 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003113 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003114 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003115 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003117 if (*s == ')' && argvars[0].v_type == VAR_STRING
3118 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003119 {
3120 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3121
3122 *arg = s + 1;
3123 argvars[1].v_type = VAR_UNKNOWN;
3124 tv->v_type = VAR_NUMBER;
3125 tv->vval.v_number = 0;
3126 f_has(argvars, tv);
3127 clear_tv(&argvars[0]);
3128 ++ppconst->pp_used;
3129 return OK;
3130 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003131 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003132 }
3133
3134 if (generate_ppconst(cctx, ppconst) == FAIL)
3135 return FAIL;
3136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 if (varlen >= sizeof(namebuf))
3138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003139 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 return FAIL;
3141 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003142 vim_strncpy(namebuf, *arg, varlen);
3143 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144
3145 *arg = skipwhite(*arg + varlen + 1);
3146 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003147 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148
Bram Moolenaar03290b82020-12-19 16:30:44 +01003149 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003150 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 {
3152 int idx;
3153
3154 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003155 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003157 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003158 if (STRCMP(name, "flatten") == 0)
3159 {
3160 emsg(_(e_cannot_use_flatten_in_vim9_script));
3161 goto theend;
3162 }
3163
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003164 if (STRCMP(name, "add") == 0 && argcount == 2)
3165 {
3166 garray_T *stack = &cctx->ctx_type_stack;
3167 type_T *type = ((type_T **)stack->ga_data)[
3168 stack->ga_len - 2];
3169
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003170 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003171 if (type->tt_type == VAR_LIST)
3172 {
3173 // inline "add(list, item)" so that the type can be checked
3174 res = generate_LISTAPPEND(cctx);
3175 idx = -1;
3176 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003177 else if (type->tt_type == VAR_BLOB)
3178 {
3179 // inline "add(blob, nr)" so that the type can be checked
3180 res = generate_BLOBAPPEND(cctx);
3181 idx = -1;
3182 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003183 }
3184
3185 if (idx >= 0)
3186 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3187 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003188 else
3189 semsg(_(e_unknownfunc), namebuf);
3190 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 }
3192
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003193 // An argument or local variable can be a function reference, this
3194 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003195 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003196 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003197 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003198 // If we can find the function by name generate the right call.
3199 // Skip global functions here, a local funcref takes precedence.
3200 ufunc = find_func(name, FALSE, cctx);
3201 if (ufunc != NULL && !func_is_global(ufunc))
3202 {
3203 res = generate_CALL(cctx, ufunc, argcount);
3204 goto theend;
3205 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
3208 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003209 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003210 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003212 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003213 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003214 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003215 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003216 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003217
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003218 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003219 goto theend;
3220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221
Bram Moolenaar0f769812020-09-12 18:32:34 +02003222 // If we can find a global function by name generate the right call.
3223 if (ufunc != NULL)
3224 {
3225 res = generate_CALL(cctx, ufunc, argcount);
3226 goto theend;
3227 }
3228
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003229 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003230 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003231 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003232 res = generate_UCALL(cctx, name, argcount);
3233 else
3234 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003235
3236theend:
3237 vim_free(tofree);
3238 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239}
3240
3241// like NAMESPACE_CHAR but with 'a' and 'l'.
3242#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3243
3244/*
3245 * Find the end of a variable or function name. Unlike find_name_end() this
3246 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003247 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 * Return a pointer to just after the name. Equal to "arg" if there is no
3249 * valid name.
3250 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003251 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003252to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253{
3254 char_u *p;
3255
3256 // Quick check for valid starting character.
3257 if (!eval_isnamec1(*arg))
3258 return arg;
3259
3260 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3261 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3262 // and can be used in slice "[n:]".
3263 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003264 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3266 break;
3267 return p;
3268}
3269
3270/*
3271 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003272 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 */
3274 char_u *
3275to_name_const_end(char_u *arg)
3276{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003277 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 typval_T rettv;
3279
3280 if (p == arg && *arg == '[')
3281 {
3282
3283 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003284 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 p = arg;
3286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 return p;
3288}
3289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290/*
3291 * parse a list: [expr, expr]
3292 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003293 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 */
3295 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003296compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297{
3298 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003299 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003301 int is_const;
3302 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003304 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003306 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003307 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003308 semsg(_(e_list_end), *arg);
3309 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003310 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003311 if (*p == ',')
3312 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003313 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003314 return FAIL;
3315 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003316 if (*p == ']')
3317 {
3318 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003319 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003320 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003321 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003322 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003323 if (!is_const)
3324 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 ++count;
3326 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003327 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003329 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3330 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003331 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003332 return FAIL;
3333 }
3334 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003335 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 p = skipwhite(p);
3337 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003338 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003340 ppconst->pp_is_const = is_all_const;
3341 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342}
3343
3344/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003345 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003346 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003347 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 */
3349 static int
3350compile_lambda(char_u **arg, cctx_T *cctx)
3351{
Bram Moolenaare462f522020-12-27 14:43:30 +01003352 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 typval_T rettv;
3354 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003355 evalarg_T evalarg;
3356
3357 CLEAR_FIELD(evalarg);
3358 evalarg.eval_flags = EVAL_EVALUATE;
3359 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003362 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3363 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003364 {
3365 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003366 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003367 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003368
Bram Moolenaar65c44152020-12-24 15:14:01 +01003369 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003371 ++ufunc->uf_refcount;
3372 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373
Bram Moolenaar65c44152020-12-24 15:14:01 +01003374 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003375 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003377 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3378 // points into it. Point to the original line to avoid a dangling pointer.
3379 if (evalarg.eval_tofree_cmdline != NULL)
3380 {
3381 size_t off = *arg - evalarg.eval_tofree_cmdline;
3382
3383 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3384 + off;
3385 }
3386
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003387 clear_evalarg(&evalarg, NULL);
3388
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003389 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003390 {
3391 // The return type will now be known.
3392 set_function_type(ufunc);
3393
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003394 // The function reference count will be 1. When the ISN_FUNCREF
3395 // instruction is deleted the reference count is decremented and the
3396 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003397 return generate_FUNCREF(cctx, ufunc);
3398 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003399
3400 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 return FAIL;
3402}
3403
3404/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003405 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003407 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 */
3409 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003410compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411{
3412 garray_T *instr = &cctx->ctx_instr;
3413 int count = 0;
3414 dict_T *d = dict_alloc();
3415 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003416 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003417 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003418 int is_const;
3419 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
3421 if (d == NULL)
3422 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003423 if (generate_ppconst(cctx, ppconst) == FAIL)
3424 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003425 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003427 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428
Bram Moolenaar23c55272020-06-21 16:58:13 +02003429 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003430 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003431 *arg = NULL;
3432 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003433 }
3434
3435 if (**arg == '}')
3436 break;
3437
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003438 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003440 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003442 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003443 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003444 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003447 if (isn->isn_type == ISN_PUSHNR)
3448 {
3449 char buf[NUMBUFLEN];
3450
3451 // Convert to string at compile time.
3452 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3453 isn->isn_type = ISN_PUSHS;
3454 isn->isn_arg.string = vim_strsave((char_u *)buf);
3455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 if (isn->isn_type == ISN_PUSHS)
3457 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003458 else if (may_generate_2STRING(-1, cctx) == FAIL)
3459 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003460 *arg = skipwhite(*arg);
3461 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003462 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003463 emsg(_(e_missing_matching_bracket_after_dict_key));
3464 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003465 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003466 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003468 else
3469 {
3470 // {"name": value},
3471 // {'name': value},
3472 // {name: value} use "name" as a literal key
3473 key = get_literal_key(arg);
3474 if (key == NULL)
3475 return FAIL;
3476 if (generate_PUSHS(cctx, key) == FAIL)
3477 return FAIL;
3478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
3480 // Check for duplicate keys, if using string keys.
3481 if (key != NULL)
3482 {
3483 item = dict_find(d, key, -1);
3484 if (item != NULL)
3485 {
3486 semsg(_(e_duplicate_key), key);
3487 goto failret;
3488 }
3489 item = dictitem_alloc(key);
3490 if (item != NULL)
3491 {
3492 item->di_tv.v_type = VAR_UNKNOWN;
3493 item->di_tv.v_lock = 0;
3494 if (dict_add(d, item) == FAIL)
3495 dictitem_free(item);
3496 }
3497 }
3498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 if (**arg != ':')
3500 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003501 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003502 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003503 else
3504 semsg(_(e_missing_dict_colon), *arg);
3505 return FAIL;
3506 }
3507 whitep = *arg + 1;
3508 if (!IS_WHITE_OR_NUL(*whitep))
3509 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003510 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 return FAIL;
3512 }
3513
Bram Moolenaar23c55272020-06-21 16:58:13 +02003514 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003515 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003516 *arg = NULL;
3517 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003518 }
3519
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003520 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003522 if (!is_const)
3523 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 ++count;
3525
Bram Moolenaar2c330432020-04-13 14:41:35 +02003526 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003527 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003528 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003529 *arg = NULL;
3530 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 if (**arg == '}')
3533 break;
3534 if (**arg != ',')
3535 {
3536 semsg(_(e_missing_dict_comma), *arg);
3537 goto failret;
3538 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003539 if (IS_WHITE_OR_NUL(*whitep))
3540 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003541 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003542 return FAIL;
3543 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003544 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003545 if (!IS_WHITE_OR_NUL(*whitep))
3546 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003547 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003548 return FAIL;
3549 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003550 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 }
3552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 *arg = *arg + 1;
3554
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003555 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003556 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003557 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003558 *arg += STRLEN(*arg);
3559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003561 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 return generate_NEWDICT(cctx, count);
3563
3564failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003565 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003566 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003567 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003568 *arg = (char_u *)"";
3569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 dict_unref(d);
3571 return FAIL;
3572}
3573
3574/*
3575 * Compile "&option".
3576 */
3577 static int
3578compile_get_option(char_u **arg, cctx_T *cctx)
3579{
3580 typval_T rettv;
3581 char_u *start = *arg;
3582 int ret;
3583
3584 // parse the option and get the current value to get the type.
3585 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 if (ret == OK)
3588 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003589 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003590 char_u *name = vim_strnsave(start, *arg - start);
3591 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3592 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593
3594 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3595 vim_free(name);
3596 }
3597 clear_tv(&rettv);
3598
3599 return ret;
3600}
3601
3602/*
3603 * Compile "$VAR".
3604 */
3605 static int
3606compile_get_env(char_u **arg, cctx_T *cctx)
3607{
3608 char_u *start = *arg;
3609 int len;
3610 int ret;
3611 char_u *name;
3612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 ++*arg;
3614 len = get_env_len(arg);
3615 if (len == 0)
3616 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003617 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 return FAIL;
3619 }
3620
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003621 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 name = vim_strnsave(start, len + 1);
3623 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3624 vim_free(name);
3625 return ret;
3626}
3627
3628/*
3629 * Compile "@r".
3630 */
3631 static int
3632compile_get_register(char_u **arg, cctx_T *cctx)
3633{
3634 int ret;
3635
3636 ++*arg;
3637 if (**arg == NUL)
3638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003639 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 return FAIL;
3641 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003642 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 {
3644 emsg_invreg(**arg);
3645 return FAIL;
3646 }
3647 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3648 ++*arg;
3649 return ret;
3650}
3651
3652/*
3653 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003654 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 */
3656 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003657apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003659 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660
3661 // this works from end to start
3662 while (p > start)
3663 {
3664 --p;
3665 if (*p == '-' || *p == '+')
3666 {
3667 // only '-' has an effect, for '+' we only check the type
3668#ifdef FEAT_FLOAT
3669 if (rettv->v_type == VAR_FLOAT)
3670 {
3671 if (*p == '-')
3672 rettv->vval.v_float = -rettv->vval.v_float;
3673 }
3674 else
3675#endif
3676 {
3677 varnumber_T val;
3678 int error = FALSE;
3679
3680 // tv_get_number_chk() accepts a string, but we don't want that
3681 // here
3682 if (check_not_string(rettv) == FAIL)
3683 return FAIL;
3684 val = tv_get_number_chk(rettv, &error);
3685 clear_tv(rettv);
3686 if (error)
3687 return FAIL;
3688 if (*p == '-')
3689 val = -val;
3690 rettv->v_type = VAR_NUMBER;
3691 rettv->vval.v_number = val;
3692 }
3693 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003694 else if (numeric_only)
3695 {
3696 ++p;
3697 break;
3698 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003699 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 {
3701 int v = tv2bool(rettv);
3702
3703 // '!' is permissive in the type.
3704 clear_tv(rettv);
3705 rettv->v_type = VAR_BOOL;
3706 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3707 }
3708 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003709 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return OK;
3711}
3712
3713/*
3714 * Recognize v: variables that are constants and set "rettv".
3715 */
3716 static void
3717get_vim_constant(char_u **arg, typval_T *rettv)
3718{
3719 if (STRNCMP(*arg, "v:true", 6) == 0)
3720 {
3721 rettv->v_type = VAR_BOOL;
3722 rettv->vval.v_number = VVAL_TRUE;
3723 *arg += 6;
3724 }
3725 else if (STRNCMP(*arg, "v:false", 7) == 0)
3726 {
3727 rettv->v_type = VAR_BOOL;
3728 rettv->vval.v_number = VVAL_FALSE;
3729 *arg += 7;
3730 }
3731 else if (STRNCMP(*arg, "v:null", 6) == 0)
3732 {
3733 rettv->v_type = VAR_SPECIAL;
3734 rettv->vval.v_number = VVAL_NULL;
3735 *arg += 6;
3736 }
3737 else if (STRNCMP(*arg, "v:none", 6) == 0)
3738 {
3739 rettv->v_type = VAR_SPECIAL;
3740 rettv->vval.v_number = VVAL_NONE;
3741 *arg += 6;
3742 }
3743}
3744
Bram Moolenaar657137c2021-01-09 15:45:23 +01003745 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003746get_compare_type(char_u *p, int *len, int *type_is)
3747{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003748 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003749 int i;
3750
3751 switch (p[0])
3752 {
3753 case '=': if (p[1] == '=')
3754 type = EXPR_EQUAL;
3755 else if (p[1] == '~')
3756 type = EXPR_MATCH;
3757 break;
3758 case '!': if (p[1] == '=')
3759 type = EXPR_NEQUAL;
3760 else if (p[1] == '~')
3761 type = EXPR_NOMATCH;
3762 break;
3763 case '>': if (p[1] != '=')
3764 {
3765 type = EXPR_GREATER;
3766 *len = 1;
3767 }
3768 else
3769 type = EXPR_GEQUAL;
3770 break;
3771 case '<': if (p[1] != '=')
3772 {
3773 type = EXPR_SMALLER;
3774 *len = 1;
3775 }
3776 else
3777 type = EXPR_SEQUAL;
3778 break;
3779 case 'i': if (p[1] == 's')
3780 {
3781 // "is" and "isnot"; but not a prefix of a name
3782 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3783 *len = 5;
3784 i = p[*len];
3785 if (!isalnum(i) && i != '_')
3786 {
3787 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3788 *type_is = TRUE;
3789 }
3790 }
3791 break;
3792 }
3793 return type;
3794}
3795
3796/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003797 * Skip over an expression, ignoring most errors.
3798 */
3799 static void
3800skip_expr_cctx(char_u **arg, cctx_T *cctx)
3801{
3802 evalarg_T evalarg;
3803
3804 CLEAR_FIELD(evalarg);
3805 evalarg.eval_cctx = cctx;
3806 skip_expr(arg, &evalarg);
3807}
3808
3809/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003811 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 */
3813 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003814compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003816 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817
3818 // this works from end to start
3819 while (p > start)
3820 {
3821 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003822 while (VIM_ISWHITE(*p))
3823 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 if (*p == '-' || *p == '+')
3825 {
3826 int negate = *p == '-';
3827 isn_T *isn;
3828
3829 // TODO: check type
3830 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3831 {
3832 --p;
3833 if (*p == '-')
3834 negate = !negate;
3835 }
3836 // only '-' has an effect, for '+' we only check the type
3837 if (negate)
3838 isn = generate_instr(cctx, ISN_NEGATENR);
3839 else
3840 isn = generate_instr(cctx, ISN_CHECKNR);
3841 if (isn == NULL)
3842 return FAIL;
3843 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003844 else if (numeric_only)
3845 {
3846 ++p;
3847 break;
3848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 else
3850 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003851 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003853 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003855 if (p[-1] == '!')
3856 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 }
3859 if (generate_2BOOL(cctx, invert) == FAIL)
3860 return FAIL;
3861 }
3862 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003863 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 return OK;
3865}
3866
3867/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003868 * Compile "(expression)": recursive!
3869 * Return FAIL/OK.
3870 */
3871 static int
3872compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3873{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003874 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003875 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003876
Bram Moolenaar24156692021-01-14 20:35:49 +01003877 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3878 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003879 if (ppconst->pp_used <= PPSIZE - 10)
3880 {
3881 ret = compile_expr1(arg, cctx, ppconst);
3882 }
3883 else
3884 {
3885 // Not enough space in ppconst, flush constants.
3886 if (generate_ppconst(cctx, ppconst) == FAIL)
3887 return FAIL;
3888 ret = compile_expr0(arg, cctx);
3889 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003890 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3891 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003892 if (**arg == ')')
3893 ++*arg;
3894 else if (ret == OK)
3895 {
3896 emsg(_(e_missing_close));
3897 ret = FAIL;
3898 }
3899 return ret;
3900}
3901
3902/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003904 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 */
3906 static int
3907compile_subscript(
3908 char_u **arg,
3909 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003910 char_u *start_leader,
3911 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003912 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003914 char_u *name_start = *end_leader;
3915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 for (;;)
3917 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003918 char_u *p = skipwhite(*arg);
3919
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003920 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003921 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003922 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003923
3924 // If a following line starts with "->{" or "->X" advance to that
3925 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003926 // Also if a following line starts with ".x".
3927 if (next != NULL &&
3928 ((next[0] == '-' && next[1] == '>'
3929 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003930 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003931 {
3932 next = next_line_from_context(cctx, TRUE);
3933 if (next == NULL)
3934 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003935 *arg = next;
3936 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003937 }
3938 }
3939
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003940 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003941 // not a function call.
3942 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003944 garray_T *stack = &cctx->ctx_type_stack;
3945 type_T *type;
3946 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003948 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003950 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003953 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3954
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003955 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3957 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003958 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 return FAIL;
3960 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003961 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003963 char_u *pstart = p;
3964
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003965 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003966 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003967 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 // something->method()
3970 // Apply the '!', '-' and '+' first:
3971 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003972 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003975 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003976 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003977 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003978 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003979 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003980 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003981 garray_T *stack = &cctx->ctx_type_stack;
3982 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003983 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003984 int expr_isn_start = cctx->ctx_instr.ga_len;
3985 int expr_isn_end;
3986 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003987
3988 // Funcref call: list->(Refs[2])(arg)
3989 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003990 //
3991 // Fist compile the function expression.
3992 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01003993 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003994
3995 // Remember the next instruction index, where the instructions
3996 // for arguments are being written.
3997 expr_isn_end = cctx->ctx_instr.ga_len;
3998
3999 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004000 if (**arg != '(')
4001 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004002 if (*skipwhite(*arg) == '(')
4003 emsg(_(e_nowhitespace));
4004 else
4005 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004006 return FAIL;
4007 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004008 *arg = skipwhite(*arg + 1);
4009 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4010 return FAIL;
4011
Bram Moolenaar2927c072021-04-05 19:41:21 +02004012 // Move the instructions for the arguments to before the
4013 // instructions of the expression and move the type of the
4014 // expression after the argument types. This is what ISN_PCALL
4015 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004016 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004017 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4018 if (arg_isn_count > 0)
4019 {
4020 int expr_isn_count = expr_isn_end - expr_isn_start;
4021 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4022
4023 if (isn == NULL)
4024 return FAIL;
4025 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4026 + expr_isn_start,
4027 sizeof(isn_T) * expr_isn_count);
4028 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4029 + expr_isn_start,
4030 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4031 sizeof(isn_T) * arg_isn_count);
4032 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4033 + expr_isn_start + arg_isn_count,
4034 isn, sizeof(isn_T) * expr_isn_count);
4035 vim_free(isn);
4036
4037 type = ((type_T **)stack->ga_data)[type_idx_start];
4038 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4039 ((type_T **)stack->ga_data) + type_idx_start + 1,
4040 sizeof(type_T *)
4041 * (stack->ga_len - type_idx_start - 1));
4042 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4043 }
4044
Bram Moolenaar7e368202020-12-25 21:56:57 +01004045 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4046 if (generate_PCALL(cctx, argcount,
4047 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 return FAIL;
4049 }
4050 else
4051 {
4052 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004053 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004054 if (!eval_isnamec1(*p))
4055 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004056 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004057 return FAIL;
4058 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004059 if (ASCII_ISALPHA(*p) && p[1] == ':')
4060 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004061 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 ;
4063 if (*p != '(')
4064 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004065 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 return FAIL;
4067 }
4068 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004069 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 return FAIL;
4071 }
4072 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004073 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004075 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004076
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004077 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004078 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004079 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004080 // TODO: blob index
4081 // TODO: more arguments
4082 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004083 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004084 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004085 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004086
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004087 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004088 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004089 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004090 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004091 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004092 // missing first index is equal to zero
4093 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004094 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004095 else
4096 {
4097 if (compile_expr0(arg, cctx) == FAIL)
4098 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004099 if (**arg == ':')
4100 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004101 semsg(_(e_white_space_required_before_and_after_str_at_str),
4102 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004103 return FAIL;
4104 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004105 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004106 return FAIL;
4107 *arg = skipwhite(*arg);
4108 }
4109 if (**arg == ':')
4110 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004111 is_slice = TRUE;
4112 ++*arg;
4113 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4114 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004115 semsg(_(e_white_space_required_before_and_after_str_at_str),
4116 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004117 return FAIL;
4118 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004119 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004120 return FAIL;
4121 if (**arg == ']')
4122 // missing second index is equal to end of string
4123 generate_PUSHNR(cctx, -1);
4124 else
4125 {
4126 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004127 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004128 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004129 return FAIL;
4130 *arg = skipwhite(*arg);
4131 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
4134 if (**arg != ']')
4135 {
4136 emsg(_(e_missbrac));
4137 return FAIL;
4138 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004139 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
Bram Moolenaare42939a2021-04-05 17:11:17 +02004141 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004142 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004144 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004146 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004147 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004148 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004149 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004150
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004151 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004152 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004153 {
4154 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004155 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004156 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004157 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004158 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 while (eval_isnamec(*p))
4160 MB_PTR_ADV(p);
4161 if (p == *arg)
4162 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004163 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 return FAIL;
4165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004166 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 return FAIL;
4168 *arg = p;
4169 }
4170 else
4171 break;
4172 }
4173
4174 // TODO - see handle_subscript():
4175 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4176 // Don't do this when "Func" is already a partial that was bound
4177 // explicitly (pt_auto is FALSE).
4178
4179 return OK;
4180}
4181
4182/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004183 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4184 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004186 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004187 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004188 *
4189 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 */
4191
4192/*
4193 * number number constant
4194 * 0zFFFFFFFF Blob constant
4195 * "string" string constant
4196 * 'string' literal string constant
4197 * &option-name option value
4198 * @r register contents
4199 * identifier variable value
4200 * function() function call
4201 * $VAR environment variable
4202 * (expression) nested expression
4203 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004204 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 *
4206 * Also handle:
4207 * ! in front logical NOT
4208 * - in front unary minus
4209 * + in front unary plus (ignored)
4210 * trailing (arg) funcref/partial call
4211 * trailing [] subscript in String or List
4212 * trailing .name entry in Dictionary
4213 * trailing ->name() method call
4214 */
4215 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004216compile_expr7(
4217 char_u **arg,
4218 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004219 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 char_u *start_leader, *end_leader;
4222 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004223 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004224 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004226 ppconst->pp_is_const = FALSE;
4227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 /*
4229 * Skip '!', '-' and '+' characters. They are handled later.
4230 */
4231 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004232 if (eval_leader(arg, TRUE) == FAIL)
4233 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 end_leader = *arg;
4235
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004236 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 switch (**arg)
4238 {
4239 /*
4240 * Number constant.
4241 */
4242 case '0': // also for blob starting with 0z
4243 case '1':
4244 case '2':
4245 case '3':
4246 case '4':
4247 case '5':
4248 case '6':
4249 case '7':
4250 case '8':
4251 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004252 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004254 // Apply "-" and "+" just before the number now, right to
4255 // left. Matters especially when "->" follows. Stops at
4256 // '!'.
4257 if (apply_leader(rettv, TRUE,
4258 start_leader, &end_leader) == FAIL)
4259 {
4260 clear_tv(rettv);
4261 return FAIL;
4262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 break;
4264
4265 /*
4266 * String constant: "string".
4267 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004268 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 return FAIL;
4270 break;
4271
4272 /*
4273 * Literal string constant: 'str''ing'.
4274 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004275 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 return FAIL;
4277 break;
4278
4279 /*
4280 * Constant Vim variable.
4281 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004282 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 ret = NOTDONE;
4284 break;
4285
4286 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004287 * "true" constant
4288 */
4289 case 't': if (STRNCMP(*arg, "true", 4) == 0
4290 && !eval_isnamec((*arg)[4]))
4291 {
4292 *arg += 4;
4293 rettv->v_type = VAR_BOOL;
4294 rettv->vval.v_number = VVAL_TRUE;
4295 }
4296 else
4297 ret = NOTDONE;
4298 break;
4299
4300 /*
4301 * "false" constant
4302 */
4303 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4304 && !eval_isnamec((*arg)[5]))
4305 {
4306 *arg += 5;
4307 rettv->v_type = VAR_BOOL;
4308 rettv->vval.v_number = VVAL_FALSE;
4309 }
4310 else
4311 ret = NOTDONE;
4312 break;
4313
4314 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004315 * "null" constant
4316 */
4317 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004318 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004319 {
4320 *arg += 4;
4321 rettv->v_type = VAR_SPECIAL;
4322 rettv->vval.v_number = VVAL_NULL;
4323 }
4324 else
4325 ret = NOTDONE;
4326 break;
4327
4328 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 * List: [expr, expr]
4330 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004331 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4332 return FAIL;
4333 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 break;
4335
4336 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 * Dictionary: {'key': val, 'key': val}
4338 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004339 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4340 return FAIL;
4341 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 break;
4343
4344 /*
4345 * Option value: &name
4346 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004347 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4348 return FAIL;
4349 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 break;
4351
4352 /*
4353 * Environment variable: $VAR.
4354 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004355 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4356 return FAIL;
4357 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 break;
4359
4360 /*
4361 * Register contents: @r.
4362 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004363 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4364 return FAIL;
4365 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 break;
4367 /*
4368 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004369 * lambda: (arg, arg) => expr
4370 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004372 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4373 ret = compile_lambda(arg, cctx);
4374 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004375 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 break;
4377
4378 default: ret = NOTDONE;
4379 break;
4380 }
4381 if (ret == FAIL)
4382 return FAIL;
4383
Bram Moolenaar1c747212020-05-09 18:28:34 +02004384 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004386 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387 clear_tv(rettv);
4388 else
4389 // A constant expression can possibly be handled compile time,
4390 // return the value instead of generating code.
4391 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
4393 else if (ret == NOTDONE)
4394 {
4395 char_u *p;
4396 int r;
4397
4398 if (!eval_isnamec1(**arg))
4399 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004400 if (!vim9_bad_comment(*arg))
4401 {
4402 if (ends_excmd(*skipwhite(*arg)))
4403 semsg(_(e_empty_expression_str), *arg);
4404 else
4405 semsg(_(e_name_expected_str), *arg);
4406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 return FAIL;
4408 }
4409
4410 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004411 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004413 {
4414 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004417 {
4418 if (generate_ppconst(cctx, ppconst) == FAIL)
4419 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004420 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 if (r == FAIL)
4423 return FAIL;
4424 }
4425
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004426 // Handle following "[]", ".member", etc.
4427 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004428 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004429 ppconst) == FAIL)
4430 return FAIL;
4431 if (ppconst->pp_used > 0)
4432 {
4433 // apply the '!', '-' and '+' before the constant
4434 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004435 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436 return FAIL;
4437 return OK;
4438 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004439 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004441 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442}
4443
4444/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004445 * Give the "white on both sides" error, taking the operator from "p[len]".
4446 */
4447 void
4448error_white_both(char_u *op, int len)
4449{
4450 char_u buf[10];
4451
4452 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004453 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004454}
4455
4456/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004457 * <type>expr7: runtime type check / conversion
4458 */
4459 static int
4460compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4461{
4462 type_T *want_type = NULL;
4463
4464 // Recognize <type>
4465 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4466 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004467 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004468 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4469 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004470 return FAIL;
4471
4472 if (**arg != '>')
4473 {
4474 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004475 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004476 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004477 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004478 return FAIL;
4479 }
4480 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004481 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004482 return FAIL;
4483 }
4484
4485 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4486 return FAIL;
4487
4488 if (want_type != NULL)
4489 {
4490 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004491 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004492 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004493
Bram Moolenaard1103582020-08-14 22:44:25 +02004494 generate_ppconst(cctx, ppconst);
4495 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004496 where.wt_index = 0;
4497 where.wt_variable = FALSE;
4498 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004499 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004500 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004501 return FAIL;
4502 }
4503 }
4504
4505 return OK;
4506}
4507
4508/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 * * number multiplication
4510 * / number division
4511 * % number modulo
4512 */
4513 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515{
4516 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004517 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004518 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004520 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004521 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 return FAIL;
4523
4524 /*
4525 * Repeat computing, until no "*", "/" or "%" is following.
4526 */
4527 for (;;)
4528 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004529 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 if (*op != '*' && *op != '/' && *op != '%')
4531 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004532 if (next != NULL)
4533 {
4534 *arg = next_line_from_context(cctx, TRUE);
4535 op = skipwhite(*arg);
4536 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004537
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004538 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004540 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004541 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004543 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004544 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004546 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004547 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004549
4550 if (ppconst->pp_used == ppconst_used + 2
4551 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4552 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004553 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004554 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4555 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4556 varnumber_T res = 0;
4557 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004559 // both are numbers: compute the result
4560 switch (*op)
4561 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004562 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004563 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004564 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004565 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004566 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004567 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004568 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004569 break;
4570 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004571 if (failed)
4572 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004573 tv1->vval.v_number = res;
4574 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004575 }
4576 else
4577 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004578 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004579 generate_two_op(cctx, op);
4580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004581 }
4582
4583 return OK;
4584}
4585
4586/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004587 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 * - number subtraction
4589 * .. string concatenation
4590 */
4591 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593{
4594 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004595 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
4599 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 return FAIL;
4602
4603 /*
4604 * Repeat computing, until no "+", "-" or ".." is following.
4605 */
4606 for (;;)
4607 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004608 op = may_peek_next_line(cctx, *arg, &next);
4609 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 break;
4611 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 if (next != NULL)
4613 {
4614 *arg = next_line_from_context(cctx, TRUE);
4615 op = skipwhite(*arg);
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004618 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004620 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004621 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
4623
Bram Moolenaare0de1712020-12-02 17:36:54 +01004624 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004625 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004627 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 return FAIL;
4630
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004632 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004633 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4634 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4635 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4636 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4639 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004640
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004641 // concat/subtract/add constant numbers
4642 if (*op == '+')
4643 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4644 else if (*op == '-')
4645 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4646 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004647 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004648 // concatenate constant strings
4649 char_u *s1 = tv1->vval.v_string;
4650 char_u *s2 = tv2->vval.v_string;
4651 size_t len1 = STRLEN(s1);
4652
4653 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4654 if (tv1->vval.v_string == NULL)
4655 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004656 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004657 return FAIL;
4658 }
4659 mch_memmove(tv1->vval.v_string, s1, len1);
4660 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004661 vim_free(s1);
4662 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004663 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004664 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 }
4666 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004667 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004668 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004669 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004670 if (*op == '.')
4671 {
4672 if (may_generate_2STRING(-2, cctx) == FAIL
4673 || may_generate_2STRING(-1, cctx) == FAIL)
4674 return FAIL;
4675 generate_instr_drop(cctx, ISN_CONCAT, 1);
4676 }
4677 else
4678 generate_two_op(cctx, op);
4679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 }
4681
4682 return OK;
4683}
4684
4685/*
4686 * expr5a == expr5b
4687 * expr5a =~ expr5b
4688 * expr5a != expr5b
4689 * expr5a !~ expr5b
4690 * expr5a > expr5b
4691 * expr5a >= expr5b
4692 * expr5a < expr5b
4693 * expr5a <= expr5b
4694 * expr5a is expr5b
4695 * expr5a isnot expr5b
4696 *
4697 * Produces instructions:
4698 * EVAL expr5a Push result of "expr5a"
4699 * EVAL expr5b Push result of "expr5b"
4700 * COMPARE one of the compare instructions
4701 */
4702 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004705 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004707 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711
4712 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 return FAIL;
4715
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004716 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004717 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
4719 /*
4720 * If there is a comparative operator, use it.
4721 */
4722 if (type != EXPR_UNKNOWN)
4723 {
4724 int ic = FALSE; // Default: do not ignore case
4725
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004726 if (next != NULL)
4727 {
4728 *arg = next_line_from_context(cctx, TRUE);
4729 p = skipwhite(*arg);
4730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 if (type_is && (p[len] == '?' || p[len] == '#'))
4732 {
4733 semsg(_(e_invexpr2), *arg);
4734 return FAIL;
4735 }
4736 // extra question mark appended: ignore case
4737 if (p[len] == '?')
4738 {
4739 ic = TRUE;
4740 ++len;
4741 }
4742 // extra '#' appended: match case (ignored)
4743 else if (p[len] == '#')
4744 ++len;
4745 // nothing appended: match case
4746
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004747 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004749 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004750 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 }
4752
4753 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004754 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004755 return FAIL;
4756
Bram Moolenaara5565e42020-05-09 15:44:01 +02004757 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 return FAIL;
4759
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 if (ppconst->pp_used == ppconst_used + 2)
4761 {
4762 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4763 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4764 int ret;
4765
4766 // Both sides are a constant, compute the result now.
4767 // First check for a valid combination of types, this is more
4768 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004769 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 ret = FAIL;
4771 else
4772 {
4773 ret = typval_compare(tv1, tv2, type, ic);
4774 tv1->v_type = VAR_BOOL;
4775 tv1->vval.v_number = tv1->vval.v_number
4776 ? VVAL_TRUE : VVAL_FALSE;
4777 clear_tv(tv2);
4778 --ppconst->pp_used;
4779 }
4780 return ret;
4781 }
4782
4783 generate_ppconst(cctx, ppconst);
4784 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785 }
4786
4787 return OK;
4788}
4789
Bram Moolenaar7f141552020-05-09 17:35:53 +02004790static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792/*
4793 * Compile || or &&.
4794 */
4795 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796compile_and_or(
4797 char_u **arg,
4798 cctx_T *cctx,
4799 char *op,
4800 ppconst_T *ppconst,
4801 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004803 char_u *next;
4804 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 int opchar = *op;
4806
4807 if (p[0] == opchar && p[1] == opchar)
4808 {
4809 garray_T *instr = &cctx->ctx_instr;
4810 garray_T end_ga;
4811
4812 /*
4813 * Repeat until there is no following "||" or "&&"
4814 */
4815 ga_init2(&end_ga, sizeof(int), 10);
4816 while (p[0] == opchar && p[1] == opchar)
4817 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004818 long start_lnum = SOURCING_LNUM;
4819 int start_ctx_lnum = cctx->ctx_lnum;
4820 int save_lnum;
4821
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004822 if (next != NULL)
4823 {
4824 *arg = next_line_from_context(cctx, TRUE);
4825 p = skipwhite(*arg);
4826 }
4827
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004828 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4829 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004830 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004831 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004832 return FAIL;
4833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004835 // TODO: use ppconst if the value is a constant and check
4836 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 generate_ppconst(cctx, ppconst);
4838
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004839 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004840 SOURCING_LNUM = start_lnum;
4841 save_lnum = cctx->ctx_lnum;
4842 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004843 if (bool_on_stack(cctx) == FAIL)
4844 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004845 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004846 ga_clear(&end_ga);
4847 return FAIL;
4848 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004849 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 if (ga_grow(&end_ga, 1) == FAIL)
4852 {
4853 ga_clear(&end_ga);
4854 return FAIL;
4855 }
4856 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4857 ++end_ga.ga_len;
4858 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004859 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
4861 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004862 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004863 {
4864 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004865 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004866 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004867
Bram Moolenaara5565e42020-05-09 15:44:01 +02004868 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4869 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
4871 ga_clear(&end_ga);
4872 return FAIL;
4873 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004874
4875 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004877 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004879 // Every part must evaluate to a bool.
4880 if (bool_on_stack(cctx) == FAIL)
4881 {
4882 ga_clear(&end_ga);
4883 return FAIL;
4884 }
4885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 // Fill in the end label in all jumps.
4887 while (end_ga.ga_len > 0)
4888 {
4889 isn_T *isn;
4890
4891 --end_ga.ga_len;
4892 isn = ((isn_T *)instr->ga_data)
4893 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4894 isn->isn_arg.jump.jump_where = instr->ga_len;
4895 }
4896 ga_clear(&end_ga);
4897 }
4898
4899 return OK;
4900}
4901
4902/*
4903 * expr4a && expr4a && expr4a logical AND
4904 *
4905 * Produces instructions:
4906 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004907 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004908 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004910 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 * EVAL expr4c Push result of "expr4c"
4912 * end:
4913 */
4914 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004915compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004917 int ppconst_used = ppconst->pp_used;
4918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004920 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 return FAIL;
4922
4923 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925}
4926
4927/*
4928 * expr3a || expr3b || expr3c logical OR
4929 *
4930 * Produces instructions:
4931 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004932 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004933 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004935 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 * EVAL expr3c Push result of "expr3c"
4937 * end:
4938 */
4939 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004940compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004942 int ppconst_used = ppconst->pp_used;
4943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004945 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 return FAIL;
4947
4948 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004949 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950}
4951
4952/*
4953 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004955 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 * JUMP_IF_FALSE alt jump if false
4957 * EVAL expr1a
4958 * JUMP_ALWAYS end
4959 * alt: EVAL expr1b
4960 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004961 *
4962 * Toplevel expression: expr2 ?? expr1
4963 * Produces instructions:
4964 * EVAL expr2 Push result of "expr2"
4965 * JUMP_AND_KEEP_IF_TRUE end jump if true
4966 * EVAL expr1
4967 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 */
4969 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004970compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971{
4972 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004973 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004974 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975
Bram Moolenaar3988f642020-08-27 22:43:03 +02004976 // Ignore all kinds of errors when not producing code.
4977 if (cctx->ctx_skip == SKIP_YES)
4978 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004979 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004980 return OK;
4981 }
4982
Bram Moolenaar61a89812020-05-07 16:58:17 +02004983 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004984 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 return FAIL;
4986
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004987 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 if (*p == '?')
4989 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004990 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 garray_T *instr = &cctx->ctx_instr;
4992 garray_T *stack = &cctx->ctx_type_stack;
4993 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004994 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004996 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004997 int has_const_expr = FALSE;
4998 int const_value = FALSE;
4999 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005001 if (next != NULL)
5002 {
5003 *arg = next_line_from_context(cctx, TRUE);
5004 p = skipwhite(*arg);
5005 }
5006
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005007 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005008 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005009 semsg(_(e_white_space_required_before_and_after_str_at_str),
5010 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005011 return FAIL;
5012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
Bram Moolenaara5565e42020-05-09 15:44:01 +02005014 if (ppconst->pp_used == ppconst_used + 1)
5015 {
5016 // the condition is a constant, we know whether the ? or the :
5017 // expression is to be evaluated.
5018 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005019 if (op_falsy)
5020 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5021 else
5022 {
5023 int error = FALSE;
5024
5025 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5026 &error);
5027 if (error)
5028 return FAIL;
5029 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005030 cctx->ctx_skip = save_skip == SKIP_YES ||
5031 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5032
5033 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5034 // "left ?? right" and "left" is truthy: produce "left"
5035 generate_ppconst(cctx, ppconst);
5036 else
5037 {
5038 clear_tv(&ppconst->pp_tv[ppconst_used]);
5039 --ppconst->pp_used;
5040 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005041 }
5042 else
5043 {
5044 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005045 if (op_falsy)
5046 end_idx = instr->ga_len;
5047 generate_JUMP(cctx, op_falsy
5048 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5049 if (op_falsy)
5050 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005051 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052
5053 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005054 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005055 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005056 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005057 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058
Bram Moolenaara5565e42020-05-09 15:44:01 +02005059 if (!has_const_expr)
5060 {
5061 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005063 if (!op_falsy)
5064 {
5065 // remember the type and drop it
5066 --stack->ga_len;
5067 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005069 end_idx = instr->ga_len;
5070 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005071
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005072 // jump here from JUMP_IF_FALSE
5073 isn = ((isn_T *)instr->ga_data) + alt_idx;
5074 isn->isn_arg.jump.jump_where = instr->ga_len;
5075 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005078 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005080 // Check for the ":".
5081 p = may_peek_next_line(cctx, *arg, &next);
5082 if (*p != ':')
5083 {
5084 emsg(_(e_missing_colon));
5085 return FAIL;
5086 }
5087 if (next != NULL)
5088 {
5089 *arg = next_line_from_context(cctx, TRUE);
5090 p = skipwhite(*arg);
5091 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005092
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005093 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5094 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005095 semsg(_(e_white_space_required_before_and_after_str_at_str),
5096 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005097 return FAIL;
5098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005100 // evaluate the third expression
5101 if (has_const_expr)
5102 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005103 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005104 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005105 return FAIL;
5106 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5107 return FAIL;
5108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109
Bram Moolenaara5565e42020-05-09 15:44:01 +02005110 if (!has_const_expr)
5111 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005112 type_T **typep;
5113
Bram Moolenaara5565e42020-05-09 15:44:01 +02005114 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115
Bram Moolenaara5565e42020-05-09 15:44:01 +02005116 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005117 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5118 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005119
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005120 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005121 isn = ((isn_T *)instr->ga_data) + end_idx;
5122 isn->isn_arg.jump.jump_where = instr->ga_len;
5123 }
5124
5125 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 }
5127 return OK;
5128}
5129
5130/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005132 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5133 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005134 */
5135 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005136compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005137{
5138 ppconst_T ppconst;
5139
5140 CLEAR_FIELD(ppconst);
5141 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5142 {
5143 clear_ppconst(&ppconst);
5144 return FAIL;
5145 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005146 if (is_const != NULL)
5147 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148 if (generate_ppconst(cctx, &ppconst) == FAIL)
5149 return FAIL;
5150 return OK;
5151}
5152
5153/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005154 * Toplevel expression.
5155 */
5156 static int
5157compile_expr0(char_u **arg, cctx_T *cctx)
5158{
5159 return compile_expr0_ext(arg, cctx, NULL);
5160}
5161
5162/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 * compile "return [expr]"
5164 */
5165 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005166compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167{
5168 char_u *p = arg;
5169 garray_T *stack = &cctx->ctx_type_stack;
5170 type_T *stack_type;
5171
5172 if (*p != NUL && *p != '|' && *p != '\n')
5173 {
5174 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005175 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 return NULL;
5177
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005178 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005179 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005180 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005181 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005182 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5183 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005184 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005185 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005186 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005187 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005188 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005189 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5190 && stack_type->tt_type != VAR_VOID
5191 && stack_type->tt_type != VAR_UNKNOWN)
5192 {
5193 emsg(_(e_returning_value_in_function_without_return_type));
5194 return NULL;
5195 }
5196 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005197 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005198 return NULL;
5199 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 }
5202 else
5203 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005204 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005205 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005206 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5207 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005209 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 return NULL;
5211 }
5212
5213 // No argument, return zero.
5214 generate_PUSHNR(cctx, 0);
5215 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005216
5217 // Undo any command modifiers.
5218 generate_undo_cmdmods(cctx);
5219
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005220 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 return NULL;
5222
5223 // "return val | endif" is possible
5224 return skipwhite(p);
5225}
5226
5227/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005228 * Get a line from the compilation context, compatible with exarg_T getline().
5229 * Return a pointer to the line in allocated memory.
5230 * Return NULL for end-of-file or some error.
5231 */
5232 static char_u *
5233exarg_getline(
5234 int c UNUSED,
5235 void *cookie,
5236 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005237 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005238{
5239 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005240 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005241
Bram Moolenaar66250c92020-08-20 15:02:42 +02005242 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005243 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005244 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005245 return NULL;
5246 ++cctx->ctx_lnum;
5247 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5248 // Comment lines result in NULL pointers, skip them.
5249 if (p != NULL)
5250 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005251 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005252}
5253
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005254 void
5255fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5256{
5257 eap->getline = exarg_getline;
5258 eap->cookie = cctx;
5259}
5260
Bram Moolenaar04b12692020-05-04 23:24:44 +02005261/*
5262 * Compile a nested :def command.
5263 */
5264 static char_u *
5265compile_nested_function(exarg_T *eap, cctx_T *cctx)
5266{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005267 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005269 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005270 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005271 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005272 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005273
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005274 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005275 {
5276 emsg(_(e_cannot_use_bang_with_nested_def));
5277 return NULL;
5278 }
5279
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005280 if (*name_start == '/')
5281 {
5282 name_end = skip_regexp(name_start + 1, '/', TRUE);
5283 if (*name_end == '/')
5284 ++name_end;
5285 eap->nextcmd = check_nextcmd(name_end);
5286 }
5287 if (name_end == name_start || *skipwhite(name_end) != '(')
5288 {
5289 if (!ends_excmd2(name_start, name_end))
5290 {
5291 semsg(_(e_invalid_command_str), eap->cmd);
5292 return NULL;
5293 }
5294
5295 // "def" or "def Name": list functions
5296 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5297 return NULL;
5298 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5299 }
5300
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005301 // Only g:Func() can use a namespace.
5302 if (name_start[1] == ':' && !is_global)
5303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005304 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005305 return NULL;
5306 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005307 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005308 return NULL;
5309
Bram Moolenaar04b12692020-05-04 23:24:44 +02005310 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005311 fill_exarg_from_cctx(eap, cctx);
5312
Bram Moolenaar04b12692020-05-04 23:24:44 +02005313 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005314 lambda_name = vim_strsave(get_lambda_name());
5315 if (lambda_name == NULL)
5316 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005317 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005318
Bram Moolenaar822ba242020-05-24 23:00:18 +02005319 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005320 {
5321 r = eap->skip ? OK : FAIL;
5322 goto theend;
5323 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005324
5325 // copy over the block scope IDs before compiling
5326 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5327 {
5328 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5329
5330 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5331 if (ufunc->uf_block_ids != NULL)
5332 {
5333 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5334 sizeof(int) * block_depth);
5335 ufunc->uf_block_depth = block_depth;
5336 }
5337 }
5338
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005339 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5340 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005341 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005342 {
5343 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005344 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005345 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005346
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005347 if (is_global)
5348 {
5349 char_u *func_name = vim_strnsave(name_start + 2,
5350 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005351
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005352 if (func_name == NULL)
5353 r = FAIL;
5354 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005355 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005356 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005357 lambda_name = NULL;
5358 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005359 }
5360 else
5361 {
5362 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005363 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005364 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005365
Bram Moolenaareef21022020-08-01 22:16:43 +02005366 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005367 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005368 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005369 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005370 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5371 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005372 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005373
5374theend:
5375 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005376 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005377}
5378
5379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 * Return the length of an assignment operator, or zero if there isn't one.
5381 */
5382 int
5383assignment_len(char_u *p, int *heredoc)
5384{
5385 if (*p == '=')
5386 {
5387 if (p[1] == '<' && p[2] == '<')
5388 {
5389 *heredoc = TRUE;
5390 return 3;
5391 }
5392 return 1;
5393 }
5394 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5395 return 2;
5396 if (STRNCMP(p, "..=", 3) == 0)
5397 return 3;
5398 return 0;
5399}
5400
5401// words that cannot be used as a variable
5402static char *reserved[] = {
5403 "true",
5404 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005405 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 NULL
5407};
5408
Bram Moolenaar752fc692021-01-04 21:57:11 +01005409// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005410typedef enum {
5411 dest_local,
5412 dest_option,
5413 dest_env,
5414 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005415 dest_buffer,
5416 dest_window,
5417 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005418 dest_vimvar,
5419 dest_script,
5420 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005421 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005422} assign_dest_T;
5423
Bram Moolenaar752fc692021-01-04 21:57:11 +01005424// Used by compile_lhs() to store information about the LHS of an assignment
5425// and one argument of ":unlet" with an index.
5426typedef struct {
5427 assign_dest_T lhs_dest; // type of destination
5428
5429 char_u *lhs_name; // allocated name including
5430 // "[expr]" or ".name".
5431 size_t lhs_varlen; // length of the variable without
5432 // "[expr]" or ".name"
5433 char_u *lhs_dest_end; // end of the destination, including
5434 // "[expr]" or ".name".
5435
5436 int lhs_has_index; // has "[expr]" or ".name"
5437
5438 int lhs_new_local; // create new local variable
5439 int lhs_opt_flags; // for when destination is an option
5440 int lhs_vimvaridx; // for when destination is a v:var
5441
5442 lvar_T lhs_local_lvar; // used for existing local destination
5443 lvar_T lhs_arg_lvar; // used for argument destination
5444 lvar_T *lhs_lvar; // points to destination lvar
5445 int lhs_scriptvar_sid;
5446 int lhs_scriptvar_idx;
5447
5448 int lhs_has_type; // type was specified
5449 type_T *lhs_type;
5450 type_T *lhs_member_type;
5451} lhs_T;
5452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005454 * Generate the load instruction for "name".
5455 */
5456 static void
5457generate_loadvar(
5458 cctx_T *cctx,
5459 assign_dest_T dest,
5460 char_u *name,
5461 lvar_T *lvar,
5462 type_T *type)
5463{
5464 switch (dest)
5465 {
5466 case dest_option:
5467 // TODO: check the option exists
5468 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5469 break;
5470 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005471 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5472 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5473 else
5474 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005475 break;
5476 case dest_buffer:
5477 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5478 break;
5479 case dest_window:
5480 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5481 break;
5482 case dest_tab:
5483 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5484 break;
5485 case dest_script:
5486 compile_load_scriptvar(cctx,
5487 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5488 break;
5489 case dest_env:
5490 // Include $ in the name here
5491 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5492 break;
5493 case dest_reg:
5494 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5495 break;
5496 case dest_vimvar:
5497 generate_LOADV(cctx, name + 2, TRUE);
5498 break;
5499 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005500 if (lvar->lv_from_outer > 0)
5501 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5502 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005503 else
5504 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5505 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005506 case dest_expr:
5507 // list or dict value should already be on the stack.
5508 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005509 }
5510}
5511
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005512/*
5513 * Skip over "[expr]" or ".member".
5514 * Does not check for any errors.
5515 */
5516 static char_u *
5517skip_index(char_u *start)
5518{
5519 char_u *p = start;
5520
5521 if (*p == '[')
5522 {
5523 p = skipwhite(p + 1);
5524 (void)skip_expr(&p, NULL);
5525 p = skipwhite(p);
5526 if (*p == ']')
5527 return p + 1;
5528 return p;
5529 }
5530 // if (*p == '.')
5531 return to_name_end(p + 1, TRUE);
5532}
5533
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005534 void
5535vim9_declare_error(char_u *name)
5536{
5537 char *scope = "";
5538
5539 switch (*name)
5540 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005541 case 'g': scope = _("global"); break;
5542 case 'b': scope = _("buffer"); break;
5543 case 'w': scope = _("window"); break;
5544 case 't': scope = _("tab"); break;
5545 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005546 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005547 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005548 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005549 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005550 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005551 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005552 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005553 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005554 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005555}
5556
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005557/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005558 * For one assignment figure out the type of destination. Return it in "dest".
5559 * When not recognized "dest" is not set.
5560 * For an option "opt_flags" is set.
5561 * For a v:var "vimvaridx" is set.
5562 * "type" is set to the destination type if known, unchanted otherwise.
5563 * Return FAIL if an error message was given.
5564 */
5565 static int
5566get_var_dest(
5567 char_u *name,
5568 assign_dest_T *dest,
5569 int cmdidx,
5570 int *opt_flags,
5571 int *vimvaridx,
5572 type_T **type,
5573 cctx_T *cctx)
5574{
5575 char_u *p;
5576
5577 if (*name == '&')
5578 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005579 int cc;
5580 long numval;
5581 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005582
5583 *dest = dest_option;
5584 if (cmdidx == CMD_final || cmdidx == CMD_const)
5585 {
5586 emsg(_(e_const_option));
5587 return FAIL;
5588 }
5589 p = name;
5590 p = find_option_end(&p, opt_flags);
5591 if (p == NULL)
5592 {
5593 // cannot happen?
5594 emsg(_(e_letunexp));
5595 return FAIL;
5596 }
5597 cc = *p;
5598 *p = NUL;
5599 opt_type = get_option_value(skip_option_env_lead(name),
5600 &numval, NULL, *opt_flags);
5601 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005602 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005603 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005604 case gov_unknown:
5605 semsg(_(e_unknown_option), name);
5606 return FAIL;
5607 case gov_string:
5608 case gov_hidden_string:
5609 *type = &t_string;
5610 break;
5611 case gov_bool:
5612 case gov_hidden_bool:
5613 *type = &t_bool;
5614 break;
5615 case gov_number:
5616 case gov_hidden_number:
5617 *type = &t_number;
5618 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005619 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 }
5621 else if (*name == '$')
5622 {
5623 *dest = dest_env;
5624 *type = &t_string;
5625 }
5626 else if (*name == '@')
5627 {
5628 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5629 {
5630 emsg_invreg(name[1]);
5631 return FAIL;
5632 }
5633 *dest = dest_reg;
5634 *type = &t_string;
5635 }
5636 else if (STRNCMP(name, "g:", 2) == 0)
5637 {
5638 *dest = dest_global;
5639 }
5640 else if (STRNCMP(name, "b:", 2) == 0)
5641 {
5642 *dest = dest_buffer;
5643 }
5644 else if (STRNCMP(name, "w:", 2) == 0)
5645 {
5646 *dest = dest_window;
5647 }
5648 else if (STRNCMP(name, "t:", 2) == 0)
5649 {
5650 *dest = dest_tab;
5651 }
5652 else if (STRNCMP(name, "v:", 2) == 0)
5653 {
5654 typval_T *vtv;
5655 int di_flags;
5656
5657 *vimvaridx = find_vim_var(name + 2, &di_flags);
5658 if (*vimvaridx < 0)
5659 {
5660 semsg(_(e_variable_not_found_str), name);
5661 return FAIL;
5662 }
5663 // We use the current value of "sandbox" here, is that OK?
5664 if (var_check_ro(di_flags, name, FALSE))
5665 return FAIL;
5666 *dest = dest_vimvar;
5667 vtv = get_vim_var_tv(*vimvaridx);
5668 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5669 }
5670 return OK;
5671}
5672
5673/*
5674 * Generate a STORE instruction for "dest", not being "dest_local".
5675 * Return FAIL when out of memory.
5676 */
5677 static int
5678generate_store_var(
5679 cctx_T *cctx,
5680 assign_dest_T dest,
5681 int opt_flags,
5682 int vimvaridx,
5683 int scriptvar_idx,
5684 int scriptvar_sid,
5685 type_T *type,
5686 char_u *name)
5687{
5688 switch (dest)
5689 {
5690 case dest_option:
5691 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5692 opt_flags);
5693 case dest_global:
5694 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005695 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5696 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005697 case dest_buffer:
5698 // include b: with the name, easier to execute that way
5699 return generate_STORE(cctx, ISN_STOREB, 0, name);
5700 case dest_window:
5701 // include w: with the name, easier to execute that way
5702 return generate_STORE(cctx, ISN_STOREW, 0, name);
5703 case dest_tab:
5704 // include t: with the name, easier to execute that way
5705 return generate_STORE(cctx, ISN_STORET, 0, name);
5706 case dest_env:
5707 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5708 case dest_reg:
5709 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5710 case dest_vimvar:
5711 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5712 case dest_script:
5713 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005714 // "s:" may be included in the name.
5715 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005716 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005717 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5718 scriptvar_sid, scriptvar_idx, type);
5719 case dest_local:
5720 case dest_expr:
5721 // cannot happen
5722 break;
5723 }
5724 return FAIL;
5725}
5726
Bram Moolenaar752fc692021-01-04 21:57:11 +01005727 static int
5728is_decl_command(int cmdidx)
5729{
5730 return cmdidx == CMD_let || cmdidx == CMD_var
5731 || cmdidx == CMD_final || cmdidx == CMD_const;
5732}
5733
5734/*
5735 * Figure out the LHS type and other properties for an assignment or one item
5736 * of ":unlet" with an index.
5737 * Returns OK or FAIL.
5738 */
5739 static int
5740compile_lhs(
5741 char_u *var_start,
5742 lhs_T *lhs,
5743 int cmdidx,
5744 int heredoc,
5745 int oplen,
5746 cctx_T *cctx)
5747{
5748 char_u *var_end;
5749 int is_decl = is_decl_command(cmdidx);
5750
5751 CLEAR_POINTER(lhs);
5752 lhs->lhs_dest = dest_local;
5753 lhs->lhs_vimvaridx = -1;
5754 lhs->lhs_scriptvar_idx = -1;
5755
5756 // "dest_end" is the end of the destination, including "[expr]" or
5757 // ".name".
5758 // "var_end" is the end of the variable/option/etc. name.
5759 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5760 if (*var_start == '@')
5761 var_end = var_start + 2;
5762 else
5763 {
5764 // skip over the leading "&", "&l:", "&g:" and "$"
5765 var_end = skip_option_env_lead(var_start);
5766 var_end = to_name_end(var_end, TRUE);
5767 }
5768
5769 // "a: type" is declaring variable "a" with a type, not dict "a:".
5770 if (is_decl && lhs->lhs_dest_end == var_start + 2
5771 && lhs->lhs_dest_end[-1] == ':')
5772 --lhs->lhs_dest_end;
5773 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5774 --var_end;
5775
5776 // compute the length of the destination without "[expr]" or ".name"
5777 lhs->lhs_varlen = var_end - var_start;
5778 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5779 if (lhs->lhs_name == NULL)
5780 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005781
5782 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5783 // Something follows after the variable: "var[idx]" or "var.key".
5784 lhs->lhs_has_index = TRUE;
5785
Bram Moolenaar752fc692021-01-04 21:57:11 +01005786 if (heredoc)
5787 lhs->lhs_type = &t_list_string;
5788 else
5789 lhs->lhs_type = &t_any;
5790
5791 if (cctx->ctx_skip != SKIP_YES)
5792 {
5793 int declare_error = FALSE;
5794
5795 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5796 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5797 &lhs->lhs_type, cctx) == FAIL)
5798 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005799 if (lhs->lhs_dest != dest_local
5800 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005801 {
5802 // Specific kind of variable recognized.
5803 declare_error = is_decl;
5804 }
5805 else
5806 {
5807 int idx;
5808
5809 // No specific kind of variable recognized, just a name.
5810 for (idx = 0; reserved[idx] != NULL; ++idx)
5811 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5812 {
5813 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5814 return FAIL;
5815 }
5816
5817
5818 if (lookup_local(var_start, lhs->lhs_varlen,
5819 &lhs->lhs_local_lvar, cctx) == OK)
5820 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5821 else
5822 {
5823 CLEAR_FIELD(lhs->lhs_arg_lvar);
5824 if (arg_exists(var_start, lhs->lhs_varlen,
5825 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5826 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5827 {
5828 if (is_decl)
5829 {
5830 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5831 return FAIL;
5832 }
5833 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5834 }
5835 }
5836 if (lhs->lhs_lvar != NULL)
5837 {
5838 if (is_decl)
5839 {
5840 semsg(_(e_variable_already_declared), lhs->lhs_name);
5841 return FAIL;
5842 }
5843 }
5844 else
5845 {
5846 int script_namespace = lhs->lhs_varlen > 1
5847 && STRNCMP(var_start, "s:", 2) == 0;
5848 int script_var = (script_namespace
5849 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005850 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005851 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005852 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005853 imported_T *import =
5854 find_imported(var_start, lhs->lhs_varlen, cctx);
5855
5856 if (script_namespace || script_var || import != NULL)
5857 {
5858 char_u *rawname = lhs->lhs_name
5859 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5860
5861 if (is_decl)
5862 {
5863 if (script_namespace)
5864 semsg(_(e_cannot_declare_script_variable_in_function),
5865 lhs->lhs_name);
5866 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005867 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005868 lhs->lhs_name);
5869 return FAIL;
5870 }
5871 else if (cctx->ctx_ufunc->uf_script_ctx_version
5872 == SCRIPT_VERSION_VIM9
5873 && script_namespace
5874 && !script_var && import == NULL)
5875 {
5876 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5877 return FAIL;
5878 }
5879
5880 lhs->lhs_dest = dest_script;
5881
5882 // existing script-local variables should have a type
5883 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5884 if (import != NULL)
5885 lhs->lhs_scriptvar_sid = import->imp_sid;
5886 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5887 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005888 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005889 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005890 lhs->lhs_scriptvar_sid, rawname,
5891 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5892 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005893 if (lhs->lhs_scriptvar_idx >= 0)
5894 {
5895 scriptitem_T *si = SCRIPT_ITEM(
5896 lhs->lhs_scriptvar_sid);
5897 svar_T *sv =
5898 ((svar_T *)si->sn_var_vals.ga_data)
5899 + lhs->lhs_scriptvar_idx;
5900 lhs->lhs_type = sv->sv_type;
5901 }
5902 }
5903 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005904 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005905 == FAIL)
5906 return FAIL;
5907 }
5908 }
5909
5910 if (declare_error)
5911 {
5912 vim9_declare_error(lhs->lhs_name);
5913 return FAIL;
5914 }
5915 }
5916
5917 // handle "a:name" as a name, not index "name" on "a"
5918 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5919 var_end = lhs->lhs_dest_end;
5920
5921 if (lhs->lhs_dest != dest_option)
5922 {
5923 if (is_decl && *var_end == ':')
5924 {
5925 char_u *p;
5926
5927 // parse optional type: "let var: type = expr"
5928 if (!VIM_ISWHITE(var_end[1]))
5929 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005930 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005931 return FAIL;
5932 }
5933 p = skipwhite(var_end + 1);
5934 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5935 if (lhs->lhs_type == NULL)
5936 return FAIL;
5937 lhs->lhs_has_type = TRUE;
5938 }
5939 else if (lhs->lhs_lvar != NULL)
5940 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5941 }
5942
Bram Moolenaare42939a2021-04-05 17:11:17 +02005943 if (oplen == 3 && !heredoc
5944 && lhs->lhs_dest != dest_global
5945 && !lhs->lhs_has_index
5946 && lhs->lhs_type->tt_type != VAR_STRING
5947 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005948 {
5949 emsg(_(e_can_only_concatenate_to_string));
5950 return FAIL;
5951 }
5952
5953 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5954 && cctx->ctx_skip != SKIP_YES)
5955 {
5956 if (oplen > 1 && !heredoc)
5957 {
5958 // +=, /=, etc. require an existing variable
5959 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5960 return FAIL;
5961 }
5962 if (!is_decl)
5963 {
5964 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5965 return FAIL;
5966 }
5967
Bram Moolenaar3f327882021-03-17 20:56:38 +01005968 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005969 if ((lhs->lhs_type->tt_type == VAR_FUNC
5970 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005971 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005972 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005973
5974 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005975 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5976 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5977 if (lhs->lhs_lvar == NULL)
5978 return FAIL;
5979 lhs->lhs_new_local = TRUE;
5980 }
5981
5982 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005983 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005984 {
5985 // Something follows after the variable: "var[idx]" or "var.key".
5986 // TODO: should we also handle "->func()" here?
5987 if (is_decl)
5988 {
5989 emsg(_(e_cannot_use_index_when_declaring_variable));
5990 return FAIL;
5991 }
5992
5993 if (var_start[lhs->lhs_varlen] == '['
5994 || var_start[lhs->lhs_varlen] == '.')
5995 {
5996 char_u *after = var_start + lhs->lhs_varlen;
5997 char_u *p;
5998
5999 // Only the last index is used below, if there are others
6000 // before it generate code for the expression. Thus for
6001 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6002 for (;;)
6003 {
6004 p = skip_index(after);
6005 if (*p != '[' && *p != '.')
6006 break;
6007 after = p;
6008 }
6009 if (after > var_start + lhs->lhs_varlen)
6010 {
6011 lhs->lhs_varlen = after - var_start;
6012 lhs->lhs_dest = dest_expr;
6013 // We don't know the type before evaluating the expression,
6014 // use "any" until then.
6015 lhs->lhs_type = &t_any;
6016 }
6017
Bram Moolenaar752fc692021-01-04 21:57:11 +01006018 if (lhs->lhs_type->tt_member == NULL)
6019 lhs->lhs_member_type = &t_any;
6020 else
6021 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6022 }
6023 else
6024 {
6025 semsg("Not supported yet: %s", var_start);
6026 return FAIL;
6027 }
6028 }
6029 return OK;
6030}
6031
6032/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006033 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6034 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006035 */
6036 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006037compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 char_u *var_start,
6039 lhs_T *lhs,
6040 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006041 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006042 cctx_T *cctx)
6043{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006044 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006045 char_u *p;
6046 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006047
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 p = var_start + varlen;
6049 if (*p == '[')
6050 {
6051 p = skipwhite(p + 1);
6052 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006053
6054 if (r == OK && *skipwhite(p) == ':')
6055 {
6056 // unlet var[idx : idx]
6057 if (is_assign)
6058 {
6059 semsg(_(e_cannot_use_range_with_assignment_str), p);
6060 return FAIL;
6061 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006062 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006063 p = skipwhite(p);
6064 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6065 {
6066 semsg(_(e_white_space_required_before_and_after_str_at_str),
6067 ":", p);
6068 return FAIL;
6069 }
6070 p = skipwhite(p + 1);
6071 r = compile_expr0(&p, cctx);
6072 }
6073
Bram Moolenaar752fc692021-01-04 21:57:11 +01006074 if (r == OK && *skipwhite(p) != ']')
6075 {
6076 // this should not happen
6077 emsg(_(e_missbrac));
6078 r = FAIL;
6079 }
6080 }
6081 else // if (*p == '.')
6082 {
6083 char_u *key_end = to_name_end(p + 1, TRUE);
6084 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6085
6086 r = generate_PUSHS(cctx, key);
6087 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006088 return r;
6089}
6090
6091/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006092 * For a LHS with an index, load the variable to be indexed.
6093 */
6094 static int
6095compile_load_lhs(
6096 lhs_T *lhs,
6097 char_u *var_start,
6098 type_T *rhs_type,
6099 cctx_T *cctx)
6100{
6101 if (lhs->lhs_dest == dest_expr)
6102 {
6103 size_t varlen = lhs->lhs_varlen;
6104 int c = var_start[varlen];
6105 char_u *p = var_start;
6106 garray_T *stack = &cctx->ctx_type_stack;
6107
6108 // Evaluate "ll[expr]" of "ll[expr][idx]"
6109 var_start[varlen] = NUL;
6110 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6111 {
6112 // this should not happen
6113 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006114 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006115 return FAIL;
6116 }
6117 var_start[varlen] = c;
6118
6119 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6120 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6121 // now we can properly check the type
6122 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6123 && rhs_type != &t_void
6124 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6125 FALSE, FALSE) == FAIL)
6126 return FAIL;
6127 }
6128 else
6129 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6130 lhs->lhs_lvar, lhs->lhs_type);
6131 return OK;
6132}
6133
6134/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006135 * Assignment to a list or dict member, or ":unlet" for the item, using the
6136 * information in "lhs".
6137 * Returns OK or FAIL.
6138 */
6139 static int
6140compile_assign_unlet(
6141 char_u *var_start,
6142 lhs_T *lhs,
6143 int is_assign,
6144 type_T *rhs_type,
6145 cctx_T *cctx)
6146{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006147 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006148 garray_T *stack = &cctx->ctx_type_stack;
6149 int range = FALSE;
6150
6151 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006152 return FAIL;
6153
6154 if (lhs->lhs_type == &t_any)
6155 {
6156 // Index on variable of unknown type: check at runtime.
6157 dest_type = VAR_ANY;
6158 }
6159 else
6160 {
6161 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006162 if (dest_type == VAR_DICT && range)
6163 {
6164 emsg(e_cannot_use_range_with_dictionary);
6165 return FAIL;
6166 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6168 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006169 if (dest_type == VAR_LIST)
6170 {
6171 if (range
6172 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006173 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006174 return FAIL;
6175 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6176 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6177 return FAIL;
6178 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006179 }
6180
6181 // Load the dict or list. On the stack we then have:
6182 // - value (for assignment, not for :unlet)
6183 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006184 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006185 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006186 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6187 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006188
6189 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6190 {
6191 if (is_assign)
6192 {
6193 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6194
6195 if (isn == NULL)
6196 return FAIL;
6197 isn->isn_arg.vartype = dest_type;
6198 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006199 else if (range)
6200 {
6201 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6202 return FAIL;
6203 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006204 else
6205 {
6206 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6207 return FAIL;
6208 }
6209 }
6210 else
6211 {
6212 emsg(_(e_indexable_type_required));
6213 return FAIL;
6214 }
6215
6216 return OK;
6217}
6218
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006219/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006220 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006221 * "let name"
6222 * "var name = expr"
6223 * "final name = expr"
6224 * "const name = expr"
6225 * "name = expr"
6226 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006227 * Return NULL for an error.
6228 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229 */
6230 static char_u *
6231compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6232{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006233 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006234 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006235 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 char_u *ret = NULL;
6237 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006238 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006239 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006241 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 int oplen = 0;
6244 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006245 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006247 int is_decl = is_decl_command(cmdidx);
6248 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006249 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006251 // Skip over the "var" or "[var, var]" to get to any "=".
6252 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6253 if (p == NULL)
6254 return *arg == '[' ? arg : NULL;
6255
6256 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006258 // TODO: should we allow this, and figure out type inference from list
6259 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006260 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 return NULL;
6262 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006263 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 sp = p;
6266 p = skipwhite(p);
6267 op = p;
6268 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006269
6270 if (var_count > 0 && oplen == 0)
6271 // can be something like "[1, 2]->func()"
6272 return arg;
6273
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006274 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006276 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006277 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006278 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280 if (heredoc)
6281 {
6282 list_T *l;
6283 listitem_T *li;
6284
6285 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006286 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006288 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006289 if (l == NULL)
6290 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291
Bram Moolenaar078269b2020-09-21 20:35:55 +02006292 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006294 // Push each line and the create the list.
6295 FOR_ALL_LIST_ITEMS(l, li)
6296 {
6297 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6298 li->li_tv.vval.v_string = NULL;
6299 }
6300 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 list_free(l);
6303 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006308 char_u *wp;
6309
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310 // for "[var, var] = expr" evaluate the expression here, loop over the
6311 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006312 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006313
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006314 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006315 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006316 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 if (compile_expr0(&p, cctx) == FAIL)
6318 return NULL;
6319 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006321 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006323 type_T *stacktype;
6324
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006325 stacktype = stack->ga_len == 0 ? &t_void
6326 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006327 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006329 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330 goto theend;
6331 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006332 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006333 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006334 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006335 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006336 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6337 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006338 if (stacktype->tt_member != NULL)
6339 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006340 }
6341 }
6342
6343 /*
6344 * Loop over variables in "[var, var] = expr".
6345 * For "var = expr" and "let var: type" this is done only once.
6346 */
6347 if (var_count > 0)
6348 var_start = skipwhite(arg + 1); // skip over the "["
6349 else
6350 var_start = arg;
6351 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6352 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006353 int instr_count = -1;
6354
Bram Moolenaar752fc692021-01-04 21:57:11 +01006355 vim_free(lhs.lhs_name);
6356
6357 /*
6358 * Figure out the LHS type and other properties.
6359 */
6360 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6361 goto theend;
6362
6363 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006364 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006365 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006366 goto theend;
6367 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006368 if (!is_decl && lhs.lhs_lvar != NULL
6369 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006370 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006371 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006372 goto theend;
6373 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006374
6375 if (!heredoc)
6376 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006377 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006378 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006379 if (oplen > 0 && var_count == 0)
6380 {
6381 // skip over the "=" and the expression
6382 p = skipwhite(op + oplen);
6383 compile_expr0(&p, cctx);
6384 }
6385 }
6386 else if (oplen > 0)
6387 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006388 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006389 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006390
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006391 // For "var = expr" evaluate the expression.
6392 if (var_count == 0)
6393 {
6394 int r;
6395
6396 // for "+=", "*=", "..=" etc. first load the current value
6397 if (*op != '=')
6398 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006399 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006400
Bram Moolenaar752fc692021-01-04 21:57:11 +01006401 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006402 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006403 int range = FALSE;
6404
6405 // Get member from list or dict. First compile the
6406 // index value.
6407 if (compile_assign_index(var_start, &lhs,
6408 TRUE, &range, cctx) == FAIL)
6409 goto theend;
6410
6411 // Get the member.
6412 if (compile_member(FALSE, cctx) == FAIL)
6413 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006414 }
6415 }
6416
6417 // Compile the expression. Temporarily hide the new local
6418 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006419 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006420 --cctx->ctx_locals.ga_len;
6421 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006422 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006423 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006424 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006425 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006426 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006427 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006428 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006429 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006430 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006431 ++cctx->ctx_locals.ga_len;
6432 if (r == FAIL)
6433 goto theend;
6434 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006435 else if (semicolon && var_idx == var_count - 1)
6436 {
6437 // For "[var; var] = expr" get the rest of the list
6438 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6439 goto theend;
6440 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006441 else
6442 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006443 // For "[var, var] = expr" get the "var_idx" item from the
6444 // list.
6445 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006446 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006447 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006448
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006449 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006450 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006451 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006452 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006453 if ((rhs_type->tt_type == VAR_FUNC
6454 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006455 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006456 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006457 goto theend;
6458
Bram Moolenaar752fc692021-01-04 21:57:11 +01006459 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006460 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006461 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006462 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006463 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006464 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006465 }
6466 else
6467 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006468 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006469 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006470 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006471 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006472 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006474 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006476 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006478 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006479 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006480 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006481 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006483
Bram Moolenaar77709b12021-04-03 21:01:01 +02006484 // Without operator check type here, otherwise below.
6485 // Use the line number of the assignment.
6486 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 if (lhs.lhs_has_index)
6488 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006489 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006490 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006491 goto theend;
6492 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006493 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006495 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006496 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006497 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006498 else if (cmdidx == CMD_final)
6499 {
6500 emsg(_(e_final_requires_a_value));
6501 goto theend;
6502 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006503 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006505 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006506 goto theend;
6507 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006509 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006510 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006511 goto theend;
6512 }
6513 else
6514 {
6515 // variables are always initialized
6516 if (ga_grow(instr, 1) == FAIL)
6517 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006518 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006519 {
6520 case VAR_BOOL:
6521 generate_PUSHBOOL(cctx, VVAL_FALSE);
6522 break;
6523 case VAR_FLOAT:
6524#ifdef FEAT_FLOAT
6525 generate_PUSHF(cctx, 0.0);
6526#endif
6527 break;
6528 case VAR_STRING:
6529 generate_PUSHS(cctx, NULL);
6530 break;
6531 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006532 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006533 break;
6534 case VAR_FUNC:
6535 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6536 break;
6537 case VAR_LIST:
6538 generate_NEWLIST(cctx, 0);
6539 break;
6540 case VAR_DICT:
6541 generate_NEWDICT(cctx, 0);
6542 break;
6543 case VAR_JOB:
6544 generate_PUSHJOB(cctx, NULL);
6545 break;
6546 case VAR_CHANNEL:
6547 generate_PUSHCHANNEL(cctx, NULL);
6548 break;
6549 case VAR_NUMBER:
6550 case VAR_UNKNOWN:
6551 case VAR_ANY:
6552 case VAR_PARTIAL:
6553 case VAR_VOID:
6554 case VAR_SPECIAL: // cannot happen
6555 generate_PUSHNR(cctx, 0);
6556 break;
6557 }
6558 }
6559 if (var_count == 0)
6560 end = p;
6561 }
6562
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006563 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006564 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006565 break;
6566
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006567 if (oplen > 0 && *op != '=')
6568 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006569 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006570 type_T *stacktype;
6571
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006572 if (*op == '.')
6573 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006574 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006575 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006576 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006577 if (
6578#ifdef FEAT_FLOAT
6579 // If variable is float operation with number is OK.
6580 !(expected == &t_float && stacktype == &t_number) &&
6581#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006582 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006583 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006584 goto theend;
6585
6586 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006587 {
6588 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6589 goto theend;
6590 }
6591 else if (*op == '+')
6592 {
6593 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006594 operator_type(lhs.lhs_member_type, stacktype),
6595 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006596 goto theend;
6597 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006598 else if (generate_two_op(cctx, op) == FAIL)
6599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601
Bram Moolenaar752fc692021-01-04 21:57:11 +01006602 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006603 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006604 // Use the info in "lhs" to store the value at the index in the
6605 // list or dict.
6606 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6607 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006608 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006609 }
6610 else
6611 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006613 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006614 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006615 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006616 generate_LOCKCONST(cctx);
6617
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006618 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006619 && (lhs.lhs_type->tt_type == VAR_DICT
6620 || lhs.lhs_type->tt_type == VAR_LIST)
6621 && lhs.lhs_type->tt_member != NULL
6622 && lhs.lhs_type->tt_member != &t_any
6623 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006624 // Set the type in the list or dict, so that it can be checked,
6625 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006627
Bram Moolenaar752fc692021-01-04 21:57:11 +01006628 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006629 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006630 if (generate_store_var(cctx, lhs.lhs_dest,
6631 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6632 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6633 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006634 goto theend;
6635 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006637 {
6638 isn_T *isn = ((isn_T *)instr->ga_data)
6639 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006640
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006641 // optimization: turn "var = 123" from ISN_PUSHNR +
6642 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006643 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006644 && instr->ga_len == instr_count + 1
6645 && isn->isn_type == ISN_PUSHNR)
6646 {
6647 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006648
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006649 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006650 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006651 isn->isn_arg.storenr.stnr_val = val;
6652 if (stack->ga_len > 0)
6653 --stack->ga_len;
6654 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006655 else if (lhs.lhs_lvar->lv_from_outer > 0)
6656 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6657 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006658 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006660 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006661 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006662
6663 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006664 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006666
6667 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006668 if (var_count > 0 && !semicolon)
6669 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006670 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006671 goto theend;
6672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673
Bram Moolenaarb2097502020-07-19 17:17:02 +02006674 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006675
6676theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006678 return ret;
6679}
6680
6681/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006682 * Check for an assignment at "eap->cmd", compile it if found.
6683 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6684 */
6685 static int
6686may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6687{
6688 char_u *pskip;
6689 char_u *p;
6690
6691 // Assuming the command starts with a variable or function name,
6692 // find what follows.
6693 // Skip over "var.member", "var[idx]" and the like.
6694 // Also "&opt = val", "$ENV = val" and "@r = val".
6695 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6696 ? eap->cmd + 1 : eap->cmd;
6697 p = to_name_end(pskip, TRUE);
6698 if (p > eap->cmd && *p != NUL)
6699 {
6700 char_u *var_end;
6701 int oplen;
6702 int heredoc;
6703
6704 if (eap->cmd[0] == '@')
6705 var_end = eap->cmd + 2;
6706 else
6707 var_end = find_name_end(pskip, NULL, NULL,
6708 FNE_CHECK_START | FNE_INCL_BR);
6709 oplen = assignment_len(skipwhite(var_end), &heredoc);
6710 if (oplen > 0)
6711 {
6712 size_t len = p - eap->cmd;
6713
6714 // Recognize an assignment if we recognize the variable
6715 // name:
6716 // "g:var = expr"
6717 // "local = expr" where "local" is a local var.
6718 // "script = expr" where "script" is a script-local var.
6719 // "import = expr" where "import" is an imported var
6720 // "&opt = expr"
6721 // "$ENV = expr"
6722 // "@r = expr"
6723 if (*eap->cmd == '&'
6724 || *eap->cmd == '$'
6725 || *eap->cmd == '@'
6726 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006727 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006728 {
6729 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6730 if (*line == NULL || *line == eap->cmd)
6731 return FAIL;
6732 return OK;
6733 }
6734 }
6735 }
6736
6737 if (*eap->cmd == '[')
6738 {
6739 // [var, var] = expr
6740 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6741 if (*line == NULL)
6742 return FAIL;
6743 if (*line != eap->cmd)
6744 return OK;
6745 }
6746 return NOTDONE;
6747}
6748
6749/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006750 * Check if "name" can be "unlet".
6751 */
6752 int
6753check_vim9_unlet(char_u *name)
6754{
6755 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6756 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006757 // "unlet s:var" is allowed in legacy script.
6758 if (*name == 's' && !script_is_vim9())
6759 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006760 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006761 return FAIL;
6762 }
6763 return OK;
6764}
6765
6766/*
6767 * Callback passed to ex_unletlock().
6768 */
6769 static int
6770compile_unlet(
6771 lval_T *lvp,
6772 char_u *name_end,
6773 exarg_T *eap,
6774 int deep UNUSED,
6775 void *coookie)
6776{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006777 cctx_T *cctx = coookie;
6778 char_u *p = lvp->ll_name;
6779 int cc = *name_end;
6780 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006781
Bram Moolenaar752fc692021-01-04 21:57:11 +01006782 if (cctx->ctx_skip == SKIP_YES)
6783 return OK;
6784
6785 *name_end = NUL;
6786 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006787 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006788 // :unlet $ENV_VAR
6789 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6790 }
6791 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6792 {
6793 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006794
Bram Moolenaar752fc692021-01-04 21:57:11 +01006795 // This is similar to assigning: lookup the list/dict, compile the
6796 // idx/key. Then instead of storing the value unlet the item.
6797 // unlet {list}[idx]
6798 // unlet {dict}[key] dict.key
6799 //
6800 // Figure out the LHS type and other properties.
6801 //
6802 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6803
6804 // : unlet an indexed item
6805 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006806 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006807 iemsg("called compile_lhs() without an index");
6808 ret = FAIL;
6809 }
6810 else
6811 {
6812 // Use the info in "lhs" to unlet the item at the index in the
6813 // list or dict.
6814 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006815 }
6816
Bram Moolenaar752fc692021-01-04 21:57:11 +01006817 vim_free(lhs.lhs_name);
6818 }
6819 else if (check_vim9_unlet(p) == FAIL)
6820 {
6821 ret = FAIL;
6822 }
6823 else
6824 {
6825 // Normal name. Only supports g:, w:, t: and b: namespaces.
6826 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006827 }
6828
Bram Moolenaar752fc692021-01-04 21:57:11 +01006829 *name_end = cc;
6830 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006831}
6832
6833/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006834 * Callback passed to ex_unletlock().
6835 */
6836 static int
6837compile_lock_unlock(
6838 lval_T *lvp,
6839 char_u *name_end,
6840 exarg_T *eap,
6841 int deep UNUSED,
6842 void *coookie)
6843{
6844 cctx_T *cctx = coookie;
6845 int cc = *name_end;
6846 char_u *p = lvp->ll_name;
6847 int ret = OK;
6848 size_t len;
6849 char_u *buf;
6850
6851 if (cctx->ctx_skip == SKIP_YES)
6852 return OK;
6853
6854 // Cannot use :lockvar and :unlockvar on local variables.
6855 if (p[1] != ':')
6856 {
6857 char_u *end = skip_var_one(p, FALSE);
6858
6859 if (lookup_local(p, end - p, NULL, cctx) == OK)
6860 {
6861 emsg(_(e_cannot_lock_unlock_local_variable));
6862 return FAIL;
6863 }
6864 }
6865
6866 // Checking is done at runtime.
6867 *name_end = NUL;
6868 len = name_end - p + 20;
6869 buf = alloc(len);
6870 if (buf == NULL)
6871 ret = FAIL;
6872 else
6873 {
6874 vim_snprintf((char *)buf, len, "%s %s",
6875 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6876 p);
6877 ret = generate_EXEC(cctx, buf);
6878
6879 vim_free(buf);
6880 *name_end = cc;
6881 }
6882 return ret;
6883}
6884
6885/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006886 * compile "unlet var", "lock var" and "unlock var"
6887 * "arg" points to "var".
6888 */
6889 static char_u *
6890compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6891{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006892 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6893 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6894 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006895 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6896}
6897
6898/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 * Compile an :import command.
6900 */
6901 static char_u *
6902compile_import(char_u *arg, cctx_T *cctx)
6903{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006904 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006905}
6906
6907/*
6908 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6909 */
6910 static int
6911compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6912{
6913 garray_T *instr = &cctx->ctx_instr;
6914 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6915
6916 if (endlabel == NULL)
6917 return FAIL;
6918 endlabel->el_next = *el;
6919 *el = endlabel;
6920 endlabel->el_end_label = instr->ga_len;
6921
6922 generate_JUMP(cctx, when, 0);
6923 return OK;
6924}
6925
6926 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006927compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928{
6929 garray_T *instr = &cctx->ctx_instr;
6930
6931 while (*el != NULL)
6932 {
6933 endlabel_T *cur = (*el);
6934 isn_T *isn;
6935
6936 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006937 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 *el = cur->el_next;
6939 vim_free(cur);
6940 }
6941}
6942
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006943 static void
6944compile_free_jump_to_end(endlabel_T **el)
6945{
6946 while (*el != NULL)
6947 {
6948 endlabel_T *cur = (*el);
6949
6950 *el = cur->el_next;
6951 vim_free(cur);
6952 }
6953}
6954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955/*
6956 * Create a new scope and set up the generic items.
6957 */
6958 static scope_T *
6959new_scope(cctx_T *cctx, scopetype_T type)
6960{
6961 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6962
6963 if (scope == NULL)
6964 return NULL;
6965 scope->se_outer = cctx->ctx_scope;
6966 cctx->ctx_scope = scope;
6967 scope->se_type = type;
6968 scope->se_local_count = cctx->ctx_locals.ga_len;
6969 return scope;
6970}
6971
6972/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006973 * Free the current scope and go back to the outer scope.
6974 */
6975 static void
6976drop_scope(cctx_T *cctx)
6977{
6978 scope_T *scope = cctx->ctx_scope;
6979
6980 if (scope == NULL)
6981 {
6982 iemsg("calling drop_scope() without a scope");
6983 return;
6984 }
6985 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006986 switch (scope->se_type)
6987 {
6988 case IF_SCOPE:
6989 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6990 case FOR_SCOPE:
6991 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6992 case WHILE_SCOPE:
6993 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6994 case TRY_SCOPE:
6995 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6996 case NO_SCOPE:
6997 case BLOCK_SCOPE:
6998 break;
6999 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007000 vim_free(scope);
7001}
7002
7003/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007004 * compile "if expr"
7005 *
7006 * "if expr" Produces instructions:
7007 * EVAL expr Push result of "expr"
7008 * JUMP_IF_FALSE end
7009 * ... body ...
7010 * end:
7011 *
7012 * "if expr | else" Produces instructions:
7013 * EVAL expr Push result of "expr"
7014 * JUMP_IF_FALSE else
7015 * ... body ...
7016 * JUMP_ALWAYS end
7017 * else:
7018 * ... body ...
7019 * end:
7020 *
7021 * "if expr1 | elseif expr2 | else" Produces instructions:
7022 * EVAL expr Push result of "expr"
7023 * JUMP_IF_FALSE elseif
7024 * ... body ...
7025 * JUMP_ALWAYS end
7026 * elseif:
7027 * EVAL expr Push result of "expr"
7028 * JUMP_IF_FALSE else
7029 * ... body ...
7030 * JUMP_ALWAYS end
7031 * else:
7032 * ... body ...
7033 * end:
7034 */
7035 static char_u *
7036compile_if(char_u *arg, cctx_T *cctx)
7037{
7038 char_u *p = arg;
7039 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007040 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007042 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007043 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044
Bram Moolenaara5565e42020-05-09 15:44:01 +02007045 CLEAR_FIELD(ppconst);
7046 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007047 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007048 clear_ppconst(&ppconst);
7049 return NULL;
7050 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007051 if (!ends_excmd2(arg, skipwhite(p)))
7052 {
7053 semsg(_(e_trailing_arg), p);
7054 return NULL;
7055 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007056 if (cctx->ctx_skip == SKIP_YES)
7057 clear_ppconst(&ppconst);
7058 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007059 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007060 int error = FALSE;
7061 int v;
7062
Bram Moolenaara5565e42020-05-09 15:44:01 +02007063 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007064 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007065 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007066 if (error)
7067 return NULL;
7068 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007069 }
7070 else
7071 {
7072 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007073 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007074 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007075 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007076 if (bool_on_stack(cctx) == FAIL)
7077 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaara91a7132021-03-25 21:12:15 +01007080 // CMDMOD_REV must come before the jump
7081 generate_undo_cmdmods(cctx);
7082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083 scope = new_scope(cctx, IF_SCOPE);
7084 if (scope == NULL)
7085 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007086 scope->se_skip_save = skip_save;
7087 // "is_had_return" will be reset if any block does not end in :return
7088 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007090 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007091 {
7092 // "where" is set when ":elseif", "else" or ":endif" is found
7093 scope->se_u.se_if.is_if_label = instr->ga_len;
7094 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7095 }
7096 else
7097 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098
Bram Moolenaarced68a02021-01-24 17:53:47 +01007099#ifdef FEAT_PROFILE
7100 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7101 && skip_save != SKIP_YES)
7102 {
7103 // generated a profile start, need to generate a profile end, since it
7104 // won't be done after returning
7105 cctx->ctx_skip = SKIP_NOT;
7106 generate_instr(cctx, ISN_PROF_END);
7107 cctx->ctx_skip = SKIP_YES;
7108 }
7109#endif
7110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 return p;
7112}
7113
7114 static char_u *
7115compile_elseif(char_u *arg, cctx_T *cctx)
7116{
7117 char_u *p = arg;
7118 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007119 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 isn_T *isn;
7121 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007122 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007123 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124
7125 if (scope == NULL || scope->se_type != IF_SCOPE)
7126 {
7127 emsg(_(e_elseif_without_if));
7128 return NULL;
7129 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007130 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007131 if (!cctx->ctx_had_return)
7132 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133
Bram Moolenaarced68a02021-01-24 17:53:47 +01007134 if (cctx->ctx_skip == SKIP_NOT)
7135 {
7136 // previous block was executed, this one and following will not
7137 cctx->ctx_skip = SKIP_YES;
7138 scope->se_u.se_if.is_seen_skip_not = TRUE;
7139 }
7140 if (scope->se_u.se_if.is_seen_skip_not)
7141 {
7142 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007143 // Do not count the "elseif" for profiling and cmdmod
7144 instr->ga_len = current_instr_idx(cctx);
7145
Bram Moolenaarced68a02021-01-24 17:53:47 +01007146 skip_expr_cctx(&p, cctx);
7147 return p;
7148 }
7149
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007150 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007151 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007152 int moved_cmdmod = FALSE;
7153
7154 // Move any CMDMOD instruction to after the jump
7155 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7156 {
7157 if (ga_grow(instr, 1) == FAIL)
7158 return NULL;
7159 ((isn_T *)instr->ga_data)[instr->ga_len] =
7160 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7161 --instr->ga_len;
7162 moved_cmdmod = TRUE;
7163 }
7164
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007165 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007167 return NULL;
7168 // previous "if" or "elseif" jumps here
7169 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7170 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007171 if (moved_cmdmod)
7172 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007173 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007174
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007175 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007176 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007177 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007178 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007179 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007180#ifdef FEAT_PROFILE
7181 if (cctx->ctx_profiling)
7182 {
7183 // the previous block was skipped, need to profile this line
7184 generate_instr(cctx, ISN_PROF_START);
7185 instr_count = instr->ga_len;
7186 }
7187#endif
7188 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007189 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007190 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007191 clear_ppconst(&ppconst);
7192 return NULL;
7193 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007194 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007195 if (!ends_excmd2(arg, skipwhite(p)))
7196 {
7197 semsg(_(e_trailing_arg), p);
7198 return NULL;
7199 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007200 if (scope->se_skip_save == SKIP_YES)
7201 clear_ppconst(&ppconst);
7202 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007203 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007204 int error = FALSE;
7205 int v;
7206
Bram Moolenaar7f141552020-05-09 17:35:53 +02007207 // The expression results in a constant.
7208 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007209 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7210 if (error)
7211 return NULL;
7212 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007213 clear_ppconst(&ppconst);
7214 scope->se_u.se_if.is_if_label = -1;
7215 }
7216 else
7217 {
7218 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007219 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007220 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007221 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007222 if (bool_on_stack(cctx) == FAIL)
7223 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224
Bram Moolenaara91a7132021-03-25 21:12:15 +01007225 // CMDMOD_REV must come before the jump
7226 generate_undo_cmdmods(cctx);
7227
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007228 // "where" is set when ":elseif", "else" or ":endif" is found
7229 scope->se_u.se_if.is_if_label = instr->ga_len;
7230 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
7233 return p;
7234}
7235
7236 static char_u *
7237compile_else(char_u *arg, cctx_T *cctx)
7238{
7239 char_u *p = arg;
7240 garray_T *instr = &cctx->ctx_instr;
7241 isn_T *isn;
7242 scope_T *scope = cctx->ctx_scope;
7243
7244 if (scope == NULL || scope->se_type != IF_SCOPE)
7245 {
7246 emsg(_(e_else_without_if));
7247 return NULL;
7248 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007249 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007250 if (!cctx->ctx_had_return)
7251 scope->se_u.se_if.is_had_return = FALSE;
7252 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253
Bram Moolenaarced68a02021-01-24 17:53:47 +01007254#ifdef FEAT_PROFILE
7255 if (cctx->ctx_profiling)
7256 {
7257 if (cctx->ctx_skip == SKIP_NOT
7258 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7259 .isn_type == ISN_PROF_START)
7260 // the previous block was executed, do not count "else" for profiling
7261 --instr->ga_len;
7262 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7263 {
7264 // the previous block was not executed, this one will, do count the
7265 // "else" for profiling
7266 cctx->ctx_skip = SKIP_NOT;
7267 generate_instr(cctx, ISN_PROF_END);
7268 generate_instr(cctx, ISN_PROF_START);
7269 cctx->ctx_skip = SKIP_YES;
7270 }
7271 }
7272#endif
7273
7274 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007275 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007276 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007277 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007278 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007279 if (!cctx->ctx_had_return
7280 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7281 JUMP_ALWAYS, cctx) == FAIL)
7282 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007283 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007284
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007285 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007286 {
7287 if (scope->se_u.se_if.is_if_label >= 0)
7288 {
7289 // previous "if" or "elseif" jumps here
7290 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7291 isn->isn_arg.jump.jump_where = instr->ga_len;
7292 scope->se_u.se_if.is_if_label = -1;
7293 }
7294 }
7295
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007296 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007297 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299
7300 return p;
7301}
7302
7303 static char_u *
7304compile_endif(char_u *arg, cctx_T *cctx)
7305{
7306 scope_T *scope = cctx->ctx_scope;
7307 ifscope_T *ifscope;
7308 garray_T *instr = &cctx->ctx_instr;
7309 isn_T *isn;
7310
Bram Moolenaarfa984412021-03-25 22:15:28 +01007311 if (misplaced_cmdmod(cctx))
7312 return NULL;
7313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 if (scope == NULL || scope->se_type != IF_SCOPE)
7315 {
7316 emsg(_(e_endif_without_if));
7317 return NULL;
7318 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007319 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007320 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007321 if (!cctx->ctx_had_return)
7322 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007323
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007324 if (scope->se_u.se_if.is_if_label >= 0)
7325 {
7326 // previous "if" or "elseif" jumps here
7327 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7328 isn->isn_arg.jump.jump_where = instr->ga_len;
7329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007331 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007332
7333#ifdef FEAT_PROFILE
7334 // even when skipping we count the endif as executed, unless the block it's
7335 // in is skipped
7336 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7337 && scope->se_skip_save != SKIP_YES)
7338 {
7339 cctx->ctx_skip = SKIP_NOT;
7340 generate_instr(cctx, ISN_PROF_START);
7341 }
7342#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 cctx->ctx_skip = scope->se_skip_save;
7344
7345 // If all the blocks end in :return and there is an :else then the
7346 // had_return flag is set.
7347 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007349 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 return arg;
7351}
7352
7353/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007354 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 *
7356 * Produces instructions:
7357 * PUSHNR -1
7358 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007359 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7361 * - if beyond end, jump to "end"
7362 * - otherwise get item from list and push it
7363 * STORE var Store item in "var"
7364 * ... body ...
7365 * JUMP top Jump back to repeat
7366 * end: DROP Drop the result of "expr"
7367 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007368 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7369 * UNPACK 2 Split item in 2
7370 * STORE var1 Store item in "var1"
7371 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 */
7373 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007374compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007376 char_u *arg;
7377 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007378 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007380 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007381 int var_count = 0;
7382 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 garray_T *stack = &cctx->ctx_type_stack;
7385 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007386 lvar_T *loop_lvar; // loop iteration variable
7387 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007389 type_T *item_type = &t_any;
7390 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391
Bram Moolenaar792f7862020-11-23 08:31:18 +01007392 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007393 if (p == NULL)
7394 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007395 if (var_count == 0)
7396 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397
7398 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007399 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007400 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7401 return NULL;
7402 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 {
7404 emsg(_(e_missing_in));
7405 return NULL;
7406 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007407 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007408 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7409 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 scope = new_scope(cctx, FOR_SCOPE);
7412 if (scope == NULL)
7413 return NULL;
7414
Bram Moolenaar792f7862020-11-23 08:31:18 +01007415 // Reserve a variable to store the loop iteration counter and initialize it
7416 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007417 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7418 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007419 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007420 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007421 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007423 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007424 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425
7426 // compile "expr", it remains on the stack until "endfor"
7427 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007428 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007429 {
7430 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007432 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007433 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007435 // If we know the type of "var" and it is a not a list or string we can
7436 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007438 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7439 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007441 // TODO: support Blob
7442 semsg(_(e_for_loop_on_str_not_supported),
7443 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007444 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445 return NULL;
7446 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007447
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007448 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007449 {
7450 if (var_count == 1)
7451 item_type = vartype->tt_member;
7452 else if (vartype->tt_member->tt_type == VAR_LIST
7453 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7454 item_type = vartype->tt_member->tt_member;
7455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
Bram Moolenaara91a7132021-03-25 21:12:15 +01007457 // CMDMOD_REV must come before the FOR instruction
7458 generate_undo_cmdmods(cctx);
7459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007460 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007461 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007462 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463
Bram Moolenaar792f7862020-11-23 08:31:18 +01007464 arg = arg_start;
7465 if (var_count > 1)
7466 {
7467 generate_UNPACK(cctx, var_count, semicolon);
7468 arg = skipwhite(arg + 1); // skip white after '['
7469
7470 // the list item is replaced by a number of items
7471 if (ga_grow(stack, var_count - 1) == FAIL)
7472 {
7473 drop_scope(cctx);
7474 return NULL;
7475 }
7476 --stack->ga_len;
7477 for (idx = 0; idx < var_count; ++idx)
7478 {
7479 ((type_T **)stack->ga_data)[stack->ga_len] =
7480 (semicolon && idx == 0) ? vartype : item_type;
7481 ++stack->ga_len;
7482 }
7483 }
7484
7485 for (idx = 0; idx < var_count; ++idx)
7486 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007487 assign_dest_T dest = dest_local;
7488 int opt_flags = 0;
7489 int vimvaridx = -1;
7490 type_T *type = &t_any;
7491
7492 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007493 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007494 name = vim_strnsave(arg, varlen);
7495 if (name == NULL)
7496 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007497
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007498 // TODO: script var not supported?
7499 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7500 &vimvaridx, &type, cctx) == FAIL)
7501 goto failed;
7502 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007503 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007504 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7505 0, 0, type, name) == FAIL)
7506 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007507 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007508 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007509 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007510 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007511 {
7512 semsg(_(e_variable_already_declared), arg);
7513 goto failed;
7514 }
7515
Bram Moolenaarea870692020-12-02 14:24:30 +01007516 if (STRNCMP(name, "s:", 2) == 0)
7517 {
7518 semsg(_(e_cannot_declare_script_variable_in_function), name);
7519 goto failed;
7520 }
7521
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007522 // Reserve a variable to store "var".
7523 // TODO: check for type
7524 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7525 if (var_lvar == NULL)
7526 // out of memory or used as an argument
7527 goto failed;
7528
7529 if (semicolon && idx == var_count - 1)
7530 var_lvar->lv_type = vartype;
7531 else
7532 var_lvar->lv_type = item_type;
7533 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7534 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007535
Bram Moolenaar036d0712021-01-17 20:23:38 +01007536 if (*p == ':')
7537 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007538 if (*p == ',' || *p == ';')
7539 ++p;
7540 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007541 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007542 }
7543
7544 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007545
7546failed:
7547 vim_free(name);
7548 drop_scope(cctx);
7549 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550}
7551
7552/*
7553 * compile "endfor"
7554 */
7555 static char_u *
7556compile_endfor(char_u *arg, cctx_T *cctx)
7557{
7558 garray_T *instr = &cctx->ctx_instr;
7559 scope_T *scope = cctx->ctx_scope;
7560 forscope_T *forscope;
7561 isn_T *isn;
7562
Bram Moolenaarfa984412021-03-25 22:15:28 +01007563 if (misplaced_cmdmod(cctx))
7564 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007566 if (scope == NULL || scope->se_type != FOR_SCOPE)
7567 {
7568 emsg(_(e_for));
7569 return NULL;
7570 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007571 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007573 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574
7575 // At end of ":for" scope jump back to the FOR instruction.
7576 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7577
7578 // Fill in the "end" label in the FOR statement so it can jump here
7579 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7580 isn->isn_arg.forloop.for_end = instr->ga_len;
7581
7582 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007583 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584
7585 // Below the ":for" scope drop the "expr" list from the stack.
7586 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7587 return NULL;
7588
7589 vim_free(scope);
7590
7591 return arg;
7592}
7593
7594/*
7595 * compile "while expr"
7596 *
7597 * Produces instructions:
7598 * top: EVAL expr Push result of "expr"
7599 * JUMP_IF_FALSE end jump if false
7600 * ... body ...
7601 * JUMP top Jump back to repeat
7602 * end:
7603 *
7604 */
7605 static char_u *
7606compile_while(char_u *arg, cctx_T *cctx)
7607{
7608 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609 scope_T *scope;
7610
7611 scope = new_scope(cctx, WHILE_SCOPE);
7612 if (scope == NULL)
7613 return NULL;
7614
Bram Moolenaara91a7132021-03-25 21:12:15 +01007615 // "endwhile" jumps back here, one before when profiling or using cmdmods
7616 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617
7618 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007619 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007621 if (!ends_excmd2(arg, skipwhite(p)))
7622 {
7623 semsg(_(e_trailing_arg), p);
7624 return NULL;
7625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007626
Bram Moolenaar13106602020-10-04 16:06:05 +02007627 if (bool_on_stack(cctx) == FAIL)
7628 return FAIL;
7629
Bram Moolenaara91a7132021-03-25 21:12:15 +01007630 // CMDMOD_REV must come before the jump
7631 generate_undo_cmdmods(cctx);
7632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007634 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635 JUMP_IF_FALSE, cctx) == FAIL)
7636 return FAIL;
7637
7638 return p;
7639}
7640
7641/*
7642 * compile "endwhile"
7643 */
7644 static char_u *
7645compile_endwhile(char_u *arg, cctx_T *cctx)
7646{
7647 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007648 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649
Bram Moolenaarfa984412021-03-25 22:15:28 +01007650 if (misplaced_cmdmod(cctx))
7651 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007652 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7653 {
7654 emsg(_(e_while));
7655 return NULL;
7656 }
7657 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007658 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659
Bram Moolenaarf002a412021-01-24 13:34:18 +01007660#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007661 // count the endwhile before jumping
7662 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007663#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007665 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007666 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
7668 // Fill in the "end" label in the WHILE statement so it can jump here.
7669 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007670 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7671 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672
7673 vim_free(scope);
7674
7675 return arg;
7676}
7677
7678/*
7679 * compile "continue"
7680 */
7681 static char_u *
7682compile_continue(char_u *arg, cctx_T *cctx)
7683{
7684 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007685 int try_scopes = 0;
7686 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687
7688 for (;;)
7689 {
7690 if (scope == NULL)
7691 {
7692 emsg(_(e_continue));
7693 return NULL;
7694 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007695 if (scope->se_type == FOR_SCOPE)
7696 {
7697 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007698 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007699 }
7700 if (scope->se_type == WHILE_SCOPE)
7701 {
7702 loop_label = scope->se_u.se_while.ws_top_label;
7703 break;
7704 }
7705 if (scope->se_type == TRY_SCOPE)
7706 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007707 scope = scope->se_outer;
7708 }
7709
Bram Moolenaarc150c092021-02-13 15:02:46 +01007710 if (try_scopes > 0)
7711 // Inside one or more try/catch blocks we first need to jump to the
7712 // "finally" or "endtry" to cleanup.
7713 generate_TRYCONT(cctx, try_scopes, loop_label);
7714 else
7715 // Jump back to the FOR or WHILE instruction.
7716 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007718 return arg;
7719}
7720
7721/*
7722 * compile "break"
7723 */
7724 static char_u *
7725compile_break(char_u *arg, cctx_T *cctx)
7726{
7727 scope_T *scope = cctx->ctx_scope;
7728 endlabel_T **el;
7729
7730 for (;;)
7731 {
7732 if (scope == NULL)
7733 {
7734 emsg(_(e_break));
7735 return NULL;
7736 }
7737 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7738 break;
7739 scope = scope->se_outer;
7740 }
7741
7742 // Jump to the end of the FOR or WHILE loop.
7743 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007744 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007746 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7748 return FAIL;
7749
7750 return arg;
7751}
7752
7753/*
7754 * compile "{" start of block
7755 */
7756 static char_u *
7757compile_block(char_u *arg, cctx_T *cctx)
7758{
7759 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7760 return NULL;
7761 return skipwhite(arg + 1);
7762}
7763
7764/*
7765 * compile end of block: drop one scope
7766 */
7767 static void
7768compile_endblock(cctx_T *cctx)
7769{
7770 scope_T *scope = cctx->ctx_scope;
7771
7772 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007773 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007774 vim_free(scope);
7775}
7776
7777/*
7778 * compile "try"
7779 * Creates a new scope for the try-endtry, pointing to the first catch and
7780 * finally.
7781 * Creates another scope for the "try" block itself.
7782 * TRY instruction sets up exception handling at runtime.
7783 *
7784 * "try"
7785 * TRY -> catch1, -> finally push trystack entry
7786 * ... try block
7787 * "throw {exception}"
7788 * EVAL {exception}
7789 * THROW create exception
7790 * ... try block
7791 * " catch {expr}"
7792 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007793 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007794 * EVAL {expr}
7795 * MATCH
7796 * JUMP nomatch -> catch2
7797 * CATCH remove exception
7798 * ... catch block
7799 * " catch"
7800 * JUMP -> finally
7801 * catch2: CATCH remove exception
7802 * ... catch block
7803 * " finally"
7804 * finally:
7805 * ... finally block
7806 * " endtry"
7807 * ENDTRY pop trystack entry, may rethrow
7808 */
7809 static char_u *
7810compile_try(char_u *arg, cctx_T *cctx)
7811{
7812 garray_T *instr = &cctx->ctx_instr;
7813 scope_T *try_scope;
7814 scope_T *scope;
7815
Bram Moolenaarfa984412021-03-25 22:15:28 +01007816 if (misplaced_cmdmod(cctx))
7817 return NULL;
7818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 // scope that holds the jumps that go to catch/finally/endtry
7820 try_scope = new_scope(cctx, TRY_SCOPE);
7821 if (try_scope == NULL)
7822 return NULL;
7823
Bram Moolenaar69f70502021-01-01 16:10:46 +01007824 if (cctx->ctx_skip != SKIP_YES)
7825 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007826 isn_T *isn;
7827
7828 // "try_catch" is set when the first ":catch" is found or when no catch
7829 // is found and ":finally" is found.
7830 // "try_finally" is set when ":finally" is found
7831 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007832 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007833 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7834 return NULL;
7835 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7836 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007837 return NULL;
7838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007839
7840 // scope for the try block itself
7841 scope = new_scope(cctx, BLOCK_SCOPE);
7842 if (scope == NULL)
7843 return NULL;
7844
7845 return arg;
7846}
7847
7848/*
7849 * compile "catch {expr}"
7850 */
7851 static char_u *
7852compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7853{
7854 scope_T *scope = cctx->ctx_scope;
7855 garray_T *instr = &cctx->ctx_instr;
7856 char_u *p;
7857 isn_T *isn;
7858
Bram Moolenaarfa984412021-03-25 22:15:28 +01007859 if (misplaced_cmdmod(cctx))
7860 return NULL;
7861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 // end block scope from :try or :catch
7863 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7864 compile_endblock(cctx);
7865 scope = cctx->ctx_scope;
7866
7867 // Error if not in a :try scope
7868 if (scope == NULL || scope->se_type != TRY_SCOPE)
7869 {
7870 emsg(_(e_catch));
7871 return NULL;
7872 }
7873
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007874 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007875 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007876 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 return NULL;
7878 }
7879
Bram Moolenaar69f70502021-01-01 16:10:46 +01007880 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007882#ifdef FEAT_PROFILE
7883 // the profile-start should be after the jump
7884 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7885 .isn_type == ISN_PROF_START)
7886 --instr->ga_len;
7887#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007888 // Jump from end of previous block to :finally or :endtry
7889 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7890 JUMP_ALWAYS, cctx) == FAIL)
7891 return NULL;
7892
7893 // End :try or :catch scope: set value in ISN_TRY instruction
7894 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007895 if (isn->isn_arg.try.try_ref->try_catch == 0)
7896 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007897 if (scope->se_u.se_try.ts_catch_label != 0)
7898 {
7899 // Previous catch without match jumps here
7900 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7901 isn->isn_arg.jump.jump_where = instr->ga_len;
7902 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007903#ifdef FEAT_PROFILE
7904 if (cctx->ctx_profiling)
7905 {
7906 // a "throw" that jumps here needs to be counted
7907 generate_instr(cctx, ISN_PROF_END);
7908 // the "catch" is also counted
7909 generate_instr(cctx, ISN_PROF_START);
7910 }
7911#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007912 }
7913
7914 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007915 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007916 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007917 scope->se_u.se_try.ts_caught_all = TRUE;
7918 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 }
7920 else
7921 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007922 char_u *end;
7923 char_u *pat;
7924 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007925 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007926 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 // Push v:exception, push {expr} and MATCH
7929 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7930
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007931 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007932 if (*end != *p)
7933 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007934 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007935 vim_free(tofree);
7936 return FAIL;
7937 }
7938 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007939 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007940 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007941 len = (int)(end - tofree);
7942 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007943 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007944 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007945 if (pat == NULL)
7946 return FAIL;
7947 if (generate_PUSHS(cctx, pat) == FAIL)
7948 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7951 return NULL;
7952
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007953 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007954 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7955 return NULL;
7956 }
7957
Bram Moolenaar69f70502021-01-01 16:10:46 +01007958 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007959 return NULL;
7960
7961 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7962 return NULL;
7963 return p;
7964}
7965
7966 static char_u *
7967compile_finally(char_u *arg, cctx_T *cctx)
7968{
7969 scope_T *scope = cctx->ctx_scope;
7970 garray_T *instr = &cctx->ctx_instr;
7971 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007972 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007973
Bram Moolenaarfa984412021-03-25 22:15:28 +01007974 if (misplaced_cmdmod(cctx))
7975 return NULL;
7976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977 // end block scope from :try or :catch
7978 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7979 compile_endblock(cctx);
7980 scope = cctx->ctx_scope;
7981
7982 // Error if not in a :try scope
7983 if (scope == NULL || scope->se_type != TRY_SCOPE)
7984 {
7985 emsg(_(e_finally));
7986 return NULL;
7987 }
7988
7989 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007990 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007991 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007992 {
7993 emsg(_(e_finally_dup));
7994 return NULL;
7995 }
7996
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007997 this_instr = instr->ga_len;
7998#ifdef FEAT_PROFILE
7999 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8000 .isn_type == ISN_PROF_START)
8001 // jump to the profile start of the "finally"
8002 --this_instr;
8003#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008005 // Fill in the "end" label in jumps at the end of the blocks.
8006 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8007 this_instr, cctx);
8008
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008009 // If there is no :catch then an exception jumps to :finally.
8010 if (isn->isn_arg.try.try_ref->try_catch == 0)
8011 isn->isn_arg.try.try_ref->try_catch = this_instr;
8012 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008013 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 {
8015 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008016 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008017 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008018 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008019 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008020 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8021 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 // TODO: set index in ts_finally_label jumps
8024
8025 return arg;
8026}
8027
8028 static char_u *
8029compile_endtry(char_u *arg, cctx_T *cctx)
8030{
8031 scope_T *scope = cctx->ctx_scope;
8032 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008033 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008034
Bram Moolenaarfa984412021-03-25 22:15:28 +01008035 if (misplaced_cmdmod(cctx))
8036 return NULL;
8037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038 // end block scope from :catch or :finally
8039 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8040 compile_endblock(cctx);
8041 scope = cctx->ctx_scope;
8042
8043 // Error if not in a :try scope
8044 if (scope == NULL || scope->se_type != TRY_SCOPE)
8045 {
8046 if (scope == NULL)
8047 emsg(_(e_no_endtry));
8048 else if (scope->se_type == WHILE_SCOPE)
8049 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008050 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051 emsg(_(e_endfor));
8052 else
8053 emsg(_(e_endif));
8054 return NULL;
8055 }
8056
Bram Moolenaarc150c092021-02-13 15:02:46 +01008057 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008058 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008060 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8061 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008062 {
8063 emsg(_(e_missing_catch_or_finally));
8064 return NULL;
8065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008067#ifdef FEAT_PROFILE
8068 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8069 .isn_type == ISN_PROF_START)
8070 // move the profile start after "endtry" so that it's not counted when
8071 // the exception is rethrown.
8072 --instr->ga_len;
8073#endif
8074
Bram Moolenaar69f70502021-01-01 16:10:46 +01008075 // Fill in the "end" label in jumps at the end of the blocks, if not
8076 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008077 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8078 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079
Bram Moolenaar69f70502021-01-01 16:10:46 +01008080 if (scope->se_u.se_try.ts_catch_label != 0)
8081 {
8082 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008083 isn_T *isn = ((isn_T *)instr->ga_data)
8084 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008085 isn->isn_arg.jump.jump_where = instr->ga_len;
8086 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008087 }
8088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 compile_endblock(cctx);
8090
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008091 if (cctx->ctx_skip != SKIP_YES)
8092 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008093 // End :catch or :finally scope: set instruction index in ISN_TRY
8094 // instruction
8095 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008096 if (cctx->ctx_skip != SKIP_YES
8097 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8098 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008099#ifdef FEAT_PROFILE
8100 if (cctx->ctx_profiling)
8101 generate_instr(cctx, ISN_PROF_START);
8102#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008104 return arg;
8105}
8106
8107/*
8108 * compile "throw {expr}"
8109 */
8110 static char_u *
8111compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8112{
8113 char_u *p = skipwhite(arg);
8114
Bram Moolenaara5565e42020-05-09 15:44:01 +02008115 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008116 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008117 if (cctx->ctx_skip == SKIP_YES)
8118 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008119 if (may_generate_2STRING(-1, cctx) == FAIL)
8120 return NULL;
8121 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8122 return NULL;
8123
8124 return p;
8125}
8126
8127/*
8128 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008129 * compile "echomsg expr"
8130 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008131 * compile "execute expr"
8132 */
8133 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008134compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008135{
8136 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008137 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008138 int count = 0;
8139
8140 for (;;)
8141 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008142 if (ends_excmd2(prev, p))
8143 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008144 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008145 return NULL;
8146 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008147 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008148 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008149 }
8150
Bram Moolenaare4984292020-12-13 14:19:25 +01008151 if (count > 0)
8152 {
8153 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8154 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8155 else if (cmdidx == CMD_execute)
8156 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8157 else if (cmdidx == CMD_echomsg)
8158 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8159 else
8160 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008162 return p;
8163}
8164
8165/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008166 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008167 * instruction to compute it and return OK.
8168 * Otherwise return FAIL, the caller must deal with any range.
8169 */
8170 static int
8171compile_variable_range(exarg_T *eap, cctx_T *cctx)
8172{
8173 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8174 char_u *p = skipdigits(eap->cmd);
8175
8176 if (p == range_end)
8177 return FAIL;
8178 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8179}
8180
8181/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008182 * :put r
8183 * :put ={expr}
8184 */
8185 static char_u *
8186compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8187{
8188 char_u *line = arg;
8189 linenr_T lnum;
8190 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008191 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008192
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008193 eap->regname = *line;
8194
8195 if (eap->regname == '=')
8196 {
8197 char_u *p = line + 1;
8198
8199 if (compile_expr0(&p, cctx) == FAIL)
8200 return NULL;
8201 line = p;
8202 }
8203 else if (eap->regname != NUL)
8204 ++line;
8205
Bram Moolenaar08597872020-12-10 19:43:40 +01008206 if (compile_variable_range(eap, cctx) == OK)
8207 {
8208 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8209 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008210 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008211 {
8212 // Either no range or a number.
8213 // "errormsg" will not be set because the range is ADDR_LINES.
8214 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008215 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008216 return NULL;
8217 if (eap->addr_count == 0)
8218 lnum = -1;
8219 else
8220 lnum = eap->line2;
8221 if (above)
8222 --lnum;
8223 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008224
8225 generate_PUT(cctx, eap->regname, lnum);
8226 return line;
8227}
8228
8229/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008230 * A command that is not compiled, execute with legacy code.
8231 */
8232 static char_u *
8233compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8234{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008235 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008236 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008237 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008238
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008239 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008240 goto theend;
8241
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008242 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008243 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008244 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008245 int usefilter = FALSE;
8246
8247 has_expr = argt & (EX_XFILE | EX_EXPAND);
8248
8249 // If the command can be followed by a bar, find the bar and truncate
8250 // it, so that the following command can be compiled.
8251 // The '|' is overwritten with a NUL, it is put back below.
8252 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8253 && *eap->arg == '!')
8254 // :w !filter or :r !filter or :r! filter
8255 usefilter = TRUE;
8256 if ((argt & EX_TRLBAR) && !usefilter)
8257 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008258 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008259 separate_nextcmd(eap);
8260 if (eap->nextcmd != NULL)
8261 nextcmd = eap->nextcmd;
8262 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008263 else if (eap->cmdidx == CMD_wincmd)
8264 {
8265 p = eap->arg;
8266 if (*p != NUL)
8267 ++p;
8268 if (*p == 'g' || *p == Ctrl_G)
8269 ++p;
8270 p = skipwhite(p);
8271 if (*p == '|')
8272 {
8273 *p = NUL;
8274 nextcmd = p + 1;
8275 }
8276 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008277 }
8278
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008279 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8280 {
8281 // expand filename in "syntax include [@group] filename"
8282 has_expr = TRUE;
8283 eap->arg = skipwhite(eap->arg + 7);
8284 if (*eap->arg == '@')
8285 eap->arg = skiptowhite(eap->arg);
8286 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008287
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008288 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8289 && STRLEN(eap->arg) > 4)
8290 {
8291 int delim = *eap->arg;
8292
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008293 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008294 if (*p == delim)
8295 {
8296 eap->arg = p + 1;
8297 has_expr = TRUE;
8298 }
8299 }
8300
Bram Moolenaarecac5912021-01-05 19:23:28 +01008301 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8302 {
8303 // TODO: should only expand when appropriate for the command
8304 eap->arg = skiptowhite(eap->arg);
8305 has_expr = TRUE;
8306 }
8307
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008308 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008309 {
8310 int count = 0;
8311 char_u *start = skipwhite(line);
8312
8313 // :cmd xxx`=expr1`yyy`=expr2`zzz
8314 // PUSHS ":cmd xxx"
8315 // eval expr1
8316 // PUSHS "yyy"
8317 // eval expr2
8318 // PUSHS "zzz"
8319 // EXECCONCAT 5
8320 for (;;)
8321 {
8322 if (p > start)
8323 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008324 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008325 ++count;
8326 }
8327 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008328 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008329 return NULL;
8330 may_generate_2STRING(-1, cctx);
8331 ++count;
8332 p = skipwhite(p);
8333 if (*p != '`')
8334 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008335 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008336 return NULL;
8337 }
8338 start = p + 1;
8339
8340 p = (char_u *)strstr((char *)start, "`=");
8341 if (p == NULL)
8342 {
8343 if (*skipwhite(start) != NUL)
8344 {
8345 generate_PUSHS(cctx, vim_strsave(start));
8346 ++count;
8347 }
8348 break;
8349 }
8350 }
8351 generate_EXECCONCAT(cctx, count);
8352 }
8353 else
8354 generate_EXEC(cctx, line);
8355
8356theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008357 if (*nextcmd != NUL)
8358 {
8359 // the parser expects a pointer to the bar, put it back
8360 --nextcmd;
8361 *nextcmd = '|';
8362 }
8363
8364 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008365}
8366
8367/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008368 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008369 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008370 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008371 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008372add_def_function(ufunc_T *ufunc)
8373{
8374 dfunc_T *dfunc;
8375
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008376 if (def_functions.ga_len == 0)
8377 {
8378 // The first position is not used, so that a zero uf_dfunc_idx means it
8379 // wasn't set.
8380 if (ga_grow(&def_functions, 1) == FAIL)
8381 return FAIL;
8382 ++def_functions.ga_len;
8383 }
8384
Bram Moolenaar09689a02020-05-09 22:50:08 +02008385 // Add the function to "def_functions".
8386 if (ga_grow(&def_functions, 1) == FAIL)
8387 return FAIL;
8388 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8389 CLEAR_POINTER(dfunc);
8390 dfunc->df_idx = def_functions.ga_len;
8391 ufunc->uf_dfunc_idx = dfunc->df_idx;
8392 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008393 dfunc->df_name = vim_strsave(ufunc->uf_name);
8394 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008395 ++def_functions.ga_len;
8396 return OK;
8397}
8398
8399/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 * After ex_function() has collected all the function lines: parse and compile
8401 * the lines into instructions.
8402 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008403 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8404 * the return statement (used for lambda). When uf_ret_type is already set
8405 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008406 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008407 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008408 * This can be used recursively through compile_lambda(), which may reallocate
8409 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008410 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008412 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008413compile_def_function(
8414 ufunc_T *ufunc,
8415 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008416 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008417 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 char_u *line = NULL;
8420 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008421 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 cctx_T cctx;
8423 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008424 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 int ret = FAIL;
8426 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008427 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008428 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008429 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008430#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008431 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008432#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008434 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008435 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008436 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008438 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8439 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008440 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008442 else
8443 {
8444 if (add_def_function(ufunc) == FAIL)
8445 return FAIL;
8446 new_def_function = TRUE;
8447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448
Bram Moolenaar985116a2020-07-12 17:31:09 +02008449 ufunc->uf_def_status = UF_COMPILING;
8450
Bram Moolenaara80faa82020-04-12 19:37:17 +02008451 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008452
Bram Moolenaarf002a412021-01-24 13:34:18 +01008453#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008454 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008455#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 cctx.ctx_ufunc = ufunc;
8457 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008458 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008459 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8460 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8461 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8462 cctx.ctx_type_list = &ufunc->uf_type_list;
8463 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8464 instr = &cctx.ctx_instr;
8465
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008466 // Set the context to the function, it may be compiled when called from
8467 // another script. Set the script version to the most modern one.
8468 // The line number will be set in next_line_from_context().
8469 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008470 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8471
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008472 // Make sure error messages are OK.
8473 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8474 if (do_estack_push)
8475 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008476 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008477
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008478 if (ufunc->uf_def_args.ga_len > 0)
8479 {
8480 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008481 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008482 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008483 int i;
8484 char_u *arg;
8485 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008486 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008487
8488 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008489 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008490 for (i = 0; i < count; ++i)
8491 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008492 garray_T *stack = &cctx.ctx_type_stack;
8493 type_T *val_type;
8494 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008495 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008496 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008497 int jump_instr_idx = instr->ga_len;
8498 isn_T *isn;
8499
8500 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8501 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8502 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008503
8504 // Make sure later arguments are not found.
8505 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008506
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008507 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008508 r = compile_expr0(&arg, &cctx);
8509
8510 ufunc->uf_args.ga_len = uf_args_len;
8511 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008512 goto erret;
8513
8514 // If no type specified use the type of the default value.
8515 // Otherwise check that the default value type matches the
8516 // specified type.
8517 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008518 where.wt_index = arg_idx + 1;
8519 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008520 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008521 {
8522 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008523 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008524 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008525 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008526 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008527 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008528
8529 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008530 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008531
8532 // set instruction index in JUMP_IF_ARG_SET to here
8533 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8534 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008535 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008536
8537 if (did_set_arg_type)
8538 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008539 }
8540
8541 /*
8542 * Loop over all the lines of the function and generate instructions.
8543 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008544 for (;;)
8545 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008546 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008547 int starts_with_colon = FALSE;
8548 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008549 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008550
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008551 // Bail out on the first error to avoid a flood of errors and report
8552 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008553 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008554 goto erret;
8555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008556 if (line != NULL && *line == '|')
8557 // the line continues after a '|'
8558 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008559 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008560 && !(*line == '#' && (line == cctx.ctx_line_start
8561 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008563 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 goto erret;
8565 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008566 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8567 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 else
8569 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008570 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008571 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008572 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008573 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008574#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008575 if (cctx.ctx_skip != SKIP_YES)
8576 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008577#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008578 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008580 }
8581
Bram Moolenaara80faa82020-04-12 19:37:17 +02008582 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 ea.cmdlinep = &line;
8584 ea.cmd = skipwhite(line);
8585
Bram Moolenaarb2049902021-01-24 12:53:53 +01008586 if (*ea.cmd == '#')
8587 {
8588 // "#" starts a comment
8589 line = (char_u *)"";
8590 continue;
8591 }
8592
Bram Moolenaarf002a412021-01-24 13:34:18 +01008593#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008594 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8595 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008596 {
8597 may_generate_prof_end(&cctx, prof_lnum);
8598
8599 prof_lnum = cctx.ctx_lnum;
8600 generate_instr(&cctx, ISN_PROF_START);
8601 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008602#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008603
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008604 // Some things can be recognized by the first character.
8605 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008607 case '}':
8608 {
8609 // "}" ends a block scope
8610 scopetype_T stype = cctx.ctx_scope == NULL
8611 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008613 if (stype == BLOCK_SCOPE)
8614 {
8615 compile_endblock(&cctx);
8616 line = ea.cmd;
8617 }
8618 else
8619 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008620 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008621 goto erret;
8622 }
8623 if (line != NULL)
8624 line = skipwhite(ea.cmd + 1);
8625 continue;
8626 }
8627
8628 case '{':
8629 // "{" starts a block scope
8630 // "{'a': 1}->func() is something else
8631 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8632 {
8633 line = compile_block(ea.cmd, &cctx);
8634 continue;
8635 }
8636 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637 }
8638
8639 /*
8640 * COMMAND MODIFIERS
8641 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008642 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008643 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8644 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008645 {
8646 if (errormsg != NULL)
8647 goto erret;
8648 // empty line or comment
8649 line = (char_u *)"";
8650 continue;
8651 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008652 generate_cmdmods(&cctx, &local_cmdmod);
8653 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008654
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008655 // Check if there was a colon after the last command modifier or before
8656 // the current position.
8657 for (p = ea.cmd; p >= line; --p)
8658 {
8659 if (*p == ':')
8660 starts_with_colon = TRUE;
8661 if (p < ea.cmd && !VIM_ISWHITE(*p))
8662 break;
8663 }
8664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008665 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008666 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008667 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008668 {
8669 if (*ea.cmd == '(')
8670 // not for "call()"
8671 ea.cmd = p;
8672 else
8673 ea.cmd = skipwhite(ea.cmd);
8674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008675
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008676 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008678 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008679
Bram Moolenaar17126b12021-01-07 22:03:02 +01008680 // Check for assignment after command modifiers.
8681 assign = may_compile_assignment(&ea, &line, &cctx);
8682 if (assign == OK)
8683 goto nextline;
8684 if (assign == FAIL)
8685 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008686 }
8687
8688 /*
8689 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008690 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008692 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008693 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008694 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008695 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008696 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008697 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008698 if (!starts_with_colon)
8699 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008700 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008701 goto erret;
8702 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008703 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008704 if (ends_excmd2(line, ea.cmd))
8705 {
8706 // A range without a command: jump to the line.
8707 // TODO: compile to a more efficient command, possibly
8708 // calling parse_cmd_address().
8709 ea.cmdidx = CMD_SIZE;
8710 line = compile_exec(line, &ea, &cctx);
8711 goto nextline;
8712 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008713 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008714 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008715 p = find_ex_command(&ea, NULL, starts_with_colon
8716 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008717
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008718 if (p == NULL)
8719 {
8720 if (cctx.ctx_skip != SKIP_YES)
8721 emsg(_(e_ambiguous_use_of_user_defined_command));
8722 goto erret;
8723 }
8724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008725 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8726 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008727 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008728 {
8729 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008730 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008731 }
8732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008733 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008734 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008736 // CMD_var cannot happen, compile_assignment() above would be
8737 // used. Most likely an assignment to a non-existing variable.
8738 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008739 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 }
8742
Bram Moolenaar3988f642020-08-27 22:43:03 +02008743 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008744 && ea.cmdidx != CMD_elseif
8745 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008746 && ea.cmdidx != CMD_endif
8747 && ea.cmdidx != CMD_endfor
8748 && ea.cmdidx != CMD_endwhile
8749 && ea.cmdidx != CMD_catch
8750 && ea.cmdidx != CMD_finally
8751 && ea.cmdidx != CMD_endtry)
8752 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008753 emsg(_(e_unreachable_code_after_return));
8754 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008755 }
8756
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008757 p = skipwhite(p);
8758 if (ea.cmdidx != CMD_SIZE
8759 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8760 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008761 if (ea.cmdidx >= 0)
8762 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008763 if ((ea.argt & EX_BANG) && *p == '!')
8764 {
8765 ea.forceit = TRUE;
8766 p = skipwhite(p + 1);
8767 }
8768 }
8769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 switch (ea.cmdidx)
8771 {
8772 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008773 ea.arg = p;
8774 line = compile_nested_function(&ea, &cctx);
8775 break;
8776
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008777 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008778 // TODO: should we allow this, e.g. to declare a global
8779 // function?
8780 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008781 goto erret;
8782
8783 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008784 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008785 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008786 break;
8787
8788 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008789 emsg(_(e_cannot_use_let_in_vim9_script));
8790 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008791 case CMD_var:
8792 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793 case CMD_const:
8794 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008795 if (line == p)
8796 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008797 break;
8798
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008799 case CMD_unlet:
8800 case CMD_unlockvar:
8801 case CMD_lockvar:
8802 line = compile_unletlock(p, &ea, &cctx);
8803 break;
8804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 case CMD_import:
8806 line = compile_import(p, &cctx);
8807 break;
8808
8809 case CMD_if:
8810 line = compile_if(p, &cctx);
8811 break;
8812 case CMD_elseif:
8813 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008814 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 break;
8816 case CMD_else:
8817 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008818 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819 break;
8820 case CMD_endif:
8821 line = compile_endif(p, &cctx);
8822 break;
8823
8824 case CMD_while:
8825 line = compile_while(p, &cctx);
8826 break;
8827 case CMD_endwhile:
8828 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008829 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 break;
8831
8832 case CMD_for:
8833 line = compile_for(p, &cctx);
8834 break;
8835 case CMD_endfor:
8836 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008837 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 break;
8839 case CMD_continue:
8840 line = compile_continue(p, &cctx);
8841 break;
8842 case CMD_break:
8843 line = compile_break(p, &cctx);
8844 break;
8845
8846 case CMD_try:
8847 line = compile_try(p, &cctx);
8848 break;
8849 case CMD_catch:
8850 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008851 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008852 break;
8853 case CMD_finally:
8854 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008855 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008856 break;
8857 case CMD_endtry:
8858 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008859 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008860 break;
8861 case CMD_throw:
8862 line = compile_throw(p, &cctx);
8863 break;
8864
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008865 case CMD_eval:
8866 if (compile_expr0(&p, &cctx) == FAIL)
8867 goto erret;
8868
Bram Moolenaar3988f642020-08-27 22:43:03 +02008869 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008870 generate_instr_drop(&cctx, ISN_DROP, 1);
8871
8872 line = skipwhite(p);
8873 break;
8874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008876 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008877 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008878 case CMD_echomsg:
8879 case CMD_echoerr:
8880 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008881 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008882
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008883 case CMD_put:
8884 ea.cmd = cmd;
8885 line = compile_put(p, &ea, &cctx);
8886 break;
8887
Bram Moolenaar3988f642020-08-27 22:43:03 +02008888 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008889
Bram Moolenaarae616492020-07-28 20:07:27 +02008890 case CMD_append:
8891 case CMD_change:
8892 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008893 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008894 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008895 case CMD_xit:
8896 not_in_vim9(&ea);
8897 goto erret;
8898
Bram Moolenaar002262f2020-07-08 17:47:57 +02008899 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008900 if (cctx.ctx_skip != SKIP_YES)
8901 {
8902 semsg(_(e_invalid_command_str), ea.cmd);
8903 goto erret;
8904 }
8905 // We don't check for a next command here.
8906 line = (char_u *)"";
8907 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008909 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008910 if (cctx.ctx_skip == SKIP_YES)
8911 {
8912 // We don't check for a next command here.
8913 line = (char_u *)"";
8914 }
8915 else
8916 {
8917 // Not recognized, execute with do_cmdline_cmd().
8918 ea.arg = p;
8919 line = compile_exec(line, &ea, &cctx);
8920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008921 break;
8922 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008923nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008924 if (line == NULL)
8925 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008926 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008927
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008928 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008929 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008930
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008931 if (cctx.ctx_type_stack.ga_len < 0)
8932 {
8933 iemsg("Type stack underflow");
8934 goto erret;
8935 }
8936 }
8937
8938 if (cctx.ctx_scope != NULL)
8939 {
8940 if (cctx.ctx_scope->se_type == IF_SCOPE)
8941 emsg(_(e_endif));
8942 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8943 emsg(_(e_endwhile));
8944 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8945 emsg(_(e_endfor));
8946 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008947 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008948 goto erret;
8949 }
8950
Bram Moolenaarefd88552020-06-18 20:50:10 +02008951 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 {
8953 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8954 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008955 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 goto erret;
8957 }
8958
8959 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008960 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008961 }
8962
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008963 {
8964 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8965 + ufunc->uf_dfunc_idx;
8966 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008967 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008968#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008969 if (cctx.ctx_profiling)
8970 {
8971 dfunc->df_instr_prof = instr->ga_data;
8972 dfunc->df_instr_prof_count = instr->ga_len;
8973 }
8974 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008975#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008976 {
8977 dfunc->df_instr = instr->ga_data;
8978 dfunc->df_instr_count = instr->ga_len;
8979 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008980 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008981 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008982 if (cctx.ctx_outer_used)
8983 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008984 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008986
8987 ret = OK;
8988
8989erret:
8990 if (ret == FAIL)
8991 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008992 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008993 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8994 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008995
8996 for (idx = 0; idx < instr->ga_len; ++idx)
8997 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008998 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008999 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009000
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009001 // If using the last entry in the table and it was added above, we
9002 // might as well remove it.
9003 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009004 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009005 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009006 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009007 ufunc->uf_dfunc_idx = 0;
9008 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009009 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009010
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009011 while (cctx.ctx_scope != NULL)
9012 drop_scope(&cctx);
9013
Bram Moolenaar20431c92020-03-20 18:39:46 +01009014 // Don't execute this function body.
9015 ga_clear_strings(&ufunc->uf_lines);
9016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009017 if (errormsg != NULL)
9018 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009019 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009020 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009021 }
9022
9023 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009024 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009025 if (do_estack_push)
9026 estack_pop();
9027
Bram Moolenaar20431c92020-03-20 18:39:46 +01009028 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009029 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009030 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009031 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009032}
9033
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009034 void
9035set_function_type(ufunc_T *ufunc)
9036{
9037 int varargs = ufunc->uf_va_name != NULL;
9038 int argcount = ufunc->uf_args.ga_len;
9039
9040 // Create a type for the function, with the return type and any
9041 // argument types.
9042 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9043 // The type is included in "tt_args".
9044 if (argcount > 0 || varargs)
9045 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009046 if (ufunc->uf_type_list.ga_itemsize == 0)
9047 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009048 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9049 argcount, &ufunc->uf_type_list);
9050 // Add argument types to the function type.
9051 if (func_type_add_arg_types(ufunc->uf_func_type,
9052 argcount + varargs,
9053 &ufunc->uf_type_list) == FAIL)
9054 return;
9055 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9056 ufunc->uf_func_type->tt_min_argcount =
9057 argcount - ufunc->uf_def_args.ga_len;
9058 if (ufunc->uf_arg_types == NULL)
9059 {
9060 int i;
9061
9062 // lambda does not have argument types.
9063 for (i = 0; i < argcount; ++i)
9064 ufunc->uf_func_type->tt_args[i] = &t_any;
9065 }
9066 else
9067 mch_memmove(ufunc->uf_func_type->tt_args,
9068 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9069 if (varargs)
9070 {
9071 ufunc->uf_func_type->tt_args[argcount] =
9072 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
9073 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9074 }
9075 }
9076 else
9077 // No arguments, can use a predefined type.
9078 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9079 argcount, &ufunc->uf_type_list);
9080}
9081
9082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009083/*
9084 * Delete an instruction, free what it contains.
9085 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009086 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009087delete_instr(isn_T *isn)
9088{
9089 switch (isn->isn_type)
9090 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009091 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009092 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009093 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009094 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009095 case ISN_LOADENV:
9096 case ISN_LOADG:
9097 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009098 case ISN_LOADT:
9099 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009100 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009101 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009102 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009103 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009104 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009105 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009106 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009108 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009109 case ISN_STOREW:
9110 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009111 vim_free(isn->isn_arg.string);
9112 break;
9113
9114 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009115 case ISN_STORES:
9116 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009117 break;
9118
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009119 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009120 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009121 vim_free(isn->isn_arg.unlet.ul_name);
9122 break;
9123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009124 case ISN_STOREOPT:
9125 vim_free(isn->isn_arg.storeopt.so_name);
9126 break;
9127
9128 case ISN_PUSHBLOB: // push blob isn_arg.blob
9129 blob_unref(isn->isn_arg.blob);
9130 break;
9131
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009132 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009133#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009134 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009135#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009136 break;
9137
9138 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009140 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009141#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009142 break;
9143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009144 case ISN_UCALL:
9145 vim_free(isn->isn_arg.ufunc.cuf_name);
9146 break;
9147
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009148 case ISN_FUNCREF:
9149 {
9150 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9151 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009152 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009153
Bram Moolenaar077a4232020-12-22 18:33:27 +01009154 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9155 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009156 }
9157 break;
9158
9159 case ISN_DCALL:
9160 {
9161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9162 + isn->isn_arg.dfunc.cdf_idx;
9163
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009164 if (dfunc->df_ufunc != NULL
9165 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009166 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009167 }
9168 break;
9169
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009170 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009171 {
9172 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9173 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9174
9175 if (ufunc != NULL)
9176 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009177 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009178 func_ptr_unref(ufunc);
9179 }
9180
9181 vim_free(lambda);
9182 vim_free(isn->isn_arg.newfunc.nf_global);
9183 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009184 break;
9185
Bram Moolenaar5e654232020-09-16 15:22:00 +02009186 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009187 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009188 free_type(isn->isn_arg.type.ct_type);
9189 break;
9190
Bram Moolenaar02194d22020-10-24 23:08:38 +02009191 case ISN_CMDMOD:
9192 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9193 ->cmod_filter_regmatch.regprog);
9194 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9195 break;
9196
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009197 case ISN_LOADSCRIPT:
9198 case ISN_STORESCRIPT:
9199 vim_free(isn->isn_arg.script.scriptref);
9200 break;
9201
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009202 case ISN_TRY:
9203 vim_free(isn->isn_arg.try.try_ref);
9204 break;
9205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009206 case ISN_2BOOL:
9207 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009208 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 case ISN_ADDBLOB:
9210 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009211 case ISN_ANYINDEX:
9212 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009213 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009214 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009215 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009216 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009217 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009218 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009219 case ISN_COMPAREANY:
9220 case ISN_COMPAREBLOB:
9221 case ISN_COMPAREBOOL:
9222 case ISN_COMPAREDICT:
9223 case ISN_COMPAREFLOAT:
9224 case ISN_COMPAREFUNC:
9225 case ISN_COMPARELIST:
9226 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009227 case ISN_COMPARESPECIAL:
9228 case ISN_COMPARESTRING:
9229 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009230 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009231 case ISN_DROP:
9232 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009233 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009234 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009236 case ISN_EXECCONCAT:
9237 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009238 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009239 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009240 case ISN_GETITEM:
9241 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009242 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009243 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009244 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009245 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009247 case ISN_LOADBDICT:
9248 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009249 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009250 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009251 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009253 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009254 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009255 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009256 case ISN_NEGATENR:
9257 case ISN_NEWDICT:
9258 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009259 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009260 case ISN_OPFLOAT:
9261 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009262 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009263 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009264 case ISN_PROF_END:
9265 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009266 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267 case ISN_PUSHF:
9268 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009270 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009272 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009273 case ISN_SHUFFLE:
9274 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009276 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009277 case ISN_STORENR:
9278 case ISN_STOREOUTER:
9279 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009280 case ISN_STOREV:
9281 case ISN_STRINDEX:
9282 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009283 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009284 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009285 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009286 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009287 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 // nothing allocated
9289 break;
9290 }
9291}
9292
9293/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009294 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009295 */
9296 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009297delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009298{
9299 int idx;
9300
9301 ga_clear(&dfunc->df_def_args_isn);
9302
9303 if (dfunc->df_instr != NULL)
9304 {
9305 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9306 delete_instr(dfunc->df_instr + idx);
9307 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009308 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009309 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009310#ifdef FEAT_PROFILE
9311 if (dfunc->df_instr_prof != NULL)
9312 {
9313 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9314 delete_instr(dfunc->df_instr_prof + idx);
9315 VIM_CLEAR(dfunc->df_instr_prof);
9316 dfunc->df_instr_prof = NULL;
9317 }
9318#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009319
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009320 if (mark_deleted)
9321 dfunc->df_deleted = TRUE;
9322 if (dfunc->df_ufunc != NULL)
9323 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009324}
9325
9326/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009327 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009328 * function, unless another user function still uses it.
9329 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009330 */
9331 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009332unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009333{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009334 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009335 {
9336 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9337 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009338
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009339 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009340 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009341 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009342 ufunc->uf_dfunc_idx = 0;
9343 if (dfunc->df_ufunc == ufunc)
9344 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009345 }
9346}
9347
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009348/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009349 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009350 */
9351 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009352link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009353{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009354 if (ufunc->uf_dfunc_idx > 0)
9355 {
9356 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9357 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009358
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009359 ++dfunc->df_refcount;
9360 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009361}
9362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009363#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009364/*
9365 * Free all functions defined with ":def".
9366 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009367 void
9368free_def_functions(void)
9369{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009370 int idx;
9371
9372 for (idx = 0; idx < def_functions.ga_len; ++idx)
9373 {
9374 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9375
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009376 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009377 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009378 }
9379
9380 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009381}
9382#endif
9383
9384
9385#endif // FEAT_EVAL