blob: 2cfc12798da7b9e298cbed59a4e5bca971052fdd [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 Moolenaar2a389082021-04-09 20:24:31 +02001859 else if (ufunc->uf_va_type == NULL
1860 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001861 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001862 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001863 else
1864 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001865 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001866 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001867 {
1868 arg_type_mismatch(expected, actual, i + 1);
1869 return FAIL;
1870 }
1871 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001872 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001873 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001874 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001875 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001876 }
1877
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001879 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001880 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001882 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 {
1884 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1885 isn->isn_arg.dfunc.cdf_argcount = argcount;
1886 }
1887 else
1888 {
1889 // A user function may be deleted and redefined later, can't use the
1890 // ufunc pointer, need to look it up again at runtime.
1891 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1892 isn->isn_arg.ufunc.cuf_argcount = argcount;
1893 }
1894
1895 stack->ga_len -= argcount; // drop the arguments
1896 if (ga_grow(stack, 1) == FAIL)
1897 return FAIL;
1898 // add return value
1899 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1900 ++stack->ga_len;
1901
1902 return OK;
1903}
1904
1905/*
1906 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1907 */
1908 static int
1909generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1910{
1911 isn_T *isn;
1912 garray_T *stack = &cctx->ctx_type_stack;
1913
Bram Moolenaar080457c2020-03-03 21:53:32 +01001914 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1916 return FAIL;
1917 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1918 isn->isn_arg.ufunc.cuf_argcount = argcount;
1919
1920 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001921 if (ga_grow(stack, 1) == FAIL)
1922 return FAIL;
1923 // add return value
1924 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1925 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926
1927 return OK;
1928}
1929
1930/*
1931 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001932 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 */
1934 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001935generate_PCALL(
1936 cctx_T *cctx,
1937 int argcount,
1938 char_u *name,
1939 type_T *type,
1940 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941{
1942 isn_T *isn;
1943 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001944 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945
Bram Moolenaar080457c2020-03-03 21:53:32 +01001946 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001947
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001948 if (type->tt_type == VAR_ANY)
1949 ret_type = &t_any;
1950 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001951 {
1952 if (type->tt_argcount != -1)
1953 {
1954 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1955
1956 if (argcount < type->tt_min_argcount - varargs)
1957 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001958 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001959 return FAIL;
1960 }
1961 if (!varargs && argcount > type->tt_argcount)
1962 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001963 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001964 return FAIL;
1965 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001966 if (type->tt_args != NULL)
1967 {
1968 int i;
1969
1970 for (i = 0; i < argcount; ++i)
1971 {
1972 int offset = -argcount + i - 1;
1973 type_T *actual = ((type_T **)stack->ga_data)[
1974 stack->ga_len + offset];
1975 type_T *expected;
1976
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001977 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001978 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001979 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001980 else if (i >= type->tt_min_argcount
1981 && actual == &t_special)
1982 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001983 else
1984 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001985 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001986 cctx, TRUE, FALSE) == FAIL)
1987 {
1988 arg_type_mismatch(expected, actual, i + 1);
1989 return FAIL;
1990 }
1991 }
1992 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001993 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001994 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001995 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001996 else
1997 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001998 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001999 return FAIL;
2000 }
2001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2003 return FAIL;
2004 isn->isn_arg.pfunc.cpf_top = at_top;
2005 isn->isn_arg.pfunc.cpf_argcount = argcount;
2006
2007 stack->ga_len -= argcount; // drop the arguments
2008
2009 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002010 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002012 // If partial is above the arguments it must be cleared and replaced with
2013 // the return value.
2014 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2015 return FAIL;
2016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 return OK;
2018}
2019
2020/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002021 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 */
2023 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002024generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025{
2026 isn_T *isn;
2027 garray_T *stack = &cctx->ctx_type_stack;
2028 type_T *type;
2029
Bram Moolenaar080457c2020-03-03 21:53:32 +01002030 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002031 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002033 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002035 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002037 if (type->tt_type != VAR_DICT && type != &t_any)
2038 {
2039 emsg(_(e_dictreq));
2040 return FAIL;
2041 }
2042 // change dict type to dict member type
2043 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002044 {
2045 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2046 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2047 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048
2049 return OK;
2050}
2051
2052/*
2053 * Generate an ISN_ECHO instruction.
2054 */
2055 static int
2056generate_ECHO(cctx_T *cctx, int with_white, int count)
2057{
2058 isn_T *isn;
2059
Bram Moolenaar080457c2020-03-03 21:53:32 +01002060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2062 return FAIL;
2063 isn->isn_arg.echo.echo_with_white = with_white;
2064 isn->isn_arg.echo.echo_count = count;
2065
2066 return OK;
2067}
2068
Bram Moolenaarad39c092020-02-26 18:23:43 +01002069/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002070 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002071 */
2072 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002073generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002074{
2075 isn_T *isn;
2076
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002077 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002078 return FAIL;
2079 isn->isn_arg.number = count;
2080
2081 return OK;
2082}
2083
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002084/*
2085 * Generate an ISN_PUT instruction.
2086 */
2087 static int
2088generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2089{
2090 isn_T *isn;
2091
2092 RETURN_OK_IF_SKIP(cctx);
2093 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2094 return FAIL;
2095 isn->isn_arg.put.put_regname = regname;
2096 isn->isn_arg.put.put_lnum = lnum;
2097 return OK;
2098}
2099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 static int
2101generate_EXEC(cctx_T *cctx, char_u *line)
2102{
2103 isn_T *isn;
2104
Bram Moolenaar080457c2020-03-03 21:53:32 +01002105 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2107 return FAIL;
2108 isn->isn_arg.string = vim_strsave(line);
2109 return OK;
2110}
2111
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002112 static int
2113generate_EXECCONCAT(cctx_T *cctx, int count)
2114{
2115 isn_T *isn;
2116
2117 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2118 return FAIL;
2119 isn->isn_arg.number = count;
2120 return OK;
2121}
2122
Bram Moolenaar08597872020-12-10 19:43:40 +01002123/*
2124 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2125 */
2126 static int
2127generate_RANGE(cctx_T *cctx, char_u *range)
2128{
2129 isn_T *isn;
2130 garray_T *stack = &cctx->ctx_type_stack;
2131
2132 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2133 return FAIL;
2134 isn->isn_arg.string = range;
2135
2136 if (ga_grow(stack, 1) == FAIL)
2137 return FAIL;
2138 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2139 ++stack->ga_len;
2140 return OK;
2141}
2142
Bram Moolenaar792f7862020-11-23 08:31:18 +01002143 static int
2144generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2145{
2146 isn_T *isn;
2147
2148 RETURN_OK_IF_SKIP(cctx);
2149 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2150 return FAIL;
2151 isn->isn_arg.unpack.unp_count = var_count;
2152 isn->isn_arg.unpack.unp_semicolon = semicolon;
2153 return OK;
2154}
2155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002157 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002158 */
2159 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002160generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002161{
2162 isn_T *isn;
2163
Bram Moolenaarfa984412021-03-25 22:15:28 +01002164 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002165 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002166 cctx->ctx_has_cmdmod = TRUE;
2167
2168 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002169 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002170 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2171 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2172 return FAIL;
2173 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002174 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002175 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002176 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002177
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002178 return OK;
2179}
2180
2181 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002182generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002183{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002184 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2185 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002186 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002187 return OK;
2188}
2189
Bram Moolenaarfa984412021-03-25 22:15:28 +01002190 static int
2191misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002192{
2193 garray_T *instr = &cctx->ctx_instr;
2194
Bram Moolenaara91a7132021-03-25 21:12:15 +01002195 if (cctx->ctx_has_cmdmod
2196 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2197 == ISN_CMDMOD)
2198 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002199 emsg(_(e_misplaced_command_modifier));
2200 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002201 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002202 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002203}
2204
2205/*
2206 * Get the index of the current instruction.
2207 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2208 */
2209 static int
2210current_instr_idx(cctx_T *cctx)
2211{
2212 garray_T *instr = &cctx->ctx_instr;
2213 int idx = instr->ga_len;
2214
2215 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2216 .isn_type == ISN_CMDMOD)
2217 --idx;
2218#ifdef FEAT_PROFILE
2219 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2220 .isn_type == ISN_PROF_START)
2221 --idx;
2222#endif
2223 return idx;
2224}
2225
Bram Moolenaarf002a412021-01-24 13:34:18 +01002226#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002227 static void
2228may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2229{
2230 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002231 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002232}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002233#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002234
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002235/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002237 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002239 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002240reserve_local(
2241 cctx_T *cctx,
2242 char_u *name,
2243 size_t len,
2244 int isConst,
2245 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 lvar_T *lvar;
2248
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002249 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002251 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002252 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 }
2254
2255 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002256 return NULL;
2257 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002258 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002260 // Every local variable uses the next entry on the stack. We could re-use
2261 // the last ones when leaving a scope, but then variables used in a closure
2262 // might get overwritten. To keep things simple do not re-use stack
2263 // entries. This is less efficient, but memory is cheap these days.
2264 lvar->lv_idx = cctx->ctx_locals_count++;
2265
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002266 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 lvar->lv_const = isConst;
2268 lvar->lv_type = type;
2269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002270 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271}
2272
2273/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002274 * Remove local variables above "new_top".
2275 */
2276 static void
2277unwind_locals(cctx_T *cctx, int new_top)
2278{
2279 if (cctx->ctx_locals.ga_len > new_top)
2280 {
2281 int idx;
2282 lvar_T *lvar;
2283
2284 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2285 {
2286 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2287 vim_free(lvar->lv_name);
2288 }
2289 }
2290 cctx->ctx_locals.ga_len = new_top;
2291}
2292
2293/*
2294 * Free all local variables.
2295 */
2296 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002297free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002298{
2299 unwind_locals(cctx, 0);
2300 ga_clear(&cctx->ctx_locals);
2301}
2302
2303/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002304 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2305 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2306 * error if the variable was defined with :const.
2307 */
2308 static int
2309check_item_writable(svar_T *sv, int check_writable, char_u *name)
2310{
2311 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2312 || (check_writable == ASSIGN_FINAL
2313 && sv->sv_const == ASSIGN_CONST))
2314 {
2315 semsg(_(e_readonlyvar), name);
2316 return FAIL;
2317 }
2318 return OK;
2319}
2320
2321/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002323 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 * Returns the index in "sn_var_vals" if found.
2325 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002326 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 */
2328 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002329get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330{
2331 hashtab_T *ht;
2332 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002333 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002334 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 int idx;
2336
Bram Moolenaare3d46852020-08-29 13:39:17 +02002337 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002339 if (sid == current_sctx.sc_sid)
2340 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002341 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002342
2343 if (sav == NULL)
2344 return -2;
2345 idx = sav->sav_var_vals_idx;
2346 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002347 if (check_item_writable(sv, check_writable, name) == FAIL)
2348 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002349 return idx;
2350 }
2351
2352 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 ht = &SCRIPT_VARS(sid);
2354 di = find_var_in_ht(ht, 0, name, TRUE);
2355 if (di == NULL)
2356 return -2;
2357
2358 // Now find the svar_T index in sn_var_vals.
2359 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2360 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002361 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 if (sv->sv_tv == &di->di_tv)
2363 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002364 if (check_item_writable(sv, check_writable, name) == FAIL)
2365 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 return idx;
2367 }
2368 }
2369 return -1;
2370}
2371
2372/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002373 * Find "name" in imported items of the current script or in "cctx" if not
2374 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 */
2376 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002377find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 int idx;
2380
Bram Moolenaare3d46852020-08-29 13:39:17 +02002381 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002382 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 if (cctx != NULL)
2384 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2385 {
2386 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2387 + idx;
2388
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002389 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2390 : STRLEN(import->imp_name) == len
2391 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 return import;
2393 }
2394
Bram Moolenaarefa94442020-08-08 22:16:00 +02002395 return find_imported_in_script(name, len, current_sctx.sc_sid);
2396}
2397
2398 imported_T *
2399find_imported_in_script(char_u *name, size_t len, int sid)
2400{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002401 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002402 int idx;
2403
Bram Moolenaare3d46852020-08-29 13:39:17 +02002404 if (!SCRIPT_ID_VALID(sid))
2405 return NULL;
2406 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2408 {
2409 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2410
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002411 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2412 : STRLEN(import->imp_name) == len
2413 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 return import;
2415 }
2416 return NULL;
2417}
2418
2419/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002420 * Free all imported variables.
2421 */
2422 static void
2423free_imported(cctx_T *cctx)
2424{
2425 int idx;
2426
2427 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2428 {
2429 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2430
2431 vim_free(import->imp_name);
2432 }
2433 ga_clear(&cctx->ctx_imports);
2434}
2435
2436/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002437 * Return a pointer to the next line that isn't empty or only contains a
2438 * comment. Skips over white space.
2439 * Returns NULL if there is none.
2440 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002441 char_u *
2442peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002443{
2444 int lnum = cctx->ctx_lnum;
2445
2446 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2447 {
2448 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002449 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002450
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002451 // ignore NULLs inserted for continuation lines
2452 if (line != NULL)
2453 {
2454 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002455 if (vim9_bad_comment(p))
2456 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002457 if (*p != NUL && !vim9_comment_start(p))
2458 return p;
2459 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002460 }
2461 return NULL;
2462}
2463
2464/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002465 * Called when checking for a following operator at "arg". When the rest of
2466 * the line is empty or only a comment, peek the next line. If there is a next
2467 * line return a pointer to it and set "nextp".
2468 * Otherwise skip over white space.
2469 */
2470 static char_u *
2471may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2472{
2473 char_u *p = skipwhite(arg);
2474
2475 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002476 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002477 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002478 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002479 if (*nextp != NULL)
2480 return *nextp;
2481 }
2482 return p;
2483}
2484
2485/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002486 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002487 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002488 * Returns NULL when at the end.
2489 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002490 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002491next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002492{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002493 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002494
2495 do
2496 {
2497 ++cctx->ctx_lnum;
2498 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002499 {
2500 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002501 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002502 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002503 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002504 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002505 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002506 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002507 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002508 return line;
2509}
2510
2511/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002512 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002513 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002514 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002515 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2516 */
2517 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002518may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002519{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002520 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002521 if (vim9_bad_comment(*arg))
2522 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002523 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002524 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002525 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002526
2527 if (next == NULL)
2528 return FAIL;
2529 *arg = skipwhite(next);
2530 }
2531 return OK;
2532}
2533
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002534/*
2535 * Idem, and give an error when failed.
2536 */
2537 static int
2538may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2539{
2540 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2541 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002542 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002543 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002544 return FAIL;
2545 }
2546 return OK;
2547}
2548
2549
Bram Moolenaara5565e42020-05-09 15:44:01 +02002550// Structure passed between the compile_expr* functions to keep track of
2551// constants that have been parsed but for which no code was produced yet. If
2552// possible expressions on these constants are applied at compile time. If
2553// that is not possible, the code to push the constants needs to be generated
2554// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002555// Using 50 should be more than enough of 5 levels of ().
2556#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002557typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002558 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002559 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002560 int pp_is_const; // all generated code was constants, used for a
2561 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002562} ppconst_T;
2563
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002564static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002565static int compile_expr0(char_u **arg, cctx_T *cctx);
2566static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2567
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568/*
2569 * Generate a PUSH instruction for "tv".
2570 * "tv" will be consumed or cleared.
2571 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2572 */
2573 static int
2574generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2575{
2576 if (tv != NULL)
2577 {
2578 switch (tv->v_type)
2579 {
2580 case VAR_UNKNOWN:
2581 break;
2582 case VAR_BOOL:
2583 generate_PUSHBOOL(cctx, tv->vval.v_number);
2584 break;
2585 case VAR_SPECIAL:
2586 generate_PUSHSPEC(cctx, tv->vval.v_number);
2587 break;
2588 case VAR_NUMBER:
2589 generate_PUSHNR(cctx, tv->vval.v_number);
2590 break;
2591#ifdef FEAT_FLOAT
2592 case VAR_FLOAT:
2593 generate_PUSHF(cctx, tv->vval.v_float);
2594 break;
2595#endif
2596 case VAR_BLOB:
2597 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2598 tv->vval.v_blob = NULL;
2599 break;
2600 case VAR_STRING:
2601 generate_PUSHS(cctx, tv->vval.v_string);
2602 tv->vval.v_string = NULL;
2603 break;
2604 default:
2605 iemsg("constant type not supported");
2606 clear_tv(tv);
2607 return FAIL;
2608 }
2609 tv->v_type = VAR_UNKNOWN;
2610 }
2611 return OK;
2612}
2613
2614/*
2615 * Generate code for any ppconst entries.
2616 */
2617 static int
2618generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2619{
2620 int i;
2621 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002622 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002623
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002624 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002625 for (i = 0; i < ppconst->pp_used; ++i)
2626 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2627 ret = FAIL;
2628 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002629 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002630 return ret;
2631}
2632
2633/*
2634 * Clear ppconst constants. Used when failing.
2635 */
2636 static void
2637clear_ppconst(ppconst_T *ppconst)
2638{
2639 int i;
2640
2641 for (i = 0; i < ppconst->pp_used; ++i)
2642 clear_tv(&ppconst->pp_tv[i]);
2643 ppconst->pp_used = 0;
2644}
2645
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002646/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002647 * Compile getting a member from a list/dict/string/blob. Stack has the
2648 * indexable value and the index.
2649 */
2650 static int
2651compile_member(int is_slice, cctx_T *cctx)
2652{
2653 type_T **typep;
2654 garray_T *stack = &cctx->ctx_type_stack;
2655 vartype_T vtype;
2656 type_T *valtype;
2657
2658 // We can index a list and a dict. If we don't know the type
2659 // we can use the index value type.
2660 // TODO: If we don't know use an instruction to figure it out at
2661 // runtime.
2662 typep = ((type_T **)stack->ga_data) + stack->ga_len
2663 - (is_slice ? 3 : 2);
2664 vtype = (*typep)->tt_type;
2665 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2666 // If the index is a string, the variable must be a Dict.
2667 if (*typep == &t_any && valtype == &t_string)
2668 vtype = VAR_DICT;
2669 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2670 {
2671 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2672 return FAIL;
2673 if (is_slice)
2674 {
2675 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2676 if (need_type(valtype, &t_number, -2, 0, cctx,
2677 FALSE, FALSE) == FAIL)
2678 return FAIL;
2679 }
2680 }
2681
2682 if (vtype == VAR_DICT)
2683 {
2684 if (is_slice)
2685 {
2686 emsg(_(e_cannot_slice_dictionary));
2687 return FAIL;
2688 }
2689 if ((*typep)->tt_type == VAR_DICT)
2690 {
2691 *typep = (*typep)->tt_member;
2692 if (*typep == &t_unknown)
2693 // empty dict was used
2694 *typep = &t_any;
2695 }
2696 else
2697 {
2698 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2699 FALSE, FALSE) == FAIL)
2700 return FAIL;
2701 *typep = &t_any;
2702 }
2703 if (may_generate_2STRING(-1, cctx) == FAIL)
2704 return FAIL;
2705 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2706 return FAIL;
2707 }
2708 else if (vtype == VAR_STRING)
2709 {
2710 *typep = &t_string;
2711 if ((is_slice
2712 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2713 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2714 return FAIL;
2715 }
2716 else if (vtype == VAR_BLOB)
2717 {
2718 emsg("Sorry, blob index and slice not implemented yet");
2719 return FAIL;
2720 }
2721 else if (vtype == VAR_LIST || *typep == &t_any)
2722 {
2723 if (is_slice)
2724 {
2725 if (generate_instr_drop(cctx,
2726 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2727 2) == FAIL)
2728 return FAIL;
2729 }
2730 else
2731 {
2732 if ((*typep)->tt_type == VAR_LIST)
2733 {
2734 *typep = (*typep)->tt_member;
2735 if (*typep == &t_unknown)
2736 // empty list was used
2737 *typep = &t_any;
2738 }
2739 if (generate_instr_drop(cctx,
2740 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2741 return FAIL;
2742 }
2743 }
2744 else
2745 {
2746 emsg(_(e_string_list_dict_or_blob_required));
2747 return FAIL;
2748 }
2749 return OK;
2750}
2751
2752/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002753 * Generate an instruction to load script-local variable "name", without the
2754 * leading "s:".
2755 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 */
2757 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002758compile_load_scriptvar(
2759 cctx_T *cctx,
2760 char_u *name, // variable NUL terminated
2761 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002762 char_u **end, // end of variable
2763 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002765 scriptitem_T *si;
2766 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 imported_T *import;
2768
Bram Moolenaare3d46852020-08-29 13:39:17 +02002769 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2770 return FAIL;
2771 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002772 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002773 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002775 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002776 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2777 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 }
2779 if (idx >= 0)
2780 {
2781 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2782
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002783 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 current_sctx.sc_sid, idx, sv->sv_type);
2785 return OK;
2786 }
2787
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002788 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 if (import != NULL)
2790 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002791 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002792 {
2793 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002794 char_u *exp_name;
2795 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002796 ufunc_T *ufunc;
2797 type_T *type;
2798
2799 // Used "import * as Name", need to lookup the member.
2800 if (*p != '.')
2801 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002802 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002803 return FAIL;
2804 }
2805 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002806 if (VIM_ISWHITE(*p))
2807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002808 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002809 return FAIL;
2810 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002811
Bram Moolenaar1c991142020-07-04 13:15:31 +02002812 // isolate one name
2813 exp_name = p;
2814 while (eval_isnamec(*p))
2815 ++p;
2816 cc = *p;
2817 *p = NUL;
2818
Bram Moolenaaredba7072021-03-13 21:14:18 +01002819 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2820 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002821 *p = cc;
2822 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002823 *end = p;
2824
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002825 if (idx < 0)
2826 {
2827 if (*p == '(' && ufunc != NULL)
2828 {
2829 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2830 return OK;
2831 }
2832 return FAIL;
2833 }
2834
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002835 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2836 import->imp_sid,
2837 idx,
2838 type);
2839 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002840 else if (import->imp_funcname != NULL)
2841 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002842 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002843 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2844 import->imp_sid,
2845 import->imp_var_vals_idx,
2846 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 return OK;
2848 }
2849
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002850 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002851 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 return FAIL;
2853}
2854
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002855 static int
2856generate_funcref(cctx_T *cctx, char_u *name)
2857{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002858 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002859
2860 if (ufunc == NULL)
2861 return FAIL;
2862
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002863 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002864 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2865 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002866 == FAIL)
2867 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002868 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002869}
2870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871/*
2872 * Compile a variable name into a load instruction.
2873 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002874 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 * When "error" is FALSE do not give an error when not found.
2876 */
2877 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002878compile_load(
2879 char_u **arg,
2880 char_u *end_arg,
2881 cctx_T *cctx,
2882 int is_expr,
2883 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884{
2885 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002886 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002887 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002889 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890
2891 if (*(*arg + 1) == ':')
2892 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002893 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002894 {
2895 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
Bram Moolenaarfa596382021-04-07 21:58:16 +02002897 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002898 switch (**arg)
2899 {
2900 case 'g': isn_type = ISN_LOADGDICT; break;
2901 case 'w': isn_type = ISN_LOADWDICT; break;
2902 case 't': isn_type = ISN_LOADTDICT; break;
2903 case 'b': isn_type = ISN_LOADBDICT; break;
2904 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002905 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002906 goto theend;
2907 }
2908 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2909 goto theend;
2910 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 else
2913 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002914 isntype_T isn_type = ISN_DROP;
2915
Bram Moolenaarfa596382021-04-07 21:58:16 +02002916 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002917 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2918 if (name == NULL)
2919 return FAIL;
2920
2921 switch (**arg)
2922 {
2923 case 'v': res = generate_LOADV(cctx, name, error);
2924 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002925 case 's': if (is_expr && ASCII_ISUPPER(*name)
2926 && find_func(name, FALSE, cctx) != NULL)
2927 res = generate_funcref(cctx, name);
2928 else
2929 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002930 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002931 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002932 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002933 {
2934 if (is_expr && ASCII_ISUPPER(*name)
2935 && find_func(name, FALSE, cctx) != NULL)
2936 res = generate_funcref(cctx, name);
2937 else
2938 isn_type = ISN_LOADG;
2939 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002940 else
2941 {
2942 isn_type = ISN_LOADAUTO;
2943 vim_free(name);
2944 name = vim_strnsave(*arg, end - *arg);
2945 if (name == NULL)
2946 return FAIL;
2947 }
2948 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002949 case 'w': isn_type = ISN_LOADW; break;
2950 case 't': isn_type = ISN_LOADT; break;
2951 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002952 default: // cannot happen, just in case
2953 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002954 goto theend;
2955 }
2956 if (isn_type != ISN_DROP)
2957 {
2958 // Global, Buffer-local, Window-local and Tabpage-local
2959 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002960 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002961 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 }
2964 }
2965 else
2966 {
2967 size_t len = end - *arg;
2968 int idx;
2969 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002970 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
2972 name = vim_strnsave(*arg, end - *arg);
2973 if (name == NULL)
2974 return FAIL;
2975
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002976 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002978 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002979 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 }
2981 else
2982 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002983 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002984
Bram Moolenaar709664c2020-12-12 14:33:41 +01002985 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002987 type = lvar.lv_type;
2988 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002989 if (lvar.lv_from_outer != 0)
2990 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002991 else
2992 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 }
2994 else
2995 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002996 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002997 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02002998 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002999 || find_imported(name, 0, cctx) != NULL)
3000 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003001
Bram Moolenaar0f769812020-09-12 18:32:34 +02003002 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003003 // uppercase letter it can be a user defined function.
3004 // generate_funcref() will fail if the function can't be found.
3005 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003006 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 }
3008 }
3009 if (gen_load)
3010 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003011 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003012 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003013 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003014 cctx->ctx_outer_used = TRUE;
3015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
3017
3018 *arg = end;
3019
3020theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003021 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003022 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 vim_free(name);
3024 return res;
3025}
3026
3027/*
3028 * Compile the argument expressions.
3029 * "arg" points to just after the "(" and is advanced to after the ")"
3030 */
3031 static int
3032compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3033{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003034 char_u *p = *arg;
3035 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003036 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037
Bram Moolenaare6085c52020-04-12 20:19:16 +02003038 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003040 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3041 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003042 if (*p == ')')
3043 {
3044 *arg = p + 1;
3045 return OK;
3046 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003047 if (must_end)
3048 {
3049 semsg(_(e_missing_comma_before_argument_str), p);
3050 return FAIL;
3051 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003052
Bram Moolenaara5565e42020-05-09 15:44:01 +02003053 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 return FAIL;
3055 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003056
3057 if (*p != ',' && *skipwhite(p) == ',')
3058 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003059 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003060 p = skipwhite(p);
3061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003063 {
3064 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003065 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003066 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003067 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003068 else
3069 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003070 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003071 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003073failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003074 emsg(_(e_missing_close));
3075 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076}
3077
3078/*
3079 * Compile a function call: name(arg1, arg2)
3080 * "arg" points to "name", "arg + varlen" to the "(".
3081 * "argcount_init" is 1 for "value->method()"
3082 * Instructions:
3083 * EVAL arg1
3084 * EVAL arg2
3085 * BCALL / DCALL / UCALL
3086 */
3087 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003088compile_call(
3089 char_u **arg,
3090 size_t varlen,
3091 cctx_T *cctx,
3092 ppconst_T *ppconst,
3093 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094{
3095 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003096 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 int argcount = argcount_init;
3098 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003099 char_u fname_buf[FLEN_FIXED + 1];
3100 char_u *tofree = NULL;
3101 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003102 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003103 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003104 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105
Bram Moolenaara5565e42020-05-09 15:44:01 +02003106 // we can evaluate "has('name')" at compile time
3107 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3108 {
3109 char_u *s = skipwhite(*arg + varlen + 1);
3110 typval_T argvars[2];
3111
3112 argvars[0].v_type = VAR_UNKNOWN;
3113 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003115 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003116 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003117 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003118 if (*s == ')' && argvars[0].v_type == VAR_STRING
3119 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003120 {
3121 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3122
3123 *arg = s + 1;
3124 argvars[1].v_type = VAR_UNKNOWN;
3125 tv->v_type = VAR_NUMBER;
3126 tv->vval.v_number = 0;
3127 f_has(argvars, tv);
3128 clear_tv(&argvars[0]);
3129 ++ppconst->pp_used;
3130 return OK;
3131 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003132 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003133 }
3134
3135 if (generate_ppconst(cctx, ppconst) == FAIL)
3136 return FAIL;
3137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 if (varlen >= sizeof(namebuf))
3139 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003140 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 return FAIL;
3142 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003143 vim_strncpy(namebuf, *arg, varlen);
3144 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145
3146 *arg = skipwhite(*arg + varlen + 1);
3147 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003148 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
Bram Moolenaar03290b82020-12-19 16:30:44 +01003150 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003151 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
3153 int idx;
3154
3155 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003156 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003158 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003159 if (STRCMP(name, "flatten") == 0)
3160 {
3161 emsg(_(e_cannot_use_flatten_in_vim9_script));
3162 goto theend;
3163 }
3164
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003165 if (STRCMP(name, "add") == 0 && argcount == 2)
3166 {
3167 garray_T *stack = &cctx->ctx_type_stack;
3168 type_T *type = ((type_T **)stack->ga_data)[
3169 stack->ga_len - 2];
3170
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003171 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003172 if (type->tt_type == VAR_LIST)
3173 {
3174 // inline "add(list, item)" so that the type can be checked
3175 res = generate_LISTAPPEND(cctx);
3176 idx = -1;
3177 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003178 else if (type->tt_type == VAR_BLOB)
3179 {
3180 // inline "add(blob, nr)" so that the type can be checked
3181 res = generate_BLOBAPPEND(cctx);
3182 idx = -1;
3183 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003184 }
3185
3186 if (idx >= 0)
3187 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3188 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003189 else
3190 semsg(_(e_unknownfunc), namebuf);
3191 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003194 // An argument or local variable can be a function reference, this
3195 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003196 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003197 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003198 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003199 // If we can find the function by name generate the right call.
3200 // Skip global functions here, a local funcref takes precedence.
3201 ufunc = find_func(name, FALSE, cctx);
3202 if (ufunc != NULL && !func_is_global(ufunc))
3203 {
3204 res = generate_CALL(cctx, ufunc, argcount);
3205 goto theend;
3206 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208
3209 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003210 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003211 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003213 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003214 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003215 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003216 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003217 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003218
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003219 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003220 goto theend;
3221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222
Bram Moolenaar0f769812020-09-12 18:32:34 +02003223 // If we can find a global function by name generate the right call.
3224 if (ufunc != NULL)
3225 {
3226 res = generate_CALL(cctx, ufunc, argcount);
3227 goto theend;
3228 }
3229
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003230 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003231 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003232 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003233 res = generate_UCALL(cctx, name, argcount);
3234 else
3235 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003236
3237theend:
3238 vim_free(tofree);
3239 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240}
3241
3242// like NAMESPACE_CHAR but with 'a' and 'l'.
3243#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3244
3245/*
3246 * Find the end of a variable or function name. Unlike find_name_end() this
3247 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003248 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 * Return a pointer to just after the name. Equal to "arg" if there is no
3250 * valid name.
3251 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003252 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003253to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254{
3255 char_u *p;
3256
3257 // Quick check for valid starting character.
3258 if (!eval_isnamec1(*arg))
3259 return arg;
3260
3261 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3262 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3263 // and can be used in slice "[n:]".
3264 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003265 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3267 break;
3268 return p;
3269}
3270
3271/*
3272 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003273 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 */
3275 char_u *
3276to_name_const_end(char_u *arg)
3277{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003278 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 typval_T rettv;
3280
3281 if (p == arg && *arg == '[')
3282 {
3283
3284 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003285 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 p = arg;
3287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 return p;
3289}
3290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291/*
3292 * parse a list: [expr, expr]
3293 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003294 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 */
3296 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003297compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298{
3299 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003300 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003302 int is_const;
3303 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003305 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003307 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003308 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003309 semsg(_(e_list_end), *arg);
3310 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003311 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003312 if (*p == ',')
3313 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003314 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003315 return FAIL;
3316 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003317 if (*p == ']')
3318 {
3319 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003320 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003321 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003322 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003323 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003324 if (!is_const)
3325 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 ++count;
3327 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003328 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003330 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3331 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003332 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003333 return FAIL;
3334 }
3335 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003336 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 p = skipwhite(p);
3338 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003339 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003341 ppconst->pp_is_const = is_all_const;
3342 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343}
3344
3345/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003346 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003347 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003348 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 */
3350 static int
3351compile_lambda(char_u **arg, cctx_T *cctx)
3352{
Bram Moolenaare462f522020-12-27 14:43:30 +01003353 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 typval_T rettv;
3355 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003356 evalarg_T evalarg;
3357
3358 CLEAR_FIELD(evalarg);
3359 evalarg.eval_flags = EVAL_EVALUATE;
3360 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361
3362 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003363 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3364 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003365 {
3366 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003367 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003368 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003369
Bram Moolenaar65c44152020-12-24 15:14:01 +01003370 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003372 ++ufunc->uf_refcount;
3373 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374
Bram Moolenaar65c44152020-12-24 15:14:01 +01003375 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003376 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003378 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3379 // points into it. Point to the original line to avoid a dangling pointer.
3380 if (evalarg.eval_tofree_cmdline != NULL)
3381 {
3382 size_t off = *arg - evalarg.eval_tofree_cmdline;
3383
3384 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3385 + off;
3386 }
3387
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003388 clear_evalarg(&evalarg, NULL);
3389
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003390 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003391 {
3392 // The return type will now be known.
3393 set_function_type(ufunc);
3394
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003395 // The function reference count will be 1. When the ISN_FUNCREF
3396 // instruction is deleted the reference count is decremented and the
3397 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003398 return generate_FUNCREF(cctx, ufunc);
3399 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003400
3401 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003402 return FAIL;
3403}
3404
3405/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003406 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003408 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 */
3410 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003411compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412{
3413 garray_T *instr = &cctx->ctx_instr;
3414 int count = 0;
3415 dict_T *d = dict_alloc();
3416 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003417 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003418 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003419 int is_const;
3420 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421
3422 if (d == NULL)
3423 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003424 if (generate_ppconst(cctx, ppconst) == FAIL)
3425 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003426 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003428 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429
Bram Moolenaar23c55272020-06-21 16:58:13 +02003430 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003431 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003432 *arg = NULL;
3433 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003434 }
3435
3436 if (**arg == '}')
3437 break;
3438
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003439 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003441 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003443 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003444 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003445 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003448 if (isn->isn_type == ISN_PUSHNR)
3449 {
3450 char buf[NUMBUFLEN];
3451
3452 // Convert to string at compile time.
3453 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3454 isn->isn_type = ISN_PUSHS;
3455 isn->isn_arg.string = vim_strsave((char_u *)buf);
3456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 if (isn->isn_type == ISN_PUSHS)
3458 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003459 else if (may_generate_2STRING(-1, cctx) == FAIL)
3460 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003461 *arg = skipwhite(*arg);
3462 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003463 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003464 emsg(_(e_missing_matching_bracket_after_dict_key));
3465 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003466 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003467 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003469 else
3470 {
3471 // {"name": value},
3472 // {'name': value},
3473 // {name: value} use "name" as a literal key
3474 key = get_literal_key(arg);
3475 if (key == NULL)
3476 return FAIL;
3477 if (generate_PUSHS(cctx, key) == FAIL)
3478 return FAIL;
3479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480
3481 // Check for duplicate keys, if using string keys.
3482 if (key != NULL)
3483 {
3484 item = dict_find(d, key, -1);
3485 if (item != NULL)
3486 {
3487 semsg(_(e_duplicate_key), key);
3488 goto failret;
3489 }
3490 item = dictitem_alloc(key);
3491 if (item != NULL)
3492 {
3493 item->di_tv.v_type = VAR_UNKNOWN;
3494 item->di_tv.v_lock = 0;
3495 if (dict_add(d, item) == FAIL)
3496 dictitem_free(item);
3497 }
3498 }
3499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 if (**arg != ':')
3501 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003502 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003503 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003504 else
3505 semsg(_(e_missing_dict_colon), *arg);
3506 return FAIL;
3507 }
3508 whitep = *arg + 1;
3509 if (!IS_WHITE_OR_NUL(*whitep))
3510 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003511 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 return FAIL;
3513 }
3514
Bram Moolenaar23c55272020-06-21 16:58:13 +02003515 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003516 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003517 *arg = NULL;
3518 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003519 }
3520
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003521 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003523 if (!is_const)
3524 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 ++count;
3526
Bram Moolenaar2c330432020-04-13 14:41:35 +02003527 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003528 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003529 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003530 *arg = NULL;
3531 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (**arg == '}')
3534 break;
3535 if (**arg != ',')
3536 {
3537 semsg(_(e_missing_dict_comma), *arg);
3538 goto failret;
3539 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003540 if (IS_WHITE_OR_NUL(*whitep))
3541 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003542 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003543 return FAIL;
3544 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003545 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003546 if (!IS_WHITE_OR_NUL(*whitep))
3547 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003548 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003549 return FAIL;
3550 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003551 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 }
3553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 *arg = *arg + 1;
3555
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003556 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003557 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003558 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003559 *arg += STRLEN(*arg);
3560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003562 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 return generate_NEWDICT(cctx, count);
3564
3565failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003566 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003567 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003568 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003569 *arg = (char_u *)"";
3570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 dict_unref(d);
3572 return FAIL;
3573}
3574
3575/*
3576 * Compile "&option".
3577 */
3578 static int
3579compile_get_option(char_u **arg, cctx_T *cctx)
3580{
3581 typval_T rettv;
3582 char_u *start = *arg;
3583 int ret;
3584
3585 // parse the option and get the current value to get the type.
3586 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003587 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 if (ret == OK)
3589 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003590 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003591 char_u *name = vim_strnsave(start, *arg - start);
3592 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3593 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594
3595 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3596 vim_free(name);
3597 }
3598 clear_tv(&rettv);
3599
3600 return ret;
3601}
3602
3603/*
3604 * Compile "$VAR".
3605 */
3606 static int
3607compile_get_env(char_u **arg, cctx_T *cctx)
3608{
3609 char_u *start = *arg;
3610 int len;
3611 int ret;
3612 char_u *name;
3613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 ++*arg;
3615 len = get_env_len(arg);
3616 if (len == 0)
3617 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003618 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 return FAIL;
3620 }
3621
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003622 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 name = vim_strnsave(start, len + 1);
3624 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3625 vim_free(name);
3626 return ret;
3627}
3628
3629/*
3630 * Compile "@r".
3631 */
3632 static int
3633compile_get_register(char_u **arg, cctx_T *cctx)
3634{
3635 int ret;
3636
3637 ++*arg;
3638 if (**arg == NUL)
3639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003640 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 return FAIL;
3642 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003643 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
3645 emsg_invreg(**arg);
3646 return FAIL;
3647 }
3648 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3649 ++*arg;
3650 return ret;
3651}
3652
3653/*
3654 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003655 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 */
3657 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003658apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003660 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
3662 // this works from end to start
3663 while (p > start)
3664 {
3665 --p;
3666 if (*p == '-' || *p == '+')
3667 {
3668 // only '-' has an effect, for '+' we only check the type
3669#ifdef FEAT_FLOAT
3670 if (rettv->v_type == VAR_FLOAT)
3671 {
3672 if (*p == '-')
3673 rettv->vval.v_float = -rettv->vval.v_float;
3674 }
3675 else
3676#endif
3677 {
3678 varnumber_T val;
3679 int error = FALSE;
3680
3681 // tv_get_number_chk() accepts a string, but we don't want that
3682 // here
3683 if (check_not_string(rettv) == FAIL)
3684 return FAIL;
3685 val = tv_get_number_chk(rettv, &error);
3686 clear_tv(rettv);
3687 if (error)
3688 return FAIL;
3689 if (*p == '-')
3690 val = -val;
3691 rettv->v_type = VAR_NUMBER;
3692 rettv->vval.v_number = val;
3693 }
3694 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003695 else if (numeric_only)
3696 {
3697 ++p;
3698 break;
3699 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003700 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 {
3702 int v = tv2bool(rettv);
3703
3704 // '!' is permissive in the type.
3705 clear_tv(rettv);
3706 rettv->v_type = VAR_BOOL;
3707 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3708 }
3709 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003710 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 return OK;
3712}
3713
3714/*
3715 * Recognize v: variables that are constants and set "rettv".
3716 */
3717 static void
3718get_vim_constant(char_u **arg, typval_T *rettv)
3719{
3720 if (STRNCMP(*arg, "v:true", 6) == 0)
3721 {
3722 rettv->v_type = VAR_BOOL;
3723 rettv->vval.v_number = VVAL_TRUE;
3724 *arg += 6;
3725 }
3726 else if (STRNCMP(*arg, "v:false", 7) == 0)
3727 {
3728 rettv->v_type = VAR_BOOL;
3729 rettv->vval.v_number = VVAL_FALSE;
3730 *arg += 7;
3731 }
3732 else if (STRNCMP(*arg, "v:null", 6) == 0)
3733 {
3734 rettv->v_type = VAR_SPECIAL;
3735 rettv->vval.v_number = VVAL_NULL;
3736 *arg += 6;
3737 }
3738 else if (STRNCMP(*arg, "v:none", 6) == 0)
3739 {
3740 rettv->v_type = VAR_SPECIAL;
3741 rettv->vval.v_number = VVAL_NONE;
3742 *arg += 6;
3743 }
3744}
3745
Bram Moolenaar657137c2021-01-09 15:45:23 +01003746 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003747get_compare_type(char_u *p, int *len, int *type_is)
3748{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003749 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003750 int i;
3751
3752 switch (p[0])
3753 {
3754 case '=': if (p[1] == '=')
3755 type = EXPR_EQUAL;
3756 else if (p[1] == '~')
3757 type = EXPR_MATCH;
3758 break;
3759 case '!': if (p[1] == '=')
3760 type = EXPR_NEQUAL;
3761 else if (p[1] == '~')
3762 type = EXPR_NOMATCH;
3763 break;
3764 case '>': if (p[1] != '=')
3765 {
3766 type = EXPR_GREATER;
3767 *len = 1;
3768 }
3769 else
3770 type = EXPR_GEQUAL;
3771 break;
3772 case '<': if (p[1] != '=')
3773 {
3774 type = EXPR_SMALLER;
3775 *len = 1;
3776 }
3777 else
3778 type = EXPR_SEQUAL;
3779 break;
3780 case 'i': if (p[1] == 's')
3781 {
3782 // "is" and "isnot"; but not a prefix of a name
3783 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3784 *len = 5;
3785 i = p[*len];
3786 if (!isalnum(i) && i != '_')
3787 {
3788 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3789 *type_is = TRUE;
3790 }
3791 }
3792 break;
3793 }
3794 return type;
3795}
3796
3797/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003798 * Skip over an expression, ignoring most errors.
3799 */
3800 static void
3801skip_expr_cctx(char_u **arg, cctx_T *cctx)
3802{
3803 evalarg_T evalarg;
3804
3805 CLEAR_FIELD(evalarg);
3806 evalarg.eval_cctx = cctx;
3807 skip_expr(arg, &evalarg);
3808}
3809
3810/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003812 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 */
3814 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003815compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003817 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818
3819 // this works from end to start
3820 while (p > start)
3821 {
3822 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003823 while (VIM_ISWHITE(*p))
3824 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 if (*p == '-' || *p == '+')
3826 {
3827 int negate = *p == '-';
3828 isn_T *isn;
3829
3830 // TODO: check type
3831 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3832 {
3833 --p;
3834 if (*p == '-')
3835 negate = !negate;
3836 }
3837 // only '-' has an effect, for '+' we only check the type
3838 if (negate)
3839 isn = generate_instr(cctx, ISN_NEGATENR);
3840 else
3841 isn = generate_instr(cctx, ISN_CHECKNR);
3842 if (isn == NULL)
3843 return FAIL;
3844 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003845 else if (numeric_only)
3846 {
3847 ++p;
3848 break;
3849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 else
3851 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003852 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003854 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003856 if (p[-1] == '!')
3857 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 }
3860 if (generate_2BOOL(cctx, invert) == FAIL)
3861 return FAIL;
3862 }
3863 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003864 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 return OK;
3866}
3867
3868/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003869 * Compile "(expression)": recursive!
3870 * Return FAIL/OK.
3871 */
3872 static int
3873compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3874{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003875 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003876 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003877
Bram Moolenaar24156692021-01-14 20:35:49 +01003878 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3879 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003880 if (ppconst->pp_used <= PPSIZE - 10)
3881 {
3882 ret = compile_expr1(arg, cctx, ppconst);
3883 }
3884 else
3885 {
3886 // Not enough space in ppconst, flush constants.
3887 if (generate_ppconst(cctx, ppconst) == FAIL)
3888 return FAIL;
3889 ret = compile_expr0(arg, cctx);
3890 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003891 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3892 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003893 if (**arg == ')')
3894 ++*arg;
3895 else if (ret == OK)
3896 {
3897 emsg(_(e_missing_close));
3898 ret = FAIL;
3899 }
3900 return ret;
3901}
3902
3903/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003905 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 */
3907 static int
3908compile_subscript(
3909 char_u **arg,
3910 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003911 char_u *start_leader,
3912 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003913 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003915 char_u *name_start = *end_leader;
3916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 for (;;)
3918 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003919 char_u *p = skipwhite(*arg);
3920
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003921 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003922 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003923 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003924
3925 // If a following line starts with "->{" or "->X" advance to that
3926 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003927 // Also if a following line starts with ".x".
3928 if (next != NULL &&
3929 ((next[0] == '-' && next[1] == '>'
3930 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003931 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003932 {
3933 next = next_line_from_context(cctx, TRUE);
3934 if (next == NULL)
3935 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003936 *arg = next;
3937 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003938 }
3939 }
3940
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003941 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003942 // not a function call.
3943 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003945 garray_T *stack = &cctx->ctx_type_stack;
3946 type_T *type;
3947 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003949 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003950 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003951 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003954 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3955
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003956 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3958 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003959 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 return FAIL;
3961 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003962 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003964 char_u *pstart = p;
3965
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003966 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003967 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003968 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 // something->method()
3971 // Apply the '!', '-' and '+' first:
3972 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003973 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003976 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003977 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003978 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003979 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003980 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003981 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003982 garray_T *stack = &cctx->ctx_type_stack;
3983 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003984 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003985 int expr_isn_start = cctx->ctx_instr.ga_len;
3986 int expr_isn_end;
3987 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003988
3989 // Funcref call: list->(Refs[2])(arg)
3990 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003991 //
3992 // Fist compile the function expression.
3993 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01003994 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003995
3996 // Remember the next instruction index, where the instructions
3997 // for arguments are being written.
3998 expr_isn_end = cctx->ctx_instr.ga_len;
3999
4000 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004001 if (**arg != '(')
4002 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004003 if (*skipwhite(*arg) == '(')
4004 emsg(_(e_nowhitespace));
4005 else
4006 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004007 return FAIL;
4008 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004009 *arg = skipwhite(*arg + 1);
4010 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4011 return FAIL;
4012
Bram Moolenaar2927c072021-04-05 19:41:21 +02004013 // Move the instructions for the arguments to before the
4014 // instructions of the expression and move the type of the
4015 // expression after the argument types. This is what ISN_PCALL
4016 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004017 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004018 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4019 if (arg_isn_count > 0)
4020 {
4021 int expr_isn_count = expr_isn_end - expr_isn_start;
4022 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4023
4024 if (isn == NULL)
4025 return FAIL;
4026 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4027 + expr_isn_start,
4028 sizeof(isn_T) * expr_isn_count);
4029 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4030 + expr_isn_start,
4031 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4032 sizeof(isn_T) * arg_isn_count);
4033 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4034 + expr_isn_start + arg_isn_count,
4035 isn, sizeof(isn_T) * expr_isn_count);
4036 vim_free(isn);
4037
4038 type = ((type_T **)stack->ga_data)[type_idx_start];
4039 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4040 ((type_T **)stack->ga_data) + type_idx_start + 1,
4041 sizeof(type_T *)
4042 * (stack->ga_len - type_idx_start - 1));
4043 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4044 }
4045
Bram Moolenaar7e368202020-12-25 21:56:57 +01004046 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4047 if (generate_PCALL(cctx, argcount,
4048 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 return FAIL;
4050 }
4051 else
4052 {
4053 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004054 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004055 if (!eval_isnamec1(*p))
4056 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004057 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004058 return FAIL;
4059 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004060 if (ASCII_ISALPHA(*p) && p[1] == ':')
4061 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004062 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 ;
4064 if (*p != '(')
4065 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004066 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068 }
4069 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004070 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 return FAIL;
4072 }
4073 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004074 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004076 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004077
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004078 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004079 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004080 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004081 // TODO: blob index
4082 // TODO: more arguments
4083 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004084 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004085 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004086 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004087
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004088 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004089 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004090 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004091 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004092 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004093 // missing first index is equal to zero
4094 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004095 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004096 else
4097 {
4098 if (compile_expr0(arg, cctx) == FAIL)
4099 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004100 if (**arg == ':')
4101 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004102 semsg(_(e_white_space_required_before_and_after_str_at_str),
4103 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004104 return FAIL;
4105 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004106 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004107 return FAIL;
4108 *arg = skipwhite(*arg);
4109 }
4110 if (**arg == ':')
4111 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004112 is_slice = TRUE;
4113 ++*arg;
4114 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4115 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004116 semsg(_(e_white_space_required_before_and_after_str_at_str),
4117 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004118 return FAIL;
4119 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004120 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004121 return FAIL;
4122 if (**arg == ']')
4123 // missing second index is equal to end of string
4124 generate_PUSHNR(cctx, -1);
4125 else
4126 {
4127 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004128 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004129 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004130 return FAIL;
4131 *arg = skipwhite(*arg);
4132 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
4135 if (**arg != ']')
4136 {
4137 emsg(_(e_missbrac));
4138 return FAIL;
4139 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004140 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141
Bram Moolenaare42939a2021-04-05 17:11:17 +02004142 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004143 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004145 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004147 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004148 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004149 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004150 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004151
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004152 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004153 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004154 {
4155 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004156 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004157 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004158 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004159 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 while (eval_isnamec(*p))
4161 MB_PTR_ADV(p);
4162 if (p == *arg)
4163 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004164 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 return FAIL;
4166 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004167 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 return FAIL;
4169 *arg = p;
4170 }
4171 else
4172 break;
4173 }
4174
4175 // TODO - see handle_subscript():
4176 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4177 // Don't do this when "Func" is already a partial that was bound
4178 // explicitly (pt_auto is FALSE).
4179
4180 return OK;
4181}
4182
4183/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004184 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4185 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004187 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004188 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 *
4190 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 */
4192
4193/*
4194 * number number constant
4195 * 0zFFFFFFFF Blob constant
4196 * "string" string constant
4197 * 'string' literal string constant
4198 * &option-name option value
4199 * @r register contents
4200 * identifier variable value
4201 * function() function call
4202 * $VAR environment variable
4203 * (expression) nested expression
4204 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004205 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 *
4207 * Also handle:
4208 * ! in front logical NOT
4209 * - in front unary minus
4210 * + in front unary plus (ignored)
4211 * trailing (arg) funcref/partial call
4212 * trailing [] subscript in String or List
4213 * trailing .name entry in Dictionary
4214 * trailing ->name() method call
4215 */
4216 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217compile_expr7(
4218 char_u **arg,
4219 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004220 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 char_u *start_leader, *end_leader;
4223 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004224 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004225 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004227 ppconst->pp_is_const = FALSE;
4228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 /*
4230 * Skip '!', '-' and '+' characters. They are handled later.
4231 */
4232 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004233 if (eval_leader(arg, TRUE) == FAIL)
4234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 end_leader = *arg;
4236
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004237 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 switch (**arg)
4239 {
4240 /*
4241 * Number constant.
4242 */
4243 case '0': // also for blob starting with 0z
4244 case '1':
4245 case '2':
4246 case '3':
4247 case '4':
4248 case '5':
4249 case '6':
4250 case '7':
4251 case '8':
4252 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004253 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004255 // Apply "-" and "+" just before the number now, right to
4256 // left. Matters especially when "->" follows. Stops at
4257 // '!'.
4258 if (apply_leader(rettv, TRUE,
4259 start_leader, &end_leader) == FAIL)
4260 {
4261 clear_tv(rettv);
4262 return FAIL;
4263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 break;
4265
4266 /*
4267 * String constant: "string".
4268 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004269 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 return FAIL;
4271 break;
4272
4273 /*
4274 * Literal string constant: 'str''ing'.
4275 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004276 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 return FAIL;
4278 break;
4279
4280 /*
4281 * Constant Vim variable.
4282 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004283 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 ret = NOTDONE;
4285 break;
4286
4287 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004288 * "true" constant
4289 */
4290 case 't': if (STRNCMP(*arg, "true", 4) == 0
4291 && !eval_isnamec((*arg)[4]))
4292 {
4293 *arg += 4;
4294 rettv->v_type = VAR_BOOL;
4295 rettv->vval.v_number = VVAL_TRUE;
4296 }
4297 else
4298 ret = NOTDONE;
4299 break;
4300
4301 /*
4302 * "false" constant
4303 */
4304 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4305 && !eval_isnamec((*arg)[5]))
4306 {
4307 *arg += 5;
4308 rettv->v_type = VAR_BOOL;
4309 rettv->vval.v_number = VVAL_FALSE;
4310 }
4311 else
4312 ret = NOTDONE;
4313 break;
4314
4315 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004316 * "null" constant
4317 */
4318 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004319 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004320 {
4321 *arg += 4;
4322 rettv->v_type = VAR_SPECIAL;
4323 rettv->vval.v_number = VVAL_NULL;
4324 }
4325 else
4326 ret = NOTDONE;
4327 break;
4328
4329 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 * List: [expr, expr]
4331 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004332 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4333 return FAIL;
4334 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 break;
4336
4337 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 * Dictionary: {'key': val, 'key': val}
4339 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004340 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4341 return FAIL;
4342 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 break;
4344
4345 /*
4346 * Option value: &name
4347 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004348 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4349 return FAIL;
4350 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 break;
4352
4353 /*
4354 * Environment variable: $VAR.
4355 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004356 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4357 return FAIL;
4358 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 break;
4360
4361 /*
4362 * Register contents: @r.
4363 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004364 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4365 return FAIL;
4366 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 break;
4368 /*
4369 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004370 * lambda: (arg, arg) => expr
4371 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004373 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4374 ret = compile_lambda(arg, cctx);
4375 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004376 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 break;
4378
4379 default: ret = NOTDONE;
4380 break;
4381 }
4382 if (ret == FAIL)
4383 return FAIL;
4384
Bram Moolenaar1c747212020-05-09 18:28:34 +02004385 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004387 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 clear_tv(rettv);
4389 else
4390 // A constant expression can possibly be handled compile time,
4391 // return the value instead of generating code.
4392 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
4394 else if (ret == NOTDONE)
4395 {
4396 char_u *p;
4397 int r;
4398
4399 if (!eval_isnamec1(**arg))
4400 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004401 if (!vim9_bad_comment(*arg))
4402 {
4403 if (ends_excmd(*skipwhite(*arg)))
4404 semsg(_(e_empty_expression_str), *arg);
4405 else
4406 semsg(_(e_name_expected_str), *arg);
4407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 return FAIL;
4409 }
4410
4411 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004412 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004414 {
4415 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004418 {
4419 if (generate_ppconst(cctx, ppconst) == FAIL)
4420 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004421 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 if (r == FAIL)
4424 return FAIL;
4425 }
4426
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004427 // Handle following "[]", ".member", etc.
4428 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004429 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 ppconst) == FAIL)
4431 return FAIL;
4432 if (ppconst->pp_used > 0)
4433 {
4434 // apply the '!', '-' and '+' before the constant
4435 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004436 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 return FAIL;
4438 return OK;
4439 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004440 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004442 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443}
4444
4445/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004446 * Give the "white on both sides" error, taking the operator from "p[len]".
4447 */
4448 void
4449error_white_both(char_u *op, int len)
4450{
4451 char_u buf[10];
4452
4453 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004454 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004455}
4456
4457/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004458 * <type>expr7: runtime type check / conversion
4459 */
4460 static int
4461compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4462{
4463 type_T *want_type = NULL;
4464
4465 // Recognize <type>
4466 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4467 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004468 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004469 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4470 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004471 return FAIL;
4472
4473 if (**arg != '>')
4474 {
4475 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004476 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004477 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004478 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004479 return FAIL;
4480 }
4481 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004482 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004483 return FAIL;
4484 }
4485
4486 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4487 return FAIL;
4488
4489 if (want_type != NULL)
4490 {
4491 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004492 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004493 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004494
Bram Moolenaard1103582020-08-14 22:44:25 +02004495 generate_ppconst(cctx, ppconst);
4496 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004497 where.wt_index = 0;
4498 where.wt_variable = FALSE;
4499 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004500 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004501 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004502 return FAIL;
4503 }
4504 }
4505
4506 return OK;
4507}
4508
4509/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 * * number multiplication
4511 * / number division
4512 * % number modulo
4513 */
4514 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516{
4517 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004518 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004519 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004521 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004522 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 return FAIL;
4524
4525 /*
4526 * Repeat computing, until no "*", "/" or "%" is following.
4527 */
4528 for (;;)
4529 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004530 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 if (*op != '*' && *op != '/' && *op != '%')
4532 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004533 if (next != NULL)
4534 {
4535 *arg = next_line_from_context(cctx, TRUE);
4536 op = skipwhite(*arg);
4537 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004538
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004539 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004541 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004542 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004544 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004545 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004547 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004548 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004550
4551 if (ppconst->pp_used == ppconst_used + 2
4552 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4553 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004554 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004555 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4556 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4557 varnumber_T res = 0;
4558 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560 // both are numbers: compute the result
4561 switch (*op)
4562 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004563 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004564 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004565 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004566 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004567 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004568 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004569 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004570 break;
4571 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004572 if (failed)
4573 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 tv1->vval.v_number = res;
4575 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 }
4577 else
4578 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004579 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580 generate_two_op(cctx, op);
4581 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
4583
4584 return OK;
4585}
4586
4587/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004588 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 * - number subtraction
4590 * .. string concatenation
4591 */
4592 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004593compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594{
4595 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004596 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599
4600 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004601 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 return FAIL;
4603
4604 /*
4605 * Repeat computing, until no "+", "-" or ".." is following.
4606 */
4607 for (;;)
4608 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004609 op = may_peek_next_line(cctx, *arg, &next);
4610 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 break;
4612 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004613 if (next != NULL)
4614 {
4615 *arg = next_line_from_context(cctx, TRUE);
4616 op = skipwhite(*arg);
4617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004619 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004621 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 }
4624
Bram Moolenaare0de1712020-12-02 17:36:54 +01004625 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004628 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 return FAIL;
4631
Bram Moolenaara5565e42020-05-09 15:44:01 +02004632 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004633 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4635 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4636 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4637 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4640 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004641
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004642 // concat/subtract/add constant numbers
4643 if (*op == '+')
4644 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4645 else if (*op == '-')
4646 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4647 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004648 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004649 // concatenate constant strings
4650 char_u *s1 = tv1->vval.v_string;
4651 char_u *s2 = tv2->vval.v_string;
4652 size_t len1 = STRLEN(s1);
4653
4654 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4655 if (tv1->vval.v_string == NULL)
4656 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004658 return FAIL;
4659 }
4660 mch_memmove(tv1->vval.v_string, s1, len1);
4661 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004662 vim_free(s1);
4663 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004664 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 }
4667 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004668 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004670 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004671 if (*op == '.')
4672 {
4673 if (may_generate_2STRING(-2, cctx) == FAIL
4674 || may_generate_2STRING(-1, cctx) == FAIL)
4675 return FAIL;
4676 generate_instr_drop(cctx, ISN_CONCAT, 1);
4677 }
4678 else
4679 generate_two_op(cctx, op);
4680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 }
4682
4683 return OK;
4684}
4685
4686/*
4687 * expr5a == expr5b
4688 * expr5a =~ expr5b
4689 * expr5a != expr5b
4690 * expr5a !~ expr5b
4691 * expr5a > expr5b
4692 * expr5a >= expr5b
4693 * expr5a < expr5b
4694 * expr5a <= expr5b
4695 * expr5a is expr5b
4696 * expr5a isnot expr5b
4697 *
4698 * Produces instructions:
4699 * EVAL expr5a Push result of "expr5a"
4700 * EVAL expr5b Push result of "expr5b"
4701 * COMPARE one of the compare instructions
4702 */
4703 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004704compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004706 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004708 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712
4713 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004714 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 return FAIL;
4716
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004717 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004718 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719
4720 /*
4721 * If there is a comparative operator, use it.
4722 */
4723 if (type != EXPR_UNKNOWN)
4724 {
4725 int ic = FALSE; // Default: do not ignore case
4726
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004727 if (next != NULL)
4728 {
4729 *arg = next_line_from_context(cctx, TRUE);
4730 p = skipwhite(*arg);
4731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 if (type_is && (p[len] == '?' || p[len] == '#'))
4733 {
4734 semsg(_(e_invexpr2), *arg);
4735 return FAIL;
4736 }
4737 // extra question mark appended: ignore case
4738 if (p[len] == '?')
4739 {
4740 ic = TRUE;
4741 ++len;
4742 }
4743 // extra '#' appended: match case (ignored)
4744 else if (p[len] == '#')
4745 ++len;
4746 // nothing appended: match case
4747
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004748 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004750 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 }
4753
4754 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004755 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004756 return FAIL;
4757
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 return FAIL;
4760
Bram Moolenaara5565e42020-05-09 15:44:01 +02004761 if (ppconst->pp_used == ppconst_used + 2)
4762 {
4763 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4764 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4765 int ret;
4766
4767 // Both sides are a constant, compute the result now.
4768 // First check for a valid combination of types, this is more
4769 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004770 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004771 ret = FAIL;
4772 else
4773 {
4774 ret = typval_compare(tv1, tv2, type, ic);
4775 tv1->v_type = VAR_BOOL;
4776 tv1->vval.v_number = tv1->vval.v_number
4777 ? VVAL_TRUE : VVAL_FALSE;
4778 clear_tv(tv2);
4779 --ppconst->pp_used;
4780 }
4781 return ret;
4782 }
4783
4784 generate_ppconst(cctx, ppconst);
4785 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 }
4787
4788 return OK;
4789}
4790
Bram Moolenaar7f141552020-05-09 17:35:53 +02004791static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793/*
4794 * Compile || or &&.
4795 */
4796 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004797compile_and_or(
4798 char_u **arg,
4799 cctx_T *cctx,
4800 char *op,
4801 ppconst_T *ppconst,
4802 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004804 char_u *next;
4805 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 int opchar = *op;
4807
4808 if (p[0] == opchar && p[1] == opchar)
4809 {
4810 garray_T *instr = &cctx->ctx_instr;
4811 garray_T end_ga;
4812
4813 /*
4814 * Repeat until there is no following "||" or "&&"
4815 */
4816 ga_init2(&end_ga, sizeof(int), 10);
4817 while (p[0] == opchar && p[1] == opchar)
4818 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004819 long start_lnum = SOURCING_LNUM;
4820 int start_ctx_lnum = cctx->ctx_lnum;
4821 int save_lnum;
4822
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004823 if (next != NULL)
4824 {
4825 *arg = next_line_from_context(cctx, TRUE);
4826 p = skipwhite(*arg);
4827 }
4828
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004829 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4830 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004831 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004832 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004833 return FAIL;
4834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004836 // TODO: use ppconst if the value is a constant and check
4837 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004838 generate_ppconst(cctx, ppconst);
4839
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004840 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004841 SOURCING_LNUM = start_lnum;
4842 save_lnum = cctx->ctx_lnum;
4843 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004844 if (bool_on_stack(cctx) == FAIL)
4845 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004846 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004847 ga_clear(&end_ga);
4848 return FAIL;
4849 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004850 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 if (ga_grow(&end_ga, 1) == FAIL)
4853 {
4854 ga_clear(&end_ga);
4855 return FAIL;
4856 }
4857 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4858 ++end_ga.ga_len;
4859 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004860 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861
4862 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004863 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004864 {
4865 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004866 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004867 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004868
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4870 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
4872 ga_clear(&end_ga);
4873 return FAIL;
4874 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004875
4876 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004878 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004880 // Every part must evaluate to a bool.
4881 if (bool_on_stack(cctx) == FAIL)
4882 {
4883 ga_clear(&end_ga);
4884 return FAIL;
4885 }
4886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 // Fill in the end label in all jumps.
4888 while (end_ga.ga_len > 0)
4889 {
4890 isn_T *isn;
4891
4892 --end_ga.ga_len;
4893 isn = ((isn_T *)instr->ga_data)
4894 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4895 isn->isn_arg.jump.jump_where = instr->ga_len;
4896 }
4897 ga_clear(&end_ga);
4898 }
4899
4900 return OK;
4901}
4902
4903/*
4904 * expr4a && expr4a && expr4a logical AND
4905 *
4906 * Produces instructions:
4907 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004908 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004909 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004911 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 * EVAL expr4c Push result of "expr4c"
4913 * end:
4914 */
4915 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004918 int ppconst_used = ppconst->pp_used;
4919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004921 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 return FAIL;
4923
4924 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004925 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926}
4927
4928/*
4929 * expr3a || expr3b || expr3c logical OR
4930 *
4931 * Produces instructions:
4932 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004933 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004934 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004936 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 * EVAL expr3c Push result of "expr3c"
4938 * end:
4939 */
4940 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004943 int ppconst_used = ppconst->pp_used;
4944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004946 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 return FAIL;
4948
4949 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004950 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951}
4952
4953/*
4954 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004956 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 * JUMP_IF_FALSE alt jump if false
4958 * EVAL expr1a
4959 * JUMP_ALWAYS end
4960 * alt: EVAL expr1b
4961 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004962 *
4963 * Toplevel expression: expr2 ?? expr1
4964 * Produces instructions:
4965 * EVAL expr2 Push result of "expr2"
4966 * JUMP_AND_KEEP_IF_TRUE end jump if true
4967 * EVAL expr1
4968 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 */
4970 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004971compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972{
4973 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004975 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976
Bram Moolenaar3988f642020-08-27 22:43:03 +02004977 // Ignore all kinds of errors when not producing code.
4978 if (cctx->ctx_skip == SKIP_YES)
4979 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004980 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004981 return OK;
4982 }
4983
Bram Moolenaar61a89812020-05-07 16:58:17 +02004984 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004985 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 return FAIL;
4987
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004988 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 if (*p == '?')
4990 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004991 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 garray_T *instr = &cctx->ctx_instr;
4993 garray_T *stack = &cctx->ctx_type_stack;
4994 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004995 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004997 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004998 int has_const_expr = FALSE;
4999 int const_value = FALSE;
5000 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005002 if (next != NULL)
5003 {
5004 *arg = next_line_from_context(cctx, TRUE);
5005 p = skipwhite(*arg);
5006 }
5007
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005008 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005009 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005010 semsg(_(e_white_space_required_before_and_after_str_at_str),
5011 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005012 return FAIL;
5013 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014
Bram Moolenaara5565e42020-05-09 15:44:01 +02005015 if (ppconst->pp_used == ppconst_used + 1)
5016 {
5017 // the condition is a constant, we know whether the ? or the :
5018 // expression is to be evaluated.
5019 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005020 if (op_falsy)
5021 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5022 else
5023 {
5024 int error = FALSE;
5025
5026 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5027 &error);
5028 if (error)
5029 return FAIL;
5030 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005031 cctx->ctx_skip = save_skip == SKIP_YES ||
5032 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5033
5034 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5035 // "left ?? right" and "left" is truthy: produce "left"
5036 generate_ppconst(cctx, ppconst);
5037 else
5038 {
5039 clear_tv(&ppconst->pp_tv[ppconst_used]);
5040 --ppconst->pp_used;
5041 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005042 }
5043 else
5044 {
5045 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005046 if (op_falsy)
5047 end_idx = instr->ga_len;
5048 generate_JUMP(cctx, op_falsy
5049 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5050 if (op_falsy)
5051 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053
5054 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005055 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005056 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005057 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005058 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
Bram Moolenaara5565e42020-05-09 15:44:01 +02005060 if (!has_const_expr)
5061 {
5062 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005064 if (!op_falsy)
5065 {
5066 // remember the type and drop it
5067 --stack->ga_len;
5068 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005070 end_idx = instr->ga_len;
5071 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005072
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005073 // jump here from JUMP_IF_FALSE
5074 isn = ((isn_T *)instr->ga_data) + alt_idx;
5075 isn->isn_arg.jump.jump_where = instr->ga_len;
5076 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005079 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005081 // Check for the ":".
5082 p = may_peek_next_line(cctx, *arg, &next);
5083 if (*p != ':')
5084 {
5085 emsg(_(e_missing_colon));
5086 return FAIL;
5087 }
5088 if (next != NULL)
5089 {
5090 *arg = next_line_from_context(cctx, TRUE);
5091 p = skipwhite(*arg);
5092 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005093
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005094 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5095 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005096 semsg(_(e_white_space_required_before_and_after_str_at_str),
5097 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005098 return FAIL;
5099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005101 // evaluate the third expression
5102 if (has_const_expr)
5103 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005104 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005105 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005106 return FAIL;
5107 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5108 return FAIL;
5109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110
Bram Moolenaara5565e42020-05-09 15:44:01 +02005111 if (!has_const_expr)
5112 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005113 type_T **typep;
5114
Bram Moolenaara5565e42020-05-09 15:44:01 +02005115 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaara5565e42020-05-09 15:44:01 +02005117 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005118 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5119 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005120
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005121 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005122 isn = ((isn_T *)instr->ga_data) + end_idx;
5123 isn->isn_arg.jump.jump_where = instr->ga_len;
5124 }
5125
5126 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 }
5128 return OK;
5129}
5130
5131/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005133 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5134 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005135 */
5136 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005137compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138{
5139 ppconst_T ppconst;
5140
5141 CLEAR_FIELD(ppconst);
5142 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5143 {
5144 clear_ppconst(&ppconst);
5145 return FAIL;
5146 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005147 if (is_const != NULL)
5148 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005149 if (generate_ppconst(cctx, &ppconst) == FAIL)
5150 return FAIL;
5151 return OK;
5152}
5153
5154/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005155 * Toplevel expression.
5156 */
5157 static int
5158compile_expr0(char_u **arg, cctx_T *cctx)
5159{
5160 return compile_expr0_ext(arg, cctx, NULL);
5161}
5162
5163/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 * compile "return [expr]"
5165 */
5166 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005167compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168{
5169 char_u *p = arg;
5170 garray_T *stack = &cctx->ctx_type_stack;
5171 type_T *stack_type;
5172
5173 if (*p != NUL && *p != '|' && *p != '\n')
5174 {
5175 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005176 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 return NULL;
5178
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005179 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005180 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005181 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005182 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005183 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5184 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005185 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005186 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005187 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005188 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005189 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005190 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5191 && stack_type->tt_type != VAR_VOID
5192 && stack_type->tt_type != VAR_UNKNOWN)
5193 {
5194 emsg(_(e_returning_value_in_function_without_return_type));
5195 return NULL;
5196 }
5197 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005198 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005199 return NULL;
5200 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 }
5203 else
5204 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005205 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005206 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005207 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5208 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005210 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 return NULL;
5212 }
5213
5214 // No argument, return zero.
5215 generate_PUSHNR(cctx, 0);
5216 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005217
5218 // Undo any command modifiers.
5219 generate_undo_cmdmods(cctx);
5220
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005221 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 return NULL;
5223
5224 // "return val | endif" is possible
5225 return skipwhite(p);
5226}
5227
5228/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005229 * Get a line from the compilation context, compatible with exarg_T getline().
5230 * Return a pointer to the line in allocated memory.
5231 * Return NULL for end-of-file or some error.
5232 */
5233 static char_u *
5234exarg_getline(
5235 int c UNUSED,
5236 void *cookie,
5237 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005238 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005239{
5240 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005241 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005242
Bram Moolenaar66250c92020-08-20 15:02:42 +02005243 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005244 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005245 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005246 return NULL;
5247 ++cctx->ctx_lnum;
5248 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5249 // Comment lines result in NULL pointers, skip them.
5250 if (p != NULL)
5251 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005252 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005253}
5254
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005255 void
5256fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5257{
5258 eap->getline = exarg_getline;
5259 eap->cookie = cctx;
5260}
5261
Bram Moolenaar04b12692020-05-04 23:24:44 +02005262/*
5263 * Compile a nested :def command.
5264 */
5265 static char_u *
5266compile_nested_function(exarg_T *eap, cctx_T *cctx)
5267{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005268 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005269 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005270 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005271 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005272 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005273 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005274
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005275 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005276 {
5277 emsg(_(e_cannot_use_bang_with_nested_def));
5278 return NULL;
5279 }
5280
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005281 if (*name_start == '/')
5282 {
5283 name_end = skip_regexp(name_start + 1, '/', TRUE);
5284 if (*name_end == '/')
5285 ++name_end;
5286 eap->nextcmd = check_nextcmd(name_end);
5287 }
5288 if (name_end == name_start || *skipwhite(name_end) != '(')
5289 {
5290 if (!ends_excmd2(name_start, name_end))
5291 {
5292 semsg(_(e_invalid_command_str), eap->cmd);
5293 return NULL;
5294 }
5295
5296 // "def" or "def Name": list functions
5297 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5298 return NULL;
5299 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5300 }
5301
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005302 // Only g:Func() can use a namespace.
5303 if (name_start[1] == ':' && !is_global)
5304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005305 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005306 return NULL;
5307 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005308 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005309 return NULL;
5310
Bram Moolenaar04b12692020-05-04 23:24:44 +02005311 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005312 fill_exarg_from_cctx(eap, cctx);
5313
Bram Moolenaar04b12692020-05-04 23:24:44 +02005314 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005315 lambda_name = vim_strsave(get_lambda_name());
5316 if (lambda_name == NULL)
5317 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005318 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005319
Bram Moolenaar822ba242020-05-24 23:00:18 +02005320 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005321 {
5322 r = eap->skip ? OK : FAIL;
5323 goto theend;
5324 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005325
5326 // copy over the block scope IDs before compiling
5327 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5328 {
5329 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5330
5331 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5332 if (ufunc->uf_block_ids != NULL)
5333 {
5334 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5335 sizeof(int) * block_depth);
5336 ufunc->uf_block_depth = block_depth;
5337 }
5338 }
5339
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005340 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5341 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005342 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005343 {
5344 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005345 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005346 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005347
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005348 if (is_global)
5349 {
5350 char_u *func_name = vim_strnsave(name_start + 2,
5351 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005352
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005353 if (func_name == NULL)
5354 r = FAIL;
5355 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005356 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005357 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005358 lambda_name = NULL;
5359 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005360 }
5361 else
5362 {
5363 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005364 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005365 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005366
Bram Moolenaareef21022020-08-01 22:16:43 +02005367 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005368 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005369 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005370 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005371 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5372 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005373 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005374
5375theend:
5376 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005377 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005378}
5379
5380/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381 * Return the length of an assignment operator, or zero if there isn't one.
5382 */
5383 int
5384assignment_len(char_u *p, int *heredoc)
5385{
5386 if (*p == '=')
5387 {
5388 if (p[1] == '<' && p[2] == '<')
5389 {
5390 *heredoc = TRUE;
5391 return 3;
5392 }
5393 return 1;
5394 }
5395 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5396 return 2;
5397 if (STRNCMP(p, "..=", 3) == 0)
5398 return 3;
5399 return 0;
5400}
5401
5402// words that cannot be used as a variable
5403static char *reserved[] = {
5404 "true",
5405 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005406 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 NULL
5408};
5409
Bram Moolenaar752fc692021-01-04 21:57:11 +01005410// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005411typedef enum {
5412 dest_local,
5413 dest_option,
5414 dest_env,
5415 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005416 dest_buffer,
5417 dest_window,
5418 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005419 dest_vimvar,
5420 dest_script,
5421 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005422 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005423} assign_dest_T;
5424
Bram Moolenaar752fc692021-01-04 21:57:11 +01005425// Used by compile_lhs() to store information about the LHS of an assignment
5426// and one argument of ":unlet" with an index.
5427typedef struct {
5428 assign_dest_T lhs_dest; // type of destination
5429
5430 char_u *lhs_name; // allocated name including
5431 // "[expr]" or ".name".
5432 size_t lhs_varlen; // length of the variable without
5433 // "[expr]" or ".name"
5434 char_u *lhs_dest_end; // end of the destination, including
5435 // "[expr]" or ".name".
5436
5437 int lhs_has_index; // has "[expr]" or ".name"
5438
5439 int lhs_new_local; // create new local variable
5440 int lhs_opt_flags; // for when destination is an option
5441 int lhs_vimvaridx; // for when destination is a v:var
5442
5443 lvar_T lhs_local_lvar; // used for existing local destination
5444 lvar_T lhs_arg_lvar; // used for argument destination
5445 lvar_T *lhs_lvar; // points to destination lvar
5446 int lhs_scriptvar_sid;
5447 int lhs_scriptvar_idx;
5448
5449 int lhs_has_type; // type was specified
5450 type_T *lhs_type;
5451 type_T *lhs_member_type;
5452} lhs_T;
5453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005455 * Generate the load instruction for "name".
5456 */
5457 static void
5458generate_loadvar(
5459 cctx_T *cctx,
5460 assign_dest_T dest,
5461 char_u *name,
5462 lvar_T *lvar,
5463 type_T *type)
5464{
5465 switch (dest)
5466 {
5467 case dest_option:
5468 // TODO: check the option exists
5469 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5470 break;
5471 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005472 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5473 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5474 else
5475 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005476 break;
5477 case dest_buffer:
5478 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5479 break;
5480 case dest_window:
5481 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5482 break;
5483 case dest_tab:
5484 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5485 break;
5486 case dest_script:
5487 compile_load_scriptvar(cctx,
5488 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5489 break;
5490 case dest_env:
5491 // Include $ in the name here
5492 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5493 break;
5494 case dest_reg:
5495 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5496 break;
5497 case dest_vimvar:
5498 generate_LOADV(cctx, name + 2, TRUE);
5499 break;
5500 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005501 if (lvar->lv_from_outer > 0)
5502 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5503 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005504 else
5505 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5506 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005507 case dest_expr:
5508 // list or dict value should already be on the stack.
5509 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005510 }
5511}
5512
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005513/*
5514 * Skip over "[expr]" or ".member".
5515 * Does not check for any errors.
5516 */
5517 static char_u *
5518skip_index(char_u *start)
5519{
5520 char_u *p = start;
5521
5522 if (*p == '[')
5523 {
5524 p = skipwhite(p + 1);
5525 (void)skip_expr(&p, NULL);
5526 p = skipwhite(p);
5527 if (*p == ']')
5528 return p + 1;
5529 return p;
5530 }
5531 // if (*p == '.')
5532 return to_name_end(p + 1, TRUE);
5533}
5534
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005535 void
5536vim9_declare_error(char_u *name)
5537{
5538 char *scope = "";
5539
5540 switch (*name)
5541 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005542 case 'g': scope = _("global"); break;
5543 case 'b': scope = _("buffer"); break;
5544 case 'w': scope = _("window"); break;
5545 case 't': scope = _("tab"); break;
5546 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005547 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005548 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005549 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005550 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005551 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005552 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005553 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005554 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005555 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005556}
5557
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005558/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005559 * For one assignment figure out the type of destination. Return it in "dest".
5560 * When not recognized "dest" is not set.
5561 * For an option "opt_flags" is set.
5562 * For a v:var "vimvaridx" is set.
5563 * "type" is set to the destination type if known, unchanted otherwise.
5564 * Return FAIL if an error message was given.
5565 */
5566 static int
5567get_var_dest(
5568 char_u *name,
5569 assign_dest_T *dest,
5570 int cmdidx,
5571 int *opt_flags,
5572 int *vimvaridx,
5573 type_T **type,
5574 cctx_T *cctx)
5575{
5576 char_u *p;
5577
5578 if (*name == '&')
5579 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005580 int cc;
5581 long numval;
5582 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005583
5584 *dest = dest_option;
5585 if (cmdidx == CMD_final || cmdidx == CMD_const)
5586 {
5587 emsg(_(e_const_option));
5588 return FAIL;
5589 }
5590 p = name;
5591 p = find_option_end(&p, opt_flags);
5592 if (p == NULL)
5593 {
5594 // cannot happen?
5595 emsg(_(e_letunexp));
5596 return FAIL;
5597 }
5598 cc = *p;
5599 *p = NUL;
5600 opt_type = get_option_value(skip_option_env_lead(name),
5601 &numval, NULL, *opt_flags);
5602 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005603 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005604 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005605 case gov_unknown:
5606 semsg(_(e_unknown_option), name);
5607 return FAIL;
5608 case gov_string:
5609 case gov_hidden_string:
5610 *type = &t_string;
5611 break;
5612 case gov_bool:
5613 case gov_hidden_bool:
5614 *type = &t_bool;
5615 break;
5616 case gov_number:
5617 case gov_hidden_number:
5618 *type = &t_number;
5619 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005621 }
5622 else if (*name == '$')
5623 {
5624 *dest = dest_env;
5625 *type = &t_string;
5626 }
5627 else if (*name == '@')
5628 {
5629 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5630 {
5631 emsg_invreg(name[1]);
5632 return FAIL;
5633 }
5634 *dest = dest_reg;
5635 *type = &t_string;
5636 }
5637 else if (STRNCMP(name, "g:", 2) == 0)
5638 {
5639 *dest = dest_global;
5640 }
5641 else if (STRNCMP(name, "b:", 2) == 0)
5642 {
5643 *dest = dest_buffer;
5644 }
5645 else if (STRNCMP(name, "w:", 2) == 0)
5646 {
5647 *dest = dest_window;
5648 }
5649 else if (STRNCMP(name, "t:", 2) == 0)
5650 {
5651 *dest = dest_tab;
5652 }
5653 else if (STRNCMP(name, "v:", 2) == 0)
5654 {
5655 typval_T *vtv;
5656 int di_flags;
5657
5658 *vimvaridx = find_vim_var(name + 2, &di_flags);
5659 if (*vimvaridx < 0)
5660 {
5661 semsg(_(e_variable_not_found_str), name);
5662 return FAIL;
5663 }
5664 // We use the current value of "sandbox" here, is that OK?
5665 if (var_check_ro(di_flags, name, FALSE))
5666 return FAIL;
5667 *dest = dest_vimvar;
5668 vtv = get_vim_var_tv(*vimvaridx);
5669 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5670 }
5671 return OK;
5672}
5673
5674/*
5675 * Generate a STORE instruction for "dest", not being "dest_local".
5676 * Return FAIL when out of memory.
5677 */
5678 static int
5679generate_store_var(
5680 cctx_T *cctx,
5681 assign_dest_T dest,
5682 int opt_flags,
5683 int vimvaridx,
5684 int scriptvar_idx,
5685 int scriptvar_sid,
5686 type_T *type,
5687 char_u *name)
5688{
5689 switch (dest)
5690 {
5691 case dest_option:
5692 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5693 opt_flags);
5694 case dest_global:
5695 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005696 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5697 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005698 case dest_buffer:
5699 // include b: with the name, easier to execute that way
5700 return generate_STORE(cctx, ISN_STOREB, 0, name);
5701 case dest_window:
5702 // include w: with the name, easier to execute that way
5703 return generate_STORE(cctx, ISN_STOREW, 0, name);
5704 case dest_tab:
5705 // include t: with the name, easier to execute that way
5706 return generate_STORE(cctx, ISN_STORET, 0, name);
5707 case dest_env:
5708 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5709 case dest_reg:
5710 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5711 case dest_vimvar:
5712 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5713 case dest_script:
5714 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005715 // "s:" may be included in the name.
5716 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005717 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005718 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5719 scriptvar_sid, scriptvar_idx, type);
5720 case dest_local:
5721 case dest_expr:
5722 // cannot happen
5723 break;
5724 }
5725 return FAIL;
5726}
5727
Bram Moolenaar752fc692021-01-04 21:57:11 +01005728 static int
5729is_decl_command(int cmdidx)
5730{
5731 return cmdidx == CMD_let || cmdidx == CMD_var
5732 || cmdidx == CMD_final || cmdidx == CMD_const;
5733}
5734
5735/*
5736 * Figure out the LHS type and other properties for an assignment or one item
5737 * of ":unlet" with an index.
5738 * Returns OK or FAIL.
5739 */
5740 static int
5741compile_lhs(
5742 char_u *var_start,
5743 lhs_T *lhs,
5744 int cmdidx,
5745 int heredoc,
5746 int oplen,
5747 cctx_T *cctx)
5748{
5749 char_u *var_end;
5750 int is_decl = is_decl_command(cmdidx);
5751
5752 CLEAR_POINTER(lhs);
5753 lhs->lhs_dest = dest_local;
5754 lhs->lhs_vimvaridx = -1;
5755 lhs->lhs_scriptvar_idx = -1;
5756
5757 // "dest_end" is the end of the destination, including "[expr]" or
5758 // ".name".
5759 // "var_end" is the end of the variable/option/etc. name.
5760 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5761 if (*var_start == '@')
5762 var_end = var_start + 2;
5763 else
5764 {
5765 // skip over the leading "&", "&l:", "&g:" and "$"
5766 var_end = skip_option_env_lead(var_start);
5767 var_end = to_name_end(var_end, TRUE);
5768 }
5769
5770 // "a: type" is declaring variable "a" with a type, not dict "a:".
5771 if (is_decl && lhs->lhs_dest_end == var_start + 2
5772 && lhs->lhs_dest_end[-1] == ':')
5773 --lhs->lhs_dest_end;
5774 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5775 --var_end;
5776
5777 // compute the length of the destination without "[expr]" or ".name"
5778 lhs->lhs_varlen = var_end - var_start;
5779 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5780 if (lhs->lhs_name == NULL)
5781 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005782
5783 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5784 // Something follows after the variable: "var[idx]" or "var.key".
5785 lhs->lhs_has_index = TRUE;
5786
Bram Moolenaar752fc692021-01-04 21:57:11 +01005787 if (heredoc)
5788 lhs->lhs_type = &t_list_string;
5789 else
5790 lhs->lhs_type = &t_any;
5791
5792 if (cctx->ctx_skip != SKIP_YES)
5793 {
5794 int declare_error = FALSE;
5795
5796 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5797 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5798 &lhs->lhs_type, cctx) == FAIL)
5799 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005800 if (lhs->lhs_dest != dest_local
5801 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005802 {
5803 // Specific kind of variable recognized.
5804 declare_error = is_decl;
5805 }
5806 else
5807 {
5808 int idx;
5809
5810 // No specific kind of variable recognized, just a name.
5811 for (idx = 0; reserved[idx] != NULL; ++idx)
5812 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5813 {
5814 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5815 return FAIL;
5816 }
5817
5818
5819 if (lookup_local(var_start, lhs->lhs_varlen,
5820 &lhs->lhs_local_lvar, cctx) == OK)
5821 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5822 else
5823 {
5824 CLEAR_FIELD(lhs->lhs_arg_lvar);
5825 if (arg_exists(var_start, lhs->lhs_varlen,
5826 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5827 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5828 {
5829 if (is_decl)
5830 {
5831 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5832 return FAIL;
5833 }
5834 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5835 }
5836 }
5837 if (lhs->lhs_lvar != NULL)
5838 {
5839 if (is_decl)
5840 {
5841 semsg(_(e_variable_already_declared), lhs->lhs_name);
5842 return FAIL;
5843 }
5844 }
5845 else
5846 {
5847 int script_namespace = lhs->lhs_varlen > 1
5848 && STRNCMP(var_start, "s:", 2) == 0;
5849 int script_var = (script_namespace
5850 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005851 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005852 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005853 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005854 imported_T *import =
5855 find_imported(var_start, lhs->lhs_varlen, cctx);
5856
5857 if (script_namespace || script_var || import != NULL)
5858 {
5859 char_u *rawname = lhs->lhs_name
5860 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5861
5862 if (is_decl)
5863 {
5864 if (script_namespace)
5865 semsg(_(e_cannot_declare_script_variable_in_function),
5866 lhs->lhs_name);
5867 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005868 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005869 lhs->lhs_name);
5870 return FAIL;
5871 }
5872 else if (cctx->ctx_ufunc->uf_script_ctx_version
5873 == SCRIPT_VERSION_VIM9
5874 && script_namespace
5875 && !script_var && import == NULL)
5876 {
5877 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5878 return FAIL;
5879 }
5880
5881 lhs->lhs_dest = dest_script;
5882
5883 // existing script-local variables should have a type
5884 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5885 if (import != NULL)
5886 lhs->lhs_scriptvar_sid = import->imp_sid;
5887 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5888 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005889 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005890 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005891 lhs->lhs_scriptvar_sid, rawname,
5892 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5893 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005894 if (lhs->lhs_scriptvar_idx >= 0)
5895 {
5896 scriptitem_T *si = SCRIPT_ITEM(
5897 lhs->lhs_scriptvar_sid);
5898 svar_T *sv =
5899 ((svar_T *)si->sn_var_vals.ga_data)
5900 + lhs->lhs_scriptvar_idx;
5901 lhs->lhs_type = sv->sv_type;
5902 }
5903 }
5904 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005905 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 == FAIL)
5907 return FAIL;
5908 }
5909 }
5910
5911 if (declare_error)
5912 {
5913 vim9_declare_error(lhs->lhs_name);
5914 return FAIL;
5915 }
5916 }
5917
5918 // handle "a:name" as a name, not index "name" on "a"
5919 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5920 var_end = lhs->lhs_dest_end;
5921
5922 if (lhs->lhs_dest != dest_option)
5923 {
5924 if (is_decl && *var_end == ':')
5925 {
5926 char_u *p;
5927
5928 // parse optional type: "let var: type = expr"
5929 if (!VIM_ISWHITE(var_end[1]))
5930 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005931 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 return FAIL;
5933 }
5934 p = skipwhite(var_end + 1);
5935 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5936 if (lhs->lhs_type == NULL)
5937 return FAIL;
5938 lhs->lhs_has_type = TRUE;
5939 }
5940 else if (lhs->lhs_lvar != NULL)
5941 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5942 }
5943
Bram Moolenaare42939a2021-04-05 17:11:17 +02005944 if (oplen == 3 && !heredoc
5945 && lhs->lhs_dest != dest_global
5946 && !lhs->lhs_has_index
5947 && lhs->lhs_type->tt_type != VAR_STRING
5948 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005949 {
5950 emsg(_(e_can_only_concatenate_to_string));
5951 return FAIL;
5952 }
5953
5954 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5955 && cctx->ctx_skip != SKIP_YES)
5956 {
5957 if (oplen > 1 && !heredoc)
5958 {
5959 // +=, /=, etc. require an existing variable
5960 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5961 return FAIL;
5962 }
5963 if (!is_decl)
5964 {
5965 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5966 return FAIL;
5967 }
5968
Bram Moolenaar3f327882021-03-17 20:56:38 +01005969 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005970 if ((lhs->lhs_type->tt_type == VAR_FUNC
5971 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005972 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005973 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005974
5975 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005976 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5977 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5978 if (lhs->lhs_lvar == NULL)
5979 return FAIL;
5980 lhs->lhs_new_local = TRUE;
5981 }
5982
5983 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005984 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005985 {
5986 // Something follows after the variable: "var[idx]" or "var.key".
5987 // TODO: should we also handle "->func()" here?
5988 if (is_decl)
5989 {
5990 emsg(_(e_cannot_use_index_when_declaring_variable));
5991 return FAIL;
5992 }
5993
5994 if (var_start[lhs->lhs_varlen] == '['
5995 || var_start[lhs->lhs_varlen] == '.')
5996 {
5997 char_u *after = var_start + lhs->lhs_varlen;
5998 char_u *p;
5999
6000 // Only the last index is used below, if there are others
6001 // before it generate code for the expression. Thus for
6002 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6003 for (;;)
6004 {
6005 p = skip_index(after);
6006 if (*p != '[' && *p != '.')
6007 break;
6008 after = p;
6009 }
6010 if (after > var_start + lhs->lhs_varlen)
6011 {
6012 lhs->lhs_varlen = after - var_start;
6013 lhs->lhs_dest = dest_expr;
6014 // We don't know the type before evaluating the expression,
6015 // use "any" until then.
6016 lhs->lhs_type = &t_any;
6017 }
6018
Bram Moolenaar752fc692021-01-04 21:57:11 +01006019 if (lhs->lhs_type->tt_member == NULL)
6020 lhs->lhs_member_type = &t_any;
6021 else
6022 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6023 }
6024 else
6025 {
6026 semsg("Not supported yet: %s", var_start);
6027 return FAIL;
6028 }
6029 }
6030 return OK;
6031}
6032
6033/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006034 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6035 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006036 */
6037 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006038compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006039 char_u *var_start,
6040 lhs_T *lhs,
6041 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006042 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006043 cctx_T *cctx)
6044{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006045 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006046 char_u *p;
6047 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048
Bram Moolenaar752fc692021-01-04 21:57:11 +01006049 p = var_start + varlen;
6050 if (*p == '[')
6051 {
6052 p = skipwhite(p + 1);
6053 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006054
6055 if (r == OK && *skipwhite(p) == ':')
6056 {
6057 // unlet var[idx : idx]
6058 if (is_assign)
6059 {
6060 semsg(_(e_cannot_use_range_with_assignment_str), p);
6061 return FAIL;
6062 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006063 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006064 p = skipwhite(p);
6065 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6066 {
6067 semsg(_(e_white_space_required_before_and_after_str_at_str),
6068 ":", p);
6069 return FAIL;
6070 }
6071 p = skipwhite(p + 1);
6072 r = compile_expr0(&p, cctx);
6073 }
6074
Bram Moolenaar752fc692021-01-04 21:57:11 +01006075 if (r == OK && *skipwhite(p) != ']')
6076 {
6077 // this should not happen
6078 emsg(_(e_missbrac));
6079 r = FAIL;
6080 }
6081 }
6082 else // if (*p == '.')
6083 {
6084 char_u *key_end = to_name_end(p + 1, TRUE);
6085 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6086
6087 r = generate_PUSHS(cctx, key);
6088 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006089 return r;
6090}
6091
6092/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006093 * For a LHS with an index, load the variable to be indexed.
6094 */
6095 static int
6096compile_load_lhs(
6097 lhs_T *lhs,
6098 char_u *var_start,
6099 type_T *rhs_type,
6100 cctx_T *cctx)
6101{
6102 if (lhs->lhs_dest == dest_expr)
6103 {
6104 size_t varlen = lhs->lhs_varlen;
6105 int c = var_start[varlen];
6106 char_u *p = var_start;
6107 garray_T *stack = &cctx->ctx_type_stack;
6108
6109 // Evaluate "ll[expr]" of "ll[expr][idx]"
6110 var_start[varlen] = NUL;
6111 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6112 {
6113 // this should not happen
6114 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006115 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006116 return FAIL;
6117 }
6118 var_start[varlen] = c;
6119
6120 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6121 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6122 // now we can properly check the type
6123 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6124 && rhs_type != &t_void
6125 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6126 FALSE, FALSE) == FAIL)
6127 return FAIL;
6128 }
6129 else
6130 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6131 lhs->lhs_lvar, lhs->lhs_type);
6132 return OK;
6133}
6134
6135/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006136 * Assignment to a list or dict member, or ":unlet" for the item, using the
6137 * information in "lhs".
6138 * Returns OK or FAIL.
6139 */
6140 static int
6141compile_assign_unlet(
6142 char_u *var_start,
6143 lhs_T *lhs,
6144 int is_assign,
6145 type_T *rhs_type,
6146 cctx_T *cctx)
6147{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006148 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006149 garray_T *stack = &cctx->ctx_type_stack;
6150 int range = FALSE;
6151
6152 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006153 return FAIL;
6154
6155 if (lhs->lhs_type == &t_any)
6156 {
6157 // Index on variable of unknown type: check at runtime.
6158 dest_type = VAR_ANY;
6159 }
6160 else
6161 {
6162 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006163 if (dest_type == VAR_DICT && range)
6164 {
6165 emsg(e_cannot_use_range_with_dictionary);
6166 return FAIL;
6167 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006168 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6169 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006170 if (dest_type == VAR_LIST)
6171 {
6172 if (range
6173 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006174 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006175 return FAIL;
6176 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6177 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6178 return FAIL;
6179 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 }
6181
6182 // Load the dict or list. On the stack we then have:
6183 // - value (for assignment, not for :unlet)
6184 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006185 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006186 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006187 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6188 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006189
6190 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6191 {
6192 if (is_assign)
6193 {
6194 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6195
6196 if (isn == NULL)
6197 return FAIL;
6198 isn->isn_arg.vartype = dest_type;
6199 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006200 else if (range)
6201 {
6202 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6203 return FAIL;
6204 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205 else
6206 {
6207 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6208 return FAIL;
6209 }
6210 }
6211 else
6212 {
6213 emsg(_(e_indexable_type_required));
6214 return FAIL;
6215 }
6216
6217 return OK;
6218}
6219
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006220/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006222 * "let name"
6223 * "var name = expr"
6224 * "final name = expr"
6225 * "const name = expr"
6226 * "name = expr"
6227 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006228 * Return NULL for an error.
6229 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 */
6231 static char_u *
6232compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6233{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006234 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006235 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006236 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006237 char_u *ret = NULL;
6238 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006239 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006242 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 int oplen = 0;
6245 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006246 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006248 int is_decl = is_decl_command(cmdidx);
6249 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006250 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006252 // Skip over the "var" or "[var, var]" to get to any "=".
6253 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6254 if (p == NULL)
6255 return *arg == '[' ? arg : NULL;
6256
6257 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006259 // TODO: should we allow this, and figure out type inference from list
6260 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006261 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006262 return NULL;
6263 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 sp = p;
6267 p = skipwhite(p);
6268 op = p;
6269 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006270
6271 if (var_count > 0 && oplen == 0)
6272 // can be something like "[1, 2]->func()"
6273 return arg;
6274
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006275 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006276 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006277 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006278 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006279 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 if (heredoc)
6282 {
6283 list_T *l;
6284 listitem_T *li;
6285
6286 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006287 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006289 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006290 if (l == NULL)
6291 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292
Bram Moolenaar078269b2020-09-21 20:35:55 +02006293 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006295 // Push each line and the create the list.
6296 FOR_ALL_LIST_ITEMS(l, li)
6297 {
6298 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6299 li->li_tv.vval.v_string = NULL;
6300 }
6301 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 list_free(l);
6304 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006305 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006307 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006309 char_u *wp;
6310
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 // for "[var, var] = expr" evaluate the expression here, loop over the
6312 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006313 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006314
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006315 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006316 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006317 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006318 if (compile_expr0(&p, cctx) == FAIL)
6319 return NULL;
6320 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006322 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006324 type_T *stacktype;
6325
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006326 stacktype = stack->ga_len == 0 ? &t_void
6327 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006328 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006330 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006331 goto theend;
6332 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006333 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006334 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006335 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006336 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006337 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6338 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006339 if (stacktype->tt_member != NULL)
6340 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006341 }
6342 }
6343
6344 /*
6345 * Loop over variables in "[var, var] = expr".
6346 * For "var = expr" and "let var: type" this is done only once.
6347 */
6348 if (var_count > 0)
6349 var_start = skipwhite(arg + 1); // skip over the "["
6350 else
6351 var_start = arg;
6352 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6353 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006354 int instr_count = -1;
6355
Bram Moolenaar752fc692021-01-04 21:57:11 +01006356 vim_free(lhs.lhs_name);
6357
6358 /*
6359 * Figure out the LHS type and other properties.
6360 */
6361 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6362 goto theend;
6363
6364 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006365 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006367 goto theend;
6368 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006369 if (!is_decl && lhs.lhs_lvar != NULL
6370 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006371 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006373 goto theend;
6374 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006375
6376 if (!heredoc)
6377 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006378 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006379 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006380 if (oplen > 0 && var_count == 0)
6381 {
6382 // skip over the "=" and the expression
6383 p = skipwhite(op + oplen);
6384 compile_expr0(&p, cctx);
6385 }
6386 }
6387 else if (oplen > 0)
6388 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006389 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006390 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006391
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392 // For "var = expr" evaluate the expression.
6393 if (var_count == 0)
6394 {
6395 int r;
6396
6397 // for "+=", "*=", "..=" etc. first load the current value
6398 if (*op != '=')
6399 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006400 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006401
Bram Moolenaar752fc692021-01-04 21:57:11 +01006402 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006403 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006404 int range = FALSE;
6405
6406 // Get member from list or dict. First compile the
6407 // index value.
6408 if (compile_assign_index(var_start, &lhs,
6409 TRUE, &range, cctx) == FAIL)
6410 goto theend;
6411
6412 // Get the member.
6413 if (compile_member(FALSE, cctx) == FAIL)
6414 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006415 }
6416 }
6417
6418 // Compile the expression. Temporarily hide the new local
6419 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006421 --cctx->ctx_locals.ga_len;
6422 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006423 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006424 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006425 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006427 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006428 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006429 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006430 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006431 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006432 ++cctx->ctx_locals.ga_len;
6433 if (r == FAIL)
6434 goto theend;
6435 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006436 else if (semicolon && var_idx == var_count - 1)
6437 {
6438 // For "[var; var] = expr" get the rest of the list
6439 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6440 goto theend;
6441 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006442 else
6443 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006444 // For "[var, var] = expr" get the "var_idx" item from the
6445 // list.
6446 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006447 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006448 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006449
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006450 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006451 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006453 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006454 if ((rhs_type->tt_type == VAR_FUNC
6455 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006456 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006457 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006458 goto theend;
6459
Bram Moolenaar752fc692021-01-04 21:57:11 +01006460 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006461 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006462 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006463 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006464 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006465 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006466 }
6467 else
6468 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006469 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006470 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006471 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006473 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006475 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006476 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006477 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006479 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006480 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006481 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006482 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006484
Bram Moolenaar77709b12021-04-03 21:01:01 +02006485 // Without operator check type here, otherwise below.
6486 // Use the line number of the assignment.
6487 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 if (lhs.lhs_has_index)
6489 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006490 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006491 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006492 goto theend;
6493 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006494 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006496 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006497 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006499 else if (cmdidx == CMD_final)
6500 {
6501 emsg(_(e_final_requires_a_value));
6502 goto theend;
6503 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006504 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006506 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006507 goto theend;
6508 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006510 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006511 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006512 goto theend;
6513 }
6514 else
6515 {
6516 // variables are always initialized
6517 if (ga_grow(instr, 1) == FAIL)
6518 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006519 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006520 {
6521 case VAR_BOOL:
6522 generate_PUSHBOOL(cctx, VVAL_FALSE);
6523 break;
6524 case VAR_FLOAT:
6525#ifdef FEAT_FLOAT
6526 generate_PUSHF(cctx, 0.0);
6527#endif
6528 break;
6529 case VAR_STRING:
6530 generate_PUSHS(cctx, NULL);
6531 break;
6532 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006533 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006534 break;
6535 case VAR_FUNC:
6536 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6537 break;
6538 case VAR_LIST:
6539 generate_NEWLIST(cctx, 0);
6540 break;
6541 case VAR_DICT:
6542 generate_NEWDICT(cctx, 0);
6543 break;
6544 case VAR_JOB:
6545 generate_PUSHJOB(cctx, NULL);
6546 break;
6547 case VAR_CHANNEL:
6548 generate_PUSHCHANNEL(cctx, NULL);
6549 break;
6550 case VAR_NUMBER:
6551 case VAR_UNKNOWN:
6552 case VAR_ANY:
6553 case VAR_PARTIAL:
6554 case VAR_VOID:
6555 case VAR_SPECIAL: // cannot happen
6556 generate_PUSHNR(cctx, 0);
6557 break;
6558 }
6559 }
6560 if (var_count == 0)
6561 end = p;
6562 }
6563
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006564 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006565 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006566 break;
6567
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006568 if (oplen > 0 && *op != '=')
6569 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006570 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006571 type_T *stacktype;
6572
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006573 if (*op == '.')
6574 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006575 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006576 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006577 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006578 if (
6579#ifdef FEAT_FLOAT
6580 // If variable is float operation with number is OK.
6581 !(expected == &t_float && stacktype == &t_number) &&
6582#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006583 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006584 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006585 goto theend;
6586
6587 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006588 {
6589 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6590 goto theend;
6591 }
6592 else if (*op == '+')
6593 {
6594 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006595 operator_type(lhs.lhs_member_type, stacktype),
6596 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006597 goto theend;
6598 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006599 else if (generate_two_op(cctx, op) == FAIL)
6600 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602
Bram Moolenaar752fc692021-01-04 21:57:11 +01006603 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006604 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006605 // Use the info in "lhs" to store the value at the index in the
6606 // list or dict.
6607 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6608 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006609 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006610 }
6611 else
6612 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006613 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006614 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006615 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006616 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006617 generate_LOCKCONST(cctx);
6618
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006619 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 && (lhs.lhs_type->tt_type == VAR_DICT
6621 || lhs.lhs_type->tt_type == VAR_LIST)
6622 && lhs.lhs_type->tt_member != NULL
6623 && lhs.lhs_type->tt_member != &t_any
6624 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006625 // Set the type in the list or dict, so that it can be checked,
6626 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006627 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006628
Bram Moolenaar752fc692021-01-04 21:57:11 +01006629 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006631 if (generate_store_var(cctx, lhs.lhs_dest,
6632 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6633 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6634 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006635 goto theend;
6636 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006637 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006638 {
6639 isn_T *isn = ((isn_T *)instr->ga_data)
6640 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006641
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006642 // optimization: turn "var = 123" from ISN_PUSHNR +
6643 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006644 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006645 && instr->ga_len == instr_count + 1
6646 && isn->isn_type == ISN_PUSHNR)
6647 {
6648 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006649
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006650 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006651 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006652 isn->isn_arg.storenr.stnr_val = val;
6653 if (stack->ga_len > 0)
6654 --stack->ga_len;
6655 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006656 else if (lhs.lhs_lvar->lv_from_outer > 0)
6657 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6658 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006659 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006660 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006661 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006662 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006663
6664 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006666 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006667
6668 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006669 if (var_count > 0 && !semicolon)
6670 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006671 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006672 goto theend;
6673 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006674
Bram Moolenaarb2097502020-07-19 17:17:02 +02006675 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006676
6677theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006678 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006679 return ret;
6680}
6681
6682/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006683 * Check for an assignment at "eap->cmd", compile it if found.
6684 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6685 */
6686 static int
6687may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6688{
6689 char_u *pskip;
6690 char_u *p;
6691
6692 // Assuming the command starts with a variable or function name,
6693 // find what follows.
6694 // Skip over "var.member", "var[idx]" and the like.
6695 // Also "&opt = val", "$ENV = val" and "@r = val".
6696 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6697 ? eap->cmd + 1 : eap->cmd;
6698 p = to_name_end(pskip, TRUE);
6699 if (p > eap->cmd && *p != NUL)
6700 {
6701 char_u *var_end;
6702 int oplen;
6703 int heredoc;
6704
6705 if (eap->cmd[0] == '@')
6706 var_end = eap->cmd + 2;
6707 else
6708 var_end = find_name_end(pskip, NULL, NULL,
6709 FNE_CHECK_START | FNE_INCL_BR);
6710 oplen = assignment_len(skipwhite(var_end), &heredoc);
6711 if (oplen > 0)
6712 {
6713 size_t len = p - eap->cmd;
6714
6715 // Recognize an assignment if we recognize the variable
6716 // name:
6717 // "g:var = expr"
6718 // "local = expr" where "local" is a local var.
6719 // "script = expr" where "script" is a script-local var.
6720 // "import = expr" where "import" is an imported var
6721 // "&opt = expr"
6722 // "$ENV = expr"
6723 // "@r = expr"
6724 if (*eap->cmd == '&'
6725 || *eap->cmd == '$'
6726 || *eap->cmd == '@'
6727 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006728 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006729 {
6730 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6731 if (*line == NULL || *line == eap->cmd)
6732 return FAIL;
6733 return OK;
6734 }
6735 }
6736 }
6737
6738 if (*eap->cmd == '[')
6739 {
6740 // [var, var] = expr
6741 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6742 if (*line == NULL)
6743 return FAIL;
6744 if (*line != eap->cmd)
6745 return OK;
6746 }
6747 return NOTDONE;
6748}
6749
6750/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006751 * Check if "name" can be "unlet".
6752 */
6753 int
6754check_vim9_unlet(char_u *name)
6755{
6756 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6757 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006758 // "unlet s:var" is allowed in legacy script.
6759 if (*name == 's' && !script_is_vim9())
6760 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006761 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006762 return FAIL;
6763 }
6764 return OK;
6765}
6766
6767/*
6768 * Callback passed to ex_unletlock().
6769 */
6770 static int
6771compile_unlet(
6772 lval_T *lvp,
6773 char_u *name_end,
6774 exarg_T *eap,
6775 int deep UNUSED,
6776 void *coookie)
6777{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006778 cctx_T *cctx = coookie;
6779 char_u *p = lvp->ll_name;
6780 int cc = *name_end;
6781 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006782
Bram Moolenaar752fc692021-01-04 21:57:11 +01006783 if (cctx->ctx_skip == SKIP_YES)
6784 return OK;
6785
6786 *name_end = NUL;
6787 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006788 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006789 // :unlet $ENV_VAR
6790 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6791 }
6792 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6793 {
6794 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006795
Bram Moolenaar752fc692021-01-04 21:57:11 +01006796 // This is similar to assigning: lookup the list/dict, compile the
6797 // idx/key. Then instead of storing the value unlet the item.
6798 // unlet {list}[idx]
6799 // unlet {dict}[key] dict.key
6800 //
6801 // Figure out the LHS type and other properties.
6802 //
6803 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6804
6805 // : unlet an indexed item
6806 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006807 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006808 iemsg("called compile_lhs() without an index");
6809 ret = FAIL;
6810 }
6811 else
6812 {
6813 // Use the info in "lhs" to unlet the item at the index in the
6814 // list or dict.
6815 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006816 }
6817
Bram Moolenaar752fc692021-01-04 21:57:11 +01006818 vim_free(lhs.lhs_name);
6819 }
6820 else if (check_vim9_unlet(p) == FAIL)
6821 {
6822 ret = FAIL;
6823 }
6824 else
6825 {
6826 // Normal name. Only supports g:, w:, t: and b: namespaces.
6827 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006828 }
6829
Bram Moolenaar752fc692021-01-04 21:57:11 +01006830 *name_end = cc;
6831 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006832}
6833
6834/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006835 * Callback passed to ex_unletlock().
6836 */
6837 static int
6838compile_lock_unlock(
6839 lval_T *lvp,
6840 char_u *name_end,
6841 exarg_T *eap,
6842 int deep UNUSED,
6843 void *coookie)
6844{
6845 cctx_T *cctx = coookie;
6846 int cc = *name_end;
6847 char_u *p = lvp->ll_name;
6848 int ret = OK;
6849 size_t len;
6850 char_u *buf;
6851
6852 if (cctx->ctx_skip == SKIP_YES)
6853 return OK;
6854
6855 // Cannot use :lockvar and :unlockvar on local variables.
6856 if (p[1] != ':')
6857 {
6858 char_u *end = skip_var_one(p, FALSE);
6859
6860 if (lookup_local(p, end - p, NULL, cctx) == OK)
6861 {
6862 emsg(_(e_cannot_lock_unlock_local_variable));
6863 return FAIL;
6864 }
6865 }
6866
6867 // Checking is done at runtime.
6868 *name_end = NUL;
6869 len = name_end - p + 20;
6870 buf = alloc(len);
6871 if (buf == NULL)
6872 ret = FAIL;
6873 else
6874 {
6875 vim_snprintf((char *)buf, len, "%s %s",
6876 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6877 p);
6878 ret = generate_EXEC(cctx, buf);
6879
6880 vim_free(buf);
6881 *name_end = cc;
6882 }
6883 return ret;
6884}
6885
6886/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006887 * compile "unlet var", "lock var" and "unlock var"
6888 * "arg" points to "var".
6889 */
6890 static char_u *
6891compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6892{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006893 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6894 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6895 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006896 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6897}
6898
6899/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006900 * Compile an :import command.
6901 */
6902 static char_u *
6903compile_import(char_u *arg, cctx_T *cctx)
6904{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006905 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906}
6907
6908/*
6909 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6910 */
6911 static int
6912compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6913{
6914 garray_T *instr = &cctx->ctx_instr;
6915 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6916
6917 if (endlabel == NULL)
6918 return FAIL;
6919 endlabel->el_next = *el;
6920 *el = endlabel;
6921 endlabel->el_end_label = instr->ga_len;
6922
6923 generate_JUMP(cctx, when, 0);
6924 return OK;
6925}
6926
6927 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006928compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929{
6930 garray_T *instr = &cctx->ctx_instr;
6931
6932 while (*el != NULL)
6933 {
6934 endlabel_T *cur = (*el);
6935 isn_T *isn;
6936
6937 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006938 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006939 *el = cur->el_next;
6940 vim_free(cur);
6941 }
6942}
6943
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006944 static void
6945compile_free_jump_to_end(endlabel_T **el)
6946{
6947 while (*el != NULL)
6948 {
6949 endlabel_T *cur = (*el);
6950
6951 *el = cur->el_next;
6952 vim_free(cur);
6953 }
6954}
6955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956/*
6957 * Create a new scope and set up the generic items.
6958 */
6959 static scope_T *
6960new_scope(cctx_T *cctx, scopetype_T type)
6961{
6962 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6963
6964 if (scope == NULL)
6965 return NULL;
6966 scope->se_outer = cctx->ctx_scope;
6967 cctx->ctx_scope = scope;
6968 scope->se_type = type;
6969 scope->se_local_count = cctx->ctx_locals.ga_len;
6970 return scope;
6971}
6972
6973/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006974 * Free the current scope and go back to the outer scope.
6975 */
6976 static void
6977drop_scope(cctx_T *cctx)
6978{
6979 scope_T *scope = cctx->ctx_scope;
6980
6981 if (scope == NULL)
6982 {
6983 iemsg("calling drop_scope() without a scope");
6984 return;
6985 }
6986 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006987 switch (scope->se_type)
6988 {
6989 case IF_SCOPE:
6990 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6991 case FOR_SCOPE:
6992 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6993 case WHILE_SCOPE:
6994 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6995 case TRY_SCOPE:
6996 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6997 case NO_SCOPE:
6998 case BLOCK_SCOPE:
6999 break;
7000 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007001 vim_free(scope);
7002}
7003
7004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 * compile "if expr"
7006 *
7007 * "if expr" Produces instructions:
7008 * EVAL expr Push result of "expr"
7009 * JUMP_IF_FALSE end
7010 * ... body ...
7011 * end:
7012 *
7013 * "if expr | else" Produces instructions:
7014 * EVAL expr Push result of "expr"
7015 * JUMP_IF_FALSE else
7016 * ... body ...
7017 * JUMP_ALWAYS end
7018 * else:
7019 * ... body ...
7020 * end:
7021 *
7022 * "if expr1 | elseif expr2 | else" Produces instructions:
7023 * EVAL expr Push result of "expr"
7024 * JUMP_IF_FALSE elseif
7025 * ... body ...
7026 * JUMP_ALWAYS end
7027 * elseif:
7028 * EVAL expr Push result of "expr"
7029 * JUMP_IF_FALSE else
7030 * ... body ...
7031 * JUMP_ALWAYS end
7032 * else:
7033 * ... body ...
7034 * end:
7035 */
7036 static char_u *
7037compile_if(char_u *arg, cctx_T *cctx)
7038{
7039 char_u *p = arg;
7040 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007041 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007043 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007044 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045
Bram Moolenaara5565e42020-05-09 15:44:01 +02007046 CLEAR_FIELD(ppconst);
7047 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007048 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007049 clear_ppconst(&ppconst);
7050 return NULL;
7051 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007052 if (!ends_excmd2(arg, skipwhite(p)))
7053 {
7054 semsg(_(e_trailing_arg), p);
7055 return NULL;
7056 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007057 if (cctx->ctx_skip == SKIP_YES)
7058 clear_ppconst(&ppconst);
7059 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007060 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007061 int error = FALSE;
7062 int v;
7063
Bram Moolenaara5565e42020-05-09 15:44:01 +02007064 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007065 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007066 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007067 if (error)
7068 return NULL;
7069 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007070 }
7071 else
7072 {
7073 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007074 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007075 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007076 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007077 if (bool_on_stack(cctx) == FAIL)
7078 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007079 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080
Bram Moolenaara91a7132021-03-25 21:12:15 +01007081 // CMDMOD_REV must come before the jump
7082 generate_undo_cmdmods(cctx);
7083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 scope = new_scope(cctx, IF_SCOPE);
7085 if (scope == NULL)
7086 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007087 scope->se_skip_save = skip_save;
7088 // "is_had_return" will be reset if any block does not end in :return
7089 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007091 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007092 {
7093 // "where" is set when ":elseif", "else" or ":endif" is found
7094 scope->se_u.se_if.is_if_label = instr->ga_len;
7095 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7096 }
7097 else
7098 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099
Bram Moolenaarced68a02021-01-24 17:53:47 +01007100#ifdef FEAT_PROFILE
7101 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7102 && skip_save != SKIP_YES)
7103 {
7104 // generated a profile start, need to generate a profile end, since it
7105 // won't be done after returning
7106 cctx->ctx_skip = SKIP_NOT;
7107 generate_instr(cctx, ISN_PROF_END);
7108 cctx->ctx_skip = SKIP_YES;
7109 }
7110#endif
7111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007112 return p;
7113}
7114
7115 static char_u *
7116compile_elseif(char_u *arg, cctx_T *cctx)
7117{
7118 char_u *p = arg;
7119 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007120 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007121 isn_T *isn;
7122 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007123 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007124 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125
7126 if (scope == NULL || scope->se_type != IF_SCOPE)
7127 {
7128 emsg(_(e_elseif_without_if));
7129 return NULL;
7130 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007131 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007132 if (!cctx->ctx_had_return)
7133 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007134
Bram Moolenaarced68a02021-01-24 17:53:47 +01007135 if (cctx->ctx_skip == SKIP_NOT)
7136 {
7137 // previous block was executed, this one and following will not
7138 cctx->ctx_skip = SKIP_YES;
7139 scope->se_u.se_if.is_seen_skip_not = TRUE;
7140 }
7141 if (scope->se_u.se_if.is_seen_skip_not)
7142 {
7143 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007144 // Do not count the "elseif" for profiling and cmdmod
7145 instr->ga_len = current_instr_idx(cctx);
7146
Bram Moolenaarced68a02021-01-24 17:53:47 +01007147 skip_expr_cctx(&p, cctx);
7148 return p;
7149 }
7150
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007151 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007152 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007153 int moved_cmdmod = FALSE;
7154
7155 // Move any CMDMOD instruction to after the jump
7156 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7157 {
7158 if (ga_grow(instr, 1) == FAIL)
7159 return NULL;
7160 ((isn_T *)instr->ga_data)[instr->ga_len] =
7161 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7162 --instr->ga_len;
7163 moved_cmdmod = TRUE;
7164 }
7165
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007166 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007167 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007168 return NULL;
7169 // previous "if" or "elseif" jumps here
7170 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7171 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007172 if (moved_cmdmod)
7173 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007175
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007176 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007177 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007178 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007179 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007180 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007181#ifdef FEAT_PROFILE
7182 if (cctx->ctx_profiling)
7183 {
7184 // the previous block was skipped, need to profile this line
7185 generate_instr(cctx, ISN_PROF_START);
7186 instr_count = instr->ga_len;
7187 }
7188#endif
7189 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007190 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007191 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007192 clear_ppconst(&ppconst);
7193 return NULL;
7194 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007195 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007196 if (!ends_excmd2(arg, skipwhite(p)))
7197 {
7198 semsg(_(e_trailing_arg), p);
7199 return NULL;
7200 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007201 if (scope->se_skip_save == SKIP_YES)
7202 clear_ppconst(&ppconst);
7203 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007204 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007205 int error = FALSE;
7206 int v;
7207
Bram Moolenaar7f141552020-05-09 17:35:53 +02007208 // The expression results in a constant.
7209 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007210 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7211 if (error)
7212 return NULL;
7213 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007214 clear_ppconst(&ppconst);
7215 scope->se_u.se_if.is_if_label = -1;
7216 }
7217 else
7218 {
7219 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007220 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007221 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007222 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007223 if (bool_on_stack(cctx) == FAIL)
7224 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
Bram Moolenaara91a7132021-03-25 21:12:15 +01007226 // CMDMOD_REV must come before the jump
7227 generate_undo_cmdmods(cctx);
7228
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007229 // "where" is set when ":elseif", "else" or ":endif" is found
7230 scope->se_u.se_if.is_if_label = instr->ga_len;
7231 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007233
7234 return p;
7235}
7236
7237 static char_u *
7238compile_else(char_u *arg, cctx_T *cctx)
7239{
7240 char_u *p = arg;
7241 garray_T *instr = &cctx->ctx_instr;
7242 isn_T *isn;
7243 scope_T *scope = cctx->ctx_scope;
7244
7245 if (scope == NULL || scope->se_type != IF_SCOPE)
7246 {
7247 emsg(_(e_else_without_if));
7248 return NULL;
7249 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007250 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007251 if (!cctx->ctx_had_return)
7252 scope->se_u.se_if.is_had_return = FALSE;
7253 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
Bram Moolenaarced68a02021-01-24 17:53:47 +01007255#ifdef FEAT_PROFILE
7256 if (cctx->ctx_profiling)
7257 {
7258 if (cctx->ctx_skip == SKIP_NOT
7259 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7260 .isn_type == ISN_PROF_START)
7261 // the previous block was executed, do not count "else" for profiling
7262 --instr->ga_len;
7263 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7264 {
7265 // the previous block was not executed, this one will, do count the
7266 // "else" for profiling
7267 cctx->ctx_skip = SKIP_NOT;
7268 generate_instr(cctx, ISN_PROF_END);
7269 generate_instr(cctx, ISN_PROF_START);
7270 cctx->ctx_skip = SKIP_YES;
7271 }
7272 }
7273#endif
7274
7275 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007276 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007277 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007278 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007279 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007280 if (!cctx->ctx_had_return
7281 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7282 JUMP_ALWAYS, cctx) == FAIL)
7283 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007284 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007285
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007286 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007287 {
7288 if (scope->se_u.se_if.is_if_label >= 0)
7289 {
7290 // previous "if" or "elseif" jumps here
7291 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7292 isn->isn_arg.jump.jump_where = instr->ga_len;
7293 scope->se_u.se_if.is_if_label = -1;
7294 }
7295 }
7296
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007297 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007298 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300
7301 return p;
7302}
7303
7304 static char_u *
7305compile_endif(char_u *arg, cctx_T *cctx)
7306{
7307 scope_T *scope = cctx->ctx_scope;
7308 ifscope_T *ifscope;
7309 garray_T *instr = &cctx->ctx_instr;
7310 isn_T *isn;
7311
Bram Moolenaarfa984412021-03-25 22:15:28 +01007312 if (misplaced_cmdmod(cctx))
7313 return NULL;
7314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007315 if (scope == NULL || scope->se_type != IF_SCOPE)
7316 {
7317 emsg(_(e_endif_without_if));
7318 return NULL;
7319 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007320 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007321 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007322 if (!cctx->ctx_had_return)
7323 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007325 if (scope->se_u.se_if.is_if_label >= 0)
7326 {
7327 // previous "if" or "elseif" jumps here
7328 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7329 isn->isn_arg.jump.jump_where = instr->ga_len;
7330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007332 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007333
7334#ifdef FEAT_PROFILE
7335 // even when skipping we count the endif as executed, unless the block it's
7336 // in is skipped
7337 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7338 && scope->se_skip_save != SKIP_YES)
7339 {
7340 cctx->ctx_skip = SKIP_NOT;
7341 generate_instr(cctx, ISN_PROF_START);
7342 }
7343#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007344 cctx->ctx_skip = scope->se_skip_save;
7345
7346 // If all the blocks end in :return and there is an :else then the
7347 // had_return flag is set.
7348 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007350 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 return arg;
7352}
7353
7354/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007355 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 *
7357 * Produces instructions:
7358 * PUSHNR -1
7359 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007360 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7362 * - if beyond end, jump to "end"
7363 * - otherwise get item from list and push it
7364 * STORE var Store item in "var"
7365 * ... body ...
7366 * JUMP top Jump back to repeat
7367 * end: DROP Drop the result of "expr"
7368 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007369 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7370 * UNPACK 2 Split item in 2
7371 * STORE var1 Store item in "var1"
7372 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 */
7374 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007375compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007377 char_u *arg;
7378 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007379 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007381 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007382 int var_count = 0;
7383 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 garray_T *stack = &cctx->ctx_type_stack;
7386 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007387 lvar_T *loop_lvar; // loop iteration variable
7388 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007390 type_T *item_type = &t_any;
7391 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392
Bram Moolenaar792f7862020-11-23 08:31:18 +01007393 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007394 if (p == NULL)
7395 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007396 if (var_count == 0)
7397 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398
7399 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007400 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007401 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7402 return NULL;
7403 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 {
7405 emsg(_(e_missing_in));
7406 return NULL;
7407 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007408 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007409 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7410 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 scope = new_scope(cctx, FOR_SCOPE);
7413 if (scope == NULL)
7414 return NULL;
7415
Bram Moolenaar792f7862020-11-23 08:31:18 +01007416 // Reserve a variable to store the loop iteration counter and initialize it
7417 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007418 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7419 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007420 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007421 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007422 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007424 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007425 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426
7427 // compile "expr", it remains on the stack until "endfor"
7428 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007429 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007430 {
7431 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007433 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007434 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007436 // If we know the type of "var" and it is a not a list or string we can
7437 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007439 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7440 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007442 // TODO: support Blob
7443 semsg(_(e_for_loop_on_str_not_supported),
7444 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007445 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 return NULL;
7447 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007448
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007449 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007450 {
7451 if (var_count == 1)
7452 item_type = vartype->tt_member;
7453 else if (vartype->tt_member->tt_type == VAR_LIST
7454 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7455 item_type = vartype->tt_member->tt_member;
7456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457
Bram Moolenaara91a7132021-03-25 21:12:15 +01007458 // CMDMOD_REV must come before the FOR instruction
7459 generate_undo_cmdmods(cctx);
7460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007462 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007463 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007464
Bram Moolenaar792f7862020-11-23 08:31:18 +01007465 arg = arg_start;
7466 if (var_count > 1)
7467 {
7468 generate_UNPACK(cctx, var_count, semicolon);
7469 arg = skipwhite(arg + 1); // skip white after '['
7470
7471 // the list item is replaced by a number of items
7472 if (ga_grow(stack, var_count - 1) == FAIL)
7473 {
7474 drop_scope(cctx);
7475 return NULL;
7476 }
7477 --stack->ga_len;
7478 for (idx = 0; idx < var_count; ++idx)
7479 {
7480 ((type_T **)stack->ga_data)[stack->ga_len] =
7481 (semicolon && idx == 0) ? vartype : item_type;
7482 ++stack->ga_len;
7483 }
7484 }
7485
7486 for (idx = 0; idx < var_count; ++idx)
7487 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007488 assign_dest_T dest = dest_local;
7489 int opt_flags = 0;
7490 int vimvaridx = -1;
7491 type_T *type = &t_any;
7492
7493 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007494 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007495 name = vim_strnsave(arg, varlen);
7496 if (name == NULL)
7497 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007498
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007499 // TODO: script var not supported?
7500 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7501 &vimvaridx, &type, cctx) == FAIL)
7502 goto failed;
7503 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007504 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007505 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7506 0, 0, type, name) == FAIL)
7507 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007508 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007509 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007510 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007511 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007512 {
7513 semsg(_(e_variable_already_declared), arg);
7514 goto failed;
7515 }
7516
Bram Moolenaarea870692020-12-02 14:24:30 +01007517 if (STRNCMP(name, "s:", 2) == 0)
7518 {
7519 semsg(_(e_cannot_declare_script_variable_in_function), name);
7520 goto failed;
7521 }
7522
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007523 // Reserve a variable to store "var".
7524 // TODO: check for type
7525 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7526 if (var_lvar == NULL)
7527 // out of memory or used as an argument
7528 goto failed;
7529
7530 if (semicolon && idx == var_count - 1)
7531 var_lvar->lv_type = vartype;
7532 else
7533 var_lvar->lv_type = item_type;
7534 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7535 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007536
Bram Moolenaar036d0712021-01-17 20:23:38 +01007537 if (*p == ':')
7538 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007539 if (*p == ',' || *p == ';')
7540 ++p;
7541 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007542 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007543 }
7544
7545 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007546
7547failed:
7548 vim_free(name);
7549 drop_scope(cctx);
7550 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551}
7552
7553/*
7554 * compile "endfor"
7555 */
7556 static char_u *
7557compile_endfor(char_u *arg, cctx_T *cctx)
7558{
7559 garray_T *instr = &cctx->ctx_instr;
7560 scope_T *scope = cctx->ctx_scope;
7561 forscope_T *forscope;
7562 isn_T *isn;
7563
Bram Moolenaarfa984412021-03-25 22:15:28 +01007564 if (misplaced_cmdmod(cctx))
7565 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007567 if (scope == NULL || scope->se_type != FOR_SCOPE)
7568 {
7569 emsg(_(e_for));
7570 return NULL;
7571 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007572 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007574 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575
7576 // At end of ":for" scope jump back to the FOR instruction.
7577 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7578
7579 // Fill in the "end" label in the FOR statement so it can jump here
7580 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7581 isn->isn_arg.forloop.for_end = instr->ga_len;
7582
7583 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007584 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585
7586 // Below the ":for" scope drop the "expr" list from the stack.
7587 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7588 return NULL;
7589
7590 vim_free(scope);
7591
7592 return arg;
7593}
7594
7595/*
7596 * compile "while expr"
7597 *
7598 * Produces instructions:
7599 * top: EVAL expr Push result of "expr"
7600 * JUMP_IF_FALSE end jump if false
7601 * ... body ...
7602 * JUMP top Jump back to repeat
7603 * end:
7604 *
7605 */
7606 static char_u *
7607compile_while(char_u *arg, cctx_T *cctx)
7608{
7609 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007610 scope_T *scope;
7611
7612 scope = new_scope(cctx, WHILE_SCOPE);
7613 if (scope == NULL)
7614 return NULL;
7615
Bram Moolenaara91a7132021-03-25 21:12:15 +01007616 // "endwhile" jumps back here, one before when profiling or using cmdmods
7617 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618
7619 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007620 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007621 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007622 if (!ends_excmd2(arg, skipwhite(p)))
7623 {
7624 semsg(_(e_trailing_arg), p);
7625 return NULL;
7626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627
Bram Moolenaar13106602020-10-04 16:06:05 +02007628 if (bool_on_stack(cctx) == FAIL)
7629 return FAIL;
7630
Bram Moolenaara91a7132021-03-25 21:12:15 +01007631 // CMDMOD_REV must come before the jump
7632 generate_undo_cmdmods(cctx);
7633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007635 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 JUMP_IF_FALSE, cctx) == FAIL)
7637 return FAIL;
7638
7639 return p;
7640}
7641
7642/*
7643 * compile "endwhile"
7644 */
7645 static char_u *
7646compile_endwhile(char_u *arg, cctx_T *cctx)
7647{
7648 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007649 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650
Bram Moolenaarfa984412021-03-25 22:15:28 +01007651 if (misplaced_cmdmod(cctx))
7652 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7654 {
7655 emsg(_(e_while));
7656 return NULL;
7657 }
7658 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007659 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007660
Bram Moolenaarf002a412021-01-24 13:34:18 +01007661#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007662 // count the endwhile before jumping
7663 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007664#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007667 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007668
7669 // Fill in the "end" label in the WHILE statement so it can jump here.
7670 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007671 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7672 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007673
7674 vim_free(scope);
7675
7676 return arg;
7677}
7678
7679/*
7680 * compile "continue"
7681 */
7682 static char_u *
7683compile_continue(char_u *arg, cctx_T *cctx)
7684{
7685 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007686 int try_scopes = 0;
7687 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688
7689 for (;;)
7690 {
7691 if (scope == NULL)
7692 {
7693 emsg(_(e_continue));
7694 return NULL;
7695 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007696 if (scope->se_type == FOR_SCOPE)
7697 {
7698 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007699 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007700 }
7701 if (scope->se_type == WHILE_SCOPE)
7702 {
7703 loop_label = scope->se_u.se_while.ws_top_label;
7704 break;
7705 }
7706 if (scope->se_type == TRY_SCOPE)
7707 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 scope = scope->se_outer;
7709 }
7710
Bram Moolenaarc150c092021-02-13 15:02:46 +01007711 if (try_scopes > 0)
7712 // Inside one or more try/catch blocks we first need to jump to the
7713 // "finally" or "endtry" to cleanup.
7714 generate_TRYCONT(cctx, try_scopes, loop_label);
7715 else
7716 // Jump back to the FOR or WHILE instruction.
7717 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007719 return arg;
7720}
7721
7722/*
7723 * compile "break"
7724 */
7725 static char_u *
7726compile_break(char_u *arg, cctx_T *cctx)
7727{
7728 scope_T *scope = cctx->ctx_scope;
7729 endlabel_T **el;
7730
7731 for (;;)
7732 {
7733 if (scope == NULL)
7734 {
7735 emsg(_(e_break));
7736 return NULL;
7737 }
7738 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7739 break;
7740 scope = scope->se_outer;
7741 }
7742
7743 // Jump to the end of the FOR or WHILE loop.
7744 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007745 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007747 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7749 return FAIL;
7750
7751 return arg;
7752}
7753
7754/*
7755 * compile "{" start of block
7756 */
7757 static char_u *
7758compile_block(char_u *arg, cctx_T *cctx)
7759{
7760 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7761 return NULL;
7762 return skipwhite(arg + 1);
7763}
7764
7765/*
7766 * compile end of block: drop one scope
7767 */
7768 static void
7769compile_endblock(cctx_T *cctx)
7770{
7771 scope_T *scope = cctx->ctx_scope;
7772
7773 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007774 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 vim_free(scope);
7776}
7777
7778/*
7779 * compile "try"
7780 * Creates a new scope for the try-endtry, pointing to the first catch and
7781 * finally.
7782 * Creates another scope for the "try" block itself.
7783 * TRY instruction sets up exception handling at runtime.
7784 *
7785 * "try"
7786 * TRY -> catch1, -> finally push trystack entry
7787 * ... try block
7788 * "throw {exception}"
7789 * EVAL {exception}
7790 * THROW create exception
7791 * ... try block
7792 * " catch {expr}"
7793 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007794 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 * EVAL {expr}
7796 * MATCH
7797 * JUMP nomatch -> catch2
7798 * CATCH remove exception
7799 * ... catch block
7800 * " catch"
7801 * JUMP -> finally
7802 * catch2: CATCH remove exception
7803 * ... catch block
7804 * " finally"
7805 * finally:
7806 * ... finally block
7807 * " endtry"
7808 * ENDTRY pop trystack entry, may rethrow
7809 */
7810 static char_u *
7811compile_try(char_u *arg, cctx_T *cctx)
7812{
7813 garray_T *instr = &cctx->ctx_instr;
7814 scope_T *try_scope;
7815 scope_T *scope;
7816
Bram Moolenaarfa984412021-03-25 22:15:28 +01007817 if (misplaced_cmdmod(cctx))
7818 return NULL;
7819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007820 // scope that holds the jumps that go to catch/finally/endtry
7821 try_scope = new_scope(cctx, TRY_SCOPE);
7822 if (try_scope == NULL)
7823 return NULL;
7824
Bram Moolenaar69f70502021-01-01 16:10:46 +01007825 if (cctx->ctx_skip != SKIP_YES)
7826 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007827 isn_T *isn;
7828
7829 // "try_catch" is set when the first ":catch" is found or when no catch
7830 // is found and ":finally" is found.
7831 // "try_finally" is set when ":finally" is found
7832 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007833 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007834 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7835 return NULL;
7836 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7837 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007838 return NULL;
7839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840
7841 // scope for the try block itself
7842 scope = new_scope(cctx, BLOCK_SCOPE);
7843 if (scope == NULL)
7844 return NULL;
7845
7846 return arg;
7847}
7848
7849/*
7850 * compile "catch {expr}"
7851 */
7852 static char_u *
7853compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7854{
7855 scope_T *scope = cctx->ctx_scope;
7856 garray_T *instr = &cctx->ctx_instr;
7857 char_u *p;
7858 isn_T *isn;
7859
Bram Moolenaarfa984412021-03-25 22:15:28 +01007860 if (misplaced_cmdmod(cctx))
7861 return NULL;
7862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 // end block scope from :try or :catch
7864 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7865 compile_endblock(cctx);
7866 scope = cctx->ctx_scope;
7867
7868 // Error if not in a :try scope
7869 if (scope == NULL || scope->se_type != TRY_SCOPE)
7870 {
7871 emsg(_(e_catch));
7872 return NULL;
7873 }
7874
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007875 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007876 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007877 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007878 return NULL;
7879 }
7880
Bram Moolenaar69f70502021-01-01 16:10:46 +01007881 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007883#ifdef FEAT_PROFILE
7884 // the profile-start should be after the jump
7885 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7886 .isn_type == ISN_PROF_START)
7887 --instr->ga_len;
7888#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007889 // Jump from end of previous block to :finally or :endtry
7890 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7891 JUMP_ALWAYS, cctx) == FAIL)
7892 return NULL;
7893
7894 // End :try or :catch scope: set value in ISN_TRY instruction
7895 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007896 if (isn->isn_arg.try.try_ref->try_catch == 0)
7897 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007898 if (scope->se_u.se_try.ts_catch_label != 0)
7899 {
7900 // Previous catch without match jumps here
7901 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7902 isn->isn_arg.jump.jump_where = instr->ga_len;
7903 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007904#ifdef FEAT_PROFILE
7905 if (cctx->ctx_profiling)
7906 {
7907 // a "throw" that jumps here needs to be counted
7908 generate_instr(cctx, ISN_PROF_END);
7909 // the "catch" is also counted
7910 generate_instr(cctx, ISN_PROF_START);
7911 }
7912#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007913 }
7914
7915 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007916 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007917 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007918 scope->se_u.se_try.ts_caught_all = TRUE;
7919 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007920 }
7921 else
7922 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007923 char_u *end;
7924 char_u *pat;
7925 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007926 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007927 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007929 // Push v:exception, push {expr} and MATCH
7930 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7931
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007932 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007933 if (*end != *p)
7934 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007935 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007936 vim_free(tofree);
7937 return FAIL;
7938 }
7939 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007940 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007941 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007942 len = (int)(end - tofree);
7943 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007944 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007945 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007946 if (pat == NULL)
7947 return FAIL;
7948 if (generate_PUSHS(cctx, pat) == FAIL)
7949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007951 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7952 return NULL;
7953
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007954 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007955 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7956 return NULL;
7957 }
7958
Bram Moolenaar69f70502021-01-01 16:10:46 +01007959 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007960 return NULL;
7961
7962 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7963 return NULL;
7964 return p;
7965}
7966
7967 static char_u *
7968compile_finally(char_u *arg, cctx_T *cctx)
7969{
7970 scope_T *scope = cctx->ctx_scope;
7971 garray_T *instr = &cctx->ctx_instr;
7972 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007973 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007974
Bram Moolenaarfa984412021-03-25 22:15:28 +01007975 if (misplaced_cmdmod(cctx))
7976 return NULL;
7977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 // end block scope from :try or :catch
7979 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7980 compile_endblock(cctx);
7981 scope = cctx->ctx_scope;
7982
7983 // Error if not in a :try scope
7984 if (scope == NULL || scope->se_type != TRY_SCOPE)
7985 {
7986 emsg(_(e_finally));
7987 return NULL;
7988 }
7989
7990 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007991 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007992 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 {
7994 emsg(_(e_finally_dup));
7995 return NULL;
7996 }
7997
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007998 this_instr = instr->ga_len;
7999#ifdef FEAT_PROFILE
8000 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8001 .isn_type == ISN_PROF_START)
8002 // jump to the profile start of the "finally"
8003 --this_instr;
8004#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008006 // Fill in the "end" label in jumps at the end of the blocks.
8007 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8008 this_instr, cctx);
8009
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008010 // If there is no :catch then an exception jumps to :finally.
8011 if (isn->isn_arg.try.try_ref->try_catch == 0)
8012 isn->isn_arg.try.try_ref->try_catch = this_instr;
8013 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008014 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015 {
8016 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008017 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008018 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008019 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008021 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8022 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024 // TODO: set index in ts_finally_label jumps
8025
8026 return arg;
8027}
8028
8029 static char_u *
8030compile_endtry(char_u *arg, cctx_T *cctx)
8031{
8032 scope_T *scope = cctx->ctx_scope;
8033 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008034 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008035
Bram Moolenaarfa984412021-03-25 22:15:28 +01008036 if (misplaced_cmdmod(cctx))
8037 return NULL;
8038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 // end block scope from :catch or :finally
8040 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8041 compile_endblock(cctx);
8042 scope = cctx->ctx_scope;
8043
8044 // Error if not in a :try scope
8045 if (scope == NULL || scope->se_type != TRY_SCOPE)
8046 {
8047 if (scope == NULL)
8048 emsg(_(e_no_endtry));
8049 else if (scope->se_type == WHILE_SCOPE)
8050 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008051 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052 emsg(_(e_endfor));
8053 else
8054 emsg(_(e_endif));
8055 return NULL;
8056 }
8057
Bram Moolenaarc150c092021-02-13 15:02:46 +01008058 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008059 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008060 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008061 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8062 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008063 {
8064 emsg(_(e_missing_catch_or_finally));
8065 return NULL;
8066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008067
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008068#ifdef FEAT_PROFILE
8069 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8070 .isn_type == ISN_PROF_START)
8071 // move the profile start after "endtry" so that it's not counted when
8072 // the exception is rethrown.
8073 --instr->ga_len;
8074#endif
8075
Bram Moolenaar69f70502021-01-01 16:10:46 +01008076 // Fill in the "end" label in jumps at the end of the blocks, if not
8077 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008078 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8079 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080
Bram Moolenaar69f70502021-01-01 16:10:46 +01008081 if (scope->se_u.se_try.ts_catch_label != 0)
8082 {
8083 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008084 isn_T *isn = ((isn_T *)instr->ga_data)
8085 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008086 isn->isn_arg.jump.jump_where = instr->ga_len;
8087 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008088 }
8089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090 compile_endblock(cctx);
8091
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008092 if (cctx->ctx_skip != SKIP_YES)
8093 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008094 // End :catch or :finally scope: set instruction index in ISN_TRY
8095 // instruction
8096 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008097 if (cctx->ctx_skip != SKIP_YES
8098 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8099 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008100#ifdef FEAT_PROFILE
8101 if (cctx->ctx_profiling)
8102 generate_instr(cctx, ISN_PROF_START);
8103#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008105 return arg;
8106}
8107
8108/*
8109 * compile "throw {expr}"
8110 */
8111 static char_u *
8112compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8113{
8114 char_u *p = skipwhite(arg);
8115
Bram Moolenaara5565e42020-05-09 15:44:01 +02008116 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008118 if (cctx->ctx_skip == SKIP_YES)
8119 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008120 if (may_generate_2STRING(-1, cctx) == FAIL)
8121 return NULL;
8122 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8123 return NULL;
8124
8125 return p;
8126}
8127
8128/*
8129 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008130 * compile "echomsg expr"
8131 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008132 * compile "execute expr"
8133 */
8134 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008135compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008136{
8137 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008138 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008139 int count = 0;
8140
8141 for (;;)
8142 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008143 if (ends_excmd2(prev, p))
8144 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008145 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008146 return NULL;
8147 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008148 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008149 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008150 }
8151
Bram Moolenaare4984292020-12-13 14:19:25 +01008152 if (count > 0)
8153 {
8154 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8155 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8156 else if (cmdidx == CMD_execute)
8157 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8158 else if (cmdidx == CMD_echomsg)
8159 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8160 else
8161 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163 return p;
8164}
8165
8166/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008167 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008168 * instruction to compute it and return OK.
8169 * Otherwise return FAIL, the caller must deal with any range.
8170 */
8171 static int
8172compile_variable_range(exarg_T *eap, cctx_T *cctx)
8173{
8174 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8175 char_u *p = skipdigits(eap->cmd);
8176
8177 if (p == range_end)
8178 return FAIL;
8179 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8180}
8181
8182/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008183 * :put r
8184 * :put ={expr}
8185 */
8186 static char_u *
8187compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8188{
8189 char_u *line = arg;
8190 linenr_T lnum;
8191 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008192 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008193
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008194 eap->regname = *line;
8195
8196 if (eap->regname == '=')
8197 {
8198 char_u *p = line + 1;
8199
8200 if (compile_expr0(&p, cctx) == FAIL)
8201 return NULL;
8202 line = p;
8203 }
8204 else if (eap->regname != NUL)
8205 ++line;
8206
Bram Moolenaar08597872020-12-10 19:43:40 +01008207 if (compile_variable_range(eap, cctx) == OK)
8208 {
8209 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8210 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008211 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008212 {
8213 // Either no range or a number.
8214 // "errormsg" will not be set because the range is ADDR_LINES.
8215 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008216 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008217 return NULL;
8218 if (eap->addr_count == 0)
8219 lnum = -1;
8220 else
8221 lnum = eap->line2;
8222 if (above)
8223 --lnum;
8224 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008225
8226 generate_PUT(cctx, eap->regname, lnum);
8227 return line;
8228}
8229
8230/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008231 * A command that is not compiled, execute with legacy code.
8232 */
8233 static char_u *
8234compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8235{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008236 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008237 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008238 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008239
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008240 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008241 goto theend;
8242
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008243 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008244 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008245 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008246 int usefilter = FALSE;
8247
8248 has_expr = argt & (EX_XFILE | EX_EXPAND);
8249
8250 // If the command can be followed by a bar, find the bar and truncate
8251 // it, so that the following command can be compiled.
8252 // The '|' is overwritten with a NUL, it is put back below.
8253 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8254 && *eap->arg == '!')
8255 // :w !filter or :r !filter or :r! filter
8256 usefilter = TRUE;
8257 if ((argt & EX_TRLBAR) && !usefilter)
8258 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008259 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008260 separate_nextcmd(eap);
8261 if (eap->nextcmd != NULL)
8262 nextcmd = eap->nextcmd;
8263 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008264 else if (eap->cmdidx == CMD_wincmd)
8265 {
8266 p = eap->arg;
8267 if (*p != NUL)
8268 ++p;
8269 if (*p == 'g' || *p == Ctrl_G)
8270 ++p;
8271 p = skipwhite(p);
8272 if (*p == '|')
8273 {
8274 *p = NUL;
8275 nextcmd = p + 1;
8276 }
8277 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008278 }
8279
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008280 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8281 {
8282 // expand filename in "syntax include [@group] filename"
8283 has_expr = TRUE;
8284 eap->arg = skipwhite(eap->arg + 7);
8285 if (*eap->arg == '@')
8286 eap->arg = skiptowhite(eap->arg);
8287 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008288
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008289 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8290 && STRLEN(eap->arg) > 4)
8291 {
8292 int delim = *eap->arg;
8293
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008294 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008295 if (*p == delim)
8296 {
8297 eap->arg = p + 1;
8298 has_expr = TRUE;
8299 }
8300 }
8301
Bram Moolenaarecac5912021-01-05 19:23:28 +01008302 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8303 {
8304 // TODO: should only expand when appropriate for the command
8305 eap->arg = skiptowhite(eap->arg);
8306 has_expr = TRUE;
8307 }
8308
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008309 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008310 {
8311 int count = 0;
8312 char_u *start = skipwhite(line);
8313
8314 // :cmd xxx`=expr1`yyy`=expr2`zzz
8315 // PUSHS ":cmd xxx"
8316 // eval expr1
8317 // PUSHS "yyy"
8318 // eval expr2
8319 // PUSHS "zzz"
8320 // EXECCONCAT 5
8321 for (;;)
8322 {
8323 if (p > start)
8324 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008325 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008326 ++count;
8327 }
8328 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008329 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008330 return NULL;
8331 may_generate_2STRING(-1, cctx);
8332 ++count;
8333 p = skipwhite(p);
8334 if (*p != '`')
8335 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008336 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008337 return NULL;
8338 }
8339 start = p + 1;
8340
8341 p = (char_u *)strstr((char *)start, "`=");
8342 if (p == NULL)
8343 {
8344 if (*skipwhite(start) != NUL)
8345 {
8346 generate_PUSHS(cctx, vim_strsave(start));
8347 ++count;
8348 }
8349 break;
8350 }
8351 }
8352 generate_EXECCONCAT(cctx, count);
8353 }
8354 else
8355 generate_EXEC(cctx, line);
8356
8357theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008358 if (*nextcmd != NUL)
8359 {
8360 // the parser expects a pointer to the bar, put it back
8361 --nextcmd;
8362 *nextcmd = '|';
8363 }
8364
8365 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008366}
8367
8368/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008369 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008370 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008371 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008372 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008373add_def_function(ufunc_T *ufunc)
8374{
8375 dfunc_T *dfunc;
8376
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008377 if (def_functions.ga_len == 0)
8378 {
8379 // The first position is not used, so that a zero uf_dfunc_idx means it
8380 // wasn't set.
8381 if (ga_grow(&def_functions, 1) == FAIL)
8382 return FAIL;
8383 ++def_functions.ga_len;
8384 }
8385
Bram Moolenaar09689a02020-05-09 22:50:08 +02008386 // Add the function to "def_functions".
8387 if (ga_grow(&def_functions, 1) == FAIL)
8388 return FAIL;
8389 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8390 CLEAR_POINTER(dfunc);
8391 dfunc->df_idx = def_functions.ga_len;
8392 ufunc->uf_dfunc_idx = dfunc->df_idx;
8393 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008394 dfunc->df_name = vim_strsave(ufunc->uf_name);
8395 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008396 ++def_functions.ga_len;
8397 return OK;
8398}
8399
8400/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 * After ex_function() has collected all the function lines: parse and compile
8402 * the lines into instructions.
8403 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008404 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8405 * the return statement (used for lambda). When uf_ret_type is already set
8406 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008407 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008408 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008409 * This can be used recursively through compile_lambda(), which may reallocate
8410 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008411 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008413 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008414compile_def_function(
8415 ufunc_T *ufunc,
8416 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008417 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008418 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008420 char_u *line = NULL;
8421 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 cctx_T cctx;
8424 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008425 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 int ret = FAIL;
8427 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008428 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008429 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008430 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008431#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008432 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008433#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008434
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008435 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008436 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008437 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008439 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8440 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008441 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008442 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008443 else
8444 {
8445 if (add_def_function(ufunc) == FAIL)
8446 return FAIL;
8447 new_def_function = TRUE;
8448 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449
Bram Moolenaar985116a2020-07-12 17:31:09 +02008450 ufunc->uf_def_status = UF_COMPILING;
8451
Bram Moolenaara80faa82020-04-12 19:37:17 +02008452 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008453
Bram Moolenaarf002a412021-01-24 13:34:18 +01008454#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008455 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008456#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008457 cctx.ctx_ufunc = ufunc;
8458 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008459 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8461 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8462 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8463 cctx.ctx_type_list = &ufunc->uf_type_list;
8464 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8465 instr = &cctx.ctx_instr;
8466
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008467 // Set the context to the function, it may be compiled when called from
8468 // another script. Set the script version to the most modern one.
8469 // The line number will be set in next_line_from_context().
8470 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8472
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008473 // Make sure error messages are OK.
8474 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8475 if (do_estack_push)
8476 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008477 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008478
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008479 if (ufunc->uf_def_args.ga_len > 0)
8480 {
8481 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008482 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008483 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008484 int i;
8485 char_u *arg;
8486 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008487 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008488
8489 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008490 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008491 for (i = 0; i < count; ++i)
8492 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008493 garray_T *stack = &cctx.ctx_type_stack;
8494 type_T *val_type;
8495 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008496 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008497 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008498 int jump_instr_idx = instr->ga_len;
8499 isn_T *isn;
8500
8501 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8502 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8503 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008504
8505 // Make sure later arguments are not found.
8506 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008507
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008508 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008509 r = compile_expr0(&arg, &cctx);
8510
8511 ufunc->uf_args.ga_len = uf_args_len;
8512 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008513 goto erret;
8514
8515 // If no type specified use the type of the default value.
8516 // Otherwise check that the default value type matches the
8517 // specified type.
8518 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008519 where.wt_index = arg_idx + 1;
8520 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008521 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008522 {
8523 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008524 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008525 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008526 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008527 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008528 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008529
8530 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008531 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008532
8533 // set instruction index in JUMP_IF_ARG_SET to here
8534 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8535 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008536 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008537
8538 if (did_set_arg_type)
8539 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008540 }
8541
8542 /*
8543 * Loop over all the lines of the function and generate instructions.
8544 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008545 for (;;)
8546 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008547 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008548 int starts_with_colon = FALSE;
8549 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008550 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008551
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008552 // Bail out on the first error to avoid a flood of errors and report
8553 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008554 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008555 goto erret;
8556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008557 if (line != NULL && *line == '|')
8558 // the line continues after a '|'
8559 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008560 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008561 && !(*line == '#' && (line == cctx.ctx_line_start
8562 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008564 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008565 goto erret;
8566 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008567 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8568 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008569 else
8570 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008571 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008572 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008573 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008574 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008575#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008576 if (cctx.ctx_skip != SKIP_YES)
8577 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008578#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 }
8582
Bram Moolenaara80faa82020-04-12 19:37:17 +02008583 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008584 ea.cmdlinep = &line;
8585 ea.cmd = skipwhite(line);
8586
Bram Moolenaarb2049902021-01-24 12:53:53 +01008587 if (*ea.cmd == '#')
8588 {
8589 // "#" starts a comment
8590 line = (char_u *)"";
8591 continue;
8592 }
8593
Bram Moolenaarf002a412021-01-24 13:34:18 +01008594#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008595 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8596 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008597 {
8598 may_generate_prof_end(&cctx, prof_lnum);
8599
8600 prof_lnum = cctx.ctx_lnum;
8601 generate_instr(&cctx, ISN_PROF_START);
8602 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008603#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008604
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008605 // Some things can be recognized by the first character.
8606 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008608 case '}':
8609 {
8610 // "}" ends a block scope
8611 scopetype_T stype = cctx.ctx_scope == NULL
8612 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008613
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008614 if (stype == BLOCK_SCOPE)
8615 {
8616 compile_endblock(&cctx);
8617 line = ea.cmd;
8618 }
8619 else
8620 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008621 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008622 goto erret;
8623 }
8624 if (line != NULL)
8625 line = skipwhite(ea.cmd + 1);
8626 continue;
8627 }
8628
8629 case '{':
8630 // "{" starts a block scope
8631 // "{'a': 1}->func() is something else
8632 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8633 {
8634 line = compile_block(ea.cmd, &cctx);
8635 continue;
8636 }
8637 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 }
8639
8640 /*
8641 * COMMAND MODIFIERS
8642 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008643 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008644 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8645 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008646 {
8647 if (errormsg != NULL)
8648 goto erret;
8649 // empty line or comment
8650 line = (char_u *)"";
8651 continue;
8652 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008653 generate_cmdmods(&cctx, &local_cmdmod);
8654 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008656 // Check if there was a colon after the last command modifier or before
8657 // the current position.
8658 for (p = ea.cmd; p >= line; --p)
8659 {
8660 if (*p == ':')
8661 starts_with_colon = TRUE;
8662 if (p < ea.cmd && !VIM_ISWHITE(*p))
8663 break;
8664 }
8665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008666 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008667 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008669 {
8670 if (*ea.cmd == '(')
8671 // not for "call()"
8672 ea.cmd = p;
8673 else
8674 ea.cmd = skipwhite(ea.cmd);
8675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008677 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008679 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008680
Bram Moolenaar17126b12021-01-07 22:03:02 +01008681 // Check for assignment after command modifiers.
8682 assign = may_compile_assignment(&ea, &line, &cctx);
8683 if (assign == OK)
8684 goto nextline;
8685 if (assign == FAIL)
8686 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008687 }
8688
8689 /*
8690 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008691 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008693 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008694 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008695 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008696 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008697 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008698 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008699 if (!starts_with_colon)
8700 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008701 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008702 goto erret;
8703 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008704 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008705 if (ends_excmd2(line, ea.cmd))
8706 {
8707 // A range without a command: jump to the line.
8708 // TODO: compile to a more efficient command, possibly
8709 // calling parse_cmd_address().
8710 ea.cmdidx = CMD_SIZE;
8711 line = compile_exec(line, &ea, &cctx);
8712 goto nextline;
8713 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008714 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008715 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008716 p = find_ex_command(&ea, NULL, starts_with_colon
8717 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008718
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008719 if (p == NULL)
8720 {
8721 if (cctx.ctx_skip != SKIP_YES)
8722 emsg(_(e_ambiguous_use_of_user_defined_command));
8723 goto erret;
8724 }
8725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008726 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8727 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008728 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008729 {
8730 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008731 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008732 }
8733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008734 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008735 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008736 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008737 // CMD_var cannot happen, compile_assignment() above would be
8738 // used. Most likely an assignment to a non-existing variable.
8739 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008740 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008742 }
8743
Bram Moolenaar3988f642020-08-27 22:43:03 +02008744 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008745 && ea.cmdidx != CMD_elseif
8746 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008747 && ea.cmdidx != CMD_endif
8748 && ea.cmdidx != CMD_endfor
8749 && ea.cmdidx != CMD_endwhile
8750 && ea.cmdidx != CMD_catch
8751 && ea.cmdidx != CMD_finally
8752 && ea.cmdidx != CMD_endtry)
8753 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008754 emsg(_(e_unreachable_code_after_return));
8755 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008756 }
8757
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008758 p = skipwhite(p);
8759 if (ea.cmdidx != CMD_SIZE
8760 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8761 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008762 if (ea.cmdidx >= 0)
8763 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008764 if ((ea.argt & EX_BANG) && *p == '!')
8765 {
8766 ea.forceit = TRUE;
8767 p = skipwhite(p + 1);
8768 }
8769 }
8770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008771 switch (ea.cmdidx)
8772 {
8773 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008774 ea.arg = p;
8775 line = compile_nested_function(&ea, &cctx);
8776 break;
8777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008779 // TODO: should we allow this, e.g. to declare a global
8780 // function?
8781 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008782 goto erret;
8783
8784 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008785 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008786 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 break;
8788
8789 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008790 emsg(_(e_cannot_use_let_in_vim9_script));
8791 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008792 case CMD_var:
8793 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008794 case CMD_const:
8795 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008796 if (line == p)
8797 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 break;
8799
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008800 case CMD_unlet:
8801 case CMD_unlockvar:
8802 case CMD_lockvar:
8803 line = compile_unletlock(p, &ea, &cctx);
8804 break;
8805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806 case CMD_import:
8807 line = compile_import(p, &cctx);
8808 break;
8809
8810 case CMD_if:
8811 line = compile_if(p, &cctx);
8812 break;
8813 case CMD_elseif:
8814 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008815 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008816 break;
8817 case CMD_else:
8818 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008819 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 break;
8821 case CMD_endif:
8822 line = compile_endif(p, &cctx);
8823 break;
8824
8825 case CMD_while:
8826 line = compile_while(p, &cctx);
8827 break;
8828 case CMD_endwhile:
8829 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008830 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008831 break;
8832
8833 case CMD_for:
8834 line = compile_for(p, &cctx);
8835 break;
8836 case CMD_endfor:
8837 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008838 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008839 break;
8840 case CMD_continue:
8841 line = compile_continue(p, &cctx);
8842 break;
8843 case CMD_break:
8844 line = compile_break(p, &cctx);
8845 break;
8846
8847 case CMD_try:
8848 line = compile_try(p, &cctx);
8849 break;
8850 case CMD_catch:
8851 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008852 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008853 break;
8854 case CMD_finally:
8855 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008856 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008857 break;
8858 case CMD_endtry:
8859 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008860 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008861 break;
8862 case CMD_throw:
8863 line = compile_throw(p, &cctx);
8864 break;
8865
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008866 case CMD_eval:
8867 if (compile_expr0(&p, &cctx) == FAIL)
8868 goto erret;
8869
Bram Moolenaar3988f642020-08-27 22:43:03 +02008870 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008871 generate_instr_drop(&cctx, ISN_DROP, 1);
8872
8873 line = skipwhite(p);
8874 break;
8875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008876 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008877 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008878 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008879 case CMD_echomsg:
8880 case CMD_echoerr:
8881 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008882 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008883
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008884 case CMD_put:
8885 ea.cmd = cmd;
8886 line = compile_put(p, &ea, &cctx);
8887 break;
8888
Bram Moolenaar3988f642020-08-27 22:43:03 +02008889 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008890
Bram Moolenaarae616492020-07-28 20:07:27 +02008891 case CMD_append:
8892 case CMD_change:
8893 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008894 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008895 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008896 case CMD_xit:
8897 not_in_vim9(&ea);
8898 goto erret;
8899
Bram Moolenaar002262f2020-07-08 17:47:57 +02008900 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008901 if (cctx.ctx_skip != SKIP_YES)
8902 {
8903 semsg(_(e_invalid_command_str), ea.cmd);
8904 goto erret;
8905 }
8906 // We don't check for a next command here.
8907 line = (char_u *)"";
8908 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008911 if (cctx.ctx_skip == SKIP_YES)
8912 {
8913 // We don't check for a next command here.
8914 line = (char_u *)"";
8915 }
8916 else
8917 {
8918 // Not recognized, execute with do_cmdline_cmd().
8919 ea.arg = p;
8920 line = compile_exec(line, &ea, &cctx);
8921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922 break;
8923 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008924nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008925 if (line == NULL)
8926 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008927 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008928
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008929 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008930 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008932 if (cctx.ctx_type_stack.ga_len < 0)
8933 {
8934 iemsg("Type stack underflow");
8935 goto erret;
8936 }
8937 }
8938
8939 if (cctx.ctx_scope != NULL)
8940 {
8941 if (cctx.ctx_scope->se_type == IF_SCOPE)
8942 emsg(_(e_endif));
8943 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8944 emsg(_(e_endwhile));
8945 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8946 emsg(_(e_endfor));
8947 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008948 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008949 goto erret;
8950 }
8951
Bram Moolenaarefd88552020-06-18 20:50:10 +02008952 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008953 {
8954 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8955 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008956 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 goto erret;
8958 }
8959
8960 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008961 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008962 }
8963
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008964 {
8965 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8966 + ufunc->uf_dfunc_idx;
8967 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008968 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008969#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008970 if (cctx.ctx_profiling)
8971 {
8972 dfunc->df_instr_prof = instr->ga_data;
8973 dfunc->df_instr_prof_count = instr->ga_len;
8974 }
8975 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008976#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008977 {
8978 dfunc->df_instr = instr->ga_data;
8979 dfunc->df_instr_count = instr->ga_len;
8980 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008981 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008982 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008983 if (cctx.ctx_outer_used)
8984 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008985 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008987
8988 ret = OK;
8989
8990erret:
8991 if (ret == FAIL)
8992 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008993 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008994 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8995 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008996
8997 for (idx = 0; idx < instr->ga_len; ++idx)
8998 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008999 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009000 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009001
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009002 // If using the last entry in the table and it was added above, we
9003 // might as well remove it.
9004 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009005 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009006 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009007 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009008 ufunc->uf_dfunc_idx = 0;
9009 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009010 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009011
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009012 while (cctx.ctx_scope != NULL)
9013 drop_scope(&cctx);
9014
Bram Moolenaar20431c92020-03-20 18:39:46 +01009015 // Don't execute this function body.
9016 ga_clear_strings(&ufunc->uf_lines);
9017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018 if (errormsg != NULL)
9019 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009020 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009021 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022 }
9023
9024 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009025 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009026 if (do_estack_push)
9027 estack_pop();
9028
Bram Moolenaar20431c92020-03-20 18:39:46 +01009029 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009030 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009031 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009032 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009033}
9034
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009035 void
9036set_function_type(ufunc_T *ufunc)
9037{
9038 int varargs = ufunc->uf_va_name != NULL;
9039 int argcount = ufunc->uf_args.ga_len;
9040
9041 // Create a type for the function, with the return type and any
9042 // argument types.
9043 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9044 // The type is included in "tt_args".
9045 if (argcount > 0 || varargs)
9046 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009047 if (ufunc->uf_type_list.ga_itemsize == 0)
9048 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009049 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9050 argcount, &ufunc->uf_type_list);
9051 // Add argument types to the function type.
9052 if (func_type_add_arg_types(ufunc->uf_func_type,
9053 argcount + varargs,
9054 &ufunc->uf_type_list) == FAIL)
9055 return;
9056 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9057 ufunc->uf_func_type->tt_min_argcount =
9058 argcount - ufunc->uf_def_args.ga_len;
9059 if (ufunc->uf_arg_types == NULL)
9060 {
9061 int i;
9062
9063 // lambda does not have argument types.
9064 for (i = 0; i < argcount; ++i)
9065 ufunc->uf_func_type->tt_args[i] = &t_any;
9066 }
9067 else
9068 mch_memmove(ufunc->uf_func_type->tt_args,
9069 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9070 if (varargs)
9071 {
9072 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009073 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009074 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9075 }
9076 }
9077 else
9078 // No arguments, can use a predefined type.
9079 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9080 argcount, &ufunc->uf_type_list);
9081}
9082
9083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009084/*
9085 * Delete an instruction, free what it contains.
9086 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009087 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009088delete_instr(isn_T *isn)
9089{
9090 switch (isn->isn_type)
9091 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009092 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009093 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009094 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009095 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009096 case ISN_LOADENV:
9097 case ISN_LOADG:
9098 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009099 case ISN_LOADT:
9100 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009101 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009102 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009103 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009104 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009105 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009106 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009107 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009108 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009109 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009110 case ISN_STOREW:
9111 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009112 vim_free(isn->isn_arg.string);
9113 break;
9114
9115 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009116 case ISN_STORES:
9117 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009118 break;
9119
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009120 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009121 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009122 vim_free(isn->isn_arg.unlet.ul_name);
9123 break;
9124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009125 case ISN_STOREOPT:
9126 vim_free(isn->isn_arg.storeopt.so_name);
9127 break;
9128
9129 case ISN_PUSHBLOB: // push blob isn_arg.blob
9130 blob_unref(isn->isn_arg.blob);
9131 break;
9132
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009133 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009134#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009135 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009136#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009137 break;
9138
9139 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009141 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009142#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009143 break;
9144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009145 case ISN_UCALL:
9146 vim_free(isn->isn_arg.ufunc.cuf_name);
9147 break;
9148
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009149 case ISN_FUNCREF:
9150 {
9151 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9152 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009153 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009154
Bram Moolenaar077a4232020-12-22 18:33:27 +01009155 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9156 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009157 }
9158 break;
9159
9160 case ISN_DCALL:
9161 {
9162 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9163 + isn->isn_arg.dfunc.cdf_idx;
9164
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009165 if (dfunc->df_ufunc != NULL
9166 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009167 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009168 }
9169 break;
9170
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009171 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009172 {
9173 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9174 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9175
9176 if (ufunc != NULL)
9177 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009178 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009179 func_ptr_unref(ufunc);
9180 }
9181
9182 vim_free(lambda);
9183 vim_free(isn->isn_arg.newfunc.nf_global);
9184 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009185 break;
9186
Bram Moolenaar5e654232020-09-16 15:22:00 +02009187 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009188 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009189 free_type(isn->isn_arg.type.ct_type);
9190 break;
9191
Bram Moolenaar02194d22020-10-24 23:08:38 +02009192 case ISN_CMDMOD:
9193 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9194 ->cmod_filter_regmatch.regprog);
9195 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9196 break;
9197
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009198 case ISN_LOADSCRIPT:
9199 case ISN_STORESCRIPT:
9200 vim_free(isn->isn_arg.script.scriptref);
9201 break;
9202
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009203 case ISN_TRY:
9204 vim_free(isn->isn_arg.try.try_ref);
9205 break;
9206
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207 case ISN_2BOOL:
9208 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009209 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009210 case ISN_ADDBLOB:
9211 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009212 case ISN_ANYINDEX:
9213 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009215 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009216 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009217 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009218 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009219 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009220 case ISN_COMPAREANY:
9221 case ISN_COMPAREBLOB:
9222 case ISN_COMPAREBOOL:
9223 case ISN_COMPAREDICT:
9224 case ISN_COMPAREFLOAT:
9225 case ISN_COMPAREFUNC:
9226 case ISN_COMPARELIST:
9227 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 case ISN_COMPARESPECIAL:
9229 case ISN_COMPARESTRING:
9230 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009231 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009232 case ISN_DROP:
9233 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009234 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009235 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009237 case ISN_EXECCONCAT:
9238 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009239 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009240 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009241 case ISN_GETITEM:
9242 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009243 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009244 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009245 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009246 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009248 case ISN_LOADBDICT:
9249 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009250 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009251 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009252 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009253 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009254 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009255 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009256 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 case ISN_NEGATENR:
9258 case ISN_NEWDICT:
9259 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009261 case ISN_OPFLOAT:
9262 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009263 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009264 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009265 case ISN_PROF_END:
9266 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009267 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009268 case ISN_PUSHF:
9269 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009270 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009271 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009272 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009273 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009274 case ISN_SHUFFLE:
9275 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009276 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009277 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009278 case ISN_STORENR:
9279 case ISN_STOREOUTER:
9280 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009281 case ISN_STOREV:
9282 case ISN_STRINDEX:
9283 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009284 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009285 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009286 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009287 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009288 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009289 // nothing allocated
9290 break;
9291 }
9292}
9293
9294/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009295 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009296 */
9297 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009298delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009299{
9300 int idx;
9301
9302 ga_clear(&dfunc->df_def_args_isn);
9303
9304 if (dfunc->df_instr != NULL)
9305 {
9306 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9307 delete_instr(dfunc->df_instr + idx);
9308 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009309 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009310 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009311#ifdef FEAT_PROFILE
9312 if (dfunc->df_instr_prof != NULL)
9313 {
9314 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9315 delete_instr(dfunc->df_instr_prof + idx);
9316 VIM_CLEAR(dfunc->df_instr_prof);
9317 dfunc->df_instr_prof = NULL;
9318 }
9319#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009320
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009321 if (mark_deleted)
9322 dfunc->df_deleted = TRUE;
9323 if (dfunc->df_ufunc != NULL)
9324 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009325}
9326
9327/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009328 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009329 * function, unless another user function still uses it.
9330 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009331 */
9332 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009333unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009334{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009335 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009336 {
9337 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9338 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009339
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009340 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009341 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009342 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009343 ufunc->uf_dfunc_idx = 0;
9344 if (dfunc->df_ufunc == ufunc)
9345 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009346 }
9347}
9348
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009349/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009350 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009351 */
9352 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009353link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009354{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009355 if (ufunc->uf_dfunc_idx > 0)
9356 {
9357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9358 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009359
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009360 ++dfunc->df_refcount;
9361 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009362}
9363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009365/*
9366 * Free all functions defined with ":def".
9367 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009368 void
9369free_def_functions(void)
9370{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009371 int idx;
9372
9373 for (idx = 0; idx < def_functions.ga_len; ++idx)
9374 {
9375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9376
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009377 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009378 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009379 }
9380
9381 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009382}
9383#endif
9384
9385
9386#endif // FEAT_EVAL