blob: 02c75c4d6a6994a4cb41fb4b2bf019124e49d398 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaarda479c72021-04-10 21:01:38 +0200425 // underscore argument is OK
426 if (len == 1 && *p == '_')
427 return OK;
428
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200429 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 {
431 if (is_arg)
432 semsg(_(e_argument_already_declared_in_script_str), p);
433 else
434 semsg(_(e_variable_already_declared_in_script_str), p);
435 return FAIL;
436 }
437
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100439 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100440 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200441 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200442 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200443 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100444 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200445 // A local or script-local function can shadow a global function.
446 if (ufunc == NULL || !func_is_global(ufunc)
447 || (p[0] == 'g' && p[1] == ':'))
448 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100449 if (is_arg)
450 semsg(_(e_argument_name_shadows_existing_variable_str), p);
451 else
452 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200453 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200454 return FAIL;
455 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100456 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200457 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100458 return OK;
459}
460
Bram Moolenaar65b95452020-07-19 14:03:09 +0200461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462/////////////////////////////////////////////////////////////////////
463// Following generate_ functions expect the caller to call ga_grow().
464
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200465#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
466#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Generate an instruction without arguments.
470 * Returns a pointer to the new instruction, NULL if failed.
471 */
472 static isn_T *
473generate_instr(cctx_T *cctx, isntype_T isn_type)
474{
475 garray_T *instr = &cctx->ctx_instr;
476 isn_T *isn;
477
Bram Moolenaar080457c2020-03-03 21:53:32 +0100478 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 if (ga_grow(instr, 1) == FAIL)
480 return NULL;
481 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
482 isn->isn_type = isn_type;
483 isn->isn_lnum = cctx->ctx_lnum + 1;
484 ++instr->ga_len;
485
486 return isn;
487}
488
489/*
490 * Generate an instruction without arguments.
491 * "drop" will be removed from the stack.
492 * Returns a pointer to the new instruction, NULL if failed.
493 */
494 static isn_T *
495generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
496{
497 garray_T *stack = &cctx->ctx_type_stack;
498
Bram Moolenaar080457c2020-03-03 21:53:32 +0100499 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 stack->ga_len -= drop;
501 return generate_instr(cctx, isn_type);
502}
503
504/*
505 * Generate instruction "isn_type" and put "type" on the type stack.
506 */
507 static isn_T *
508generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
509{
510 isn_T *isn;
511 garray_T *stack = &cctx->ctx_type_stack;
512
513 if ((isn = generate_instr(cctx, isn_type)) == NULL)
514 return NULL;
515
516 if (ga_grow(stack, 1) == FAIL)
517 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200518 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 ++stack->ga_len;
520
521 return isn;
522}
523
524/*
525 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200526 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 */
528 static int
529may_generate_2STRING(int offset, cctx_T *cctx)
530{
531 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200532 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100534 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100536 RETURN_OK_IF_SKIP(cctx);
537 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200538 switch ((*type)->tt_type)
539 {
540 // nothing to be done
541 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200543 // conversion possible
544 case VAR_SPECIAL:
545 case VAR_BOOL:
546 case VAR_NUMBER:
547 case VAR_FLOAT:
548 break;
549
550 // conversion possible (with runtime check)
551 case VAR_ANY:
552 case VAR_UNKNOWN:
553 isntype = ISN_2STRING_ANY;
554 break;
555
556 // conversion not possible
557 case VAR_VOID:
558 case VAR_BLOB:
559 case VAR_FUNC:
560 case VAR_PARTIAL:
561 case VAR_LIST:
562 case VAR_DICT:
563 case VAR_JOB:
564 case VAR_CHANNEL:
565 to_string_error((*type)->tt_type);
566 return FAIL;
567 }
568
569 *type = &t_string;
570 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return FAIL;
572 isn->isn_arg.number = offset;
573
574 return OK;
575}
576
577 static int
578check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
579{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200580 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200582 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 {
584 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200585 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200587 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 }
590 return OK;
591}
592
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 static int
594generate_add_instr(
595 cctx_T *cctx,
596 vartype_T vartype,
597 type_T *type1,
598 type_T *type2)
599{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100600 garray_T *stack = &cctx->ctx_type_stack;
601 isn_T *isn = generate_instr_drop(cctx,
602 vartype == VAR_NUMBER ? ISN_OPNR
603 : vartype == VAR_LIST ? ISN_ADDLIST
604 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100606 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200607#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100608 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200609
610 if (vartype != VAR_LIST && vartype != VAR_BLOB
611 && type1->tt_type != VAR_ANY
612 && type2->tt_type != VAR_ANY
613 && check_number_or_float(
614 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
615 return FAIL;
616
617 if (isn != NULL)
618 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100619
620 // When concatenating two lists with different member types the member type
621 // becomes "any".
622 if (vartype == VAR_LIST
623 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
624 && type1->tt_member != type2->tt_member)
625 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
626
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200627 return isn == NULL ? FAIL : OK;
628}
629
630/*
631 * Get the type to use for an instruction for an operation on "type1" and
632 * "type2". If they are matching use a type-specific instruction. Otherwise
633 * fall back to runtime type checking.
634 */
635 static vartype_T
636operator_type(type_T *type1, type_T *type2)
637{
638 if (type1->tt_type == type2->tt_type
639 && (type1->tt_type == VAR_NUMBER
640 || type1->tt_type == VAR_LIST
641#ifdef FEAT_FLOAT
642 || type1->tt_type == VAR_FLOAT
643#endif
644 || type1->tt_type == VAR_BLOB))
645 return type1->tt_type;
646 return VAR_ANY;
647}
648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649/*
650 * Generate an instruction with two arguments. The instruction depends on the
651 * type of the arguments.
652 */
653 static int
654generate_two_op(cctx_T *cctx, char_u *op)
655{
656 garray_T *stack = &cctx->ctx_type_stack;
657 type_T *type1;
658 type_T *type2;
659 vartype_T vartype;
660 isn_T *isn;
661
Bram Moolenaar080457c2020-03-03 21:53:32 +0100662 RETURN_OK_IF_SKIP(cctx);
663
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200664 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668
669 switch (*op)
670 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200671 case '+':
672 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 break;
675
676 case '-':
677 case '*':
678 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
679 op) == FAIL)
680 return FAIL;
681 if (vartype == VAR_NUMBER)
682 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
683#ifdef FEAT_FLOAT
684 else if (vartype == VAR_FLOAT)
685 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
686#endif
687 else
688 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
689 if (isn != NULL)
690 isn->isn_arg.op.op_type = *op == '*'
691 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
692 break;
693
Bram Moolenaar4c683752020-04-05 21:38:23 +0200694 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200696 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && type2->tt_type != VAR_NUMBER))
698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200699 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 return FAIL;
701 }
702 isn = generate_instr_drop(cctx,
703 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
704 if (isn != NULL)
705 isn->isn_arg.op.op_type = EXPR_REM;
706 break;
707 }
708
709 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200710 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 {
712 type_T *type = &t_any;
713
714#ifdef FEAT_FLOAT
715 // float+number and number+float results in float
716 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
717 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
718 type = &t_float;
719#endif
720 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
721 }
722
723 return OK;
724}
725
726/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 * Get the instruction to use for comparing "type1" with "type2"
728 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100731get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732{
733 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
Bram Moolenaar4c683752020-04-05 21:38:23 +0200735 if (type1 == VAR_UNKNOWN)
736 type1 = VAR_ANY;
737 if (type2 == VAR_UNKNOWN)
738 type2 = VAR_ANY;
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 if (type1 == type2)
741 {
742 switch (type1)
743 {
744 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
745 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
746 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
747 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
748 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
749 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
750 case VAR_LIST: isntype = ISN_COMPARELIST; break;
751 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
752 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 default: isntype = ISN_COMPAREANY; break;
754 }
755 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200756 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100758 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 isntype = ISN_COMPAREANY;
760
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 && (isntype == ISN_COMPAREBOOL
763 || isntype == ISN_COMPARESPECIAL
764 || isntype == ISN_COMPARENR
765 || isntype == ISN_COMPAREFLOAT))
766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200767 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200769 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 }
771 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100772 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
774 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
776 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 && (type1 == VAR_BLOB || type2 == VAR_BLOB
778 || type1 == VAR_LIST || type2 == VAR_LIST))))
779 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200780 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200782 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200784 return isntype;
785}
786
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200787 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100788check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200789{
790 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
791 return FAIL;
792 return OK;
793}
794
Bram Moolenaara5565e42020-05-09 15:44:01 +0200795/*
796 * Generate an ISN_COMPARE* instruction with a boolean result.
797 */
798 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100799generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200800{
801 isntype_T isntype;
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804 vartype_T type1;
805 vartype_T type2;
806
807 RETURN_OK_IF_SKIP(cctx);
808
809 // Get the known type of the two items on the stack. If they are matching
810 // use a type-specific instruction. Otherwise fall back to runtime type
811 // checking.
812 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
813 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200815 if (isntype == ISN_DROP)
816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
818 if ((isn = generate_instr(cctx, isntype)) == NULL)
819 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100820 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 isn->isn_arg.op.op_ic = ic;
822
823 // takes two arguments, puts one bool back
824 if (stack->ga_len >= 2)
825 {
826 --stack->ga_len;
827 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
828 }
829
830 return OK;
831}
832
833/*
834 * Generate an ISN_2BOOL instruction.
835 */
836 static int
837generate_2BOOL(cctx_T *cctx, int invert)
838{
839 isn_T *isn;
840 garray_T *stack = &cctx->ctx_type_stack;
841
Bram Moolenaar080457c2020-03-03 21:53:32 +0100842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
844 return FAIL;
845 isn->isn_arg.number = invert;
846
847 // type becomes bool
848 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
849
850 return OK;
851}
852
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200853/*
854 * Generate an ISN_COND2BOOL instruction.
855 */
856 static int
857generate_COND2BOOL(cctx_T *cctx)
858{
859 isn_T *isn;
860 garray_T *stack = &cctx->ctx_type_stack;
861
862 RETURN_OK_IF_SKIP(cctx);
863 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
864 return FAIL;
865
866 // type becomes bool
867 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
868
869 return OK;
870}
871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873generate_TYPECHECK(
874 cctx_T *cctx,
875 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100876 int offset,
877 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878{
879 isn_T *isn;
880 garray_T *stack = &cctx->ctx_type_stack;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
884 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100886 isn->isn_arg.type.ct_off = (int8_T)offset;
887 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar5e654232020-09-16 15:22:00 +0200889 // type becomes expected
890 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
892 return OK;
893}
894
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100895 static int
896generate_SETTYPE(
897 cctx_T *cctx,
898 type_T *expected)
899{
900 isn_T *isn;
901
902 RETURN_OK_IF_SKIP(cctx);
903 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
904 return FAIL;
905 isn->isn_arg.type.ct_type = alloc_type(expected);
906 return OK;
907}
908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
911 * used. Return FALSE if the types will never match.
912 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100913 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200914use_typecheck(type_T *actual, type_T *expected)
915{
916 if (actual->tt_type == VAR_ANY
917 || actual->tt_type == VAR_UNKNOWN
918 || (actual->tt_type == VAR_FUNC
919 && (expected->tt_type == VAR_FUNC
920 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100921 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
922 && ((actual->tt_member == &t_void)
923 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200924 return TRUE;
925 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
926 && actual->tt_type == expected->tt_type)
927 // This takes care of a nested list or dict.
928 return use_typecheck(actual->tt_member, expected->tt_member);
929 return FALSE;
930}
931
932/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200933 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200934 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 * - "actual" is a type that can be "expected" type: add a runtime check; or
936 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200937 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
938 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200939 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100940 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200941need_type(
942 type_T *actual,
943 type_T *expected,
944 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100945 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200946 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200947 int silent,
948 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200949{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100950 where_T where;
951
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200952 if (expected == &t_bool && actual != &t_bool
953 && (actual->tt_flags & TTFLAG_BOOL_OK))
954 {
955 // Using "0", "1" or the result of an expression with "&&" or "||" as a
956 // boolean is OK but requires a conversion.
957 generate_2BOOL(cctx, FALSE);
958 return OK;
959 }
960
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100961 where.wt_index = arg_idx;
962 where.wt_variable = FALSE;
963 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200964 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200965
966 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200967 // If it's a constant a runtime check makes no sense.
968 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200969 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100970 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200973
974 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100975 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200977}
978
979/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100980 * Check that the top of the type stack has a type that can be used as a
981 * condition. Give an error and return FAIL if not.
982 */
983 static int
984bool_on_stack(cctx_T *cctx)
985{
986 garray_T *stack = &cctx->ctx_type_stack;
987 type_T *type;
988
989 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
990 if (type == &t_bool)
991 return OK;
992
993 if (type == &t_any || type == &t_number)
994 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
995 // This requires a runtime type check.
996 return generate_COND2BOOL(cctx);
997
Bram Moolenaar351ead02021-01-16 16:07:01 +0100998 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100999}
1000
1001/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 * Generate an ISN_PUSHNR instruction.
1003 */
1004 static int
1005generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1006{
1007 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001008 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009
Bram Moolenaar080457c2020-03-03 21:53:32 +01001010 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1012 return FAIL;
1013 isn->isn_arg.number = number;
1014
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001015 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001016 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001017 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 return OK;
1019}
1020
1021/*
1022 * Generate an ISN_PUSHBOOL instruction.
1023 */
1024 static int
1025generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.number = number;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHSPEC instruction.
1039 */
1040 static int
1041generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1042{
1043 isn_T *isn;
1044
Bram Moolenaar080457c2020-03-03 21:53:32 +01001045 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1047 return FAIL;
1048 isn->isn_arg.number = number;
1049
1050 return OK;
1051}
1052
1053#ifdef FEAT_FLOAT
1054/*
1055 * Generate an ISN_PUSHF instruction.
1056 */
1057 static int
1058generate_PUSHF(cctx_T *cctx, float_T fnumber)
1059{
1060 isn_T *isn;
1061
Bram Moolenaar080457c2020-03-03 21:53:32 +01001062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.fnumber = fnumber;
1066
1067 return OK;
1068}
1069#endif
1070
1071/*
1072 * Generate an ISN_PUSHS instruction.
1073 * Consumes "str".
1074 */
1075 static int
1076generate_PUSHS(cctx_T *cctx, char_u *str)
1077{
1078 isn_T *isn;
1079
Bram Moolenaar508b5612021-01-02 18:17:26 +01001080 if (cctx->ctx_skip == SKIP_YES)
1081 {
1082 vim_free(str);
1083 return OK;
1084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.string = str;
1088
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 * Generate an ISN_PUSHCHANNEL instruction.
1094 * Consumes "channel".
1095 */
1096 static int
1097generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1098{
1099 isn_T *isn;
1100
Bram Moolenaar080457c2020-03-03 21:53:32 +01001101 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001102 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1103 return FAIL;
1104 isn->isn_arg.channel = channel;
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_PUSHJOB instruction.
1111 * Consumes "job".
1112 */
1113 static int
1114generate_PUSHJOB(cctx_T *cctx, job_T *job)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001120 return FAIL;
1121 isn->isn_arg.job = job;
1122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 * Generate an ISN_PUSHBLOB instruction.
1128 * Consumes "blob".
1129 */
1130 static int
1131generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.blob = blob;
1139
1140 return OK;
1141}
1142
1143/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001144 * Generate an ISN_PUSHFUNC instruction with name "name".
1145 * Consumes "name".
1146 */
1147 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001148generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001153 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001154 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001155 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001161 * Generate an ISN_GETITEM instruction with "index".
1162 */
1163 static int
1164generate_GETITEM(cctx_T *cctx, int index)
1165{
1166 isn_T *isn;
1167 garray_T *stack = &cctx->ctx_type_stack;
1168 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1169 type_T *item_type = &t_any;
1170
1171 RETURN_OK_IF_SKIP(cctx);
1172
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001173 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001174 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 emsg(_(e_listreq));
1177 return FAIL;
1178 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001179 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001180 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1181 return FAIL;
1182 isn->isn_arg.number = index;
1183
1184 // add the item type to the type stack
1185 if (ga_grow(stack, 1) == FAIL)
1186 return FAIL;
1187 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1188 ++stack->ga_len;
1189 return OK;
1190}
1191
1192/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001193 * Generate an ISN_SLICE instruction with "count".
1194 */
1195 static int
1196generate_SLICE(cctx_T *cctx, int count)
1197{
1198 isn_T *isn;
1199
1200 RETURN_OK_IF_SKIP(cctx);
1201 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.number = count;
1204 return OK;
1205}
1206
1207/*
1208 * Generate an ISN_CHECKLEN instruction with "min_len".
1209 */
1210 static int
1211generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1212{
1213 isn_T *isn;
1214
1215 RETURN_OK_IF_SKIP(cctx);
1216
1217 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1218 return FAIL;
1219 isn->isn_arg.checklen.cl_min_len = min_len;
1220 isn->isn_arg.checklen.cl_more_OK = more_OK;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 * Generate an ISN_STORE instruction.
1227 */
1228 static int
1229generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1235 return FAIL;
1236 if (name != NULL)
1237 isn->isn_arg.string = vim_strsave(name);
1238 else
1239 isn->isn_arg.number = idx;
1240
1241 return OK;
1242}
1243
1244/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001245 * Generate an ISN_STOREOUTER instruction.
1246 */
1247 static int
1248generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1249{
1250 isn_T *isn;
1251
1252 RETURN_OK_IF_SKIP(cctx);
1253 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1254 return FAIL;
1255 isn->isn_arg.outer.outer_idx = idx;
1256 isn->isn_arg.outer.outer_depth = level;
1257
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1263 */
1264 static int
1265generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1266{
1267 isn_T *isn;
1268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1271 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001272 isn->isn_arg.storenr.stnr_idx = idx;
1273 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_STOREOPT instruction
1280 */
1281 static int
1282generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001287 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 return FAIL;
1289 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1290 isn->isn_arg.storeopt.so_flags = opt_flags;
1291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_LOAD or similar instruction.
1297 */
1298 static int
1299generate_LOAD(
1300 cctx_T *cctx,
1301 isntype_T isn_type,
1302 int idx,
1303 char_u *name,
1304 type_T *type)
1305{
1306 isn_T *isn;
1307
Bram Moolenaar080457c2020-03-03 21:53:32 +01001308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1310 return FAIL;
1311 if (name != NULL)
1312 isn->isn_arg.string = vim_strsave(name);
1313 else
1314 isn->isn_arg.number = idx;
1315
1316 return OK;
1317}
1318
1319/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001320 * Generate an ISN_LOADOUTER instruction
1321 */
1322 static int
1323generate_LOADOUTER(
1324 cctx_T *cctx,
1325 int idx,
1326 int nesting,
1327 type_T *type)
1328{
1329 isn_T *isn;
1330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.outer.outer_idx = idx;
1335 isn->isn_arg.outer.outer_depth = nesting;
1336
1337 return OK;
1338}
1339
1340/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001341 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 */
1343 static int
1344generate_LOADV(
1345 cctx_T *cctx,
1346 char_u *name,
1347 int error)
1348{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001349 int di_flags;
1350 int vidx = find_vim_var(name, &di_flags);
1351 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352
Bram Moolenaar080457c2020-03-03 21:53:32 +01001353 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 if (vidx < 0)
1355 {
1356 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001357 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001358 return FAIL;
1359 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001362 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363}
1364
1365/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366 * Generate an ISN_UNLET instruction.
1367 */
1368 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001369generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001370{
1371 isn_T *isn;
1372
1373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001374 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001375 return FAIL;
1376 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1377 isn->isn_arg.unlet.ul_forceit = forceit;
1378
1379 return OK;
1380}
1381
1382/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001383 * Generate an ISN_LOCKCONST instruction.
1384 */
1385 static int
1386generate_LOCKCONST(cctx_T *cctx)
1387{
1388 isn_T *isn;
1389
1390 RETURN_OK_IF_SKIP(cctx);
1391 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1392 return FAIL;
1393 return OK;
1394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Generate an ISN_LOADS instruction.
1398 */
1399 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001402 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001404 int sid,
1405 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406{
1407 isn_T *isn;
1408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001410 if (isn_type == ISN_LOADS)
1411 isn = generate_instr_type(cctx, isn_type, type);
1412 else
1413 isn = generate_instr_drop(cctx, isn_type, 1);
1414 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001416 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1417 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 return OK;
1420}
1421
1422/*
1423 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1424 */
1425 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001426generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 cctx_T *cctx,
1428 isntype_T isn_type,
1429 int sid,
1430 int idx,
1431 type_T *type)
1432{
1433 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001434 scriptref_T *sref;
1435 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
Bram Moolenaar080457c2020-03-03 21:53:32 +01001437 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 if (isn_type == ISN_LOADSCRIPT)
1439 isn = generate_instr_type(cctx, isn_type, type);
1440 else
1441 isn = generate_instr_drop(cctx, isn_type, 1);
1442 if (isn == NULL)
1443 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001444
1445 // This requires three arguments, which doesn't fit in an instruction, thus
1446 // we need to allocate a struct for this.
1447 sref = ALLOC_ONE(scriptref_T);
1448 if (sref == NULL)
1449 return FAIL;
1450 isn->isn_arg.script.scriptref = sref;
1451 sref->sref_sid = sid;
1452 sref->sref_idx = idx;
1453 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001454 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 return OK;
1456}
1457
1458/*
1459 * Generate an ISN_NEWLIST instruction.
1460 */
1461 static int
1462generate_NEWLIST(cctx_T *cctx, int count)
1463{
1464 isn_T *isn;
1465 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 type_T *type;
1467 type_T *member;
1468
Bram Moolenaar080457c2020-03-03 21:53:32 +01001469 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1471 return FAIL;
1472 isn->isn_arg.number = count;
1473
Bram Moolenaar127542b2020-08-09 17:22:04 +02001474 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001475 if (count == 0)
1476 member = &t_void;
1477 else
1478 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001479 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1480 cctx->ctx_type_list);
1481 type = get_list_type(member, cctx->ctx_type_list);
1482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 // drop the value types
1484 stack->ga_len -= count;
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // add the list type to the type stack
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1490 ++stack->ga_len;
1491
1492 return OK;
1493}
1494
1495/*
1496 * Generate an ISN_NEWDICT instruction.
1497 */
1498 static int
1499generate_NEWDICT(cctx_T *cctx, int count)
1500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 type_T *type;
1504 type_T *member;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.number = count;
1510
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001511 if (count == 0)
1512 member = &t_void;
1513 else
1514 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001515 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1516 cctx->ctx_type_list);
1517 type = get_dict_type(member, cctx->ctx_type_list);
1518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 // drop the key and value types
1520 stack->ga_len -= 2 * count;
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // add the dict type to the type stack
1523 if (ga_grow(stack, 1) == FAIL)
1524 return FAIL;
1525 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1526 ++stack->ga_len;
1527
1528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_FUNCREF instruction.
1533 */
1534 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001535generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539
Bram Moolenaar080457c2020-03-03 21:53:32 +01001540 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1542 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001543 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001544 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001546 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001547 // the nested context, including this one.
1548 if (ufunc->uf_flags & FC_CLOSURE)
1549 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if (ga_grow(stack, 1) == FAIL)
1552 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001553 ((type_T **)stack->ga_data)[stack->ga_len] =
1554 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 ++stack->ga_len;
1556
1557 return OK;
1558}
1559
1560/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001561 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001562 * "lambda_name" and "func_name" must be in allocated memory and will be
1563 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 */
1565 static int
1566generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1567{
1568 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001569
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001570 if (cctx->ctx_skip == SKIP_YES)
1571 {
1572 vim_free(lambda_name);
1573 vim_free(func_name);
1574 return OK;
1575 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 {
1578 vim_free(lambda_name);
1579 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001580 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001581 }
1582 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 isn->isn_arg.newfunc.nf_global = func_name;
1584
1585 return OK;
1586}
1587
1588/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001589 * Generate an ISN_DEF instruction: list functions
1590 */
1591 static int
1592generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1593{
1594 isn_T *isn;
1595
1596 RETURN_OK_IF_SKIP(cctx);
1597 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1598 return FAIL;
1599 if (len > 0)
1600 {
1601 isn->isn_arg.string = vim_strnsave(name, len);
1602 if (isn->isn_arg.string == NULL)
1603 return FAIL;
1604 }
1605 return OK;
1606}
1607
1608/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 * Generate an ISN_JUMP instruction.
1610 */
1611 static int
1612generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1613{
1614 isn_T *isn;
1615 garray_T *stack = &cctx->ctx_type_stack;
1616
Bram Moolenaar080457c2020-03-03 21:53:32 +01001617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.jump.jump_when = when;
1621 isn->isn_arg.jump.jump_where = where;
1622
1623 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1624 --stack->ga_len;
1625
1626 return OK;
1627}
1628
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001629/*
1630 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1631 */
1632 static int
1633generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1634{
1635 isn_T *isn;
1636
1637 RETURN_OK_IF_SKIP(cctx);
1638 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1641 // jump_where is set later
1642 return OK;
1643}
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 static int
1646generate_FOR(cctx_T *cctx, int loop_idx)
1647{
1648 isn_T *isn;
1649 garray_T *stack = &cctx->ctx_type_stack;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.forloop.for_idx = loop_idx;
1655
1656 if (ga_grow(stack, 1) == FAIL)
1657 return FAIL;
1658 // type doesn't matter, will be stored next
1659 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1660 ++stack->ga_len;
1661
1662 return OK;
1663}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001664/*
1665 * Generate an ISN_TRYCONT instruction.
1666 */
1667 static int
1668generate_TRYCONT(cctx_T *cctx, int levels, int where)
1669{
1670 isn_T *isn;
1671
1672 RETURN_OK_IF_SKIP(cctx);
1673 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1674 return FAIL;
1675 isn->isn_arg.trycont.tct_levels = levels;
1676 isn->isn_arg.trycont.tct_where = where;
1677
1678 return OK;
1679}
1680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681
1682/*
1683 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 * Return FAIL if the number of arguments is wrong.
1686 */
1687 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001688generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689{
1690 isn_T *isn;
1691 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001692 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001693 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001694 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
Bram Moolenaar080457c2020-03-03 21:53:32 +01001696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 argoff = check_internal_func(func_idx, argcount);
1698 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 return FAIL;
1700
Bram Moolenaar389df252020-07-09 21:20:47 +02001701 if (method_call && argoff > 1)
1702 {
1703 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.shuffle.shfl_item = argcount;
1706 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1707 }
1708
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001709 if (argcount > 0)
1710 {
1711 // Check the types of the arguments.
1712 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001713 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1714 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001716 if (internal_func_is_map(func_idx))
1717 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001718 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1721 return FAIL;
1722 isn->isn_arg.bfunc.cbf_idx = func_idx;
1723 isn->isn_arg.bfunc.cbf_argcount = argcount;
1724
Bram Moolenaar94738d82020-10-21 14:25:07 +02001725 // Drop the argument types and push the return type.
1726 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 if (ga_grow(stack, 1) == FAIL)
1728 return FAIL;
1729 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001730 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001731 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733 if (maptype != NULL && maptype->tt_member != NULL
1734 && maptype->tt_member != &t_any)
1735 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001736 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 return OK;
1739}
1740
1741/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001742 * Generate an ISN_LISTAPPEND instruction. Works like add().
1743 * Argument count is already checked.
1744 */
1745 static int
1746generate_LISTAPPEND(cctx_T *cctx)
1747{
1748 garray_T *stack = &cctx->ctx_type_stack;
1749 type_T *list_type;
1750 type_T *item_type;
1751 type_T *expected;
1752
1753 // Caller already checked that list_type is a list.
1754 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1755 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1756 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001757 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001758 return FAIL;
1759
1760 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1761 return FAIL;
1762
1763 --stack->ga_len; // drop the argument
1764 return OK;
1765}
1766
1767/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001768 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1769 * Argument count is already checked.
1770 */
1771 static int
1772generate_BLOBAPPEND(cctx_T *cctx)
1773{
1774 garray_T *stack = &cctx->ctx_type_stack;
1775 type_T *item_type;
1776
1777 // Caller already checked that blob_type is a blob.
1778 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001779 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001780 return FAIL;
1781
1782 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1783 return FAIL;
1784
1785 --stack->ga_len; // drop the argument
1786 return OK;
1787}
1788
1789/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 * Return TRUE if "ufunc" should be compiled, taking into account whether
1791 * "profile" indicates profiling is to be done.
1792 */
1793 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001794func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001795{
1796 switch (ufunc->uf_def_status)
1797 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001798 case UF_TO_BE_COMPILED:
1799 return TRUE;
1800
Bram Moolenaarb2049902021-01-24 12:53:53 +01001801 case UF_COMPILED:
1802 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001803#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001804 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1805 + ufunc->uf_dfunc_idx;
1806
1807 return profile ? dfunc->df_instr_prof == NULL
1808 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001809#else
1810 break;
1811#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001812 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001813
1814 case UF_NOT_COMPILED:
1815 case UF_COMPILE_ERROR:
1816 case UF_COMPILING:
1817 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001818 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001819 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001820}
1821
1822/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 * Generate an ISN_DCALL or ISN_UCALL instruction.
1824 * Return FAIL if the number of arguments is wrong.
1825 */
1826 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001827generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
1831 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001832 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
Bram Moolenaar080457c2020-03-03 21:53:32 +01001834 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 if (argcount > regular_args && !has_varargs(ufunc))
1836 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001837 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 return FAIL;
1839 }
1840 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1841 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001842 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 return FAIL;
1844 }
1845
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001846 if (ufunc->uf_def_status != UF_NOT_COMPILED
1847 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001848 {
1849 int i;
1850
1851 for (i = 0; i < argcount; ++i)
1852 {
1853 type_T *expected;
1854 type_T *actual;
1855
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001856 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1857 if (actual == &t_special
1858 && i >= regular_args - ufunc->uf_def_args.ga_len)
1859 {
1860 // assume v:none used for default argument value
1861 continue;
1862 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001863 if (i < regular_args)
1864 {
1865 if (ufunc->uf_arg_types == NULL)
1866 continue;
1867 expected = ufunc->uf_arg_types[i];
1868 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001869 else if (ufunc->uf_va_type == NULL
1870 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001871 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001872 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 else
1874 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001875 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001876 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001877 {
1878 arg_type_mismatch(expected, actual, i + 1);
1879 return FAIL;
1880 }
1881 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001882 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001883 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001884 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001885 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001886 }
1887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001889 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001890 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001892 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 {
1894 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1895 isn->isn_arg.dfunc.cdf_argcount = argcount;
1896 }
1897 else
1898 {
1899 // A user function may be deleted and redefined later, can't use the
1900 // ufunc pointer, need to look it up again at runtime.
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903 }
1904
1905 stack->ga_len -= argcount; // drop the arguments
1906 if (ga_grow(stack, 1) == FAIL)
1907 return FAIL;
1908 // add return value
1909 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1910 ++stack->ga_len;
1911
1912 return OK;
1913}
1914
1915/*
1916 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1917 */
1918 static int
1919generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1920{
1921 isn_T *isn;
1922 garray_T *stack = &cctx->ctx_type_stack;
1923
Bram Moolenaar080457c2020-03-03 21:53:32 +01001924 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1926 return FAIL;
1927 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1928 isn->isn_arg.ufunc.cuf_argcount = argcount;
1929
1930 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001931 if (ga_grow(stack, 1) == FAIL)
1932 return FAIL;
1933 // add return value
1934 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1935 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
1937 return OK;
1938}
1939
1940/*
1941 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001942 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 */
1944 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001945generate_PCALL(
1946 cctx_T *cctx,
1947 int argcount,
1948 char_u *name,
1949 type_T *type,
1950 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951{
1952 isn_T *isn;
1953 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaar080457c2020-03-03 21:53:32 +01001956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001957
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001958 if (type->tt_type == VAR_ANY)
1959 ret_type = &t_any;
1960 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001961 {
1962 if (type->tt_argcount != -1)
1963 {
1964 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1965
1966 if (argcount < type->tt_min_argcount - varargs)
1967 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001968 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001969 return FAIL;
1970 }
1971 if (!varargs && argcount > type->tt_argcount)
1972 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001973 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 return FAIL;
1975 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001976 if (type->tt_args != NULL)
1977 {
1978 int i;
1979
1980 for (i = 0; i < argcount; ++i)
1981 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02001982 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001983 type_T *actual = ((type_T **)stack->ga_data)[
1984 stack->ga_len + offset];
1985 type_T *expected;
1986
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001987 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001988 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001989 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001990 else if (i >= type->tt_min_argcount
1991 && actual == &t_special)
1992 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001993 else
1994 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001995 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001996 cctx, TRUE, FALSE) == FAIL)
1997 {
1998 arg_type_mismatch(expected, actual, i + 1);
1999 return FAIL;
2000 }
2001 }
2002 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002003 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002004 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002005 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002006 else
2007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002008 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002009 return FAIL;
2010 }
2011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2013 return FAIL;
2014 isn->isn_arg.pfunc.cpf_top = at_top;
2015 isn->isn_arg.pfunc.cpf_argcount = argcount;
2016
2017 stack->ga_len -= argcount; // drop the arguments
2018
2019 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002022 // If partial is above the arguments it must be cleared and replaced with
2023 // the return value.
2024 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2025 return FAIL;
2026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 return OK;
2028}
2029
2030/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002031 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 */
2033 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002034generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035{
2036 isn_T *isn;
2037 garray_T *stack = &cctx->ctx_type_stack;
2038 type_T *type;
2039
Bram Moolenaar080457c2020-03-03 21:53:32 +01002040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002041 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002043 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002045 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002047 if (type->tt_type != VAR_DICT && type != &t_any)
2048 {
2049 emsg(_(e_dictreq));
2050 return FAIL;
2051 }
2052 // change dict type to dict member type
2053 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002054 {
2055 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2056 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058
2059 return OK;
2060}
2061
2062/*
2063 * Generate an ISN_ECHO instruction.
2064 */
2065 static int
2066generate_ECHO(cctx_T *cctx, int with_white, int count)
2067{
2068 isn_T *isn;
2069
Bram Moolenaar080457c2020-03-03 21:53:32 +01002070 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2072 return FAIL;
2073 isn->isn_arg.echo.echo_with_white = with_white;
2074 isn->isn_arg.echo.echo_count = count;
2075
2076 return OK;
2077}
2078
Bram Moolenaarad39c092020-02-26 18:23:43 +01002079/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002080 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 */
2082 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084{
2085 isn_T *isn;
2086
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002087 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002088 return FAIL;
2089 isn->isn_arg.number = count;
2090
2091 return OK;
2092}
2093
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002094/*
2095 * Generate an ISN_PUT instruction.
2096 */
2097 static int
2098generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2099{
2100 isn_T *isn;
2101
2102 RETURN_OK_IF_SKIP(cctx);
2103 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.put.put_regname = regname;
2106 isn->isn_arg.put.put_lnum = lnum;
2107 return OK;
2108}
2109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 static int
2111generate_EXEC(cctx_T *cctx, char_u *line)
2112{
2113 isn_T *isn;
2114
Bram Moolenaar080457c2020-03-03 21:53:32 +01002115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2117 return FAIL;
2118 isn->isn_arg.string = vim_strsave(line);
2119 return OK;
2120}
2121
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002122 static int
2123generate_EXECCONCAT(cctx_T *cctx, int count)
2124{
2125 isn_T *isn;
2126
2127 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2128 return FAIL;
2129 isn->isn_arg.number = count;
2130 return OK;
2131}
2132
Bram Moolenaar08597872020-12-10 19:43:40 +01002133/*
2134 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2135 */
2136 static int
2137generate_RANGE(cctx_T *cctx, char_u *range)
2138{
2139 isn_T *isn;
2140 garray_T *stack = &cctx->ctx_type_stack;
2141
2142 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2143 return FAIL;
2144 isn->isn_arg.string = range;
2145
2146 if (ga_grow(stack, 1) == FAIL)
2147 return FAIL;
2148 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2149 ++stack->ga_len;
2150 return OK;
2151}
2152
Bram Moolenaar792f7862020-11-23 08:31:18 +01002153 static int
2154generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2155{
2156 isn_T *isn;
2157
2158 RETURN_OK_IF_SKIP(cctx);
2159 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2160 return FAIL;
2161 isn->isn_arg.unpack.unp_count = var_count;
2162 isn->isn_arg.unpack.unp_semicolon = semicolon;
2163 return OK;
2164}
2165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002167 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 */
2169 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002170generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171{
2172 isn_T *isn;
2173
Bram Moolenaarfa984412021-03-25 22:15:28 +01002174 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 cctx->ctx_has_cmdmod = TRUE;
2177
2178 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002179 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002180 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2181 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2182 return FAIL;
2183 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002184 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002185 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002186 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002187
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002188 return OK;
2189}
2190
2191 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002192generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002194 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2195 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002196 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002197 return OK;
2198}
2199
Bram Moolenaarfa984412021-03-25 22:15:28 +01002200 static int
2201misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002202{
2203 garray_T *instr = &cctx->ctx_instr;
2204
Bram Moolenaara91a7132021-03-25 21:12:15 +01002205 if (cctx->ctx_has_cmdmod
2206 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2207 == ISN_CMDMOD)
2208 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002209 emsg(_(e_misplaced_command_modifier));
2210 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002211 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002212 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002213}
2214
2215/*
2216 * Get the index of the current instruction.
2217 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2218 */
2219 static int
2220current_instr_idx(cctx_T *cctx)
2221{
2222 garray_T *instr = &cctx->ctx_instr;
2223 int idx = instr->ga_len;
2224
2225 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_CMDMOD)
2227 --idx;
2228#ifdef FEAT_PROFILE
2229 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2230 .isn_type == ISN_PROF_START)
2231 --idx;
2232#endif
2233 return idx;
2234}
2235
Bram Moolenaarf002a412021-01-24 13:34:18 +01002236#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 static void
2238may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2239{
2240 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002241 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002242}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002243#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002244
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002247 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002249 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002250reserve_local(
2251 cctx_T *cctx,
2252 char_u *name,
2253 size_t len,
2254 int isConst,
2255 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 lvar_T *lvar;
2258
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002259 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002261 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 }
2264
2265 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 return NULL;
2267 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002268 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002270 // Every local variable uses the next entry on the stack. We could re-use
2271 // the last ones when leaving a scope, but then variables used in a closure
2272 // might get overwritten. To keep things simple do not re-use stack
2273 // entries. This is less efficient, but memory is cheap these days.
2274 lvar->lv_idx = cctx->ctx_locals_count++;
2275
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002276 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 lvar->lv_const = isConst;
2278 lvar->lv_type = type;
2279
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002280 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281}
2282
2283/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002284 * Remove local variables above "new_top".
2285 */
2286 static void
2287unwind_locals(cctx_T *cctx, int new_top)
2288{
2289 if (cctx->ctx_locals.ga_len > new_top)
2290 {
2291 int idx;
2292 lvar_T *lvar;
2293
2294 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2295 {
2296 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2297 vim_free(lvar->lv_name);
2298 }
2299 }
2300 cctx->ctx_locals.ga_len = new_top;
2301}
2302
2303/*
2304 * Free all local variables.
2305 */
2306 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002307free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002308{
2309 unwind_locals(cctx, 0);
2310 ga_clear(&cctx->ctx_locals);
2311}
2312
2313/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002314 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2315 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2316 * error if the variable was defined with :const.
2317 */
2318 static int
2319check_item_writable(svar_T *sv, int check_writable, char_u *name)
2320{
2321 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2322 || (check_writable == ASSIGN_FINAL
2323 && sv->sv_const == ASSIGN_CONST))
2324 {
2325 semsg(_(e_readonlyvar), name);
2326 return FAIL;
2327 }
2328 return OK;
2329}
2330
2331/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002333 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 * Returns the index in "sn_var_vals" if found.
2335 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002336 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 */
2338 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002339get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340{
2341 hashtab_T *ht;
2342 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002343 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002344 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 int idx;
2346
Bram Moolenaare3d46852020-08-29 13:39:17 +02002347 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002349 if (sid == current_sctx.sc_sid)
2350 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002351 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002352
2353 if (sav == NULL)
2354 return -2;
2355 idx = sav->sav_var_vals_idx;
2356 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002357 if (check_item_writable(sv, check_writable, name) == FAIL)
2358 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002359 return idx;
2360 }
2361
2362 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 ht = &SCRIPT_VARS(sid);
2364 di = find_var_in_ht(ht, 0, name, TRUE);
2365 if (di == NULL)
2366 return -2;
2367
2368 // Now find the svar_T index in sn_var_vals.
2369 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2370 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002371 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (sv->sv_tv == &di->di_tv)
2373 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002374 if (check_item_writable(sv, check_writable, name) == FAIL)
2375 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 return idx;
2377 }
2378 }
2379 return -1;
2380}
2381
2382/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002383 * Find "name" in imported items of the current script or in "cctx" if not
2384 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 */
2386 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002387find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 int idx;
2390
Bram Moolenaare3d46852020-08-29 13:39:17 +02002391 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002392 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 if (cctx != NULL)
2394 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2395 {
2396 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2397 + idx;
2398
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002399 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2400 : STRLEN(import->imp_name) == len
2401 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return import;
2403 }
2404
Bram Moolenaarefa94442020-08-08 22:16:00 +02002405 return find_imported_in_script(name, len, current_sctx.sc_sid);
2406}
2407
2408 imported_T *
2409find_imported_in_script(char_u *name, size_t len, int sid)
2410{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002411 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002412 int idx;
2413
Bram Moolenaare3d46852020-08-29 13:39:17 +02002414 if (!SCRIPT_ID_VALID(sid))
2415 return NULL;
2416 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2418 {
2419 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2420
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002421 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2422 : STRLEN(import->imp_name) == len
2423 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 return import;
2425 }
2426 return NULL;
2427}
2428
2429/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002430 * Free all imported variables.
2431 */
2432 static void
2433free_imported(cctx_T *cctx)
2434{
2435 int idx;
2436
2437 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2438 {
2439 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2440
2441 vim_free(import->imp_name);
2442 }
2443 ga_clear(&cctx->ctx_imports);
2444}
2445
2446/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002447 * Return a pointer to the next line that isn't empty or only contains a
2448 * comment. Skips over white space.
2449 * Returns NULL if there is none.
2450 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002451 char_u *
2452peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002453{
2454 int lnum = cctx->ctx_lnum;
2455
2456 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2457 {
2458 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002459 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002460
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002461 // ignore NULLs inserted for continuation lines
2462 if (line != NULL)
2463 {
2464 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002465 if (vim9_bad_comment(p))
2466 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002467 if (*p != NUL && !vim9_comment_start(p))
2468 return p;
2469 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002470 }
2471 return NULL;
2472}
2473
2474/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002475 * Called when checking for a following operator at "arg". When the rest of
2476 * the line is empty or only a comment, peek the next line. If there is a next
2477 * line return a pointer to it and set "nextp".
2478 * Otherwise skip over white space.
2479 */
2480 static char_u *
2481may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2482{
2483 char_u *p = skipwhite(arg);
2484
2485 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002486 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002487 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002488 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002489 if (*nextp != NULL)
2490 return *nextp;
2491 }
2492 return p;
2493}
2494
2495/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498 * Returns NULL when at the end.
2499 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002500 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002501next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002502{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002503 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002504
2505 do
2506 {
2507 ++cctx->ctx_lnum;
2508 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002509 {
2510 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002511 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002512 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002513 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002514 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002515 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002516 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002517 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002518 return line;
2519}
2520
2521/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002522 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002523 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2526 */
2527 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002529{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002530 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002531 if (vim9_bad_comment(*arg))
2532 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002533 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002536
2537 if (next == NULL)
2538 return FAIL;
2539 *arg = skipwhite(next);
2540 }
2541 return OK;
2542}
2543
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002544/*
2545 * Idem, and give an error when failed.
2546 */
2547 static int
2548may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2549{
2550 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2551 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002552 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002553 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002554 return FAIL;
2555 }
2556 return OK;
2557}
2558
2559
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560// Structure passed between the compile_expr* functions to keep track of
2561// constants that have been parsed but for which no code was produced yet. If
2562// possible expressions on these constants are applied at compile time. If
2563// that is not possible, the code to push the constants needs to be generated
2564// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002565// Using 50 should be more than enough of 5 levels of ().
2566#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002567typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002568 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002569 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570 int pp_is_const; // all generated code was constants, used for a
2571 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002572} ppconst_T;
2573
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002574static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002575static int compile_expr0(char_u **arg, cctx_T *cctx);
2576static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578/*
2579 * Generate a PUSH instruction for "tv".
2580 * "tv" will be consumed or cleared.
2581 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2582 */
2583 static int
2584generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2585{
2586 if (tv != NULL)
2587 {
2588 switch (tv->v_type)
2589 {
2590 case VAR_UNKNOWN:
2591 break;
2592 case VAR_BOOL:
2593 generate_PUSHBOOL(cctx, tv->vval.v_number);
2594 break;
2595 case VAR_SPECIAL:
2596 generate_PUSHSPEC(cctx, tv->vval.v_number);
2597 break;
2598 case VAR_NUMBER:
2599 generate_PUSHNR(cctx, tv->vval.v_number);
2600 break;
2601#ifdef FEAT_FLOAT
2602 case VAR_FLOAT:
2603 generate_PUSHF(cctx, tv->vval.v_float);
2604 break;
2605#endif
2606 case VAR_BLOB:
2607 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2608 tv->vval.v_blob = NULL;
2609 break;
2610 case VAR_STRING:
2611 generate_PUSHS(cctx, tv->vval.v_string);
2612 tv->vval.v_string = NULL;
2613 break;
2614 default:
2615 iemsg("constant type not supported");
2616 clear_tv(tv);
2617 return FAIL;
2618 }
2619 tv->v_type = VAR_UNKNOWN;
2620 }
2621 return OK;
2622}
2623
2624/*
2625 * Generate code for any ppconst entries.
2626 */
2627 static int
2628generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2629{
2630 int i;
2631 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002632 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002634 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635 for (i = 0; i < ppconst->pp_used; ++i)
2636 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2637 ret = FAIL;
2638 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002639 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640 return ret;
2641}
2642
2643/*
2644 * Clear ppconst constants. Used when failing.
2645 */
2646 static void
2647clear_ppconst(ppconst_T *ppconst)
2648{
2649 int i;
2650
2651 for (i = 0; i < ppconst->pp_used; ++i)
2652 clear_tv(&ppconst->pp_tv[i]);
2653 ppconst->pp_used = 0;
2654}
2655
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002656/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002657 * Compile getting a member from a list/dict/string/blob. Stack has the
2658 * indexable value and the index.
2659 */
2660 static int
2661compile_member(int is_slice, cctx_T *cctx)
2662{
2663 type_T **typep;
2664 garray_T *stack = &cctx->ctx_type_stack;
2665 vartype_T vtype;
2666 type_T *valtype;
2667
2668 // We can index a list and a dict. If we don't know the type
2669 // we can use the index value type.
2670 // TODO: If we don't know use an instruction to figure it out at
2671 // runtime.
2672 typep = ((type_T **)stack->ga_data) + stack->ga_len
2673 - (is_slice ? 3 : 2);
2674 vtype = (*typep)->tt_type;
2675 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2676 // If the index is a string, the variable must be a Dict.
2677 if (*typep == &t_any && valtype == &t_string)
2678 vtype = VAR_DICT;
2679 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2680 {
2681 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2682 return FAIL;
2683 if (is_slice)
2684 {
2685 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2686 if (need_type(valtype, &t_number, -2, 0, cctx,
2687 FALSE, FALSE) == FAIL)
2688 return FAIL;
2689 }
2690 }
2691
2692 if (vtype == VAR_DICT)
2693 {
2694 if (is_slice)
2695 {
2696 emsg(_(e_cannot_slice_dictionary));
2697 return FAIL;
2698 }
2699 if ((*typep)->tt_type == VAR_DICT)
2700 {
2701 *typep = (*typep)->tt_member;
2702 if (*typep == &t_unknown)
2703 // empty dict was used
2704 *typep = &t_any;
2705 }
2706 else
2707 {
2708 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2709 FALSE, FALSE) == FAIL)
2710 return FAIL;
2711 *typep = &t_any;
2712 }
2713 if (may_generate_2STRING(-1, cctx) == FAIL)
2714 return FAIL;
2715 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2716 return FAIL;
2717 }
2718 else if (vtype == VAR_STRING)
2719 {
2720 *typep = &t_string;
2721 if ((is_slice
2722 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2723 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2724 return FAIL;
2725 }
2726 else if (vtype == VAR_BLOB)
2727 {
2728 emsg("Sorry, blob index and slice not implemented yet");
2729 return FAIL;
2730 }
2731 else if (vtype == VAR_LIST || *typep == &t_any)
2732 {
2733 if (is_slice)
2734 {
2735 if (generate_instr_drop(cctx,
2736 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2737 2) == FAIL)
2738 return FAIL;
2739 }
2740 else
2741 {
2742 if ((*typep)->tt_type == VAR_LIST)
2743 {
2744 *typep = (*typep)->tt_member;
2745 if (*typep == &t_unknown)
2746 // empty list was used
2747 *typep = &t_any;
2748 }
2749 if (generate_instr_drop(cctx,
2750 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2751 return FAIL;
2752 }
2753 }
2754 else
2755 {
2756 emsg(_(e_string_list_dict_or_blob_required));
2757 return FAIL;
2758 }
2759 return OK;
2760}
2761
2762/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002763 * Generate an instruction to load script-local variable "name", without the
2764 * leading "s:".
2765 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 */
2767 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002768compile_load_scriptvar(
2769 cctx_T *cctx,
2770 char_u *name, // variable NUL terminated
2771 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002772 char_u **end, // end of variable
2773 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002775 scriptitem_T *si;
2776 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 imported_T *import;
2778
Bram Moolenaare3d46852020-08-29 13:39:17 +02002779 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2780 return FAIL;
2781 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002782 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002783 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002785 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002786 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2787 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 }
2789 if (idx >= 0)
2790 {
2791 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2792
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002793 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 current_sctx.sc_sid, idx, sv->sv_type);
2795 return OK;
2796 }
2797
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002798 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 if (import != NULL)
2800 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002801 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 {
2803 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002804 char_u *exp_name;
2805 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002806 ufunc_T *ufunc;
2807 type_T *type;
2808
2809 // Used "import * as Name", need to lookup the member.
2810 if (*p != '.')
2811 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002812 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813 return FAIL;
2814 }
2815 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002816 if (VIM_ISWHITE(*p))
2817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002818 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002819 return FAIL;
2820 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002821
Bram Moolenaar1c991142020-07-04 13:15:31 +02002822 // isolate one name
2823 exp_name = p;
2824 while (eval_isnamec(*p))
2825 ++p;
2826 cc = *p;
2827 *p = NUL;
2828
Bram Moolenaaredba7072021-03-13 21:14:18 +01002829 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2830 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002831 *p = cc;
2832 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002833 *end = p;
2834
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002835 if (idx < 0)
2836 {
2837 if (*p == '(' && ufunc != NULL)
2838 {
2839 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2840 return OK;
2841 }
2842 return FAIL;
2843 }
2844
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002845 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2846 import->imp_sid,
2847 idx,
2848 type);
2849 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002850 else if (import->imp_funcname != NULL)
2851 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002852 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002853 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2854 import->imp_sid,
2855 import->imp_var_vals_idx,
2856 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 return OK;
2858 }
2859
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002860 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002861 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return FAIL;
2863}
2864
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865 static int
2866generate_funcref(cctx_T *cctx, char_u *name)
2867{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002868 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002869
2870 if (ufunc == NULL)
2871 return FAIL;
2872
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002873 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002874 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2875 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002876 == FAIL)
2877 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002878 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002879}
2880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881/*
2882 * Compile a variable name into a load instruction.
2883 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002884 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 * When "error" is FALSE do not give an error when not found.
2886 */
2887 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002888compile_load(
2889 char_u **arg,
2890 char_u *end_arg,
2891 cctx_T *cctx,
2892 int is_expr,
2893 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
2895 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002896 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002897 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002899 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
2901 if (*(*arg + 1) == ':')
2902 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002903 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002904 {
2905 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906
Bram Moolenaarfa596382021-04-07 21:58:16 +02002907 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002908 switch (**arg)
2909 {
2910 case 'g': isn_type = ISN_LOADGDICT; break;
2911 case 'w': isn_type = ISN_LOADWDICT; break;
2912 case 't': isn_type = ISN_LOADTDICT; break;
2913 case 'b': isn_type = ISN_LOADBDICT; break;
2914 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002915 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002916 goto theend;
2917 }
2918 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2919 goto theend;
2920 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 else
2923 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002924 isntype_T isn_type = ISN_DROP;
2925
Bram Moolenaarfa596382021-04-07 21:58:16 +02002926 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002927 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2928 if (name == NULL)
2929 return FAIL;
2930
2931 switch (**arg)
2932 {
2933 case 'v': res = generate_LOADV(cctx, name, error);
2934 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002935 case 's': if (is_expr && ASCII_ISUPPER(*name)
2936 && find_func(name, FALSE, cctx) != NULL)
2937 res = generate_funcref(cctx, name);
2938 else
2939 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002940 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002941 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002942 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002943 {
2944 if (is_expr && ASCII_ISUPPER(*name)
2945 && find_func(name, FALSE, cctx) != NULL)
2946 res = generate_funcref(cctx, name);
2947 else
2948 isn_type = ISN_LOADG;
2949 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002950 else
2951 {
2952 isn_type = ISN_LOADAUTO;
2953 vim_free(name);
2954 name = vim_strnsave(*arg, end - *arg);
2955 if (name == NULL)
2956 return FAIL;
2957 }
2958 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002959 case 'w': isn_type = ISN_LOADW; break;
2960 case 't': isn_type = ISN_LOADT; break;
2961 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002962 default: // cannot happen, just in case
2963 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002964 goto theend;
2965 }
2966 if (isn_type != ISN_DROP)
2967 {
2968 // Global, Buffer-local, Window-local and Tabpage-local
2969 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002970 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002971 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 }
2974 }
2975 else
2976 {
2977 size_t len = end - *arg;
2978 int idx;
2979 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002980 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981
2982 name = vim_strnsave(*arg, end - *arg);
2983 if (name == NULL)
2984 return FAIL;
2985
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002986 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002988 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002989 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 else
2992 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002993 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002994
Bram Moolenaar709664c2020-12-12 14:33:41 +01002995 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002997 type = lvar.lv_type;
2998 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002999 if (lvar.lv_from_outer != 0)
3000 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003001 else
3002 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
3004 else
3005 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003006 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003007 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003008 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003009 || find_imported(name, 0, cctx) != NULL)
3010 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003011
Bram Moolenaar0f769812020-09-12 18:32:34 +02003012 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003013 // uppercase letter it can be a user defined function.
3014 // generate_funcref() will fail if the function can't be found.
3015 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003016 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 }
3018 }
3019 if (gen_load)
3020 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003021 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003022 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003023 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003024 cctx->ctx_outer_used = TRUE;
3025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027
3028 *arg = end;
3029
3030theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003031 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003032 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 vim_free(name);
3034 return res;
3035}
3036
3037/*
3038 * Compile the argument expressions.
3039 * "arg" points to just after the "(" and is advanced to after the ")"
3040 */
3041 static int
3042compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3043{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003044 char_u *p = *arg;
3045 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003046 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003050 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3051 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003052 if (*p == ')')
3053 {
3054 *arg = p + 1;
3055 return OK;
3056 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003057 if (must_end)
3058 {
3059 semsg(_(e_missing_comma_before_argument_str), p);
3060 return FAIL;
3061 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003062
Bram Moolenaara5565e42020-05-09 15:44:01 +02003063 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 return FAIL;
3065 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003066
3067 if (*p != ',' && *skipwhite(p) == ',')
3068 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003069 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003070 p = skipwhite(p);
3071 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003073 {
3074 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003075 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003076 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003077 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003078 else
3079 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003080 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003081 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003083failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003084 emsg(_(e_missing_close));
3085 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086}
3087
3088/*
3089 * Compile a function call: name(arg1, arg2)
3090 * "arg" points to "name", "arg + varlen" to the "(".
3091 * "argcount_init" is 1 for "value->method()"
3092 * Instructions:
3093 * EVAL arg1
3094 * EVAL arg2
3095 * BCALL / DCALL / UCALL
3096 */
3097 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003098compile_call(
3099 char_u **arg,
3100 size_t varlen,
3101 cctx_T *cctx,
3102 ppconst_T *ppconst,
3103 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104{
3105 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003106 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 int argcount = argcount_init;
3108 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003109 char_u fname_buf[FLEN_FIXED + 1];
3110 char_u *tofree = NULL;
3111 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003112 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003113 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003114 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 // we can evaluate "has('name')" at compile time
3117 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3118 {
3119 char_u *s = skipwhite(*arg + varlen + 1);
3120 typval_T argvars[2];
3121
3122 argvars[0].v_type = VAR_UNKNOWN;
3123 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003124 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003125 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003126 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003127 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003128 if (*s == ')' && argvars[0].v_type == VAR_STRING
3129 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003130 {
3131 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3132
3133 *arg = s + 1;
3134 argvars[1].v_type = VAR_UNKNOWN;
3135 tv->v_type = VAR_NUMBER;
3136 tv->vval.v_number = 0;
3137 f_has(argvars, tv);
3138 clear_tv(&argvars[0]);
3139 ++ppconst->pp_used;
3140 return OK;
3141 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003142 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003143 }
3144
3145 if (generate_ppconst(cctx, ppconst) == FAIL)
3146 return FAIL;
3147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (varlen >= sizeof(namebuf))
3149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 return FAIL;
3152 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003153 vim_strncpy(namebuf, *arg, varlen);
3154 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 *arg = skipwhite(*arg + varlen + 1);
3157 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003158 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
Bram Moolenaar03290b82020-12-19 16:30:44 +01003160 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003161 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 {
3163 int idx;
3164
3165 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003166 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003168 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003169 if (STRCMP(name, "flatten") == 0)
3170 {
3171 emsg(_(e_cannot_use_flatten_in_vim9_script));
3172 goto theend;
3173 }
3174
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003175 if (STRCMP(name, "add") == 0 && argcount == 2)
3176 {
3177 garray_T *stack = &cctx->ctx_type_stack;
3178 type_T *type = ((type_T **)stack->ga_data)[
3179 stack->ga_len - 2];
3180
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003181 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003182 if (type->tt_type == VAR_LIST)
3183 {
3184 // inline "add(list, item)" so that the type can be checked
3185 res = generate_LISTAPPEND(cctx);
3186 idx = -1;
3187 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003188 else if (type->tt_type == VAR_BLOB)
3189 {
3190 // inline "add(blob, nr)" so that the type can be checked
3191 res = generate_BLOBAPPEND(cctx);
3192 idx = -1;
3193 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003194 }
3195
3196 if (idx >= 0)
3197 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3198 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003199 else
3200 semsg(_(e_unknownfunc), namebuf);
3201 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003204 // An argument or local variable can be a function reference, this
3205 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003206 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003207 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003208 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003209 // If we can find the function by name generate the right call.
3210 // Skip global functions here, a local funcref takes precedence.
3211 ufunc = find_func(name, FALSE, cctx);
3212 if (ufunc != NULL && !func_is_global(ufunc))
3213 {
3214 res = generate_CALL(cctx, ufunc, argcount);
3215 goto theend;
3216 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003220 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003221 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003223 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003224 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003225 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003226 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003227 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003228
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003229 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003230 goto theend;
3231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232
Bram Moolenaar0f769812020-09-12 18:32:34 +02003233 // If we can find a global function by name generate the right call.
3234 if (ufunc != NULL)
3235 {
3236 res = generate_CALL(cctx, ufunc, argcount);
3237 goto theend;
3238 }
3239
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003240 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003241 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003242 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003243 res = generate_UCALL(cctx, name, argcount);
3244 else
3245 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003246
3247theend:
3248 vim_free(tofree);
3249 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250}
3251
3252// like NAMESPACE_CHAR but with 'a' and 'l'.
3253#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3254
3255/*
3256 * Find the end of a variable or function name. Unlike find_name_end() this
3257 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003258 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 * Return a pointer to just after the name. Equal to "arg" if there is no
3260 * valid name.
3261 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003262 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003263to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264{
3265 char_u *p;
3266
3267 // Quick check for valid starting character.
3268 if (!eval_isnamec1(*arg))
3269 return arg;
3270
3271 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3272 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3273 // and can be used in slice "[n:]".
3274 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003275 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3277 break;
3278 return p;
3279}
3280
3281/*
3282 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003283 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 */
3285 char_u *
3286to_name_const_end(char_u *arg)
3287{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003288 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 typval_T rettv;
3290
3291 if (p == arg && *arg == '[')
3292 {
3293
3294 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003295 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 p = arg;
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 return p;
3299}
3300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301/*
3302 * parse a list: [expr, expr]
3303 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003304 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 */
3306 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003307compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308{
3309 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003310 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003312 int is_const;
3313 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003315 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003317 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003318 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003319 semsg(_(e_list_end), *arg);
3320 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003321 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003322 if (*p == ',')
3323 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003324 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003325 return FAIL;
3326 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003327 if (*p == ']')
3328 {
3329 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003330 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003331 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003332 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003333 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003334 if (!is_const)
3335 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 ++count;
3337 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003338 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003340 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3341 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003342 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003343 return FAIL;
3344 }
3345 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003346 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 p = skipwhite(p);
3348 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003349 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003351 ppconst->pp_is_const = is_all_const;
3352 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353}
3354
3355/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003356 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003357 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003358 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 */
3360 static int
3361compile_lambda(char_u **arg, cctx_T *cctx)
3362{
Bram Moolenaare462f522020-12-27 14:43:30 +01003363 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 typval_T rettv;
3365 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003366 evalarg_T evalarg;
3367
3368 CLEAR_FIELD(evalarg);
3369 evalarg.eval_flags = EVAL_EVALUATE;
3370 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371
3372 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003373 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3374 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003375 {
3376 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003377 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003378 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003379
Bram Moolenaar65c44152020-12-24 15:14:01 +01003380 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003382 ++ufunc->uf_refcount;
3383 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384
Bram Moolenaar65c44152020-12-24 15:14:01 +01003385 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003386 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003388 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3389 // points into it. Point to the original line to avoid a dangling pointer.
3390 if (evalarg.eval_tofree_cmdline != NULL)
3391 {
3392 size_t off = *arg - evalarg.eval_tofree_cmdline;
3393
3394 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3395 + off;
3396 }
3397
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003398 clear_evalarg(&evalarg, NULL);
3399
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003400 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003401 {
3402 // The return type will now be known.
3403 set_function_type(ufunc);
3404
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003405 // The function reference count will be 1. When the ISN_FUNCREF
3406 // instruction is deleted the reference count is decremented and the
3407 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003408 return generate_FUNCREF(cctx, ufunc);
3409 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003410
3411 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 return FAIL;
3413}
3414
3415/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003416 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003418 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 */
3420 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003421compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422{
3423 garray_T *instr = &cctx->ctx_instr;
3424 int count = 0;
3425 dict_T *d = dict_alloc();
3426 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003427 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003428 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003429 int is_const;
3430 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431
3432 if (d == NULL)
3433 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003434 if (generate_ppconst(cctx, ppconst) == FAIL)
3435 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003436 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003438 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439
Bram Moolenaar23c55272020-06-21 16:58:13 +02003440 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003441 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003442 *arg = NULL;
3443 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003444 }
3445
3446 if (**arg == '}')
3447 break;
3448
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003449 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003451 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003453 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003454 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003455 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003458 if (isn->isn_type == ISN_PUSHNR)
3459 {
3460 char buf[NUMBUFLEN];
3461
3462 // Convert to string at compile time.
3463 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3464 isn->isn_type = ISN_PUSHS;
3465 isn->isn_arg.string = vim_strsave((char_u *)buf);
3466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 if (isn->isn_type == ISN_PUSHS)
3468 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003469 else if (may_generate_2STRING(-1, cctx) == FAIL)
3470 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003471 *arg = skipwhite(*arg);
3472 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003473 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003474 emsg(_(e_missing_matching_bracket_after_dict_key));
3475 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003476 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003477 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003479 else
3480 {
3481 // {"name": value},
3482 // {'name': value},
3483 // {name: value} use "name" as a literal key
3484 key = get_literal_key(arg);
3485 if (key == NULL)
3486 return FAIL;
3487 if (generate_PUSHS(cctx, key) == FAIL)
3488 return FAIL;
3489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
3491 // Check for duplicate keys, if using string keys.
3492 if (key != NULL)
3493 {
3494 item = dict_find(d, key, -1);
3495 if (item != NULL)
3496 {
3497 semsg(_(e_duplicate_key), key);
3498 goto failret;
3499 }
3500 item = dictitem_alloc(key);
3501 if (item != NULL)
3502 {
3503 item->di_tv.v_type = VAR_UNKNOWN;
3504 item->di_tv.v_lock = 0;
3505 if (dict_add(d, item) == FAIL)
3506 dictitem_free(item);
3507 }
3508 }
3509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (**arg != ':')
3511 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003512 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003513 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003514 else
3515 semsg(_(e_missing_dict_colon), *arg);
3516 return FAIL;
3517 }
3518 whitep = *arg + 1;
3519 if (!IS_WHITE_OR_NUL(*whitep))
3520 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003521 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 return FAIL;
3523 }
3524
Bram Moolenaar23c55272020-06-21 16:58:13 +02003525 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003526 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003527 *arg = NULL;
3528 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003529 }
3530
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003531 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003533 if (!is_const)
3534 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 ++count;
3536
Bram Moolenaar2c330432020-04-13 14:41:35 +02003537 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003538 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003539 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003540 *arg = NULL;
3541 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (**arg == '}')
3544 break;
3545 if (**arg != ',')
3546 {
3547 semsg(_(e_missing_dict_comma), *arg);
3548 goto failret;
3549 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003550 if (IS_WHITE_OR_NUL(*whitep))
3551 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003552 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003553 return FAIL;
3554 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003555 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003556 if (!IS_WHITE_OR_NUL(*whitep))
3557 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003558 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003559 return FAIL;
3560 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003561 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 }
3563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 *arg = *arg + 1;
3565
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003566 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003567 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003568 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003569 *arg += STRLEN(*arg);
3570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003572 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 return generate_NEWDICT(cctx, count);
3574
3575failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003576 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003577 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003578 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003579 *arg = (char_u *)"";
3580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 dict_unref(d);
3582 return FAIL;
3583}
3584
3585/*
3586 * Compile "&option".
3587 */
3588 static int
3589compile_get_option(char_u **arg, cctx_T *cctx)
3590{
3591 typval_T rettv;
3592 char_u *start = *arg;
3593 int ret;
3594
3595 // parse the option and get the current value to get the type.
3596 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003597 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (ret == OK)
3599 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003600 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003601 char_u *name = vim_strnsave(start, *arg - start);
3602 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3603 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3606 vim_free(name);
3607 }
3608 clear_tv(&rettv);
3609
3610 return ret;
3611}
3612
3613/*
3614 * Compile "$VAR".
3615 */
3616 static int
3617compile_get_env(char_u **arg, cctx_T *cctx)
3618{
3619 char_u *start = *arg;
3620 int len;
3621 int ret;
3622 char_u *name;
3623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 ++*arg;
3625 len = get_env_len(arg);
3626 if (len == 0)
3627 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003628 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 return FAIL;
3630 }
3631
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 name = vim_strnsave(start, len + 1);
3634 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3635 vim_free(name);
3636 return ret;
3637}
3638
3639/*
3640 * Compile "@r".
3641 */
3642 static int
3643compile_get_register(char_u **arg, cctx_T *cctx)
3644{
3645 int ret;
3646
3647 ++*arg;
3648 if (**arg == NUL)
3649 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003650 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 return FAIL;
3652 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003653 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 {
3655 emsg_invreg(**arg);
3656 return FAIL;
3657 }
3658 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3659 ++*arg;
3660 return ret;
3661}
3662
3663/*
3664 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003665 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 */
3667 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003668apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003670 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671
3672 // this works from end to start
3673 while (p > start)
3674 {
3675 --p;
3676 if (*p == '-' || *p == '+')
3677 {
3678 // only '-' has an effect, for '+' we only check the type
3679#ifdef FEAT_FLOAT
3680 if (rettv->v_type == VAR_FLOAT)
3681 {
3682 if (*p == '-')
3683 rettv->vval.v_float = -rettv->vval.v_float;
3684 }
3685 else
3686#endif
3687 {
3688 varnumber_T val;
3689 int error = FALSE;
3690
3691 // tv_get_number_chk() accepts a string, but we don't want that
3692 // here
3693 if (check_not_string(rettv) == FAIL)
3694 return FAIL;
3695 val = tv_get_number_chk(rettv, &error);
3696 clear_tv(rettv);
3697 if (error)
3698 return FAIL;
3699 if (*p == '-')
3700 val = -val;
3701 rettv->v_type = VAR_NUMBER;
3702 rettv->vval.v_number = val;
3703 }
3704 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003705 else if (numeric_only)
3706 {
3707 ++p;
3708 break;
3709 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003710 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 {
3712 int v = tv2bool(rettv);
3713
3714 // '!' is permissive in the type.
3715 clear_tv(rettv);
3716 rettv->v_type = VAR_BOOL;
3717 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3718 }
3719 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003720 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 return OK;
3722}
3723
3724/*
3725 * Recognize v: variables that are constants and set "rettv".
3726 */
3727 static void
3728get_vim_constant(char_u **arg, typval_T *rettv)
3729{
3730 if (STRNCMP(*arg, "v:true", 6) == 0)
3731 {
3732 rettv->v_type = VAR_BOOL;
3733 rettv->vval.v_number = VVAL_TRUE;
3734 *arg += 6;
3735 }
3736 else if (STRNCMP(*arg, "v:false", 7) == 0)
3737 {
3738 rettv->v_type = VAR_BOOL;
3739 rettv->vval.v_number = VVAL_FALSE;
3740 *arg += 7;
3741 }
3742 else if (STRNCMP(*arg, "v:null", 6) == 0)
3743 {
3744 rettv->v_type = VAR_SPECIAL;
3745 rettv->vval.v_number = VVAL_NULL;
3746 *arg += 6;
3747 }
3748 else if (STRNCMP(*arg, "v:none", 6) == 0)
3749 {
3750 rettv->v_type = VAR_SPECIAL;
3751 rettv->vval.v_number = VVAL_NONE;
3752 *arg += 6;
3753 }
3754}
3755
Bram Moolenaar657137c2021-01-09 15:45:23 +01003756 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003757get_compare_type(char_u *p, int *len, int *type_is)
3758{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003759 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003760 int i;
3761
3762 switch (p[0])
3763 {
3764 case '=': if (p[1] == '=')
3765 type = EXPR_EQUAL;
3766 else if (p[1] == '~')
3767 type = EXPR_MATCH;
3768 break;
3769 case '!': if (p[1] == '=')
3770 type = EXPR_NEQUAL;
3771 else if (p[1] == '~')
3772 type = EXPR_NOMATCH;
3773 break;
3774 case '>': if (p[1] != '=')
3775 {
3776 type = EXPR_GREATER;
3777 *len = 1;
3778 }
3779 else
3780 type = EXPR_GEQUAL;
3781 break;
3782 case '<': if (p[1] != '=')
3783 {
3784 type = EXPR_SMALLER;
3785 *len = 1;
3786 }
3787 else
3788 type = EXPR_SEQUAL;
3789 break;
3790 case 'i': if (p[1] == 's')
3791 {
3792 // "is" and "isnot"; but not a prefix of a name
3793 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3794 *len = 5;
3795 i = p[*len];
3796 if (!isalnum(i) && i != '_')
3797 {
3798 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3799 *type_is = TRUE;
3800 }
3801 }
3802 break;
3803 }
3804 return type;
3805}
3806
3807/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003808 * Skip over an expression, ignoring most errors.
3809 */
3810 static void
3811skip_expr_cctx(char_u **arg, cctx_T *cctx)
3812{
3813 evalarg_T evalarg;
3814
3815 CLEAR_FIELD(evalarg);
3816 evalarg.eval_cctx = cctx;
3817 skip_expr(arg, &evalarg);
3818}
3819
3820/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003822 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 */
3824 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003825compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003827 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 // this works from end to start
3830 while (p > start)
3831 {
3832 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003833 while (VIM_ISWHITE(*p))
3834 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 if (*p == '-' || *p == '+')
3836 {
3837 int negate = *p == '-';
3838 isn_T *isn;
3839
3840 // TODO: check type
3841 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3842 {
3843 --p;
3844 if (*p == '-')
3845 negate = !negate;
3846 }
3847 // only '-' has an effect, for '+' we only check the type
3848 if (negate)
3849 isn = generate_instr(cctx, ISN_NEGATENR);
3850 else
3851 isn = generate_instr(cctx, ISN_CHECKNR);
3852 if (isn == NULL)
3853 return FAIL;
3854 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003855 else if (numeric_only)
3856 {
3857 ++p;
3858 break;
3859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 else
3861 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003862 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003864 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003866 if (p[-1] == '!')
3867 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
3870 if (generate_2BOOL(cctx, invert) == FAIL)
3871 return FAIL;
3872 }
3873 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003874 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 return OK;
3876}
3877
3878/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003879 * Compile "(expression)": recursive!
3880 * Return FAIL/OK.
3881 */
3882 static int
3883compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3884{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003885 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003886 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003887
Bram Moolenaar24156692021-01-14 20:35:49 +01003888 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3889 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003890 if (ppconst->pp_used <= PPSIZE - 10)
3891 {
3892 ret = compile_expr1(arg, cctx, ppconst);
3893 }
3894 else
3895 {
3896 // Not enough space in ppconst, flush constants.
3897 if (generate_ppconst(cctx, ppconst) == FAIL)
3898 return FAIL;
3899 ret = compile_expr0(arg, cctx);
3900 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003901 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3902 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003903 if (**arg == ')')
3904 ++*arg;
3905 else if (ret == OK)
3906 {
3907 emsg(_(e_missing_close));
3908 ret = FAIL;
3909 }
3910 return ret;
3911}
3912
3913/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003915 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 */
3917 static int
3918compile_subscript(
3919 char_u **arg,
3920 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003921 char_u *start_leader,
3922 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003923 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003925 char_u *name_start = *end_leader;
3926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 for (;;)
3928 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003929 char_u *p = skipwhite(*arg);
3930
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003931 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003932 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003933 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003934
3935 // If a following line starts with "->{" or "->X" advance to that
3936 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003937 // Also if a following line starts with ".x".
3938 if (next != NULL &&
3939 ((next[0] == '-' && next[1] == '>'
3940 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003941 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003942 {
3943 next = next_line_from_context(cctx, TRUE);
3944 if (next == NULL)
3945 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003946 *arg = next;
3947 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003948 }
3949 }
3950
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003951 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003952 // not a function call.
3953 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003955 garray_T *stack = &cctx->ctx_type_stack;
3956 type_T *type;
3957 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003959 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003960 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003961 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003964 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3965
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003966 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3968 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
3971 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003972 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003974 char_u *pstart = p;
3975
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003976 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003977 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003978 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 // something->method()
3981 // Apply the '!', '-' and '+' first:
3982 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003983 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003986 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003987 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003988 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003989 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003990 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003991 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003992 garray_T *stack = &cctx->ctx_type_stack;
3993 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003994 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003995 int expr_isn_start = cctx->ctx_instr.ga_len;
3996 int expr_isn_end;
3997 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003998
3999 // Funcref call: list->(Refs[2])(arg)
4000 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004001 //
4002 // Fist compile the function expression.
4003 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004004 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004005
4006 // Remember the next instruction index, where the instructions
4007 // for arguments are being written.
4008 expr_isn_end = cctx->ctx_instr.ga_len;
4009
4010 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004011 if (**arg != '(')
4012 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004013 if (*skipwhite(*arg) == '(')
4014 emsg(_(e_nowhitespace));
4015 else
4016 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004017 return FAIL;
4018 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004019 *arg = skipwhite(*arg + 1);
4020 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4021 return FAIL;
4022
Bram Moolenaar2927c072021-04-05 19:41:21 +02004023 // Move the instructions for the arguments to before the
4024 // instructions of the expression and move the type of the
4025 // expression after the argument types. This is what ISN_PCALL
4026 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004027 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004028 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4029 if (arg_isn_count > 0)
4030 {
4031 int expr_isn_count = expr_isn_end - expr_isn_start;
4032 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4033
4034 if (isn == NULL)
4035 return FAIL;
4036 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4037 + expr_isn_start,
4038 sizeof(isn_T) * expr_isn_count);
4039 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4040 + expr_isn_start,
4041 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4042 sizeof(isn_T) * arg_isn_count);
4043 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4044 + expr_isn_start + arg_isn_count,
4045 isn, sizeof(isn_T) * expr_isn_count);
4046 vim_free(isn);
4047
4048 type = ((type_T **)stack->ga_data)[type_idx_start];
4049 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4050 ((type_T **)stack->ga_data) + type_idx_start + 1,
4051 sizeof(type_T *)
4052 * (stack->ga_len - type_idx_start - 1));
4053 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4054 }
4055
Bram Moolenaar7e368202020-12-25 21:56:57 +01004056 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4057 if (generate_PCALL(cctx, argcount,
4058 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 return FAIL;
4060 }
4061 else
4062 {
4063 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004064 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004065 if (!eval_isnamec1(*p))
4066 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004067 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004068 return FAIL;
4069 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004070 if (ASCII_ISALPHA(*p) && p[1] == ':')
4071 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004072 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 ;
4074 if (*p != '(')
4075 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004076 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 return FAIL;
4082 }
4083 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004084 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004086 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004087
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004088 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004089 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004090 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004091 // TODO: blob index
4092 // TODO: more arguments
4093 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004094 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004096 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004097
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004098 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004099 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004100 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004101 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004102 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004103 // missing first index is equal to zero
4104 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004105 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004106 else
4107 {
4108 if (compile_expr0(arg, cctx) == FAIL)
4109 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004110 if (**arg == ':')
4111 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004112 semsg(_(e_white_space_required_before_and_after_str_at_str),
4113 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004114 return FAIL;
4115 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004116 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004117 return FAIL;
4118 *arg = skipwhite(*arg);
4119 }
4120 if (**arg == ':')
4121 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004122 is_slice = TRUE;
4123 ++*arg;
4124 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4125 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004126 semsg(_(e_white_space_required_before_and_after_str_at_str),
4127 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004128 return FAIL;
4129 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004130 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004131 return FAIL;
4132 if (**arg == ']')
4133 // missing second index is equal to end of string
4134 generate_PUSHNR(cctx, -1);
4135 else
4136 {
4137 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004138 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004139 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004140 return FAIL;
4141 *arg = skipwhite(*arg);
4142 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
4145 if (**arg != ']')
4146 {
4147 emsg(_(e_missbrac));
4148 return FAIL;
4149 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004150 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
Bram Moolenaare42939a2021-04-05 17:11:17 +02004152 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004153 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004155 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004157 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004158 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004160 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004162 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004163 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004164 {
4165 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004166 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004167 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004168 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004169 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 while (eval_isnamec(*p))
4171 MB_PTR_ADV(p);
4172 if (p == *arg)
4173 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004174 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
4176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004177 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 return FAIL;
4179 *arg = p;
4180 }
4181 else
4182 break;
4183 }
4184
4185 // TODO - see handle_subscript():
4186 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4187 // Don't do this when "Func" is already a partial that was bound
4188 // explicitly (pt_auto is FALSE).
4189
4190 return OK;
4191}
4192
4193/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004194 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4195 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004197 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 *
4200 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 */
4202
4203/*
4204 * number number constant
4205 * 0zFFFFFFFF Blob constant
4206 * "string" string constant
4207 * 'string' literal string constant
4208 * &option-name option value
4209 * @r register contents
4210 * identifier variable value
4211 * function() function call
4212 * $VAR environment variable
4213 * (expression) nested expression
4214 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004215 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 *
4217 * Also handle:
4218 * ! in front logical NOT
4219 * - in front unary minus
4220 * + in front unary plus (ignored)
4221 * trailing (arg) funcref/partial call
4222 * trailing [] subscript in String or List
4223 * trailing .name entry in Dictionary
4224 * trailing ->name() method call
4225 */
4226 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004227compile_expr7(
4228 char_u **arg,
4229 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004230 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 char_u *start_leader, *end_leader;
4233 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004234 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004235 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004237 ppconst->pp_is_const = FALSE;
4238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 /*
4240 * Skip '!', '-' and '+' characters. They are handled later.
4241 */
4242 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004243 if (eval_leader(arg, TRUE) == FAIL)
4244 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 end_leader = *arg;
4246
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004247 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 switch (**arg)
4249 {
4250 /*
4251 * Number constant.
4252 */
4253 case '0': // also for blob starting with 0z
4254 case '1':
4255 case '2':
4256 case '3':
4257 case '4':
4258 case '5':
4259 case '6':
4260 case '7':
4261 case '8':
4262 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004263 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004265 // Apply "-" and "+" just before the number now, right to
4266 // left. Matters especially when "->" follows. Stops at
4267 // '!'.
4268 if (apply_leader(rettv, TRUE,
4269 start_leader, &end_leader) == FAIL)
4270 {
4271 clear_tv(rettv);
4272 return FAIL;
4273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 break;
4275
4276 /*
4277 * String constant: "string".
4278 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004279 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 return FAIL;
4281 break;
4282
4283 /*
4284 * Literal string constant: 'str''ing'.
4285 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004286 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 return FAIL;
4288 break;
4289
4290 /*
4291 * Constant Vim variable.
4292 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004293 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 ret = NOTDONE;
4295 break;
4296
4297 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298 * "true" constant
4299 */
4300 case 't': if (STRNCMP(*arg, "true", 4) == 0
4301 && !eval_isnamec((*arg)[4]))
4302 {
4303 *arg += 4;
4304 rettv->v_type = VAR_BOOL;
4305 rettv->vval.v_number = VVAL_TRUE;
4306 }
4307 else
4308 ret = NOTDONE;
4309 break;
4310
4311 /*
4312 * "false" constant
4313 */
4314 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4315 && !eval_isnamec((*arg)[5]))
4316 {
4317 *arg += 5;
4318 rettv->v_type = VAR_BOOL;
4319 rettv->vval.v_number = VVAL_FALSE;
4320 }
4321 else
4322 ret = NOTDONE;
4323 break;
4324
4325 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004326 * "null" constant
4327 */
4328 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004329 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004330 {
4331 *arg += 4;
4332 rettv->v_type = VAR_SPECIAL;
4333 rettv->vval.v_number = VVAL_NULL;
4334 }
4335 else
4336 ret = NOTDONE;
4337 break;
4338
4339 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 * List: [expr, expr]
4341 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004342 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4343 return FAIL;
4344 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 break;
4346
4347 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 * Dictionary: {'key': val, 'key': val}
4349 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004350 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4351 return FAIL;
4352 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 break;
4354
4355 /*
4356 * Option value: &name
4357 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004358 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4359 return FAIL;
4360 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 break;
4362
4363 /*
4364 * Environment variable: $VAR.
4365 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004366 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4367 return FAIL;
4368 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 break;
4370
4371 /*
4372 * Register contents: @r.
4373 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004374 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4375 return FAIL;
4376 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 break;
4378 /*
4379 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004380 * lambda: (arg, arg) => expr
4381 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004383 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4384 ret = compile_lambda(arg, cctx);
4385 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004386 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 break;
4388
4389 default: ret = NOTDONE;
4390 break;
4391 }
4392 if (ret == FAIL)
4393 return FAIL;
4394
Bram Moolenaar1c747212020-05-09 18:28:34 +02004395 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004397 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 clear_tv(rettv);
4399 else
4400 // A constant expression can possibly be handled compile time,
4401 // return the value instead of generating code.
4402 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
4404 else if (ret == NOTDONE)
4405 {
4406 char_u *p;
4407 int r;
4408
4409 if (!eval_isnamec1(**arg))
4410 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004411 if (!vim9_bad_comment(*arg))
4412 {
4413 if (ends_excmd(*skipwhite(*arg)))
4414 semsg(_(e_empty_expression_str), *arg);
4415 else
4416 semsg(_(e_name_expected_str), *arg);
4417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 return FAIL;
4419 }
4420
4421 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004422 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004423 if (p - *arg == (size_t)1 && **arg == '_')
4424 {
4425 emsg(_(e_cannot_use_underscore_here));
4426 return FAIL;
4427 }
4428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 {
4431 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 {
4435 if (generate_ppconst(cctx, ppconst) == FAIL)
4436 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004437 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 if (r == FAIL)
4440 return FAIL;
4441 }
4442
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004443 // Handle following "[]", ".member", etc.
4444 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004445 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 ppconst) == FAIL)
4447 return FAIL;
4448 if (ppconst->pp_used > 0)
4449 {
4450 // apply the '!', '-' and '+' before the constant
4451 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004452 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 return FAIL;
4454 return OK;
4455 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004456 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004458 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459}
4460
4461/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004462 * Give the "white on both sides" error, taking the operator from "p[len]".
4463 */
4464 void
4465error_white_both(char_u *op, int len)
4466{
4467 char_u buf[10];
4468
4469 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004470 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004471}
4472
4473/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004474 * <type>expr7: runtime type check / conversion
4475 */
4476 static int
4477compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4478{
4479 type_T *want_type = NULL;
4480
4481 // Recognize <type>
4482 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4483 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004484 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004485 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4486 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004487 return FAIL;
4488
4489 if (**arg != '>')
4490 {
4491 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004492 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004493 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004494 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004495 return FAIL;
4496 }
4497 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004498 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004499 return FAIL;
4500 }
4501
4502 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4503 return FAIL;
4504
4505 if (want_type != NULL)
4506 {
4507 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004508 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004509 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004510
Bram Moolenaard1103582020-08-14 22:44:25 +02004511 generate_ppconst(cctx, ppconst);
4512 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004513 where.wt_index = 0;
4514 where.wt_variable = FALSE;
4515 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004516 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004517 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004518 return FAIL;
4519 }
4520 }
4521
4522 return OK;
4523}
4524
4525/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 * * number multiplication
4527 * / number division
4528 * % number modulo
4529 */
4530 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532{
4533 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004534 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004535 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004537 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004538 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540
4541 /*
4542 * Repeat computing, until no "*", "/" or "%" is following.
4543 */
4544 for (;;)
4545 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004546 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 if (*op != '*' && *op != '/' && *op != '%')
4548 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004549 if (next != NULL)
4550 {
4551 *arg = next_line_from_context(cctx, TRUE);
4552 op = skipwhite(*arg);
4553 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004554
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004555 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004557 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004558 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004560 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004561 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004563 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004564 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004566
4567 if (ppconst->pp_used == ppconst_used + 2
4568 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4569 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004570 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004571 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4572 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4573 varnumber_T res = 0;
4574 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 // both are numbers: compute the result
4577 switch (*op)
4578 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004579 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004581 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004582 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004583 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004584 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004585 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004586 break;
4587 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004588 if (failed)
4589 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004590 tv1->vval.v_number = res;
4591 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004592 }
4593 else
4594 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004595 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004596 generate_two_op(cctx, op);
4597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
4599
4600 return OK;
4601}
4602
4603/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004604 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 * - number subtraction
4606 * .. string concatenation
4607 */
4608 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610{
4611 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615
4616 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 return FAIL;
4619
4620 /*
4621 * Repeat computing, until no "+", "-" or ".." is following.
4622 */
4623 for (;;)
4624 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004625 op = may_peek_next_line(cctx, *arg, &next);
4626 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 break;
4628 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004629 if (next != NULL)
4630 {
4631 *arg = next_line_from_context(cctx, TRUE);
4632 op = skipwhite(*arg);
4633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004635 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004637 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004638 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640
Bram Moolenaare0de1712020-12-02 17:36:54 +01004641 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004644 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return FAIL;
4647
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004649 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4651 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4652 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4653 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4656 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004657
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004658 // concat/subtract/add constant numbers
4659 if (*op == '+')
4660 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4661 else if (*op == '-')
4662 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4663 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004664 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004665 // concatenate constant strings
4666 char_u *s1 = tv1->vval.v_string;
4667 char_u *s2 = tv2->vval.v_string;
4668 size_t len1 = STRLEN(s1);
4669
4670 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4671 if (tv1->vval.v_string == NULL)
4672 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004674 return FAIL;
4675 }
4676 mch_memmove(tv1->vval.v_string, s1, len1);
4677 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004678 vim_free(s1);
4679 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004680 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 }
4683 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004684 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004686 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004687 if (*op == '.')
4688 {
4689 if (may_generate_2STRING(-2, cctx) == FAIL
4690 || may_generate_2STRING(-1, cctx) == FAIL)
4691 return FAIL;
4692 generate_instr_drop(cctx, ISN_CONCAT, 1);
4693 }
4694 else
4695 generate_two_op(cctx, op);
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 }
4698
4699 return OK;
4700}
4701
4702/*
4703 * expr5a == expr5b
4704 * expr5a =~ expr5b
4705 * expr5a != expr5b
4706 * expr5a !~ expr5b
4707 * expr5a > expr5b
4708 * expr5a >= expr5b
4709 * expr5a < expr5b
4710 * expr5a <= expr5b
4711 * expr5a is expr5b
4712 * expr5a isnot expr5b
4713 *
4714 * Produces instructions:
4715 * EVAL expr5a Push result of "expr5a"
4716 * EVAL expr5b Push result of "expr5b"
4717 * COMPARE one of the compare instructions
4718 */
4719 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004722 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004724 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728
4729 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 return FAIL;
4732
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004733 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004734 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
4736 /*
4737 * If there is a comparative operator, use it.
4738 */
4739 if (type != EXPR_UNKNOWN)
4740 {
4741 int ic = FALSE; // Default: do not ignore case
4742
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004743 if (next != NULL)
4744 {
4745 *arg = next_line_from_context(cctx, TRUE);
4746 p = skipwhite(*arg);
4747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 if (type_is && (p[len] == '?' || p[len] == '#'))
4749 {
4750 semsg(_(e_invexpr2), *arg);
4751 return FAIL;
4752 }
4753 // extra question mark appended: ignore case
4754 if (p[len] == '?')
4755 {
4756 ic = TRUE;
4757 ++len;
4758 }
4759 // extra '#' appended: match case (ignored)
4760 else if (p[len] == '#')
4761 ++len;
4762 // nothing appended: match case
4763
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004764 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004766 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004767 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 }
4769
4770 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004771 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004772 return FAIL;
4773
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 return FAIL;
4776
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (ppconst->pp_used == ppconst_used + 2)
4778 {
4779 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4780 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4781 int ret;
4782
4783 // Both sides are a constant, compute the result now.
4784 // First check for a valid combination of types, this is more
4785 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004786 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787 ret = FAIL;
4788 else
4789 {
4790 ret = typval_compare(tv1, tv2, type, ic);
4791 tv1->v_type = VAR_BOOL;
4792 tv1->vval.v_number = tv1->vval.v_number
4793 ? VVAL_TRUE : VVAL_FALSE;
4794 clear_tv(tv2);
4795 --ppconst->pp_used;
4796 }
4797 return ret;
4798 }
4799
4800 generate_ppconst(cctx, ppconst);
4801 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 }
4803
4804 return OK;
4805}
4806
Bram Moolenaar7f141552020-05-09 17:35:53 +02004807static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809/*
4810 * Compile || or &&.
4811 */
4812 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004813compile_and_or(
4814 char_u **arg,
4815 cctx_T *cctx,
4816 char *op,
4817 ppconst_T *ppconst,
4818 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004820 char_u *next;
4821 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 int opchar = *op;
4823
4824 if (p[0] == opchar && p[1] == opchar)
4825 {
4826 garray_T *instr = &cctx->ctx_instr;
4827 garray_T end_ga;
4828
4829 /*
4830 * Repeat until there is no following "||" or "&&"
4831 */
4832 ga_init2(&end_ga, sizeof(int), 10);
4833 while (p[0] == opchar && p[1] == opchar)
4834 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004835 long start_lnum = SOURCING_LNUM;
4836 int start_ctx_lnum = cctx->ctx_lnum;
4837 int save_lnum;
4838
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004839 if (next != NULL)
4840 {
4841 *arg = next_line_from_context(cctx, TRUE);
4842 p = skipwhite(*arg);
4843 }
4844
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004845 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4846 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004847 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004848 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004849 return FAIL;
4850 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004852 // TODO: use ppconst if the value is a constant and check
4853 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 generate_ppconst(cctx, ppconst);
4855
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004856 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004857 SOURCING_LNUM = start_lnum;
4858 save_lnum = cctx->ctx_lnum;
4859 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004860 if (bool_on_stack(cctx) == FAIL)
4861 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004862 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004863 ga_clear(&end_ga);
4864 return FAIL;
4865 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004866 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 if (ga_grow(&end_ga, 1) == FAIL)
4869 {
4870 ga_clear(&end_ga);
4871 return FAIL;
4872 }
4873 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4874 ++end_ga.ga_len;
4875 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004876 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
4878 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004879 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004880 {
4881 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004882 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004883 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004884
Bram Moolenaara5565e42020-05-09 15:44:01 +02004885 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4886 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
4888 ga_clear(&end_ga);
4889 return FAIL;
4890 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004891
4892 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004896 // Every part must evaluate to a bool.
4897 if (bool_on_stack(cctx) == FAIL)
4898 {
4899 ga_clear(&end_ga);
4900 return FAIL;
4901 }
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 // Fill in the end label in all jumps.
4904 while (end_ga.ga_len > 0)
4905 {
4906 isn_T *isn;
4907
4908 --end_ga.ga_len;
4909 isn = ((isn_T *)instr->ga_data)
4910 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4911 isn->isn_arg.jump.jump_where = instr->ga_len;
4912 }
4913 ga_clear(&end_ga);
4914 }
4915
4916 return OK;
4917}
4918
4919/*
4920 * expr4a && expr4a && expr4a logical AND
4921 *
4922 * Produces instructions:
4923 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004924 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004925 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004927 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 * EVAL expr4c Push result of "expr4c"
4929 * end:
4930 */
4931 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004934 int ppconst_used = ppconst->pp_used;
4935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 return FAIL;
4939
4940 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942}
4943
4944/*
4945 * expr3a || expr3b || expr3c logical OR
4946 *
4947 * Produces instructions:
4948 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004949 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004950 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004952 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 * EVAL expr3c Push result of "expr3c"
4954 * end:
4955 */
4956 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004957compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004959 int ppconst_used = ppconst->pp_used;
4960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 return FAIL;
4964
4965 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004966 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967}
4968
4969/*
4970 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004972 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 * JUMP_IF_FALSE alt jump if false
4974 * EVAL expr1a
4975 * JUMP_ALWAYS end
4976 * alt: EVAL expr1b
4977 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004978 *
4979 * Toplevel expression: expr2 ?? expr1
4980 * Produces instructions:
4981 * EVAL expr2 Push result of "expr2"
4982 * JUMP_AND_KEEP_IF_TRUE end jump if true
4983 * EVAL expr1
4984 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 */
4986 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004987compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988{
4989 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004991 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992
Bram Moolenaar3988f642020-08-27 22:43:03 +02004993 // Ignore all kinds of errors when not producing code.
4994 if (cctx->ctx_skip == SKIP_YES)
4995 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004996 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004997 return OK;
4998 }
4999
Bram Moolenaar61a89812020-05-07 16:58:17 +02005000 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 return FAIL;
5003
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005004 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 if (*p == '?')
5006 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005007 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 garray_T *instr = &cctx->ctx_instr;
5009 garray_T *stack = &cctx->ctx_type_stack;
5010 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005011 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005013 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005014 int has_const_expr = FALSE;
5015 int const_value = FALSE;
5016 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005018 if (next != NULL)
5019 {
5020 *arg = next_line_from_context(cctx, TRUE);
5021 p = skipwhite(*arg);
5022 }
5023
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005024 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005025 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005026 semsg(_(e_white_space_required_before_and_after_str_at_str),
5027 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005028 return FAIL;
5029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 if (ppconst->pp_used == ppconst_used + 1)
5032 {
5033 // the condition is a constant, we know whether the ? or the :
5034 // expression is to be evaluated.
5035 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005036 if (op_falsy)
5037 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5038 else
5039 {
5040 int error = FALSE;
5041
5042 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5043 &error);
5044 if (error)
5045 return FAIL;
5046 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005047 cctx->ctx_skip = save_skip == SKIP_YES ||
5048 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5049
5050 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5051 // "left ?? right" and "left" is truthy: produce "left"
5052 generate_ppconst(cctx, ppconst);
5053 else
5054 {
5055 clear_tv(&ppconst->pp_tv[ppconst_used]);
5056 --ppconst->pp_used;
5057 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005058 }
5059 else
5060 {
5061 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005062 if (op_falsy)
5063 end_idx = instr->ga_len;
5064 generate_JUMP(cctx, op_falsy
5065 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5066 if (op_falsy)
5067 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
5070 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005071 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005072 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076 if (!has_const_expr)
5077 {
5078 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005080 if (!op_falsy)
5081 {
5082 // remember the type and drop it
5083 --stack->ga_len;
5084 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005086 end_idx = instr->ga_len;
5087 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005088
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005089 // jump here from JUMP_IF_FALSE
5090 isn = ((isn_T *)instr->ga_data) + alt_idx;
5091 isn->isn_arg.jump.jump_where = instr->ga_len;
5092 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005095 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005097 // Check for the ":".
5098 p = may_peek_next_line(cctx, *arg, &next);
5099 if (*p != ':')
5100 {
5101 emsg(_(e_missing_colon));
5102 return FAIL;
5103 }
5104 if (next != NULL)
5105 {
5106 *arg = next_line_from_context(cctx, TRUE);
5107 p = skipwhite(*arg);
5108 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005109
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005110 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5111 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005112 semsg(_(e_white_space_required_before_and_after_str_at_str),
5113 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005114 return FAIL;
5115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005117 // evaluate the third expression
5118 if (has_const_expr)
5119 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005120 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005121 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005122 return FAIL;
5123 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5124 return FAIL;
5125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127 if (!has_const_expr)
5128 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005129 type_T **typep;
5130
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005134 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5135 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005137 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138 isn = ((isn_T *)instr->ga_data) + end_idx;
5139 isn->isn_arg.jump.jump_where = instr->ga_len;
5140 }
5141
5142 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 }
5144 return OK;
5145}
5146
5147/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005149 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5150 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 */
5152 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005153compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005154{
5155 ppconst_T ppconst;
5156
5157 CLEAR_FIELD(ppconst);
5158 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5159 {
5160 clear_ppconst(&ppconst);
5161 return FAIL;
5162 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005163 if (is_const != NULL)
5164 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005165 if (generate_ppconst(cctx, &ppconst) == FAIL)
5166 return FAIL;
5167 return OK;
5168}
5169
5170/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005171 * Toplevel expression.
5172 */
5173 static int
5174compile_expr0(char_u **arg, cctx_T *cctx)
5175{
5176 return compile_expr0_ext(arg, cctx, NULL);
5177}
5178
5179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 * compile "return [expr]"
5181 */
5182 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005183compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184{
5185 char_u *p = arg;
5186 garray_T *stack = &cctx->ctx_type_stack;
5187 type_T *stack_type;
5188
5189 if (*p != NUL && *p != '|' && *p != '\n')
5190 {
5191 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005192 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 return NULL;
5194
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005195 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005196 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005197 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005198 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005199 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5200 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005201 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005202 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005203 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005204 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005205 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005206 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5207 && stack_type->tt_type != VAR_VOID
5208 && stack_type->tt_type != VAR_UNKNOWN)
5209 {
5210 emsg(_(e_returning_value_in_function_without_return_type));
5211 return NULL;
5212 }
5213 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005214 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005215 return NULL;
5216 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219 else
5220 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005221 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005222 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005223 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5224 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005226 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 return NULL;
5228 }
5229
5230 // No argument, return zero.
5231 generate_PUSHNR(cctx, 0);
5232 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005233
5234 // Undo any command modifiers.
5235 generate_undo_cmdmods(cctx);
5236
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005237 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 return NULL;
5239
5240 // "return val | endif" is possible
5241 return skipwhite(p);
5242}
5243
5244/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005245 * Get a line from the compilation context, compatible with exarg_T getline().
5246 * Return a pointer to the line in allocated memory.
5247 * Return NULL for end-of-file or some error.
5248 */
5249 static char_u *
5250exarg_getline(
5251 int c UNUSED,
5252 void *cookie,
5253 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005254 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005255{
5256 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005257 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005258
Bram Moolenaar66250c92020-08-20 15:02:42 +02005259 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005260 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005261 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005262 return NULL;
5263 ++cctx->ctx_lnum;
5264 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5265 // Comment lines result in NULL pointers, skip them.
5266 if (p != NULL)
5267 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005269}
5270
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005271 void
5272fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5273{
5274 eap->getline = exarg_getline;
5275 eap->cookie = cctx;
5276}
5277
Bram Moolenaar04b12692020-05-04 23:24:44 +02005278/*
5279 * Compile a nested :def command.
5280 */
5281 static char_u *
5282compile_nested_function(exarg_T *eap, cctx_T *cctx)
5283{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005284 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005285 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005286 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005287 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005288 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005289 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005290
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005291 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005292 {
5293 emsg(_(e_cannot_use_bang_with_nested_def));
5294 return NULL;
5295 }
5296
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005297 if (*name_start == '/')
5298 {
5299 name_end = skip_regexp(name_start + 1, '/', TRUE);
5300 if (*name_end == '/')
5301 ++name_end;
5302 eap->nextcmd = check_nextcmd(name_end);
5303 }
5304 if (name_end == name_start || *skipwhite(name_end) != '(')
5305 {
5306 if (!ends_excmd2(name_start, name_end))
5307 {
5308 semsg(_(e_invalid_command_str), eap->cmd);
5309 return NULL;
5310 }
5311
5312 // "def" or "def Name": list functions
5313 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5314 return NULL;
5315 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5316 }
5317
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005318 // Only g:Func() can use a namespace.
5319 if (name_start[1] == ':' && !is_global)
5320 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005321 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005322 return NULL;
5323 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005324 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005325 return NULL;
5326
Bram Moolenaar04b12692020-05-04 23:24:44 +02005327 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005328 fill_exarg_from_cctx(eap, cctx);
5329
Bram Moolenaar04b12692020-05-04 23:24:44 +02005330 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005331 lambda_name = vim_strsave(get_lambda_name());
5332 if (lambda_name == NULL)
5333 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005334 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005335
Bram Moolenaar822ba242020-05-24 23:00:18 +02005336 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005337 {
5338 r = eap->skip ? OK : FAIL;
5339 goto theend;
5340 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005341
5342 // copy over the block scope IDs before compiling
5343 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5344 {
5345 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5346
5347 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5348 if (ufunc->uf_block_ids != NULL)
5349 {
5350 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5351 sizeof(int) * block_depth);
5352 ufunc->uf_block_depth = block_depth;
5353 }
5354 }
5355
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005356 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5357 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005358 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005359 {
5360 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005361 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005362 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005363
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005364 if (is_global)
5365 {
5366 char_u *func_name = vim_strnsave(name_start + 2,
5367 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005368
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005369 if (func_name == NULL)
5370 r = FAIL;
5371 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005372 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005373 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005374 lambda_name = NULL;
5375 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005376 }
5377 else
5378 {
5379 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005380 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005381 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005382
Bram Moolenaareef21022020-08-01 22:16:43 +02005383 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005384 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005385 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005386 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005387 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5388 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005389 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005390
5391theend:
5392 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005393 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005394}
5395
5396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 * Return the length of an assignment operator, or zero if there isn't one.
5398 */
5399 int
5400assignment_len(char_u *p, int *heredoc)
5401{
5402 if (*p == '=')
5403 {
5404 if (p[1] == '<' && p[2] == '<')
5405 {
5406 *heredoc = TRUE;
5407 return 3;
5408 }
5409 return 1;
5410 }
5411 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5412 return 2;
5413 if (STRNCMP(p, "..=", 3) == 0)
5414 return 3;
5415 return 0;
5416}
5417
5418// words that cannot be used as a variable
5419static char *reserved[] = {
5420 "true",
5421 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005422 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 NULL
5424};
5425
Bram Moolenaar752fc692021-01-04 21:57:11 +01005426// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005427typedef enum {
5428 dest_local,
5429 dest_option,
5430 dest_env,
5431 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005432 dest_buffer,
5433 dest_window,
5434 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005435 dest_vimvar,
5436 dest_script,
5437 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005438 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005439} assign_dest_T;
5440
Bram Moolenaar752fc692021-01-04 21:57:11 +01005441// Used by compile_lhs() to store information about the LHS of an assignment
5442// and one argument of ":unlet" with an index.
5443typedef struct {
5444 assign_dest_T lhs_dest; // type of destination
5445
5446 char_u *lhs_name; // allocated name including
5447 // "[expr]" or ".name".
5448 size_t lhs_varlen; // length of the variable without
5449 // "[expr]" or ".name"
5450 char_u *lhs_dest_end; // end of the destination, including
5451 // "[expr]" or ".name".
5452
5453 int lhs_has_index; // has "[expr]" or ".name"
5454
5455 int lhs_new_local; // create new local variable
5456 int lhs_opt_flags; // for when destination is an option
5457 int lhs_vimvaridx; // for when destination is a v:var
5458
5459 lvar_T lhs_local_lvar; // used for existing local destination
5460 lvar_T lhs_arg_lvar; // used for argument destination
5461 lvar_T *lhs_lvar; // points to destination lvar
5462 int lhs_scriptvar_sid;
5463 int lhs_scriptvar_idx;
5464
5465 int lhs_has_type; // type was specified
5466 type_T *lhs_type;
5467 type_T *lhs_member_type;
5468} lhs_T;
5469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 * Generate the load instruction for "name".
5472 */
5473 static void
5474generate_loadvar(
5475 cctx_T *cctx,
5476 assign_dest_T dest,
5477 char_u *name,
5478 lvar_T *lvar,
5479 type_T *type)
5480{
5481 switch (dest)
5482 {
5483 case dest_option:
5484 // TODO: check the option exists
5485 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5486 break;
5487 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005488 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5489 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5490 else
5491 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005492 break;
5493 case dest_buffer:
5494 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5495 break;
5496 case dest_window:
5497 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5498 break;
5499 case dest_tab:
5500 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5501 break;
5502 case dest_script:
5503 compile_load_scriptvar(cctx,
5504 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5505 break;
5506 case dest_env:
5507 // Include $ in the name here
5508 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5509 break;
5510 case dest_reg:
5511 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5512 break;
5513 case dest_vimvar:
5514 generate_LOADV(cctx, name + 2, TRUE);
5515 break;
5516 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005517 if (lvar->lv_from_outer > 0)
5518 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5519 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005520 else
5521 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5522 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005523 case dest_expr:
5524 // list or dict value should already be on the stack.
5525 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005526 }
5527}
5528
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005529/*
5530 * Skip over "[expr]" or ".member".
5531 * Does not check for any errors.
5532 */
5533 static char_u *
5534skip_index(char_u *start)
5535{
5536 char_u *p = start;
5537
5538 if (*p == '[')
5539 {
5540 p = skipwhite(p + 1);
5541 (void)skip_expr(&p, NULL);
5542 p = skipwhite(p);
5543 if (*p == ']')
5544 return p + 1;
5545 return p;
5546 }
5547 // if (*p == '.')
5548 return to_name_end(p + 1, TRUE);
5549}
5550
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005551 void
5552vim9_declare_error(char_u *name)
5553{
5554 char *scope = "";
5555
5556 switch (*name)
5557 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005558 case 'g': scope = _("global"); break;
5559 case 'b': scope = _("buffer"); break;
5560 case 'w': scope = _("window"); break;
5561 case 't': scope = _("tab"); break;
5562 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005563 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005564 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005565 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005566 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005567 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005568 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005569 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005570 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005571 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005572}
5573
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005574/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005575 * For one assignment figure out the type of destination. Return it in "dest".
5576 * When not recognized "dest" is not set.
5577 * For an option "opt_flags" is set.
5578 * For a v:var "vimvaridx" is set.
5579 * "type" is set to the destination type if known, unchanted otherwise.
5580 * Return FAIL if an error message was given.
5581 */
5582 static int
5583get_var_dest(
5584 char_u *name,
5585 assign_dest_T *dest,
5586 int cmdidx,
5587 int *opt_flags,
5588 int *vimvaridx,
5589 type_T **type,
5590 cctx_T *cctx)
5591{
5592 char_u *p;
5593
5594 if (*name == '&')
5595 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005596 int cc;
5597 long numval;
5598 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005599
5600 *dest = dest_option;
5601 if (cmdidx == CMD_final || cmdidx == CMD_const)
5602 {
5603 emsg(_(e_const_option));
5604 return FAIL;
5605 }
5606 p = name;
5607 p = find_option_end(&p, opt_flags);
5608 if (p == NULL)
5609 {
5610 // cannot happen?
5611 emsg(_(e_letunexp));
5612 return FAIL;
5613 }
5614 cc = *p;
5615 *p = NUL;
5616 opt_type = get_option_value(skip_option_env_lead(name),
5617 &numval, NULL, *opt_flags);
5618 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005619 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005621 case gov_unknown:
5622 semsg(_(e_unknown_option), name);
5623 return FAIL;
5624 case gov_string:
5625 case gov_hidden_string:
5626 *type = &t_string;
5627 break;
5628 case gov_bool:
5629 case gov_hidden_bool:
5630 *type = &t_bool;
5631 break;
5632 case gov_number:
5633 case gov_hidden_number:
5634 *type = &t_number;
5635 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005636 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005637 }
5638 else if (*name == '$')
5639 {
5640 *dest = dest_env;
5641 *type = &t_string;
5642 }
5643 else if (*name == '@')
5644 {
5645 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5646 {
5647 emsg_invreg(name[1]);
5648 return FAIL;
5649 }
5650 *dest = dest_reg;
5651 *type = &t_string;
5652 }
5653 else if (STRNCMP(name, "g:", 2) == 0)
5654 {
5655 *dest = dest_global;
5656 }
5657 else if (STRNCMP(name, "b:", 2) == 0)
5658 {
5659 *dest = dest_buffer;
5660 }
5661 else if (STRNCMP(name, "w:", 2) == 0)
5662 {
5663 *dest = dest_window;
5664 }
5665 else if (STRNCMP(name, "t:", 2) == 0)
5666 {
5667 *dest = dest_tab;
5668 }
5669 else if (STRNCMP(name, "v:", 2) == 0)
5670 {
5671 typval_T *vtv;
5672 int di_flags;
5673
5674 *vimvaridx = find_vim_var(name + 2, &di_flags);
5675 if (*vimvaridx < 0)
5676 {
5677 semsg(_(e_variable_not_found_str), name);
5678 return FAIL;
5679 }
5680 // We use the current value of "sandbox" here, is that OK?
5681 if (var_check_ro(di_flags, name, FALSE))
5682 return FAIL;
5683 *dest = dest_vimvar;
5684 vtv = get_vim_var_tv(*vimvaridx);
5685 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5686 }
5687 return OK;
5688}
5689
5690/*
5691 * Generate a STORE instruction for "dest", not being "dest_local".
5692 * Return FAIL when out of memory.
5693 */
5694 static int
5695generate_store_var(
5696 cctx_T *cctx,
5697 assign_dest_T dest,
5698 int opt_flags,
5699 int vimvaridx,
5700 int scriptvar_idx,
5701 int scriptvar_sid,
5702 type_T *type,
5703 char_u *name)
5704{
5705 switch (dest)
5706 {
5707 case dest_option:
5708 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5709 opt_flags);
5710 case dest_global:
5711 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005712 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5713 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005714 case dest_buffer:
5715 // include b: with the name, easier to execute that way
5716 return generate_STORE(cctx, ISN_STOREB, 0, name);
5717 case dest_window:
5718 // include w: with the name, easier to execute that way
5719 return generate_STORE(cctx, ISN_STOREW, 0, name);
5720 case dest_tab:
5721 // include t: with the name, easier to execute that way
5722 return generate_STORE(cctx, ISN_STORET, 0, name);
5723 case dest_env:
5724 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5725 case dest_reg:
5726 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5727 case dest_vimvar:
5728 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5729 case dest_script:
5730 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005731 // "s:" may be included in the name.
5732 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005733 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005734 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5735 scriptvar_sid, scriptvar_idx, type);
5736 case dest_local:
5737 case dest_expr:
5738 // cannot happen
5739 break;
5740 }
5741 return FAIL;
5742}
5743
Bram Moolenaar752fc692021-01-04 21:57:11 +01005744 static int
5745is_decl_command(int cmdidx)
5746{
5747 return cmdidx == CMD_let || cmdidx == CMD_var
5748 || cmdidx == CMD_final || cmdidx == CMD_const;
5749}
5750
5751/*
5752 * Figure out the LHS type and other properties for an assignment or one item
5753 * of ":unlet" with an index.
5754 * Returns OK or FAIL.
5755 */
5756 static int
5757compile_lhs(
5758 char_u *var_start,
5759 lhs_T *lhs,
5760 int cmdidx,
5761 int heredoc,
5762 int oplen,
5763 cctx_T *cctx)
5764{
5765 char_u *var_end;
5766 int is_decl = is_decl_command(cmdidx);
5767
5768 CLEAR_POINTER(lhs);
5769 lhs->lhs_dest = dest_local;
5770 lhs->lhs_vimvaridx = -1;
5771 lhs->lhs_scriptvar_idx = -1;
5772
5773 // "dest_end" is the end of the destination, including "[expr]" or
5774 // ".name".
5775 // "var_end" is the end of the variable/option/etc. name.
5776 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5777 if (*var_start == '@')
5778 var_end = var_start + 2;
5779 else
5780 {
5781 // skip over the leading "&", "&l:", "&g:" and "$"
5782 var_end = skip_option_env_lead(var_start);
5783 var_end = to_name_end(var_end, TRUE);
5784 }
5785
5786 // "a: type" is declaring variable "a" with a type, not dict "a:".
5787 if (is_decl && lhs->lhs_dest_end == var_start + 2
5788 && lhs->lhs_dest_end[-1] == ':')
5789 --lhs->lhs_dest_end;
5790 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5791 --var_end;
5792
5793 // compute the length of the destination without "[expr]" or ".name"
5794 lhs->lhs_varlen = var_end - var_start;
5795 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5796 if (lhs->lhs_name == NULL)
5797 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005798
5799 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5800 // Something follows after the variable: "var[idx]" or "var.key".
5801 lhs->lhs_has_index = TRUE;
5802
Bram Moolenaar752fc692021-01-04 21:57:11 +01005803 if (heredoc)
5804 lhs->lhs_type = &t_list_string;
5805 else
5806 lhs->lhs_type = &t_any;
5807
5808 if (cctx->ctx_skip != SKIP_YES)
5809 {
5810 int declare_error = FALSE;
5811
5812 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5813 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5814 &lhs->lhs_type, cctx) == FAIL)
5815 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005816 if (lhs->lhs_dest != dest_local
5817 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005818 {
5819 // Specific kind of variable recognized.
5820 declare_error = is_decl;
5821 }
5822 else
5823 {
5824 int idx;
5825
5826 // No specific kind of variable recognized, just a name.
5827 for (idx = 0; reserved[idx] != NULL; ++idx)
5828 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5829 {
5830 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5831 return FAIL;
5832 }
5833
5834
5835 if (lookup_local(var_start, lhs->lhs_varlen,
5836 &lhs->lhs_local_lvar, cctx) == OK)
5837 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5838 else
5839 {
5840 CLEAR_FIELD(lhs->lhs_arg_lvar);
5841 if (arg_exists(var_start, lhs->lhs_varlen,
5842 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5843 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5844 {
5845 if (is_decl)
5846 {
5847 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5848 return FAIL;
5849 }
5850 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5851 }
5852 }
5853 if (lhs->lhs_lvar != NULL)
5854 {
5855 if (is_decl)
5856 {
5857 semsg(_(e_variable_already_declared), lhs->lhs_name);
5858 return FAIL;
5859 }
5860 }
5861 else
5862 {
5863 int script_namespace = lhs->lhs_varlen > 1
5864 && STRNCMP(var_start, "s:", 2) == 0;
5865 int script_var = (script_namespace
5866 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005867 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005868 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005869 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005870 imported_T *import =
5871 find_imported(var_start, lhs->lhs_varlen, cctx);
5872
5873 if (script_namespace || script_var || import != NULL)
5874 {
5875 char_u *rawname = lhs->lhs_name
5876 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5877
5878 if (is_decl)
5879 {
5880 if (script_namespace)
5881 semsg(_(e_cannot_declare_script_variable_in_function),
5882 lhs->lhs_name);
5883 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005884 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005885 lhs->lhs_name);
5886 return FAIL;
5887 }
5888 else if (cctx->ctx_ufunc->uf_script_ctx_version
5889 == SCRIPT_VERSION_VIM9
5890 && script_namespace
5891 && !script_var && import == NULL)
5892 {
5893 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5894 return FAIL;
5895 }
5896
5897 lhs->lhs_dest = dest_script;
5898
5899 // existing script-local variables should have a type
5900 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5901 if (import != NULL)
5902 lhs->lhs_scriptvar_sid = import->imp_sid;
5903 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5904 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005905 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005907 lhs->lhs_scriptvar_sid, rawname,
5908 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5909 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005910 if (lhs->lhs_scriptvar_idx >= 0)
5911 {
5912 scriptitem_T *si = SCRIPT_ITEM(
5913 lhs->lhs_scriptvar_sid);
5914 svar_T *sv =
5915 ((svar_T *)si->sn_var_vals.ga_data)
5916 + lhs->lhs_scriptvar_idx;
5917 lhs->lhs_type = sv->sv_type;
5918 }
5919 }
5920 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005921 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005922 == FAIL)
5923 return FAIL;
5924 }
5925 }
5926
5927 if (declare_error)
5928 {
5929 vim9_declare_error(lhs->lhs_name);
5930 return FAIL;
5931 }
5932 }
5933
5934 // handle "a:name" as a name, not index "name" on "a"
5935 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5936 var_end = lhs->lhs_dest_end;
5937
5938 if (lhs->lhs_dest != dest_option)
5939 {
5940 if (is_decl && *var_end == ':')
5941 {
5942 char_u *p;
5943
5944 // parse optional type: "let var: type = expr"
5945 if (!VIM_ISWHITE(var_end[1]))
5946 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005947 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005948 return FAIL;
5949 }
5950 p = skipwhite(var_end + 1);
5951 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5952 if (lhs->lhs_type == NULL)
5953 return FAIL;
5954 lhs->lhs_has_type = TRUE;
5955 }
5956 else if (lhs->lhs_lvar != NULL)
5957 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5958 }
5959
Bram Moolenaare42939a2021-04-05 17:11:17 +02005960 if (oplen == 3 && !heredoc
5961 && lhs->lhs_dest != dest_global
5962 && !lhs->lhs_has_index
5963 && lhs->lhs_type->tt_type != VAR_STRING
5964 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005965 {
5966 emsg(_(e_can_only_concatenate_to_string));
5967 return FAIL;
5968 }
5969
5970 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5971 && cctx->ctx_skip != SKIP_YES)
5972 {
5973 if (oplen > 1 && !heredoc)
5974 {
5975 // +=, /=, etc. require an existing variable
5976 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5977 return FAIL;
5978 }
5979 if (!is_decl)
5980 {
5981 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5982 return FAIL;
5983 }
5984
Bram Moolenaar3f327882021-03-17 20:56:38 +01005985 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005986 if ((lhs->lhs_type->tt_type == VAR_FUNC
5987 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005988 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005989 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005990
5991 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005992 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5993 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5994 if (lhs->lhs_lvar == NULL)
5995 return FAIL;
5996 lhs->lhs_new_local = TRUE;
5997 }
5998
5999 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006000 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006001 {
6002 // Something follows after the variable: "var[idx]" or "var.key".
6003 // TODO: should we also handle "->func()" here?
6004 if (is_decl)
6005 {
6006 emsg(_(e_cannot_use_index_when_declaring_variable));
6007 return FAIL;
6008 }
6009
6010 if (var_start[lhs->lhs_varlen] == '['
6011 || var_start[lhs->lhs_varlen] == '.')
6012 {
6013 char_u *after = var_start + lhs->lhs_varlen;
6014 char_u *p;
6015
6016 // Only the last index is used below, if there are others
6017 // before it generate code for the expression. Thus for
6018 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6019 for (;;)
6020 {
6021 p = skip_index(after);
6022 if (*p != '[' && *p != '.')
6023 break;
6024 after = p;
6025 }
6026 if (after > var_start + lhs->lhs_varlen)
6027 {
6028 lhs->lhs_varlen = after - var_start;
6029 lhs->lhs_dest = dest_expr;
6030 // We don't know the type before evaluating the expression,
6031 // use "any" until then.
6032 lhs->lhs_type = &t_any;
6033 }
6034
Bram Moolenaar752fc692021-01-04 21:57:11 +01006035 if (lhs->lhs_type->tt_member == NULL)
6036 lhs->lhs_member_type = &t_any;
6037 else
6038 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6039 }
6040 else
6041 {
6042 semsg("Not supported yet: %s", var_start);
6043 return FAIL;
6044 }
6045 }
6046 return OK;
6047}
6048
6049/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006050 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6051 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006052 */
6053 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006054compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 char_u *var_start,
6056 lhs_T *lhs,
6057 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006058 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 cctx_T *cctx)
6060{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006062 char_u *p;
6063 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 p = var_start + varlen;
6066 if (*p == '[')
6067 {
6068 p = skipwhite(p + 1);
6069 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006070
6071 if (r == OK && *skipwhite(p) == ':')
6072 {
6073 // unlet var[idx : idx]
6074 if (is_assign)
6075 {
6076 semsg(_(e_cannot_use_range_with_assignment_str), p);
6077 return FAIL;
6078 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006079 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006080 p = skipwhite(p);
6081 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6082 {
6083 semsg(_(e_white_space_required_before_and_after_str_at_str),
6084 ":", p);
6085 return FAIL;
6086 }
6087 p = skipwhite(p + 1);
6088 r = compile_expr0(&p, cctx);
6089 }
6090
Bram Moolenaar752fc692021-01-04 21:57:11 +01006091 if (r == OK && *skipwhite(p) != ']')
6092 {
6093 // this should not happen
6094 emsg(_(e_missbrac));
6095 r = FAIL;
6096 }
6097 }
6098 else // if (*p == '.')
6099 {
6100 char_u *key_end = to_name_end(p + 1, TRUE);
6101 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6102
6103 r = generate_PUSHS(cctx, key);
6104 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006105 return r;
6106}
6107
6108/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006109 * For a LHS with an index, load the variable to be indexed.
6110 */
6111 static int
6112compile_load_lhs(
6113 lhs_T *lhs,
6114 char_u *var_start,
6115 type_T *rhs_type,
6116 cctx_T *cctx)
6117{
6118 if (lhs->lhs_dest == dest_expr)
6119 {
6120 size_t varlen = lhs->lhs_varlen;
6121 int c = var_start[varlen];
6122 char_u *p = var_start;
6123 garray_T *stack = &cctx->ctx_type_stack;
6124
6125 // Evaluate "ll[expr]" of "ll[expr][idx]"
6126 var_start[varlen] = NUL;
6127 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6128 {
6129 // this should not happen
6130 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006131 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006132 return FAIL;
6133 }
6134 var_start[varlen] = c;
6135
6136 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6137 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6138 // now we can properly check the type
6139 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6140 && rhs_type != &t_void
6141 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6142 FALSE, FALSE) == FAIL)
6143 return FAIL;
6144 }
6145 else
6146 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6147 lhs->lhs_lvar, lhs->lhs_type);
6148 return OK;
6149}
6150
6151/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006152 * Assignment to a list or dict member, or ":unlet" for the item, using the
6153 * information in "lhs".
6154 * Returns OK or FAIL.
6155 */
6156 static int
6157compile_assign_unlet(
6158 char_u *var_start,
6159 lhs_T *lhs,
6160 int is_assign,
6161 type_T *rhs_type,
6162 cctx_T *cctx)
6163{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006164 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006165 garray_T *stack = &cctx->ctx_type_stack;
6166 int range = FALSE;
6167
6168 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 return FAIL;
6170
6171 if (lhs->lhs_type == &t_any)
6172 {
6173 // Index on variable of unknown type: check at runtime.
6174 dest_type = VAR_ANY;
6175 }
6176 else
6177 {
6178 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006179 if (dest_type == VAR_DICT && range)
6180 {
6181 emsg(e_cannot_use_range_with_dictionary);
6182 return FAIL;
6183 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6185 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006186 if (dest_type == VAR_LIST)
6187 {
6188 if (range
6189 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006190 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006191 return FAIL;
6192 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6193 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6194 return FAIL;
6195 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 }
6197
6198 // Load the dict or list. On the stack we then have:
6199 // - value (for assignment, not for :unlet)
6200 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006201 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006202 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006203 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6204 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205
6206 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6207 {
6208 if (is_assign)
6209 {
6210 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6211
6212 if (isn == NULL)
6213 return FAIL;
6214 isn->isn_arg.vartype = dest_type;
6215 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006216 else if (range)
6217 {
6218 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6219 return FAIL;
6220 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221 else
6222 {
6223 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6224 return FAIL;
6225 }
6226 }
6227 else
6228 {
6229 emsg(_(e_indexable_type_required));
6230 return FAIL;
6231 }
6232
6233 return OK;
6234}
6235
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006236/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006237 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006238 * "let name"
6239 * "var name = expr"
6240 * "final name = expr"
6241 * "const name = expr"
6242 * "name = expr"
6243 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006244 * Return NULL for an error.
6245 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 */
6247 static char_u *
6248compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6249{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006250 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006252 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 char_u *ret = NULL;
6254 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006255 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006258 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 int oplen = 0;
6261 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006262 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 int is_decl = is_decl_command(cmdidx);
6265 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006266 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268 // Skip over the "var" or "[var, var]" to get to any "=".
6269 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6270 if (p == NULL)
6271 return *arg == '[' ? arg : NULL;
6272
6273 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006275 // TODO: should we allow this, and figure out type inference from list
6276 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006277 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 return NULL;
6279 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 sp = p;
6283 p = skipwhite(p);
6284 op = p;
6285 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006286
6287 if (var_count > 0 && oplen == 0)
6288 // can be something like "[1, 2]->func()"
6289 return arg;
6290
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006291 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006293 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006294 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006295 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 if (heredoc)
6298 {
6299 list_T *l;
6300 listitem_T *li;
6301
6302 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006303 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006305 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006306 if (l == NULL)
6307 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
Bram Moolenaar078269b2020-09-21 20:35:55 +02006309 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006311 // Push each line and the create the list.
6312 FOR_ALL_LIST_ITEMS(l, li)
6313 {
6314 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6315 li->li_tv.vval.v_string = NULL;
6316 }
6317 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 list_free(l);
6320 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006321 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006323 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006325 char_u *wp;
6326
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006327 // for "[var, var] = expr" evaluate the expression here, loop over the
6328 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006329 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006330
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006331 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006332 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006333 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006334 if (compile_expr0(&p, cctx) == FAIL)
6335 return NULL;
6336 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006338 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006340 type_T *stacktype;
6341
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006342 stacktype = stack->ga_len == 0 ? &t_void
6343 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006344 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006346 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006347 goto theend;
6348 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006349 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006350 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006351 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006352 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006353 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6354 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006355 if (stacktype->tt_member != NULL)
6356 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006357 }
6358 }
6359
6360 /*
6361 * Loop over variables in "[var, var] = expr".
6362 * For "var = expr" and "let var: type" this is done only once.
6363 */
6364 if (var_count > 0)
6365 var_start = skipwhite(arg + 1); // skip over the "["
6366 else
6367 var_start = arg;
6368 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6369 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006370 int instr_count = -1;
6371
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 vim_free(lhs.lhs_name);
6373
6374 /*
6375 * Figure out the LHS type and other properties.
6376 */
6377 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6378 goto theend;
6379
6380 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006381 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006382 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006383 goto theend;
6384 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006385 if (!is_decl && lhs.lhs_lvar != NULL
6386 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006387 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006388 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006389 goto theend;
6390 }
Bram Moolenaar962c43b2021-04-10 17:18:09 +02006391 if (is_decl && lhs.lhs_name[0] == '_' && lhs.lhs_name[1] == NUL)
6392 {
6393 emsg(_(e_cannot_use_underscore_here));
6394 goto theend;
6395 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006396
6397 if (!heredoc)
6398 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006399 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006400 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006401 if (oplen > 0 && var_count == 0)
6402 {
6403 // skip over the "=" and the expression
6404 p = skipwhite(op + oplen);
6405 compile_expr0(&p, cctx);
6406 }
6407 }
6408 else if (oplen > 0)
6409 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006410 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006411 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006412
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006413 // For "var = expr" evaluate the expression.
6414 if (var_count == 0)
6415 {
6416 int r;
6417
6418 // for "+=", "*=", "..=" etc. first load the current value
6419 if (*op != '=')
6420 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006421 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006422
Bram Moolenaar752fc692021-01-04 21:57:11 +01006423 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006424 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006425 int range = FALSE;
6426
6427 // Get member from list or dict. First compile the
6428 // index value.
6429 if (compile_assign_index(var_start, &lhs,
6430 TRUE, &range, cctx) == FAIL)
6431 goto theend;
6432
6433 // Get the member.
6434 if (compile_member(FALSE, cctx) == FAIL)
6435 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006436 }
6437 }
6438
6439 // Compile the expression. Temporarily hide the new local
6440 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006442 --cctx->ctx_locals.ga_len;
6443 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006444 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006445 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006446 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006447 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006448 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006449 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006450 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006451 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006452 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006453 ++cctx->ctx_locals.ga_len;
6454 if (r == FAIL)
6455 goto theend;
6456 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006457 else if (semicolon && var_idx == var_count - 1)
6458 {
6459 // For "[var; var] = expr" get the rest of the list
6460 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6461 goto theend;
6462 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006463 else
6464 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006465 // For "[var, var] = expr" get the "var_idx" item from the
6466 // list.
6467 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006468 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006469 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006470
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006471 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006472 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006474 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006475 if ((rhs_type->tt_type == VAR_FUNC
6476 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006477 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006479 goto theend;
6480
Bram Moolenaar752fc692021-01-04 21:57:11 +01006481 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006482 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006483 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006484 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006485 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006486 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006487 }
6488 else
6489 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006490 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006491 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006492 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006494 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006496 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006497 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006498 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006500 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006501 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006502 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006503 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006504 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006505
Bram Moolenaar77709b12021-04-03 21:01:01 +02006506 // Without operator check type here, otherwise below.
6507 // Use the line number of the assignment.
6508 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 if (lhs.lhs_has_index)
6510 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006511 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006512 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006513 goto theend;
6514 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006515 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006516 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006517 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006518 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006520 else if (cmdidx == CMD_final)
6521 {
6522 emsg(_(e_final_requires_a_value));
6523 goto theend;
6524 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006525 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006527 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006528 goto theend;
6529 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006531 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006532 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006533 goto theend;
6534 }
6535 else
6536 {
6537 // variables are always initialized
6538 if (ga_grow(instr, 1) == FAIL)
6539 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006541 {
6542 case VAR_BOOL:
6543 generate_PUSHBOOL(cctx, VVAL_FALSE);
6544 break;
6545 case VAR_FLOAT:
6546#ifdef FEAT_FLOAT
6547 generate_PUSHF(cctx, 0.0);
6548#endif
6549 break;
6550 case VAR_STRING:
6551 generate_PUSHS(cctx, NULL);
6552 break;
6553 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006554 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006555 break;
6556 case VAR_FUNC:
6557 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6558 break;
6559 case VAR_LIST:
6560 generate_NEWLIST(cctx, 0);
6561 break;
6562 case VAR_DICT:
6563 generate_NEWDICT(cctx, 0);
6564 break;
6565 case VAR_JOB:
6566 generate_PUSHJOB(cctx, NULL);
6567 break;
6568 case VAR_CHANNEL:
6569 generate_PUSHCHANNEL(cctx, NULL);
6570 break;
6571 case VAR_NUMBER:
6572 case VAR_UNKNOWN:
6573 case VAR_ANY:
6574 case VAR_PARTIAL:
6575 case VAR_VOID:
6576 case VAR_SPECIAL: // cannot happen
6577 generate_PUSHNR(cctx, 0);
6578 break;
6579 }
6580 }
6581 if (var_count == 0)
6582 end = p;
6583 }
6584
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006585 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006586 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006587 break;
6588
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006589 if (oplen > 0 && *op != '=')
6590 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006591 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006592 type_T *stacktype;
6593
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006594 if (*op == '.')
6595 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006596 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006597 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006598 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006599 if (
6600#ifdef FEAT_FLOAT
6601 // If variable is float operation with number is OK.
6602 !(expected == &t_float && stacktype == &t_number) &&
6603#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006604 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006605 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006606 goto theend;
6607
6608 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006609 {
6610 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6611 goto theend;
6612 }
6613 else if (*op == '+')
6614 {
6615 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 operator_type(lhs.lhs_member_type, stacktype),
6617 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006618 goto theend;
6619 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006620 else if (generate_two_op(cctx, op) == FAIL)
6621 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006623
Bram Moolenaar752fc692021-01-04 21:57:11 +01006624 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006625 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 // Use the info in "lhs" to store the value at the index in the
6627 // list or dict.
6628 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6629 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006631 }
6632 else
6633 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006634 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006635 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006636 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006637 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006638 generate_LOCKCONST(cctx);
6639
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006640 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006641 && (lhs.lhs_type->tt_type == VAR_DICT
6642 || lhs.lhs_type->tt_type == VAR_LIST)
6643 && lhs.lhs_type->tt_member != NULL
6644 && lhs.lhs_type->tt_member != &t_any
6645 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006646 // Set the type in the list or dict, so that it can be checked,
6647 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006648 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006649
Bram Moolenaar752fc692021-01-04 21:57:11 +01006650 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006651 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006652 if (generate_store_var(cctx, lhs.lhs_dest,
6653 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6654 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6655 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006656 goto theend;
6657 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006658 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006659 {
6660 isn_T *isn = ((isn_T *)instr->ga_data)
6661 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006662
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006663 // optimization: turn "var = 123" from ISN_PUSHNR +
6664 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006665 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006666 && instr->ga_len == instr_count + 1
6667 && isn->isn_type == ISN_PUSHNR)
6668 {
6669 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006670
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006671 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006673 isn->isn_arg.storenr.stnr_val = val;
6674 if (stack->ga_len > 0)
6675 --stack->ga_len;
6676 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006677 else if (lhs.lhs_lvar->lv_from_outer > 0)
6678 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6679 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006680 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006681 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006682 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006684
6685 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006686 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006687 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006688
6689 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006690 if (var_count > 0 && !semicolon)
6691 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006692 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006693 goto theend;
6694 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006695
Bram Moolenaarb2097502020-07-19 17:17:02 +02006696 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
6698theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006699 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006700 return ret;
6701}
6702
6703/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006704 * Check for an assignment at "eap->cmd", compile it if found.
6705 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6706 */
6707 static int
6708may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6709{
6710 char_u *pskip;
6711 char_u *p;
6712
6713 // Assuming the command starts with a variable or function name,
6714 // find what follows.
6715 // Skip over "var.member", "var[idx]" and the like.
6716 // Also "&opt = val", "$ENV = val" and "@r = val".
6717 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6718 ? eap->cmd + 1 : eap->cmd;
6719 p = to_name_end(pskip, TRUE);
6720 if (p > eap->cmd && *p != NUL)
6721 {
6722 char_u *var_end;
6723 int oplen;
6724 int heredoc;
6725
6726 if (eap->cmd[0] == '@')
6727 var_end = eap->cmd + 2;
6728 else
6729 var_end = find_name_end(pskip, NULL, NULL,
6730 FNE_CHECK_START | FNE_INCL_BR);
6731 oplen = assignment_len(skipwhite(var_end), &heredoc);
6732 if (oplen > 0)
6733 {
6734 size_t len = p - eap->cmd;
6735
6736 // Recognize an assignment if we recognize the variable
6737 // name:
6738 // "g:var = expr"
6739 // "local = expr" where "local" is a local var.
6740 // "script = expr" where "script" is a script-local var.
6741 // "import = expr" where "import" is an imported var
6742 // "&opt = expr"
6743 // "$ENV = expr"
6744 // "@r = expr"
6745 if (*eap->cmd == '&'
6746 || *eap->cmd == '$'
6747 || *eap->cmd == '@'
6748 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006749 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006750 {
6751 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6752 if (*line == NULL || *line == eap->cmd)
6753 return FAIL;
6754 return OK;
6755 }
6756 }
6757 }
6758
6759 if (*eap->cmd == '[')
6760 {
6761 // [var, var] = expr
6762 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6763 if (*line == NULL)
6764 return FAIL;
6765 if (*line != eap->cmd)
6766 return OK;
6767 }
6768 return NOTDONE;
6769}
6770
6771/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006772 * Check if "name" can be "unlet".
6773 */
6774 int
6775check_vim9_unlet(char_u *name)
6776{
6777 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6778 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006779 // "unlet s:var" is allowed in legacy script.
6780 if (*name == 's' && !script_is_vim9())
6781 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006782 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006783 return FAIL;
6784 }
6785 return OK;
6786}
6787
6788/*
6789 * Callback passed to ex_unletlock().
6790 */
6791 static int
6792compile_unlet(
6793 lval_T *lvp,
6794 char_u *name_end,
6795 exarg_T *eap,
6796 int deep UNUSED,
6797 void *coookie)
6798{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006799 cctx_T *cctx = coookie;
6800 char_u *p = lvp->ll_name;
6801 int cc = *name_end;
6802 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006803
Bram Moolenaar752fc692021-01-04 21:57:11 +01006804 if (cctx->ctx_skip == SKIP_YES)
6805 return OK;
6806
6807 *name_end = NUL;
6808 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006809 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 // :unlet $ENV_VAR
6811 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6812 }
6813 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6814 {
6815 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006816
Bram Moolenaar752fc692021-01-04 21:57:11 +01006817 // This is similar to assigning: lookup the list/dict, compile the
6818 // idx/key. Then instead of storing the value unlet the item.
6819 // unlet {list}[idx]
6820 // unlet {dict}[key] dict.key
6821 //
6822 // Figure out the LHS type and other properties.
6823 //
6824 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6825
6826 // : unlet an indexed item
6827 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006828 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006829 iemsg("called compile_lhs() without an index");
6830 ret = FAIL;
6831 }
6832 else
6833 {
6834 // Use the info in "lhs" to unlet the item at the index in the
6835 // list or dict.
6836 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006837 }
6838
Bram Moolenaar752fc692021-01-04 21:57:11 +01006839 vim_free(lhs.lhs_name);
6840 }
6841 else if (check_vim9_unlet(p) == FAIL)
6842 {
6843 ret = FAIL;
6844 }
6845 else
6846 {
6847 // Normal name. Only supports g:, w:, t: and b: namespaces.
6848 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006849 }
6850
Bram Moolenaar752fc692021-01-04 21:57:11 +01006851 *name_end = cc;
6852 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006853}
6854
6855/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006856 * Callback passed to ex_unletlock().
6857 */
6858 static int
6859compile_lock_unlock(
6860 lval_T *lvp,
6861 char_u *name_end,
6862 exarg_T *eap,
6863 int deep UNUSED,
6864 void *coookie)
6865{
6866 cctx_T *cctx = coookie;
6867 int cc = *name_end;
6868 char_u *p = lvp->ll_name;
6869 int ret = OK;
6870 size_t len;
6871 char_u *buf;
6872
6873 if (cctx->ctx_skip == SKIP_YES)
6874 return OK;
6875
6876 // Cannot use :lockvar and :unlockvar on local variables.
6877 if (p[1] != ':')
6878 {
6879 char_u *end = skip_var_one(p, FALSE);
6880
6881 if (lookup_local(p, end - p, NULL, cctx) == OK)
6882 {
6883 emsg(_(e_cannot_lock_unlock_local_variable));
6884 return FAIL;
6885 }
6886 }
6887
6888 // Checking is done at runtime.
6889 *name_end = NUL;
6890 len = name_end - p + 20;
6891 buf = alloc(len);
6892 if (buf == NULL)
6893 ret = FAIL;
6894 else
6895 {
6896 vim_snprintf((char *)buf, len, "%s %s",
6897 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6898 p);
6899 ret = generate_EXEC(cctx, buf);
6900
6901 vim_free(buf);
6902 *name_end = cc;
6903 }
6904 return ret;
6905}
6906
6907/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006908 * compile "unlet var", "lock var" and "unlock var"
6909 * "arg" points to "var".
6910 */
6911 static char_u *
6912compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6913{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006914 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6915 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6916 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006917 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6918}
6919
6920/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921 * Compile an :import command.
6922 */
6923 static char_u *
6924compile_import(char_u *arg, cctx_T *cctx)
6925{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006926 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927}
6928
6929/*
6930 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6931 */
6932 static int
6933compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6934{
6935 garray_T *instr = &cctx->ctx_instr;
6936 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6937
6938 if (endlabel == NULL)
6939 return FAIL;
6940 endlabel->el_next = *el;
6941 *el = endlabel;
6942 endlabel->el_end_label = instr->ga_len;
6943
6944 generate_JUMP(cctx, when, 0);
6945 return OK;
6946}
6947
6948 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006949compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950{
6951 garray_T *instr = &cctx->ctx_instr;
6952
6953 while (*el != NULL)
6954 {
6955 endlabel_T *cur = (*el);
6956 isn_T *isn;
6957
6958 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006959 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 *el = cur->el_next;
6961 vim_free(cur);
6962 }
6963}
6964
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006965 static void
6966compile_free_jump_to_end(endlabel_T **el)
6967{
6968 while (*el != NULL)
6969 {
6970 endlabel_T *cur = (*el);
6971
6972 *el = cur->el_next;
6973 vim_free(cur);
6974 }
6975}
6976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977/*
6978 * Create a new scope and set up the generic items.
6979 */
6980 static scope_T *
6981new_scope(cctx_T *cctx, scopetype_T type)
6982{
6983 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6984
6985 if (scope == NULL)
6986 return NULL;
6987 scope->se_outer = cctx->ctx_scope;
6988 cctx->ctx_scope = scope;
6989 scope->se_type = type;
6990 scope->se_local_count = cctx->ctx_locals.ga_len;
6991 return scope;
6992}
6993
6994/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006995 * Free the current scope and go back to the outer scope.
6996 */
6997 static void
6998drop_scope(cctx_T *cctx)
6999{
7000 scope_T *scope = cctx->ctx_scope;
7001
7002 if (scope == NULL)
7003 {
7004 iemsg("calling drop_scope() without a scope");
7005 return;
7006 }
7007 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007008 switch (scope->se_type)
7009 {
7010 case IF_SCOPE:
7011 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7012 case FOR_SCOPE:
7013 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7014 case WHILE_SCOPE:
7015 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7016 case TRY_SCOPE:
7017 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7018 case NO_SCOPE:
7019 case BLOCK_SCOPE:
7020 break;
7021 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007022 vim_free(scope);
7023}
7024
7025/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 * compile "if expr"
7027 *
7028 * "if expr" Produces instructions:
7029 * EVAL expr Push result of "expr"
7030 * JUMP_IF_FALSE end
7031 * ... body ...
7032 * end:
7033 *
7034 * "if expr | else" Produces instructions:
7035 * EVAL expr Push result of "expr"
7036 * JUMP_IF_FALSE else
7037 * ... body ...
7038 * JUMP_ALWAYS end
7039 * else:
7040 * ... body ...
7041 * end:
7042 *
7043 * "if expr1 | elseif expr2 | else" Produces instructions:
7044 * EVAL expr Push result of "expr"
7045 * JUMP_IF_FALSE elseif
7046 * ... body ...
7047 * JUMP_ALWAYS end
7048 * elseif:
7049 * EVAL expr Push result of "expr"
7050 * JUMP_IF_FALSE else
7051 * ... body ...
7052 * JUMP_ALWAYS end
7053 * else:
7054 * ... body ...
7055 * end:
7056 */
7057 static char_u *
7058compile_if(char_u *arg, cctx_T *cctx)
7059{
7060 char_u *p = arg;
7061 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007062 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007064 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007065 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
Bram Moolenaara5565e42020-05-09 15:44:01 +02007067 CLEAR_FIELD(ppconst);
7068 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007069 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007070 clear_ppconst(&ppconst);
7071 return NULL;
7072 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007073 if (!ends_excmd2(arg, skipwhite(p)))
7074 {
7075 semsg(_(e_trailing_arg), p);
7076 return NULL;
7077 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007078 if (cctx->ctx_skip == SKIP_YES)
7079 clear_ppconst(&ppconst);
7080 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007081 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007082 int error = FALSE;
7083 int v;
7084
Bram Moolenaara5565e42020-05-09 15:44:01 +02007085 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007086 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007087 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007088 if (error)
7089 return NULL;
7090 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007091 }
7092 else
7093 {
7094 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007095 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007096 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007097 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007098 if (bool_on_stack(cctx) == FAIL)
7099 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101
Bram Moolenaara91a7132021-03-25 21:12:15 +01007102 // CMDMOD_REV must come before the jump
7103 generate_undo_cmdmods(cctx);
7104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 scope = new_scope(cctx, IF_SCOPE);
7106 if (scope == NULL)
7107 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007108 scope->se_skip_save = skip_save;
7109 // "is_had_return" will be reset if any block does not end in :return
7110 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007112 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007113 {
7114 // "where" is set when ":elseif", "else" or ":endif" is found
7115 scope->se_u.se_if.is_if_label = instr->ga_len;
7116 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7117 }
7118 else
7119 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120
Bram Moolenaarced68a02021-01-24 17:53:47 +01007121#ifdef FEAT_PROFILE
7122 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7123 && skip_save != SKIP_YES)
7124 {
7125 // generated a profile start, need to generate a profile end, since it
7126 // won't be done after returning
7127 cctx->ctx_skip = SKIP_NOT;
7128 generate_instr(cctx, ISN_PROF_END);
7129 cctx->ctx_skip = SKIP_YES;
7130 }
7131#endif
7132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007133 return p;
7134}
7135
7136 static char_u *
7137compile_elseif(char_u *arg, cctx_T *cctx)
7138{
7139 char_u *p = arg;
7140 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007141 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 isn_T *isn;
7143 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007144 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007145 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146
7147 if (scope == NULL || scope->se_type != IF_SCOPE)
7148 {
7149 emsg(_(e_elseif_without_if));
7150 return NULL;
7151 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007152 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007153 if (!cctx->ctx_had_return)
7154 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007155
Bram Moolenaarced68a02021-01-24 17:53:47 +01007156 if (cctx->ctx_skip == SKIP_NOT)
7157 {
7158 // previous block was executed, this one and following will not
7159 cctx->ctx_skip = SKIP_YES;
7160 scope->se_u.se_if.is_seen_skip_not = TRUE;
7161 }
7162 if (scope->se_u.se_if.is_seen_skip_not)
7163 {
7164 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007165 // Do not count the "elseif" for profiling and cmdmod
7166 instr->ga_len = current_instr_idx(cctx);
7167
Bram Moolenaarced68a02021-01-24 17:53:47 +01007168 skip_expr_cctx(&p, cctx);
7169 return p;
7170 }
7171
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007172 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007173 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007174 int moved_cmdmod = FALSE;
7175
7176 // Move any CMDMOD instruction to after the jump
7177 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7178 {
7179 if (ga_grow(instr, 1) == FAIL)
7180 return NULL;
7181 ((isn_T *)instr->ga_data)[instr->ga_len] =
7182 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7183 --instr->ga_len;
7184 moved_cmdmod = TRUE;
7185 }
7186
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007187 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007189 return NULL;
7190 // previous "if" or "elseif" jumps here
7191 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7192 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007193 if (moved_cmdmod)
7194 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007196
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007197 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007198 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007199 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007200 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007201 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007202#ifdef FEAT_PROFILE
7203 if (cctx->ctx_profiling)
7204 {
7205 // the previous block was skipped, need to profile this line
7206 generate_instr(cctx, ISN_PROF_START);
7207 instr_count = instr->ga_len;
7208 }
7209#endif
7210 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007211 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007212 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007213 clear_ppconst(&ppconst);
7214 return NULL;
7215 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007216 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007217 if (!ends_excmd2(arg, skipwhite(p)))
7218 {
7219 semsg(_(e_trailing_arg), p);
7220 return NULL;
7221 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007222 if (scope->se_skip_save == SKIP_YES)
7223 clear_ppconst(&ppconst);
7224 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007225 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007226 int error = FALSE;
7227 int v;
7228
Bram Moolenaar7f141552020-05-09 17:35:53 +02007229 // The expression results in a constant.
7230 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007231 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7232 if (error)
7233 return NULL;
7234 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007235 clear_ppconst(&ppconst);
7236 scope->se_u.se_if.is_if_label = -1;
7237 }
7238 else
7239 {
7240 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007241 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007242 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007243 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007244 if (bool_on_stack(cctx) == FAIL)
7245 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246
Bram Moolenaara91a7132021-03-25 21:12:15 +01007247 // CMDMOD_REV must come before the jump
7248 generate_undo_cmdmods(cctx);
7249
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007250 // "where" is set when ":elseif", "else" or ":endif" is found
7251 scope->se_u.se_if.is_if_label = instr->ga_len;
7252 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
7255 return p;
7256}
7257
7258 static char_u *
7259compile_else(char_u *arg, cctx_T *cctx)
7260{
7261 char_u *p = arg;
7262 garray_T *instr = &cctx->ctx_instr;
7263 isn_T *isn;
7264 scope_T *scope = cctx->ctx_scope;
7265
7266 if (scope == NULL || scope->se_type != IF_SCOPE)
7267 {
7268 emsg(_(e_else_without_if));
7269 return NULL;
7270 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007271 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007272 if (!cctx->ctx_had_return)
7273 scope->se_u.se_if.is_had_return = FALSE;
7274 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007275
Bram Moolenaarced68a02021-01-24 17:53:47 +01007276#ifdef FEAT_PROFILE
7277 if (cctx->ctx_profiling)
7278 {
7279 if (cctx->ctx_skip == SKIP_NOT
7280 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7281 .isn_type == ISN_PROF_START)
7282 // the previous block was executed, do not count "else" for profiling
7283 --instr->ga_len;
7284 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7285 {
7286 // the previous block was not executed, this one will, do count the
7287 // "else" for profiling
7288 cctx->ctx_skip = SKIP_NOT;
7289 generate_instr(cctx, ISN_PROF_END);
7290 generate_instr(cctx, ISN_PROF_START);
7291 cctx->ctx_skip = SKIP_YES;
7292 }
7293 }
7294#endif
7295
7296 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007297 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007298 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007299 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007300 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007301 if (!cctx->ctx_had_return
7302 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7303 JUMP_ALWAYS, cctx) == FAIL)
7304 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007305 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007306
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007307 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007308 {
7309 if (scope->se_u.se_if.is_if_label >= 0)
7310 {
7311 // previous "if" or "elseif" jumps here
7312 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7313 isn->isn_arg.jump.jump_where = instr->ga_len;
7314 scope->se_u.se_if.is_if_label = -1;
7315 }
7316 }
7317
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007318 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007319 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321
7322 return p;
7323}
7324
7325 static char_u *
7326compile_endif(char_u *arg, cctx_T *cctx)
7327{
7328 scope_T *scope = cctx->ctx_scope;
7329 ifscope_T *ifscope;
7330 garray_T *instr = &cctx->ctx_instr;
7331 isn_T *isn;
7332
Bram Moolenaarfa984412021-03-25 22:15:28 +01007333 if (misplaced_cmdmod(cctx))
7334 return NULL;
7335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336 if (scope == NULL || scope->se_type != IF_SCOPE)
7337 {
7338 emsg(_(e_endif_without_if));
7339 return NULL;
7340 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007341 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007342 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007343 if (!cctx->ctx_had_return)
7344 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007346 if (scope->se_u.se_if.is_if_label >= 0)
7347 {
7348 // previous "if" or "elseif" jumps here
7349 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7350 isn->isn_arg.jump.jump_where = instr->ga_len;
7351 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007353 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007354
7355#ifdef FEAT_PROFILE
7356 // even when skipping we count the endif as executed, unless the block it's
7357 // in is skipped
7358 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7359 && scope->se_skip_save != SKIP_YES)
7360 {
7361 cctx->ctx_skip = SKIP_NOT;
7362 generate_instr(cctx, ISN_PROF_START);
7363 }
7364#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007365 cctx->ctx_skip = scope->se_skip_save;
7366
7367 // If all the blocks end in :return and there is an :else then the
7368 // had_return flag is set.
7369 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007371 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 return arg;
7373}
7374
7375/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007376 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 *
7378 * Produces instructions:
7379 * PUSHNR -1
7380 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007381 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7383 * - if beyond end, jump to "end"
7384 * - otherwise get item from list and push it
7385 * STORE var Store item in "var"
7386 * ... body ...
7387 * JUMP top Jump back to repeat
7388 * end: DROP Drop the result of "expr"
7389 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007390 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7391 * UNPACK 2 Split item in 2
7392 * STORE var1 Store item in "var1"
7393 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 */
7395 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007396compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007398 char_u *arg;
7399 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007400 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007402 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007403 int var_count = 0;
7404 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 garray_T *stack = &cctx->ctx_type_stack;
7407 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007408 lvar_T *loop_lvar; // loop iteration variable
7409 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007411 type_T *item_type = &t_any;
7412 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413
Bram Moolenaar792f7862020-11-23 08:31:18 +01007414 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007415 if (p == NULL)
7416 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007417 if (var_count == 0)
7418 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419
7420 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007421 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007422 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7423 return NULL;
7424 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 {
7426 emsg(_(e_missing_in));
7427 return NULL;
7428 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007429 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007430 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7431 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 scope = new_scope(cctx, FOR_SCOPE);
7434 if (scope == NULL)
7435 return NULL;
7436
Bram Moolenaar792f7862020-11-23 08:31:18 +01007437 // Reserve a variable to store the loop iteration counter and initialize it
7438 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007439 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7440 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007441 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007442 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007443 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007445 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007446 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447
7448 // compile "expr", it remains on the stack until "endfor"
7449 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007450 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007451 {
7452 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007454 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007455 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007457 // If we know the type of "var" and it is a not a list or string we can
7458 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007460 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7461 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007463 // TODO: support Blob
7464 semsg(_(e_for_loop_on_str_not_supported),
7465 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007466 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 return NULL;
7468 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007469
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007470 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007471 {
7472 if (var_count == 1)
7473 item_type = vartype->tt_member;
7474 else if (vartype->tt_member->tt_type == VAR_LIST
7475 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7476 item_type = vartype->tt_member->tt_member;
7477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478
Bram Moolenaara91a7132021-03-25 21:12:15 +01007479 // CMDMOD_REV must come before the FOR instruction
7480 generate_undo_cmdmods(cctx);
7481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007483 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007484 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007485
Bram Moolenaar792f7862020-11-23 08:31:18 +01007486 arg = arg_start;
7487 if (var_count > 1)
7488 {
7489 generate_UNPACK(cctx, var_count, semicolon);
7490 arg = skipwhite(arg + 1); // skip white after '['
7491
7492 // the list item is replaced by a number of items
7493 if (ga_grow(stack, var_count - 1) == FAIL)
7494 {
7495 drop_scope(cctx);
7496 return NULL;
7497 }
7498 --stack->ga_len;
7499 for (idx = 0; idx < var_count; ++idx)
7500 {
7501 ((type_T **)stack->ga_data)[stack->ga_len] =
7502 (semicolon && idx == 0) ? vartype : item_type;
7503 ++stack->ga_len;
7504 }
7505 }
7506
7507 for (idx = 0; idx < var_count; ++idx)
7508 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007509 assign_dest_T dest = dest_local;
7510 int opt_flags = 0;
7511 int vimvaridx = -1;
7512 type_T *type = &t_any;
7513
7514 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007515 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007516 name = vim_strnsave(arg, varlen);
7517 if (name == NULL)
7518 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007519
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007520 // TODO: script var not supported?
7521 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7522 &vimvaridx, &type, cctx) == FAIL)
7523 goto failed;
7524 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007525 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007526 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7527 0, 0, type, name) == FAIL)
7528 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007529 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007530 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007531 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007532 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007533 {
7534 semsg(_(e_variable_already_declared), arg);
7535 goto failed;
7536 }
7537
Bram Moolenaarea870692020-12-02 14:24:30 +01007538 if (STRNCMP(name, "s:", 2) == 0)
7539 {
7540 semsg(_(e_cannot_declare_script_variable_in_function), name);
7541 goto failed;
7542 }
7543
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007544 // Reserve a variable to store "var".
7545 // TODO: check for type
7546 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7547 if (var_lvar == NULL)
7548 // out of memory or used as an argument
7549 goto failed;
7550
7551 if (semicolon && idx == var_count - 1)
7552 var_lvar->lv_type = vartype;
7553 else
7554 var_lvar->lv_type = item_type;
7555 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7556 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007557
Bram Moolenaar036d0712021-01-17 20:23:38 +01007558 if (*p == ':')
7559 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007560 if (*p == ',' || *p == ';')
7561 ++p;
7562 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007563 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007564 }
7565
7566 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007567
7568failed:
7569 vim_free(name);
7570 drop_scope(cctx);
7571 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007572}
7573
7574/*
7575 * compile "endfor"
7576 */
7577 static char_u *
7578compile_endfor(char_u *arg, cctx_T *cctx)
7579{
7580 garray_T *instr = &cctx->ctx_instr;
7581 scope_T *scope = cctx->ctx_scope;
7582 forscope_T *forscope;
7583 isn_T *isn;
7584
Bram Moolenaarfa984412021-03-25 22:15:28 +01007585 if (misplaced_cmdmod(cctx))
7586 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588 if (scope == NULL || scope->se_type != FOR_SCOPE)
7589 {
7590 emsg(_(e_for));
7591 return NULL;
7592 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007593 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007595 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596
7597 // At end of ":for" scope jump back to the FOR instruction.
7598 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7599
7600 // Fill in the "end" label in the FOR statement so it can jump here
7601 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7602 isn->isn_arg.forloop.for_end = instr->ga_len;
7603
7604 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007605 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007606
7607 // Below the ":for" scope drop the "expr" list from the stack.
7608 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7609 return NULL;
7610
7611 vim_free(scope);
7612
7613 return arg;
7614}
7615
7616/*
7617 * compile "while expr"
7618 *
7619 * Produces instructions:
7620 * top: EVAL expr Push result of "expr"
7621 * JUMP_IF_FALSE end jump if false
7622 * ... body ...
7623 * JUMP top Jump back to repeat
7624 * end:
7625 *
7626 */
7627 static char_u *
7628compile_while(char_u *arg, cctx_T *cctx)
7629{
7630 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007631 scope_T *scope;
7632
7633 scope = new_scope(cctx, WHILE_SCOPE);
7634 if (scope == NULL)
7635 return NULL;
7636
Bram Moolenaara91a7132021-03-25 21:12:15 +01007637 // "endwhile" jumps back here, one before when profiling or using cmdmods
7638 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639
7640 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007641 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007643 if (!ends_excmd2(arg, skipwhite(p)))
7644 {
7645 semsg(_(e_trailing_arg), p);
7646 return NULL;
7647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648
Bram Moolenaar13106602020-10-04 16:06:05 +02007649 if (bool_on_stack(cctx) == FAIL)
7650 return FAIL;
7651
Bram Moolenaara91a7132021-03-25 21:12:15 +01007652 // CMDMOD_REV must come before the jump
7653 generate_undo_cmdmods(cctx);
7654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007656 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657 JUMP_IF_FALSE, cctx) == FAIL)
7658 return FAIL;
7659
7660 return p;
7661}
7662
7663/*
7664 * compile "endwhile"
7665 */
7666 static char_u *
7667compile_endwhile(char_u *arg, cctx_T *cctx)
7668{
7669 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007670 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671
Bram Moolenaarfa984412021-03-25 22:15:28 +01007672 if (misplaced_cmdmod(cctx))
7673 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7675 {
7676 emsg(_(e_while));
7677 return NULL;
7678 }
7679 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007680 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007681
Bram Moolenaarf002a412021-01-24 13:34:18 +01007682#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007683 // count the endwhile before jumping
7684 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007685#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007688 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689
7690 // Fill in the "end" label in the WHILE statement so it can jump here.
7691 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007692 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7693 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
7695 vim_free(scope);
7696
7697 return arg;
7698}
7699
7700/*
7701 * compile "continue"
7702 */
7703 static char_u *
7704compile_continue(char_u *arg, cctx_T *cctx)
7705{
7706 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007707 int try_scopes = 0;
7708 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709
7710 for (;;)
7711 {
7712 if (scope == NULL)
7713 {
7714 emsg(_(e_continue));
7715 return NULL;
7716 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007717 if (scope->se_type == FOR_SCOPE)
7718 {
7719 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007721 }
7722 if (scope->se_type == WHILE_SCOPE)
7723 {
7724 loop_label = scope->se_u.se_while.ws_top_label;
7725 break;
7726 }
7727 if (scope->se_type == TRY_SCOPE)
7728 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 scope = scope->se_outer;
7730 }
7731
Bram Moolenaarc150c092021-02-13 15:02:46 +01007732 if (try_scopes > 0)
7733 // Inside one or more try/catch blocks we first need to jump to the
7734 // "finally" or "endtry" to cleanup.
7735 generate_TRYCONT(cctx, try_scopes, loop_label);
7736 else
7737 // Jump back to the FOR or WHILE instruction.
7738 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 return arg;
7741}
7742
7743/*
7744 * compile "break"
7745 */
7746 static char_u *
7747compile_break(char_u *arg, cctx_T *cctx)
7748{
7749 scope_T *scope = cctx->ctx_scope;
7750 endlabel_T **el;
7751
7752 for (;;)
7753 {
7754 if (scope == NULL)
7755 {
7756 emsg(_(e_break));
7757 return NULL;
7758 }
7759 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7760 break;
7761 scope = scope->se_outer;
7762 }
7763
7764 // Jump to the end of the FOR or WHILE loop.
7765 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007766 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007767 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007768 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007769 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7770 return FAIL;
7771
7772 return arg;
7773}
7774
7775/*
7776 * compile "{" start of block
7777 */
7778 static char_u *
7779compile_block(char_u *arg, cctx_T *cctx)
7780{
7781 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7782 return NULL;
7783 return skipwhite(arg + 1);
7784}
7785
7786/*
7787 * compile end of block: drop one scope
7788 */
7789 static void
7790compile_endblock(cctx_T *cctx)
7791{
7792 scope_T *scope = cctx->ctx_scope;
7793
7794 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007795 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 vim_free(scope);
7797}
7798
7799/*
7800 * compile "try"
7801 * Creates a new scope for the try-endtry, pointing to the first catch and
7802 * finally.
7803 * Creates another scope for the "try" block itself.
7804 * TRY instruction sets up exception handling at runtime.
7805 *
7806 * "try"
7807 * TRY -> catch1, -> finally push trystack entry
7808 * ... try block
7809 * "throw {exception}"
7810 * EVAL {exception}
7811 * THROW create exception
7812 * ... try block
7813 * " catch {expr}"
7814 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007815 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007816 * EVAL {expr}
7817 * MATCH
7818 * JUMP nomatch -> catch2
7819 * CATCH remove exception
7820 * ... catch block
7821 * " catch"
7822 * JUMP -> finally
7823 * catch2: CATCH remove exception
7824 * ... catch block
7825 * " finally"
7826 * finally:
7827 * ... finally block
7828 * " endtry"
7829 * ENDTRY pop trystack entry, may rethrow
7830 */
7831 static char_u *
7832compile_try(char_u *arg, cctx_T *cctx)
7833{
7834 garray_T *instr = &cctx->ctx_instr;
7835 scope_T *try_scope;
7836 scope_T *scope;
7837
Bram Moolenaarfa984412021-03-25 22:15:28 +01007838 if (misplaced_cmdmod(cctx))
7839 return NULL;
7840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007841 // scope that holds the jumps that go to catch/finally/endtry
7842 try_scope = new_scope(cctx, TRY_SCOPE);
7843 if (try_scope == NULL)
7844 return NULL;
7845
Bram Moolenaar69f70502021-01-01 16:10:46 +01007846 if (cctx->ctx_skip != SKIP_YES)
7847 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007848 isn_T *isn;
7849
7850 // "try_catch" is set when the first ":catch" is found or when no catch
7851 // is found and ":finally" is found.
7852 // "try_finally" is set when ":finally" is found
7853 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007854 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007855 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7856 return NULL;
7857 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7858 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007859 return NULL;
7860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007861
7862 // scope for the try block itself
7863 scope = new_scope(cctx, BLOCK_SCOPE);
7864 if (scope == NULL)
7865 return NULL;
7866
7867 return arg;
7868}
7869
7870/*
7871 * compile "catch {expr}"
7872 */
7873 static char_u *
7874compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7875{
7876 scope_T *scope = cctx->ctx_scope;
7877 garray_T *instr = &cctx->ctx_instr;
7878 char_u *p;
7879 isn_T *isn;
7880
Bram Moolenaarfa984412021-03-25 22:15:28 +01007881 if (misplaced_cmdmod(cctx))
7882 return NULL;
7883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 // end block scope from :try or :catch
7885 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7886 compile_endblock(cctx);
7887 scope = cctx->ctx_scope;
7888
7889 // Error if not in a :try scope
7890 if (scope == NULL || scope->se_type != TRY_SCOPE)
7891 {
7892 emsg(_(e_catch));
7893 return NULL;
7894 }
7895
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007896 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007897 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007898 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 return NULL;
7900 }
7901
Bram Moolenaar69f70502021-01-01 16:10:46 +01007902 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007904#ifdef FEAT_PROFILE
7905 // the profile-start should be after the jump
7906 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7907 .isn_type == ISN_PROF_START)
7908 --instr->ga_len;
7909#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007910 // Jump from end of previous block to :finally or :endtry
7911 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7912 JUMP_ALWAYS, cctx) == FAIL)
7913 return NULL;
7914
7915 // End :try or :catch scope: set value in ISN_TRY instruction
7916 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007917 if (isn->isn_arg.try.try_ref->try_catch == 0)
7918 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007919 if (scope->se_u.se_try.ts_catch_label != 0)
7920 {
7921 // Previous catch without match jumps here
7922 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7923 isn->isn_arg.jump.jump_where = instr->ga_len;
7924 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007925#ifdef FEAT_PROFILE
7926 if (cctx->ctx_profiling)
7927 {
7928 // a "throw" that jumps here needs to be counted
7929 generate_instr(cctx, ISN_PROF_END);
7930 // the "catch" is also counted
7931 generate_instr(cctx, ISN_PROF_START);
7932 }
7933#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934 }
7935
7936 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007937 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007938 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007939 scope->se_u.se_try.ts_caught_all = TRUE;
7940 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007941 }
7942 else
7943 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007944 char_u *end;
7945 char_u *pat;
7946 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007947 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007948 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007950 // Push v:exception, push {expr} and MATCH
7951 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7952
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007953 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007954 if (*end != *p)
7955 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007956 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007957 vim_free(tofree);
7958 return FAIL;
7959 }
7960 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007961 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007962 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007963 len = (int)(end - tofree);
7964 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007965 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007966 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007967 if (pat == NULL)
7968 return FAIL;
7969 if (generate_PUSHS(cctx, pat) == FAIL)
7970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7973 return NULL;
7974
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007975 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7977 return NULL;
7978 }
7979
Bram Moolenaar69f70502021-01-01 16:10:46 +01007980 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 return NULL;
7982
7983 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7984 return NULL;
7985 return p;
7986}
7987
7988 static char_u *
7989compile_finally(char_u *arg, cctx_T *cctx)
7990{
7991 scope_T *scope = cctx->ctx_scope;
7992 garray_T *instr = &cctx->ctx_instr;
7993 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007994 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995
Bram Moolenaarfa984412021-03-25 22:15:28 +01007996 if (misplaced_cmdmod(cctx))
7997 return NULL;
7998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999 // end block scope from :try or :catch
8000 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8001 compile_endblock(cctx);
8002 scope = cctx->ctx_scope;
8003
8004 // Error if not in a :try scope
8005 if (scope == NULL || scope->se_type != TRY_SCOPE)
8006 {
8007 emsg(_(e_finally));
8008 return NULL;
8009 }
8010
8011 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008012 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008013 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 {
8015 emsg(_(e_finally_dup));
8016 return NULL;
8017 }
8018
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008019 this_instr = instr->ga_len;
8020#ifdef FEAT_PROFILE
8021 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8022 .isn_type == ISN_PROF_START)
8023 // jump to the profile start of the "finally"
8024 --this_instr;
8025#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008027 // Fill in the "end" label in jumps at the end of the blocks.
8028 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8029 this_instr, cctx);
8030
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008031 // If there is no :catch then an exception jumps to :finally.
8032 if (isn->isn_arg.try.try_ref->try_catch == 0)
8033 isn->isn_arg.try.try_ref->try_catch = this_instr;
8034 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008035 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036 {
8037 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008038 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008039 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008040 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008042 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8043 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008045 // TODO: set index in ts_finally_label jumps
8046
8047 return arg;
8048}
8049
8050 static char_u *
8051compile_endtry(char_u *arg, cctx_T *cctx)
8052{
8053 scope_T *scope = cctx->ctx_scope;
8054 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008055 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056
Bram Moolenaarfa984412021-03-25 22:15:28 +01008057 if (misplaced_cmdmod(cctx))
8058 return NULL;
8059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008060 // end block scope from :catch or :finally
8061 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8062 compile_endblock(cctx);
8063 scope = cctx->ctx_scope;
8064
8065 // Error if not in a :try scope
8066 if (scope == NULL || scope->se_type != TRY_SCOPE)
8067 {
8068 if (scope == NULL)
8069 emsg(_(e_no_endtry));
8070 else if (scope->se_type == WHILE_SCOPE)
8071 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008072 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073 emsg(_(e_endfor));
8074 else
8075 emsg(_(e_endif));
8076 return NULL;
8077 }
8078
Bram Moolenaarc150c092021-02-13 15:02:46 +01008079 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008080 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008082 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8083 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008084 {
8085 emsg(_(e_missing_catch_or_finally));
8086 return NULL;
8087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008088
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008089#ifdef FEAT_PROFILE
8090 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8091 .isn_type == ISN_PROF_START)
8092 // move the profile start after "endtry" so that it's not counted when
8093 // the exception is rethrown.
8094 --instr->ga_len;
8095#endif
8096
Bram Moolenaar69f70502021-01-01 16:10:46 +01008097 // Fill in the "end" label in jumps at the end of the blocks, if not
8098 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008099 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8100 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008101
Bram Moolenaar69f70502021-01-01 16:10:46 +01008102 if (scope->se_u.se_try.ts_catch_label != 0)
8103 {
8104 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008105 isn_T *isn = ((isn_T *)instr->ga_data)
8106 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008107 isn->isn_arg.jump.jump_where = instr->ga_len;
8108 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008109 }
8110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 compile_endblock(cctx);
8112
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008113 if (cctx->ctx_skip != SKIP_YES)
8114 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008115 // End :catch or :finally scope: set instruction index in ISN_TRY
8116 // instruction
8117 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008118 if (cctx->ctx_skip != SKIP_YES
8119 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8120 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008121#ifdef FEAT_PROFILE
8122 if (cctx->ctx_profiling)
8123 generate_instr(cctx, ISN_PROF_START);
8124#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008126 return arg;
8127}
8128
8129/*
8130 * compile "throw {expr}"
8131 */
8132 static char_u *
8133compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8134{
8135 char_u *p = skipwhite(arg);
8136
Bram Moolenaara5565e42020-05-09 15:44:01 +02008137 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008138 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008139 if (cctx->ctx_skip == SKIP_YES)
8140 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 if (may_generate_2STRING(-1, cctx) == FAIL)
8142 return NULL;
8143 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8144 return NULL;
8145
8146 return p;
8147}
8148
8149/*
8150 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008151 * compile "echomsg expr"
8152 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008153 * compile "execute expr"
8154 */
8155 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008156compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008157{
8158 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008159 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008160 int count = 0;
8161
8162 for (;;)
8163 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008164 if (ends_excmd2(prev, p))
8165 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008166 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008167 return NULL;
8168 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008169 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008170 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008171 }
8172
Bram Moolenaare4984292020-12-13 14:19:25 +01008173 if (count > 0)
8174 {
8175 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8176 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8177 else if (cmdidx == CMD_execute)
8178 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8179 else if (cmdidx == CMD_echomsg)
8180 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8181 else
8182 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 return p;
8185}
8186
8187/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008188 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008189 * instruction to compute it and return OK.
8190 * Otherwise return FAIL, the caller must deal with any range.
8191 */
8192 static int
8193compile_variable_range(exarg_T *eap, cctx_T *cctx)
8194{
8195 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8196 char_u *p = skipdigits(eap->cmd);
8197
8198 if (p == range_end)
8199 return FAIL;
8200 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8201}
8202
8203/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008204 * :put r
8205 * :put ={expr}
8206 */
8207 static char_u *
8208compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8209{
8210 char_u *line = arg;
8211 linenr_T lnum;
8212 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008213 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008214
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008215 eap->regname = *line;
8216
8217 if (eap->regname == '=')
8218 {
8219 char_u *p = line + 1;
8220
8221 if (compile_expr0(&p, cctx) == FAIL)
8222 return NULL;
8223 line = p;
8224 }
8225 else if (eap->regname != NUL)
8226 ++line;
8227
Bram Moolenaar08597872020-12-10 19:43:40 +01008228 if (compile_variable_range(eap, cctx) == OK)
8229 {
8230 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8231 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008232 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008233 {
8234 // Either no range or a number.
8235 // "errormsg" will not be set because the range is ADDR_LINES.
8236 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008237 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008238 return NULL;
8239 if (eap->addr_count == 0)
8240 lnum = -1;
8241 else
8242 lnum = eap->line2;
8243 if (above)
8244 --lnum;
8245 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008246
8247 generate_PUT(cctx, eap->regname, lnum);
8248 return line;
8249}
8250
8251/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008252 * A command that is not compiled, execute with legacy code.
8253 */
8254 static char_u *
8255compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8256{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008257 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008258 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008259 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008260
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008261 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008262 goto theend;
8263
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008264 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008265 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008266 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008267 int usefilter = FALSE;
8268
8269 has_expr = argt & (EX_XFILE | EX_EXPAND);
8270
8271 // If the command can be followed by a bar, find the bar and truncate
8272 // it, so that the following command can be compiled.
8273 // The '|' is overwritten with a NUL, it is put back below.
8274 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8275 && *eap->arg == '!')
8276 // :w !filter or :r !filter or :r! filter
8277 usefilter = TRUE;
8278 if ((argt & EX_TRLBAR) && !usefilter)
8279 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008280 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008281 separate_nextcmd(eap);
8282 if (eap->nextcmd != NULL)
8283 nextcmd = eap->nextcmd;
8284 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008285 else if (eap->cmdidx == CMD_wincmd)
8286 {
8287 p = eap->arg;
8288 if (*p != NUL)
8289 ++p;
8290 if (*p == 'g' || *p == Ctrl_G)
8291 ++p;
8292 p = skipwhite(p);
8293 if (*p == '|')
8294 {
8295 *p = NUL;
8296 nextcmd = p + 1;
8297 }
8298 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008299 }
8300
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008301 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8302 {
8303 // expand filename in "syntax include [@group] filename"
8304 has_expr = TRUE;
8305 eap->arg = skipwhite(eap->arg + 7);
8306 if (*eap->arg == '@')
8307 eap->arg = skiptowhite(eap->arg);
8308 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008309
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008310 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8311 && STRLEN(eap->arg) > 4)
8312 {
8313 int delim = *eap->arg;
8314
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008315 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008316 if (*p == delim)
8317 {
8318 eap->arg = p + 1;
8319 has_expr = TRUE;
8320 }
8321 }
8322
Bram Moolenaarecac5912021-01-05 19:23:28 +01008323 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8324 {
8325 // TODO: should only expand when appropriate for the command
8326 eap->arg = skiptowhite(eap->arg);
8327 has_expr = TRUE;
8328 }
8329
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008330 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008331 {
8332 int count = 0;
8333 char_u *start = skipwhite(line);
8334
8335 // :cmd xxx`=expr1`yyy`=expr2`zzz
8336 // PUSHS ":cmd xxx"
8337 // eval expr1
8338 // PUSHS "yyy"
8339 // eval expr2
8340 // PUSHS "zzz"
8341 // EXECCONCAT 5
8342 for (;;)
8343 {
8344 if (p > start)
8345 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008346 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008347 ++count;
8348 }
8349 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008350 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008351 return NULL;
8352 may_generate_2STRING(-1, cctx);
8353 ++count;
8354 p = skipwhite(p);
8355 if (*p != '`')
8356 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008357 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008358 return NULL;
8359 }
8360 start = p + 1;
8361
8362 p = (char_u *)strstr((char *)start, "`=");
8363 if (p == NULL)
8364 {
8365 if (*skipwhite(start) != NUL)
8366 {
8367 generate_PUSHS(cctx, vim_strsave(start));
8368 ++count;
8369 }
8370 break;
8371 }
8372 }
8373 generate_EXECCONCAT(cctx, count);
8374 }
8375 else
8376 generate_EXEC(cctx, line);
8377
8378theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008379 if (*nextcmd != NUL)
8380 {
8381 // the parser expects a pointer to the bar, put it back
8382 --nextcmd;
8383 *nextcmd = '|';
8384 }
8385
8386 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008387}
8388
8389/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008390 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008391 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008392 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008393 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008394add_def_function(ufunc_T *ufunc)
8395{
8396 dfunc_T *dfunc;
8397
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008398 if (def_functions.ga_len == 0)
8399 {
8400 // The first position is not used, so that a zero uf_dfunc_idx means it
8401 // wasn't set.
8402 if (ga_grow(&def_functions, 1) == FAIL)
8403 return FAIL;
8404 ++def_functions.ga_len;
8405 }
8406
Bram Moolenaar09689a02020-05-09 22:50:08 +02008407 // Add the function to "def_functions".
8408 if (ga_grow(&def_functions, 1) == FAIL)
8409 return FAIL;
8410 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8411 CLEAR_POINTER(dfunc);
8412 dfunc->df_idx = def_functions.ga_len;
8413 ufunc->uf_dfunc_idx = dfunc->df_idx;
8414 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008415 dfunc->df_name = vim_strsave(ufunc->uf_name);
8416 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008417 ++def_functions.ga_len;
8418 return OK;
8419}
8420
8421/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008422 * After ex_function() has collected all the function lines: parse and compile
8423 * the lines into instructions.
8424 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008425 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8426 * the return statement (used for lambda). When uf_ret_type is already set
8427 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008428 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008429 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008430 * This can be used recursively through compile_lambda(), which may reallocate
8431 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008432 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008434 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008435compile_def_function(
8436 ufunc_T *ufunc,
8437 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008438 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008439 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441 char_u *line = NULL;
8442 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444 cctx_T cctx;
8445 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008446 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008447 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008448 int ret = FAIL;
8449 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008450 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008451 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008452 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008453#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008454 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008455#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008457 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008458 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008459 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008461 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8462 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008463 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008464 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008465 else
8466 {
8467 if (add_def_function(ufunc) == FAIL)
8468 return FAIL;
8469 new_def_function = TRUE;
8470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471
Bram Moolenaar985116a2020-07-12 17:31:09 +02008472 ufunc->uf_def_status = UF_COMPILING;
8473
Bram Moolenaara80faa82020-04-12 19:37:17 +02008474 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008475
Bram Moolenaarf002a412021-01-24 13:34:18 +01008476#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008477 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008478#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008479 cctx.ctx_ufunc = ufunc;
8480 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008481 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8483 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8484 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8485 cctx.ctx_type_list = &ufunc->uf_type_list;
8486 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8487 instr = &cctx.ctx_instr;
8488
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008489 // Set the context to the function, it may be compiled when called from
8490 // another script. Set the script version to the most modern one.
8491 // The line number will be set in next_line_from_context().
8492 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008493 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8494
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008495 // Make sure error messages are OK.
8496 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8497 if (do_estack_push)
8498 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008499 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008500
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008501 if (ufunc->uf_def_args.ga_len > 0)
8502 {
8503 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008504 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008505 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008506 int i;
8507 char_u *arg;
8508 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008509 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008510
8511 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008512 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008513 for (i = 0; i < count; ++i)
8514 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008515 garray_T *stack = &cctx.ctx_type_stack;
8516 type_T *val_type;
8517 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008518 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008519 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008520 int jump_instr_idx = instr->ga_len;
8521 isn_T *isn;
8522
8523 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8524 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8525 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008526
8527 // Make sure later arguments are not found.
8528 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008529
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008530 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008531 r = compile_expr0(&arg, &cctx);
8532
8533 ufunc->uf_args.ga_len = uf_args_len;
8534 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008535 goto erret;
8536
8537 // If no type specified use the type of the default value.
8538 // Otherwise check that the default value type matches the
8539 // specified type.
8540 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008541 where.wt_index = arg_idx + 1;
8542 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008543 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008544 {
8545 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008546 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008547 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008548 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008549 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008550 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008551
8552 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008553 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008554
8555 // set instruction index in JUMP_IF_ARG_SET to here
8556 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8557 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008558 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008559
8560 if (did_set_arg_type)
8561 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008562 }
8563
8564 /*
8565 * Loop over all the lines of the function and generate instructions.
8566 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 for (;;)
8568 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008569 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008570 int starts_with_colon = FALSE;
8571 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008572 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008573
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008574 // Bail out on the first error to avoid a flood of errors and report
8575 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008576 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008577 goto erret;
8578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008579 if (line != NULL && *line == '|')
8580 // the line continues after a '|'
8581 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008582 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008583 && !(*line == '#' && (line == cctx.ctx_line_start
8584 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008585 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008586 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 goto erret;
8588 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008589 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8590 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 else
8592 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008593 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008594 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008595 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008596 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008597#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008598 if (cctx.ctx_skip != SKIP_YES)
8599 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008600#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008601 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008603 }
8604
Bram Moolenaara80faa82020-04-12 19:37:17 +02008605 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 ea.cmdlinep = &line;
8607 ea.cmd = skipwhite(line);
8608
Bram Moolenaarb2049902021-01-24 12:53:53 +01008609 if (*ea.cmd == '#')
8610 {
8611 // "#" starts a comment
8612 line = (char_u *)"";
8613 continue;
8614 }
8615
Bram Moolenaarf002a412021-01-24 13:34:18 +01008616#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008617 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8618 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008619 {
8620 may_generate_prof_end(&cctx, prof_lnum);
8621
8622 prof_lnum = cctx.ctx_lnum;
8623 generate_instr(&cctx, ISN_PROF_START);
8624 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008625#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008626
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008627 // Some things can be recognized by the first character.
8628 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008629 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008630 case '}':
8631 {
8632 // "}" ends a block scope
8633 scopetype_T stype = cctx.ctx_scope == NULL
8634 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008636 if (stype == BLOCK_SCOPE)
8637 {
8638 compile_endblock(&cctx);
8639 line = ea.cmd;
8640 }
8641 else
8642 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008643 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008644 goto erret;
8645 }
8646 if (line != NULL)
8647 line = skipwhite(ea.cmd + 1);
8648 continue;
8649 }
8650
8651 case '{':
8652 // "{" starts a block scope
8653 // "{'a': 1}->func() is something else
8654 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8655 {
8656 line = compile_block(ea.cmd, &cctx);
8657 continue;
8658 }
8659 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008660 }
8661
8662 /*
8663 * COMMAND MODIFIERS
8664 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008665 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008666 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8667 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 {
8669 if (errormsg != NULL)
8670 goto erret;
8671 // empty line or comment
8672 line = (char_u *)"";
8673 continue;
8674 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008675 generate_cmdmods(&cctx, &local_cmdmod);
8676 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008677
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008678 // Check if there was a colon after the last command modifier or before
8679 // the current position.
8680 for (p = ea.cmd; p >= line; --p)
8681 {
8682 if (*p == ':')
8683 starts_with_colon = TRUE;
8684 if (p < ea.cmd && !VIM_ISWHITE(*p))
8685 break;
8686 }
8687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008689 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008691 {
8692 if (*ea.cmd == '(')
8693 // not for "call()"
8694 ea.cmd = p;
8695 else
8696 ea.cmd = skipwhite(ea.cmd);
8697 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008698
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008699 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008700 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008701 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008702
Bram Moolenaar17126b12021-01-07 22:03:02 +01008703 // Check for assignment after command modifiers.
8704 assign = may_compile_assignment(&ea, &line, &cctx);
8705 if (assign == OK)
8706 goto nextline;
8707 if (assign == FAIL)
8708 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008709 }
8710
8711 /*
8712 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008713 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008714 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008715 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008716 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008717 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008718 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008719 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008720 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008721 if (!starts_with_colon)
8722 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008723 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008724 goto erret;
8725 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008726 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008727 if (ends_excmd2(line, ea.cmd))
8728 {
8729 // A range without a command: jump to the line.
8730 // TODO: compile to a more efficient command, possibly
8731 // calling parse_cmd_address().
8732 ea.cmdidx = CMD_SIZE;
8733 line = compile_exec(line, &ea, &cctx);
8734 goto nextline;
8735 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008736 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008737 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008738 p = find_ex_command(&ea, NULL, starts_with_colon
8739 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008740
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008741 if (p == NULL)
8742 {
8743 if (cctx.ctx_skip != SKIP_YES)
8744 emsg(_(e_ambiguous_use_of_user_defined_command));
8745 goto erret;
8746 }
8747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008748 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8749 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008750 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008751 {
8752 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008753 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008754 }
8755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008756 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008757 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008758 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008759 // CMD_var cannot happen, compile_assignment() above would be
8760 // used. Most likely an assignment to a non-existing variable.
8761 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008762 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008764 }
8765
Bram Moolenaar3988f642020-08-27 22:43:03 +02008766 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008767 && ea.cmdidx != CMD_elseif
8768 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008769 && ea.cmdidx != CMD_endif
8770 && ea.cmdidx != CMD_endfor
8771 && ea.cmdidx != CMD_endwhile
8772 && ea.cmdidx != CMD_catch
8773 && ea.cmdidx != CMD_finally
8774 && ea.cmdidx != CMD_endtry)
8775 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008776 emsg(_(e_unreachable_code_after_return));
8777 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008778 }
8779
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008780 p = skipwhite(p);
8781 if (ea.cmdidx != CMD_SIZE
8782 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8783 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008784 if (ea.cmdidx >= 0)
8785 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008786 if ((ea.argt & EX_BANG) && *p == '!')
8787 {
8788 ea.forceit = TRUE;
8789 p = skipwhite(p + 1);
8790 }
8791 }
8792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793 switch (ea.cmdidx)
8794 {
8795 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008796 ea.arg = p;
8797 line = compile_nested_function(&ea, &cctx);
8798 break;
8799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008800 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008801 // TODO: should we allow this, e.g. to declare a global
8802 // function?
8803 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804 goto erret;
8805
8806 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008807 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008808 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 break;
8810
8811 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008812 emsg(_(e_cannot_use_let_in_vim9_script));
8813 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008814 case CMD_var:
8815 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008816 case CMD_const:
8817 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008818 if (line == p)
8819 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 break;
8821
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008822 case CMD_unlet:
8823 case CMD_unlockvar:
8824 case CMD_lockvar:
8825 line = compile_unletlock(p, &ea, &cctx);
8826 break;
8827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828 case CMD_import:
8829 line = compile_import(p, &cctx);
8830 break;
8831
8832 case CMD_if:
8833 line = compile_if(p, &cctx);
8834 break;
8835 case CMD_elseif:
8836 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008837 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 break;
8839 case CMD_else:
8840 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008841 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008842 break;
8843 case CMD_endif:
8844 line = compile_endif(p, &cctx);
8845 break;
8846
8847 case CMD_while:
8848 line = compile_while(p, &cctx);
8849 break;
8850 case CMD_endwhile:
8851 line = compile_endwhile(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
8855 case CMD_for:
8856 line = compile_for(p, &cctx);
8857 break;
8858 case CMD_endfor:
8859 line = compile_endfor(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_continue:
8863 line = compile_continue(p, &cctx);
8864 break;
8865 case CMD_break:
8866 line = compile_break(p, &cctx);
8867 break;
8868
8869 case CMD_try:
8870 line = compile_try(p, &cctx);
8871 break;
8872 case CMD_catch:
8873 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008874 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875 break;
8876 case CMD_finally:
8877 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008878 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 break;
8880 case CMD_endtry:
8881 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008882 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008883 break;
8884 case CMD_throw:
8885 line = compile_throw(p, &cctx);
8886 break;
8887
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008888 case CMD_eval:
8889 if (compile_expr0(&p, &cctx) == FAIL)
8890 goto erret;
8891
Bram Moolenaar3988f642020-08-27 22:43:03 +02008892 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008893 generate_instr_drop(&cctx, ISN_DROP, 1);
8894
8895 line = skipwhite(p);
8896 break;
8897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008898 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008900 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008901 case CMD_echomsg:
8902 case CMD_echoerr:
8903 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008904 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008906 case CMD_put:
8907 ea.cmd = cmd;
8908 line = compile_put(p, &ea, &cctx);
8909 break;
8910
Bram Moolenaar3988f642020-08-27 22:43:03 +02008911 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008912
Bram Moolenaarae616492020-07-28 20:07:27 +02008913 case CMD_append:
8914 case CMD_change:
8915 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008916 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008917 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008918 case CMD_xit:
8919 not_in_vim9(&ea);
8920 goto erret;
8921
Bram Moolenaar002262f2020-07-08 17:47:57 +02008922 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008923 if (cctx.ctx_skip != SKIP_YES)
8924 {
8925 semsg(_(e_invalid_command_str), ea.cmd);
8926 goto erret;
8927 }
8928 // We don't check for a next command here.
8929 line = (char_u *)"";
8930 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008932 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008933 if (cctx.ctx_skip == SKIP_YES)
8934 {
8935 // We don't check for a next command here.
8936 line = (char_u *)"";
8937 }
8938 else
8939 {
8940 // Not recognized, execute with do_cmdline_cmd().
8941 ea.arg = p;
8942 line = compile_exec(line, &ea, &cctx);
8943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944 break;
8945 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008946nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008947 if (line == NULL)
8948 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008949 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008950
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008951 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008952 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008954 if (cctx.ctx_type_stack.ga_len < 0)
8955 {
8956 iemsg("Type stack underflow");
8957 goto erret;
8958 }
8959 }
8960
8961 if (cctx.ctx_scope != NULL)
8962 {
8963 if (cctx.ctx_scope->se_type == IF_SCOPE)
8964 emsg(_(e_endif));
8965 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8966 emsg(_(e_endwhile));
8967 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8968 emsg(_(e_endfor));
8969 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008970 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008971 goto erret;
8972 }
8973
Bram Moolenaarefd88552020-06-18 20:50:10 +02008974 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 {
8976 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008978 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008979 goto erret;
8980 }
8981
8982 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008983 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008984 }
8985
Bram Moolenaar599410c2021-04-10 14:03:43 +02008986 // When compiled with ":silent!" and there was an error don't consider the
8987 // function compiled.
8988 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008989 {
8990 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8991 + ufunc->uf_dfunc_idx;
8992 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008993 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008994#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008995 if (cctx.ctx_profiling)
8996 {
8997 dfunc->df_instr_prof = instr->ga_data;
8998 dfunc->df_instr_prof_count = instr->ga_len;
8999 }
9000 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009001#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009002 {
9003 dfunc->df_instr = instr->ga_data;
9004 dfunc->df_instr_count = instr->ga_len;
9005 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009006 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009007 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009008 if (cctx.ctx_outer_used)
9009 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009010 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009012
9013 ret = OK;
9014
9015erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009016 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009017 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009018 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009019 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9020 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009021
9022 for (idx = 0; idx < instr->ga_len; ++idx)
9023 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009024 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009025 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009026
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009027 // If using the last entry in the table and it was added above, we
9028 // might as well remove it.
9029 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009030 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009031 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009032 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009033 ufunc->uf_dfunc_idx = 0;
9034 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009035 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009036
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009037 while (cctx.ctx_scope != NULL)
9038 drop_scope(&cctx);
9039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009040 if (errormsg != NULL)
9041 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009042 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009043 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044 }
9045
9046 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009047 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009048 if (do_estack_push)
9049 estack_pop();
9050
Bram Moolenaar20431c92020-03-20 18:39:46 +01009051 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009052 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009053 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009054 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009055}
9056
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009057 void
9058set_function_type(ufunc_T *ufunc)
9059{
9060 int varargs = ufunc->uf_va_name != NULL;
9061 int argcount = ufunc->uf_args.ga_len;
9062
9063 // Create a type for the function, with the return type and any
9064 // argument types.
9065 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9066 // The type is included in "tt_args".
9067 if (argcount > 0 || varargs)
9068 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009069 if (ufunc->uf_type_list.ga_itemsize == 0)
9070 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009071 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9072 argcount, &ufunc->uf_type_list);
9073 // Add argument types to the function type.
9074 if (func_type_add_arg_types(ufunc->uf_func_type,
9075 argcount + varargs,
9076 &ufunc->uf_type_list) == FAIL)
9077 return;
9078 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9079 ufunc->uf_func_type->tt_min_argcount =
9080 argcount - ufunc->uf_def_args.ga_len;
9081 if (ufunc->uf_arg_types == NULL)
9082 {
9083 int i;
9084
9085 // lambda does not have argument types.
9086 for (i = 0; i < argcount; ++i)
9087 ufunc->uf_func_type->tt_args[i] = &t_any;
9088 }
9089 else
9090 mch_memmove(ufunc->uf_func_type->tt_args,
9091 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9092 if (varargs)
9093 {
9094 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009095 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009096 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9097 }
9098 }
9099 else
9100 // No arguments, can use a predefined type.
9101 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9102 argcount, &ufunc->uf_type_list);
9103}
9104
9105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009106/*
9107 * Delete an instruction, free what it contains.
9108 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009109 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009110delete_instr(isn_T *isn)
9111{
9112 switch (isn->isn_type)
9113 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009114 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009115 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009116 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009117 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009118 case ISN_LOADENV:
9119 case ISN_LOADG:
9120 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009121 case ISN_LOADT:
9122 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009123 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009124 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009125 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009126 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009127 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009128 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009129 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009130 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009131 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009132 case ISN_STOREW:
9133 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009134 vim_free(isn->isn_arg.string);
9135 break;
9136
9137 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009138 case ISN_STORES:
9139 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009140 break;
9141
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009142 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009143 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009144 vim_free(isn->isn_arg.unlet.ul_name);
9145 break;
9146
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009147 case ISN_STOREOPT:
9148 vim_free(isn->isn_arg.storeopt.so_name);
9149 break;
9150
9151 case ISN_PUSHBLOB: // push blob isn_arg.blob
9152 blob_unref(isn->isn_arg.blob);
9153 break;
9154
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009155 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009157 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009158#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009159 break;
9160
9161 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009162#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009163 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009164#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009165 break;
9166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009167 case ISN_UCALL:
9168 vim_free(isn->isn_arg.ufunc.cuf_name);
9169 break;
9170
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009171 case ISN_FUNCREF:
9172 {
9173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9174 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009175 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009176
Bram Moolenaar077a4232020-12-22 18:33:27 +01009177 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9178 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009179 }
9180 break;
9181
9182 case ISN_DCALL:
9183 {
9184 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9185 + isn->isn_arg.dfunc.cdf_idx;
9186
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009187 if (dfunc->df_ufunc != NULL
9188 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009189 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009190 }
9191 break;
9192
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009193 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009194 {
9195 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9196 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9197
9198 if (ufunc != NULL)
9199 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009200 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009201 func_ptr_unref(ufunc);
9202 }
9203
9204 vim_free(lambda);
9205 vim_free(isn->isn_arg.newfunc.nf_global);
9206 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009207 break;
9208
Bram Moolenaar5e654232020-09-16 15:22:00 +02009209 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009210 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009211 free_type(isn->isn_arg.type.ct_type);
9212 break;
9213
Bram Moolenaar02194d22020-10-24 23:08:38 +02009214 case ISN_CMDMOD:
9215 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9216 ->cmod_filter_regmatch.regprog);
9217 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9218 break;
9219
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009220 case ISN_LOADSCRIPT:
9221 case ISN_STORESCRIPT:
9222 vim_free(isn->isn_arg.script.scriptref);
9223 break;
9224
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009225 case ISN_TRY:
9226 vim_free(isn->isn_arg.try.try_ref);
9227 break;
9228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009229 case ISN_2BOOL:
9230 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009231 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009232 case ISN_ADDBLOB:
9233 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009234 case ISN_ANYINDEX:
9235 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009237 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009239 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009240 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009241 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 case ISN_COMPAREANY:
9243 case ISN_COMPAREBLOB:
9244 case ISN_COMPAREBOOL:
9245 case ISN_COMPAREDICT:
9246 case ISN_COMPAREFLOAT:
9247 case ISN_COMPAREFUNC:
9248 case ISN_COMPARELIST:
9249 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009250 case ISN_COMPARESPECIAL:
9251 case ISN_COMPARESTRING:
9252 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009253 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 case ISN_DROP:
9255 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009256 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009257 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009259 case ISN_EXECCONCAT:
9260 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009261 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009262 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009263 case ISN_GETITEM:
9264 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009265 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009266 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009267 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009268 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009270 case ISN_LOADBDICT:
9271 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009272 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009273 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009274 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009276 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009277 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009278 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case ISN_NEGATENR:
9280 case ISN_NEWDICT:
9281 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009282 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009283 case ISN_OPFLOAT:
9284 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009286 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009287 case ISN_PROF_END:
9288 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009289 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 case ISN_PUSHF:
9291 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009292 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009293 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009294 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009295 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009296 case ISN_SHUFFLE:
9297 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009298 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009299 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009300 case ISN_STORENR:
9301 case ISN_STOREOUTER:
9302 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009303 case ISN_STOREV:
9304 case ISN_STRINDEX:
9305 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009306 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009307 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009308 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009309 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009310 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009311 // nothing allocated
9312 break;
9313 }
9314}
9315
9316/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009317 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009318 */
9319 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009320delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009321{
9322 int idx;
9323
9324 ga_clear(&dfunc->df_def_args_isn);
9325
9326 if (dfunc->df_instr != NULL)
9327 {
9328 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9329 delete_instr(dfunc->df_instr + idx);
9330 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009331 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009332 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009333#ifdef FEAT_PROFILE
9334 if (dfunc->df_instr_prof != NULL)
9335 {
9336 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9337 delete_instr(dfunc->df_instr_prof + idx);
9338 VIM_CLEAR(dfunc->df_instr_prof);
9339 dfunc->df_instr_prof = NULL;
9340 }
9341#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009342
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009343 if (mark_deleted)
9344 dfunc->df_deleted = TRUE;
9345 if (dfunc->df_ufunc != NULL)
9346 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009347}
9348
9349/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009350 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009351 * function, unless another user function still uses it.
9352 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009353 */
9354 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009355unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009356{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009357 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009358 {
9359 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9360 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009361
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009362 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009363 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009364 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009365 ufunc->uf_dfunc_idx = 0;
9366 if (dfunc->df_ufunc == ufunc)
9367 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009368 }
9369}
9370
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009371/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009372 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009373 */
9374 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009375link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009376{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009377 if (ufunc->uf_dfunc_idx > 0)
9378 {
9379 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9380 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009381
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009382 ++dfunc->df_refcount;
9383 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009384}
9385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009387/*
9388 * Free all functions defined with ":def".
9389 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009390 void
9391free_def_functions(void)
9392{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009393 int idx;
9394
9395 for (idx = 0; idx < def_functions.ga_len; ++idx)
9396 {
9397 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9398
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009399 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009400 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009401 }
9402
9403 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009404}
9405#endif
9406
9407
9408#endif // FEAT_EVAL