blob: e5e3068d5c9beba25bb603c09b753ed6152efd2f [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200425 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100426 {
427 if (is_arg)
428 semsg(_(e_argument_already_declared_in_script_str), p);
429 else
430 semsg(_(e_variable_already_declared_in_script_str), p);
431 return FAIL;
432 }
433
Bram Moolenaarad486a02020-08-01 23:22:18 +0200434 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100435 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100436 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200437 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200439 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200441 // A local or script-local function can shadow a global function.
442 if (ufunc == NULL || !func_is_global(ufunc)
443 || (p[0] == 'g' && p[1] == ':'))
444 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 if (is_arg)
446 semsg(_(e_argument_name_shadows_existing_variable_str), p);
447 else
448 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200449 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200450 return FAIL;
451 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100452 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200453 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100454 return OK;
455}
456
Bram Moolenaar65b95452020-07-19 14:03:09 +0200457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458/////////////////////////////////////////////////////////////////////
459// Following generate_ functions expect the caller to call ga_grow().
460
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200461#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
462#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464/*
465 * Generate an instruction without arguments.
466 * Returns a pointer to the new instruction, NULL if failed.
467 */
468 static isn_T *
469generate_instr(cctx_T *cctx, isntype_T isn_type)
470{
471 garray_T *instr = &cctx->ctx_instr;
472 isn_T *isn;
473
Bram Moolenaar080457c2020-03-03 21:53:32 +0100474 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 if (ga_grow(instr, 1) == FAIL)
476 return NULL;
477 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
478 isn->isn_type = isn_type;
479 isn->isn_lnum = cctx->ctx_lnum + 1;
480 ++instr->ga_len;
481
482 return isn;
483}
484
485/*
486 * Generate an instruction without arguments.
487 * "drop" will be removed from the stack.
488 * Returns a pointer to the new instruction, NULL if failed.
489 */
490 static isn_T *
491generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
492{
493 garray_T *stack = &cctx->ctx_type_stack;
494
Bram Moolenaar080457c2020-03-03 21:53:32 +0100495 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 stack->ga_len -= drop;
497 return generate_instr(cctx, isn_type);
498}
499
500/*
501 * Generate instruction "isn_type" and put "type" on the type stack.
502 */
503 static isn_T *
504generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
505{
506 isn_T *isn;
507 garray_T *stack = &cctx->ctx_type_stack;
508
509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
510 return NULL;
511
512 if (ga_grow(stack, 1) == FAIL)
513 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200514 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 ++stack->ga_len;
516
517 return isn;
518}
519
520/*
521 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200522 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 */
524 static int
525may_generate_2STRING(int offset, cctx_T *cctx)
526{
527 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200528 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100530 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100532 RETURN_OK_IF_SKIP(cctx);
533 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200534 switch ((*type)->tt_type)
535 {
536 // nothing to be done
537 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200539 // conversion possible
540 case VAR_SPECIAL:
541 case VAR_BOOL:
542 case VAR_NUMBER:
543 case VAR_FLOAT:
544 break;
545
546 // conversion possible (with runtime check)
547 case VAR_ANY:
548 case VAR_UNKNOWN:
549 isntype = ISN_2STRING_ANY;
550 break;
551
552 // conversion not possible
553 case VAR_VOID:
554 case VAR_BLOB:
555 case VAR_FUNC:
556 case VAR_PARTIAL:
557 case VAR_LIST:
558 case VAR_DICT:
559 case VAR_JOB:
560 case VAR_CHANNEL:
561 to_string_error((*type)->tt_type);
562 return FAIL;
563 }
564
565 *type = &t_string;
566 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 return FAIL;
568 isn->isn_arg.number = offset;
569
570 return OK;
571}
572
573 static int
574check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
575{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200578 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 {
580 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200581 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200583 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200589 static int
590generate_add_instr(
591 cctx_T *cctx,
592 vartype_T vartype,
593 type_T *type1,
594 type_T *type2)
595{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100596 garray_T *stack = &cctx->ctx_type_stack;
597 isn_T *isn = generate_instr_drop(cctx,
598 vartype == VAR_NUMBER ? ISN_OPNR
599 : vartype == VAR_LIST ? ISN_ADDLIST
600 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200601#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100602 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200603#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100604 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605
606 if (vartype != VAR_LIST && vartype != VAR_BLOB
607 && type1->tt_type != VAR_ANY
608 && type2->tt_type != VAR_ANY
609 && check_number_or_float(
610 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
611 return FAIL;
612
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100615
616 // When concatenating two lists with different member types the member type
617 // becomes "any".
618 if (vartype == VAR_LIST
619 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
620 && type1->tt_member != type2->tt_member)
621 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
622
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200623 return isn == NULL ? FAIL : OK;
624}
625
626/*
627 * Get the type to use for an instruction for an operation on "type1" and
628 * "type2". If they are matching use a type-specific instruction. Otherwise
629 * fall back to runtime type checking.
630 */
631 static vartype_T
632operator_type(type_T *type1, type_T *type2)
633{
634 if (type1->tt_type == type2->tt_type
635 && (type1->tt_type == VAR_NUMBER
636 || type1->tt_type == VAR_LIST
637#ifdef FEAT_FLOAT
638 || type1->tt_type == VAR_FLOAT
639#endif
640 || type1->tt_type == VAR_BLOB))
641 return type1->tt_type;
642 return VAR_ANY;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Generate an instruction with two arguments. The instruction depends on the
647 * type of the arguments.
648 */
649 static int
650generate_two_op(cctx_T *cctx, char_u *op)
651{
652 garray_T *stack = &cctx->ctx_type_stack;
653 type_T *type1;
654 type_T *type2;
655 vartype_T vartype;
656 isn_T *isn;
657
Bram Moolenaar080457c2020-03-03 21:53:32 +0100658 RETURN_OK_IF_SKIP(cctx);
659
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
662 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 switch (*op)
666 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 case '+':
668 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 break;
671
672 case '-':
673 case '*':
674 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
675 op) == FAIL)
676 return FAIL;
677 if (vartype == VAR_NUMBER)
678 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
679#ifdef FEAT_FLOAT
680 else if (vartype == VAR_FLOAT)
681 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
682#endif
683 else
684 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
685 if (isn != NULL)
686 isn->isn_arg.op.op_type = *op == '*'
687 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
688 break;
689
Bram Moolenaar4c683752020-04-05 21:38:23 +0200690 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200692 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 && type2->tt_type != VAR_NUMBER))
694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200695 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 return FAIL;
697 }
698 isn = generate_instr_drop(cctx,
699 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_REM;
702 break;
703 }
704
705 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200706 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 type_T *type = &t_any;
709
710#ifdef FEAT_FLOAT
711 // float+number and number+float results in float
712 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
713 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
714 type = &t_float;
715#endif
716 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
717 }
718
719 return OK;
720}
721
722/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 * Get the instruction to use for comparing "type1" with "type2"
724 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200726 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100727get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728{
729 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730
Bram Moolenaar4c683752020-04-05 21:38:23 +0200731 if (type1 == VAR_UNKNOWN)
732 type1 = VAR_ANY;
733 if (type2 == VAR_UNKNOWN)
734 type2 = VAR_ANY;
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if (type1 == type2)
737 {
738 switch (type1)
739 {
740 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
741 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
742 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
743 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
744 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
745 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
746 case VAR_LIST: isntype = ISN_COMPARELIST; break;
747 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
748 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 default: isntype = ISN_COMPAREANY; break;
750 }
751 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200752 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100754 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 isntype = ISN_COMPAREANY;
756
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 && (isntype == ISN_COMPAREBOOL
759 || isntype == ISN_COMPARESPECIAL
760 || isntype == ISN_COMPARENR
761 || isntype == ISN_COMPAREFLOAT))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200765 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 }
767 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
770 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
772 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BLOB || type2 == VAR_BLOB
774 || type1 == VAR_LIST || type2 == VAR_LIST))))
775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200776 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200778 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 return isntype;
781}
782
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200783 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200785{
786 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
787 return FAIL;
788 return OK;
789}
790
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791/*
792 * Generate an ISN_COMPARE* instruction with a boolean result.
793 */
794 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200796{
797 isntype_T isntype;
798 isn_T *isn;
799 garray_T *stack = &cctx->ctx_type_stack;
800 vartype_T type1;
801 vartype_T type2;
802
803 RETURN_OK_IF_SKIP(cctx);
804
805 // Get the known type of the two items on the stack. If they are matching
806 // use a type-specific instruction. Otherwise fall back to runtime type
807 // checking.
808 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
809 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100810 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 if (isntype == ISN_DROP)
812 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 if ((isn = generate_instr(cctx, isntype)) == NULL)
815 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100816 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 isn->isn_arg.op.op_ic = ic;
818
819 // takes two arguments, puts one bool back
820 if (stack->ga_len >= 2)
821 {
822 --stack->ga_len;
823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
824 }
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_2BOOL instruction.
831 */
832 static int
833generate_2BOOL(cctx_T *cctx, int invert)
834{
835 isn_T *isn;
836 garray_T *stack = &cctx->ctx_type_stack;
837
Bram Moolenaar080457c2020-03-03 21:53:32 +0100838 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
840 return FAIL;
841 isn->isn_arg.number = invert;
842
843 // type becomes bool
844 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
845
846 return OK;
847}
848
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200849/*
850 * Generate an ISN_COND2BOOL instruction.
851 */
852 static int
853generate_COND2BOOL(cctx_T *cctx)
854{
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857
858 RETURN_OK_IF_SKIP(cctx);
859 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
860 return FAIL;
861
862 // type becomes bool
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864
865 return OK;
866}
867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200869generate_TYPECHECK(
870 cctx_T *cctx,
871 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100872 int offset,
873 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
880 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200881 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100882 isn->isn_arg.type.ct_off = (int8_T)offset;
883 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 // type becomes expected
886 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 return OK;
889}
890
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100891 static int
892generate_SETTYPE(
893 cctx_T *cctx,
894 type_T *expected)
895{
896 isn_T *isn;
897
898 RETURN_OK_IF_SKIP(cctx);
899 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
900 return FAIL;
901 isn->isn_arg.type.ct_type = alloc_type(expected);
902 return OK;
903}
904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200906 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
907 * used. Return FALSE if the types will never match.
908 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100909 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910use_typecheck(type_T *actual, type_T *expected)
911{
912 if (actual->tt_type == VAR_ANY
913 || actual->tt_type == VAR_UNKNOWN
914 || (actual->tt_type == VAR_FUNC
915 && (expected->tt_type == VAR_FUNC
916 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100917 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
918 && ((actual->tt_member == &t_void)
919 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200920 return TRUE;
921 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
922 && actual->tt_type == expected->tt_type)
923 // This takes care of a nested list or dict.
924 return use_typecheck(actual->tt_member, expected->tt_member);
925 return FALSE;
926}
927
928/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200929 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200930 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 * - "actual" is a type that can be "expected" type: add a runtime check; or
932 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200933 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
934 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100936 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200937need_type(
938 type_T *actual,
939 type_T *expected,
940 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100941 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200942 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200943 int silent,
944 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200945{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100946 where_T where;
947
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200948 if (expected == &t_bool && actual != &t_bool
949 && (actual->tt_flags & TTFLAG_BOOL_OK))
950 {
951 // Using "0", "1" or the result of an expression with "&&" or "||" as a
952 // boolean is OK but requires a conversion.
953 generate_2BOOL(cctx, FALSE);
954 return OK;
955 }
956
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100957 where.wt_index = arg_idx;
958 where.wt_variable = FALSE;
959 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200960 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200961
962 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 // If it's a constant a runtime check makes no sense.
964 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100966 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200967 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200968 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200969
970 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100971 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200972 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200973}
974
975/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100976 * Check that the top of the type stack has a type that can be used as a
977 * condition. Give an error and return FAIL if not.
978 */
979 static int
980bool_on_stack(cctx_T *cctx)
981{
982 garray_T *stack = &cctx->ctx_type_stack;
983 type_T *type;
984
985 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
986 if (type == &t_bool)
987 return OK;
988
989 if (type == &t_any || type == &t_number)
990 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
991 // This requires a runtime type check.
992 return generate_COND2BOOL(cctx);
993
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Generate an ISN_PUSHNR instruction.
999 */
1000 static int
1001generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001004 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001012 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001013 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHBOOL instruction.
1019 */
1020 static int
1021generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1022{
1023 isn_T *isn;
1024
Bram Moolenaar080457c2020-03-03 21:53:32 +01001025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.number = number;
1029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_PUSHSPEC instruction.
1035 */
1036 static int
1037generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.number = number;
1045
1046 return OK;
1047}
1048
1049#ifdef FEAT_FLOAT
1050/*
1051 * Generate an ISN_PUSHF instruction.
1052 */
1053 static int
1054generate_PUSHF(cctx_T *cctx, float_T fnumber)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.fnumber = fnumber;
1062
1063 return OK;
1064}
1065#endif
1066
1067/*
1068 * Generate an ISN_PUSHS instruction.
1069 * Consumes "str".
1070 */
1071 static int
1072generate_PUSHS(cctx_T *cctx, char_u *str)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar508b5612021-01-02 18:17:26 +01001076 if (cctx->ctx_skip == SKIP_YES)
1077 {
1078 vim_free(str);
1079 return OK;
1080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.string = str;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001089 * Generate an ISN_PUSHCHANNEL instruction.
1090 * Consumes "channel".
1091 */
1092 static int
1093generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1094{
1095 isn_T *isn;
1096
Bram Moolenaar080457c2020-03-03 21:53:32 +01001097 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001098 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1099 return FAIL;
1100 isn->isn_arg.channel = channel;
1101
1102 return OK;
1103}
1104
1105/*
1106 * Generate an ISN_PUSHJOB instruction.
1107 * Consumes "job".
1108 */
1109 static int
1110generate_PUSHJOB(cctx_T *cctx, job_T *job)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 return FAIL;
1117 isn->isn_arg.job = job;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Generate an ISN_PUSHBLOB instruction.
1124 * Consumes "blob".
1125 */
1126 static int
1127generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.blob = blob;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001140 * Generate an ISN_PUSHFUNC instruction with name "name".
1141 * Consumes "name".
1142 */
1143 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001144generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001149 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001150 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001151 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152
1153 return OK;
1154}
1155
1156/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001157 * Generate an ISN_GETITEM instruction with "index".
1158 */
1159 static int
1160generate_GETITEM(cctx_T *cctx, int index)
1161{
1162 isn_T *isn;
1163 garray_T *stack = &cctx->ctx_type_stack;
1164 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1165 type_T *item_type = &t_any;
1166
1167 RETURN_OK_IF_SKIP(cctx);
1168
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001169 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001170 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001171 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001172 emsg(_(e_listreq));
1173 return FAIL;
1174 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.number = index;
1179
1180 // add the item type to the type stack
1181 if (ga_grow(stack, 1) == FAIL)
1182 return FAIL;
1183 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1184 ++stack->ga_len;
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001189 * Generate an ISN_SLICE instruction with "count".
1190 */
1191 static int
1192generate_SLICE(cctx_T *cctx, int count)
1193{
1194 isn_T *isn;
1195
1196 RETURN_OK_IF_SKIP(cctx);
1197 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1198 return FAIL;
1199 isn->isn_arg.number = count;
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_CHECKLEN instruction with "min_len".
1205 */
1206 static int
1207generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1208{
1209 isn_T *isn;
1210
1211 RETURN_OK_IF_SKIP(cctx);
1212
1213 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.checklen.cl_min_len = min_len;
1216 isn->isn_arg.checklen.cl_more_OK = more_OK;
1217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 * Generate an ISN_STORE instruction.
1223 */
1224 static int
1225generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1226{
1227 isn_T *isn;
1228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1231 return FAIL;
1232 if (name != NULL)
1233 isn->isn_arg.string = vim_strsave(name);
1234 else
1235 isn->isn_arg.number = idx;
1236
1237 return OK;
1238}
1239
1240/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001241 * Generate an ISN_STOREOUTER instruction.
1242 */
1243 static int
1244generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1245{
1246 isn_T *isn;
1247
1248 RETURN_OK_IF_SKIP(cctx);
1249 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.outer.outer_idx = idx;
1252 isn->isn_arg.outer.outer_depth = level;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1259 */
1260 static int
1261generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1262{
1263 isn_T *isn;
1264
Bram Moolenaar080457c2020-03-03 21:53:32 +01001265 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1267 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001268 isn->isn_arg.storenr.stnr_idx = idx;
1269 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 return OK;
1272}
1273
1274/*
1275 * Generate an ISN_STOREOPT instruction
1276 */
1277 static int
1278generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1279{
1280 isn_T *isn;
1281
Bram Moolenaar080457c2020-03-03 21:53:32 +01001282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001283 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 return FAIL;
1285 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1286 isn->isn_arg.storeopt.so_flags = opt_flags;
1287
1288 return OK;
1289}
1290
1291/*
1292 * Generate an ISN_LOAD or similar instruction.
1293 */
1294 static int
1295generate_LOAD(
1296 cctx_T *cctx,
1297 isntype_T isn_type,
1298 int idx,
1299 char_u *name,
1300 type_T *type)
1301{
1302 isn_T *isn;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1306 return FAIL;
1307 if (name != NULL)
1308 isn->isn_arg.string = vim_strsave(name);
1309 else
1310 isn->isn_arg.number = idx;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001316 * Generate an ISN_LOADOUTER instruction
1317 */
1318 static int
1319generate_LOADOUTER(
1320 cctx_T *cctx,
1321 int idx,
1322 int nesting,
1323 type_T *type)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.outer.outer_idx = idx;
1331 isn->isn_arg.outer.outer_depth = nesting;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001353 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001379 * Generate an ISN_LOCKCONST instruction.
1380 */
1381 static int
1382generate_LOCKCONST(cctx_T *cctx)
1383{
1384 isn_T *isn;
1385
1386 RETURN_OK_IF_SKIP(cctx);
1387 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1388 return FAIL;
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 * Generate an ISN_LOADS instruction.
1394 */
1395 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400 int sid,
1401 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406 if (isn_type == ISN_LOADS)
1407 isn = generate_instr_type(cctx, isn_type, type);
1408 else
1409 isn = generate_instr_drop(cctx, isn_type, 1);
1410 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1413 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414
1415 return OK;
1416}
1417
1418/*
1419 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1420 */
1421 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 cctx_T *cctx,
1424 isntype_T isn_type,
1425 int sid,
1426 int idx,
1427 type_T *type)
1428{
1429 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001430 scriptref_T *sref;
1431 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if (isn_type == ISN_LOADSCRIPT)
1435 isn = generate_instr_type(cctx, isn_type, type);
1436 else
1437 isn = generate_instr_drop(cctx, isn_type, 1);
1438 if (isn == NULL)
1439 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001440
1441 // This requires three arguments, which doesn't fit in an instruction, thus
1442 // we need to allocate a struct for this.
1443 sref = ALLOC_ONE(scriptref_T);
1444 if (sref == NULL)
1445 return FAIL;
1446 isn->isn_arg.script.scriptref = sref;
1447 sref->sref_sid = sid;
1448 sref->sref_idx = idx;
1449 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001450 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_NEWLIST instruction.
1456 */
1457 static int
1458generate_NEWLIST(cctx_T *cctx, int count)
1459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 type_T *type;
1463 type_T *member;
1464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1467 return FAIL;
1468 isn->isn_arg.number = count;
1469
Bram Moolenaar127542b2020-08-09 17:22:04 +02001470 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001471 if (count == 0)
1472 member = &t_void;
1473 else
1474 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001475 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1476 cctx->ctx_type_list);
1477 type = get_list_type(member, cctx->ctx_type_list);
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // drop the value types
1480 stack->ga_len -= count;
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 // add the list type to the type stack
1483 if (ga_grow(stack, 1) == FAIL)
1484 return FAIL;
1485 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1486 ++stack->ga_len;
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_NEWDICT instruction.
1493 */
1494 static int
1495generate_NEWDICT(cctx_T *cctx, int count)
1496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 type_T *type;
1500 type_T *member;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1504 return FAIL;
1505 isn->isn_arg.number = count;
1506
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001507 if (count == 0)
1508 member = &t_void;
1509 else
1510 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001511 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1512 cctx->ctx_type_list);
1513 type = get_dict_type(member, cctx->ctx_type_list);
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 // drop the key and value types
1516 stack->ga_len -= 2 * count;
1517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // add the dict type to the type stack
1519 if (ga_grow(stack, 1) == FAIL)
1520 return FAIL;
1521 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1522 ++stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Generate an ISN_FUNCREF instruction.
1529 */
1530 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001531generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
Bram Moolenaar080457c2020-03-03 21:53:32 +01001536 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1538 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001539 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001540 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001542 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001543 // the nested context, including this one.
1544 if (ufunc->uf_flags & FC_CLOSURE)
1545 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if (ga_grow(stack, 1) == FAIL)
1548 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001549 ((type_T **)stack->ga_data)[stack->ga_len] =
1550 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 ++stack->ga_len;
1552
1553 return OK;
1554}
1555
1556/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001558 * "lambda_name" and "func_name" must be in allocated memory and will be
1559 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001560 */
1561 static int
1562generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1563{
1564 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001565
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001566 if (cctx->ctx_skip == SKIP_YES)
1567 {
1568 vim_free(lambda_name);
1569 vim_free(func_name);
1570 return OK;
1571 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 {
1574 vim_free(lambda_name);
1575 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 }
1578 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 isn->isn_arg.newfunc.nf_global = func_name;
1580
1581 return OK;
1582}
1583
1584/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001585 * Generate an ISN_DEF instruction: list functions
1586 */
1587 static int
1588generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1589{
1590 isn_T *isn;
1591
1592 RETURN_OK_IF_SKIP(cctx);
1593 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1594 return FAIL;
1595 if (len > 0)
1596 {
1597 isn->isn_arg.string = vim_strnsave(name, len);
1598 if (isn->isn_arg.string == NULL)
1599 return FAIL;
1600 }
1601 return OK;
1602}
1603
1604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 * Generate an ISN_JUMP instruction.
1606 */
1607 static int
1608generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1609{
1610 isn_T *isn;
1611 garray_T *stack = &cctx->ctx_type_stack;
1612
Bram Moolenaar080457c2020-03-03 21:53:32 +01001613 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.jump.jump_when = when;
1617 isn->isn_arg.jump.jump_where = where;
1618
1619 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1620 --stack->ga_len;
1621
1622 return OK;
1623}
1624
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001625/*
1626 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1627 */
1628 static int
1629generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1630{
1631 isn_T *isn;
1632
1633 RETURN_OK_IF_SKIP(cctx);
1634 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1637 // jump_where is set later
1638 return OK;
1639}
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 static int
1642generate_FOR(cctx_T *cctx, int loop_idx)
1643{
1644 isn_T *isn;
1645 garray_T *stack = &cctx->ctx_type_stack;
1646
Bram Moolenaar080457c2020-03-03 21:53:32 +01001647 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.forloop.for_idx = loop_idx;
1651
1652 if (ga_grow(stack, 1) == FAIL)
1653 return FAIL;
1654 // type doesn't matter, will be stored next
1655 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1656 ++stack->ga_len;
1657
1658 return OK;
1659}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001660/*
1661 * Generate an ISN_TRYCONT instruction.
1662 */
1663 static int
1664generate_TRYCONT(cctx_T *cctx, int levels, int where)
1665{
1666 isn_T *isn;
1667
1668 RETURN_OK_IF_SKIP(cctx);
1669 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.trycont.tct_levels = levels;
1672 isn->isn_arg.trycont.tct_where = where;
1673
1674 return OK;
1675}
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
1678/*
1679 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001680 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 * Return FAIL if the number of arguments is wrong.
1682 */
1683 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001684generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685{
1686 isn_T *isn;
1687 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001689 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001690 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar080457c2020-03-03 21:53:32 +01001692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001693 argoff = check_internal_func(func_idx, argcount);
1694 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 return FAIL;
1696
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 if (method_call && argoff > 1)
1698 {
1699 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.shuffle.shfl_item = argcount;
1702 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1703 }
1704
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 if (argcount > 0)
1706 {
1707 // Check the types of the arguments.
1708 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001709 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1710 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001711 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001712 if (internal_func_is_map(func_idx))
1713 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001714 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1717 return FAIL;
1718 isn->isn_arg.bfunc.cbf_idx = func_idx;
1719 isn->isn_arg.bfunc.cbf_argcount = argcount;
1720
Bram Moolenaar94738d82020-10-21 14:25:07 +02001721 // Drop the argument types and push the return type.
1722 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if (ga_grow(stack, 1) == FAIL)
1724 return FAIL;
1725 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001726 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001727 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001729 if (maptype != NULL && maptype->tt_member != NULL
1730 && maptype->tt_member != &t_any)
1731 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001732 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 return OK;
1735}
1736
1737/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001738 * Generate an ISN_LISTAPPEND instruction. Works like add().
1739 * Argument count is already checked.
1740 */
1741 static int
1742generate_LISTAPPEND(cctx_T *cctx)
1743{
1744 garray_T *stack = &cctx->ctx_type_stack;
1745 type_T *list_type;
1746 type_T *item_type;
1747 type_T *expected;
1748
1749 // Caller already checked that list_type is a list.
1750 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1751 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1752 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001753 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001754 return FAIL;
1755
1756 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1757 return FAIL;
1758
1759 --stack->ga_len; // drop the argument
1760 return OK;
1761}
1762
1763/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001764 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1765 * Argument count is already checked.
1766 */
1767 static int
1768generate_BLOBAPPEND(cctx_T *cctx)
1769{
1770 garray_T *stack = &cctx->ctx_type_stack;
1771 type_T *item_type;
1772
1773 // Caller already checked that blob_type is a blob.
1774 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001775 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001776 return FAIL;
1777
1778 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1779 return FAIL;
1780
1781 --stack->ga_len; // drop the argument
1782 return OK;
1783}
1784
1785/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 * Return TRUE if "ufunc" should be compiled, taking into account whether
1787 * "profile" indicates profiling is to be done.
1788 */
1789 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001790func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001791{
1792 switch (ufunc->uf_def_status)
1793 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001794 case UF_TO_BE_COMPILED:
1795 return TRUE;
1796
Bram Moolenaarb2049902021-01-24 12:53:53 +01001797 case UF_COMPILED:
1798 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001799#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001800 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1801 + ufunc->uf_dfunc_idx;
1802
1803 return profile ? dfunc->df_instr_prof == NULL
1804 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001805#else
1806 break;
1807#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001808 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001809
1810 case UF_NOT_COMPILED:
1811 case UF_COMPILE_ERROR:
1812 case UF_COMPILING:
1813 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001814 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001815 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001816}
1817
1818/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 * Generate an ISN_DCALL or ISN_UCALL instruction.
1820 * Return FAIL if the number of arguments is wrong.
1821 */
1822 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001823generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824{
1825 isn_T *isn;
1826 garray_T *stack = &cctx->ctx_type_stack;
1827 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001828 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaar080457c2020-03-03 21:53:32 +01001830 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 if (argcount > regular_args && !has_varargs(ufunc))
1832 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001833 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 return FAIL;
1835 }
1836 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1837 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001838 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 return FAIL;
1840 }
1841
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001842 if (ufunc->uf_def_status != UF_NOT_COMPILED
1843 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001844 {
1845 int i;
1846
1847 for (i = 0; i < argcount; ++i)
1848 {
1849 type_T *expected;
1850 type_T *actual;
1851
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001852 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1853 if (actual == &t_special
1854 && i >= regular_args - ufunc->uf_def_args.ga_len)
1855 {
1856 // assume v:none used for default argument value
1857 continue;
1858 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001859 if (i < regular_args)
1860 {
1861 if (ufunc->uf_arg_types == NULL)
1862 continue;
1863 expected = ufunc->uf_arg_types[i];
1864 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001865 else if (ufunc->uf_va_type == NULL
1866 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001867 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001868 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001869 else
1870 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001871 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001872 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 {
1874 arg_type_mismatch(expected, actual, i + 1);
1875 return FAIL;
1876 }
1877 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001878 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001879 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001880 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001881 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001882 }
1883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001885 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001886 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001888 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 {
1890 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1891 isn->isn_arg.dfunc.cdf_argcount = argcount;
1892 }
1893 else
1894 {
1895 // A user function may be deleted and redefined later, can't use the
1896 // ufunc pointer, need to look it up again at runtime.
1897 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1898 isn->isn_arg.ufunc.cuf_argcount = argcount;
1899 }
1900
1901 stack->ga_len -= argcount; // drop the arguments
1902 if (ga_grow(stack, 1) == FAIL)
1903 return FAIL;
1904 // add return value
1905 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1906 ++stack->ga_len;
1907
1908 return OK;
1909}
1910
1911/*
1912 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1913 */
1914 static int
1915generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1916{
1917 isn_T *isn;
1918 garray_T *stack = &cctx->ctx_type_stack;
1919
Bram Moolenaar080457c2020-03-03 21:53:32 +01001920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1922 return FAIL;
1923 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1924 isn->isn_arg.ufunc.cuf_argcount = argcount;
1925
1926 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001927 if (ga_grow(stack, 1) == FAIL)
1928 return FAIL;
1929 // add return value
1930 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1931 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932
1933 return OK;
1934}
1935
1936/*
1937 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001938 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 */
1940 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001941generate_PCALL(
1942 cctx_T *cctx,
1943 int argcount,
1944 char_u *name,
1945 type_T *type,
1946 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947{
1948 isn_T *isn;
1949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001950 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951
Bram Moolenaar080457c2020-03-03 21:53:32 +01001952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001953
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 if (type->tt_type == VAR_ANY)
1955 ret_type = &t_any;
1956 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001957 {
1958 if (type->tt_argcount != -1)
1959 {
1960 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1961
1962 if (argcount < type->tt_min_argcount - varargs)
1963 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001964 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001965 return FAIL;
1966 }
1967 if (!varargs && argcount > type->tt_argcount)
1968 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001969 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001970 return FAIL;
1971 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001972 if (type->tt_args != NULL)
1973 {
1974 int i;
1975
1976 for (i = 0; i < argcount; ++i)
1977 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02001978 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001979 type_T *actual = ((type_T **)stack->ga_data)[
1980 stack->ga_len + offset];
1981 type_T *expected;
1982
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001983 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001984 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001985 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001986 else if (i >= type->tt_min_argcount
1987 && actual == &t_special)
1988 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001989 else
1990 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001991 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001992 cctx, TRUE, FALSE) == FAIL)
1993 {
1994 arg_type_mismatch(expected, actual, i + 1);
1995 return FAIL;
1996 }
1997 }
1998 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001999 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002000 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002001 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002002 else
2003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002004 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002005 return FAIL;
2006 }
2007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2009 return FAIL;
2010 isn->isn_arg.pfunc.cpf_top = at_top;
2011 isn->isn_arg.pfunc.cpf_argcount = argcount;
2012
2013 stack->ga_len -= argcount; // drop the arguments
2014
2015 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002016 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002018 // If partial is above the arguments it must be cleared and replaced with
2019 // the return value.
2020 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2021 return FAIL;
2022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 return OK;
2024}
2025
2026/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002027 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 */
2029 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 isn_T *isn;
2033 garray_T *stack = &cctx->ctx_type_stack;
2034 type_T *type;
2035
Bram Moolenaar080457c2020-03-03 21:53:32 +01002036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002037 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002039 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002041 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002043 if (type->tt_type != VAR_DICT && type != &t_any)
2044 {
2045 emsg(_(e_dictreq));
2046 return FAIL;
2047 }
2048 // change dict type to dict member type
2049 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002050 {
2051 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2052 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
2055 return OK;
2056}
2057
2058/*
2059 * Generate an ISN_ECHO instruction.
2060 */
2061 static int
2062generate_ECHO(cctx_T *cctx, int with_white, int count)
2063{
2064 isn_T *isn;
2065
Bram Moolenaar080457c2020-03-03 21:53:32 +01002066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.echo.echo_with_white = with_white;
2070 isn->isn_arg.echo.echo_count = count;
2071
2072 return OK;
2073}
2074
Bram Moolenaarad39c092020-02-26 18:23:43 +01002075/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077 */
2078 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002079generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080{
2081 isn_T *isn;
2082
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084 return FAIL;
2085 isn->isn_arg.number = count;
2086
2087 return OK;
2088}
2089
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002090/*
2091 * Generate an ISN_PUT instruction.
2092 */
2093 static int
2094generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2095{
2096 isn_T *isn;
2097
2098 RETURN_OK_IF_SKIP(cctx);
2099 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2100 return FAIL;
2101 isn->isn_arg.put.put_regname = regname;
2102 isn->isn_arg.put.put_lnum = lnum;
2103 return OK;
2104}
2105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 static int
2107generate_EXEC(cctx_T *cctx, char_u *line)
2108{
2109 isn_T *isn;
2110
Bram Moolenaar080457c2020-03-03 21:53:32 +01002111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2113 return FAIL;
2114 isn->isn_arg.string = vim_strsave(line);
2115 return OK;
2116}
2117
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002118 static int
2119generate_EXECCONCAT(cctx_T *cctx, int count)
2120{
2121 isn_T *isn;
2122
2123 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2124 return FAIL;
2125 isn->isn_arg.number = count;
2126 return OK;
2127}
2128
Bram Moolenaar08597872020-12-10 19:43:40 +01002129/*
2130 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2131 */
2132 static int
2133generate_RANGE(cctx_T *cctx, char_u *range)
2134{
2135 isn_T *isn;
2136 garray_T *stack = &cctx->ctx_type_stack;
2137
2138 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2139 return FAIL;
2140 isn->isn_arg.string = range;
2141
2142 if (ga_grow(stack, 1) == FAIL)
2143 return FAIL;
2144 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2145 ++stack->ga_len;
2146 return OK;
2147}
2148
Bram Moolenaar792f7862020-11-23 08:31:18 +01002149 static int
2150generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2151{
2152 isn_T *isn;
2153
2154 RETURN_OK_IF_SKIP(cctx);
2155 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2156 return FAIL;
2157 isn->isn_arg.unpack.unp_count = var_count;
2158 isn->isn_arg.unpack.unp_semicolon = semicolon;
2159 return OK;
2160}
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002163 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164 */
2165 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002166generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002167{
2168 isn_T *isn;
2169
Bram Moolenaarfa984412021-03-25 22:15:28 +01002170 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002172 cctx->ctx_has_cmdmod = TRUE;
2173
2174 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2177 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2178 return FAIL;
2179 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002180 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002181 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002182 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002183
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002184 return OK;
2185}
2186
2187 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002188generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002189{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002190 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2191 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002192 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193 return OK;
2194}
2195
Bram Moolenaarfa984412021-03-25 22:15:28 +01002196 static int
2197misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002198{
2199 garray_T *instr = &cctx->ctx_instr;
2200
Bram Moolenaara91a7132021-03-25 21:12:15 +01002201 if (cctx->ctx_has_cmdmod
2202 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2203 == ISN_CMDMOD)
2204 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002205 emsg(_(e_misplaced_command_modifier));
2206 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002207 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002208 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002209}
2210
2211/*
2212 * Get the index of the current instruction.
2213 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2214 */
2215 static int
2216current_instr_idx(cctx_T *cctx)
2217{
2218 garray_T *instr = &cctx->ctx_instr;
2219 int idx = instr->ga_len;
2220
2221 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2222 .isn_type == ISN_CMDMOD)
2223 --idx;
2224#ifdef FEAT_PROFILE
2225 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_PROF_START)
2227 --idx;
2228#endif
2229 return idx;
2230}
2231
Bram Moolenaarf002a412021-01-24 13:34:18 +01002232#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002233 static void
2234may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2235{
2236 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002238}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002239#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002240
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002245 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002246reserve_local(
2247 cctx_T *cctx,
2248 char_u *name,
2249 size_t len,
2250 int isConst,
2251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 lvar_T *lvar;
2254
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002255 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002257 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002258 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260
2261 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
2263 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002264 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 // Every local variable uses the next entry on the stack. We could re-use
2267 // the last ones when leaving a scope, but then variables used in a closure
2268 // might get overwritten. To keep things simple do not re-use stack
2269 // entries. This is less efficient, but memory is cheap these days.
2270 lvar->lv_idx = cctx->ctx_locals_count++;
2271
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002272 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 lvar->lv_const = isConst;
2274 lvar->lv_type = type;
2275
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002276 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277}
2278
2279/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002280 * Remove local variables above "new_top".
2281 */
2282 static void
2283unwind_locals(cctx_T *cctx, int new_top)
2284{
2285 if (cctx->ctx_locals.ga_len > new_top)
2286 {
2287 int idx;
2288 lvar_T *lvar;
2289
2290 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2291 {
2292 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2293 vim_free(lvar->lv_name);
2294 }
2295 }
2296 cctx->ctx_locals.ga_len = new_top;
2297}
2298
2299/*
2300 * Free all local variables.
2301 */
2302 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002303free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002304{
2305 unwind_locals(cctx, 0);
2306 ga_clear(&cctx->ctx_locals);
2307}
2308
2309/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002310 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2311 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2312 * error if the variable was defined with :const.
2313 */
2314 static int
2315check_item_writable(svar_T *sv, int check_writable, char_u *name)
2316{
2317 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2318 || (check_writable == ASSIGN_FINAL
2319 && sv->sv_const == ASSIGN_CONST))
2320 {
2321 semsg(_(e_readonlyvar), name);
2322 return FAIL;
2323 }
2324 return OK;
2325}
2326
2327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002329 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 * Returns the index in "sn_var_vals" if found.
2331 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002332 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 */
2334 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002335get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336{
2337 hashtab_T *ht;
2338 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002340 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 int idx;
2342
Bram Moolenaare3d46852020-08-29 13:39:17 +02002343 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002345 if (sid == current_sctx.sc_sid)
2346 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002347 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348
2349 if (sav == NULL)
2350 return -2;
2351 idx = sav->sav_var_vals_idx;
2352 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002353 if (check_item_writable(sv, check_writable, name) == FAIL)
2354 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002355 return idx;
2356 }
2357
2358 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 ht = &SCRIPT_VARS(sid);
2360 di = find_var_in_ht(ht, 0, name, TRUE);
2361 if (di == NULL)
2362 return -2;
2363
2364 // Now find the svar_T index in sn_var_vals.
2365 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2366 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002367 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 if (sv->sv_tv == &di->di_tv)
2369 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002370 if (check_item_writable(sv, check_writable, name) == FAIL)
2371 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 return idx;
2373 }
2374 }
2375 return -1;
2376}
2377
2378/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002379 * Find "name" in imported items of the current script or in "cctx" if not
2380 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
2382 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002383find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int idx;
2386
Bram Moolenaare3d46852020-08-29 13:39:17 +02002387 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002388 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 if (cctx != NULL)
2390 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2391 {
2392 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2393 + idx;
2394
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002395 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2396 : STRLEN(import->imp_name) == len
2397 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 return import;
2399 }
2400
Bram Moolenaarefa94442020-08-08 22:16:00 +02002401 return find_imported_in_script(name, len, current_sctx.sc_sid);
2402}
2403
2404 imported_T *
2405find_imported_in_script(char_u *name, size_t len, int sid)
2406{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002407 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002408 int idx;
2409
Bram Moolenaare3d46852020-08-29 13:39:17 +02002410 if (!SCRIPT_ID_VALID(sid))
2411 return NULL;
2412 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2414 {
2415 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2416
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002417 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2418 : STRLEN(import->imp_name) == len
2419 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 return import;
2421 }
2422 return NULL;
2423}
2424
2425/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 * Free all imported variables.
2427 */
2428 static void
2429free_imported(cctx_T *cctx)
2430{
2431 int idx;
2432
2433 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2434 {
2435 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2436
2437 vim_free(import->imp_name);
2438 }
2439 ga_clear(&cctx->ctx_imports);
2440}
2441
2442/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002443 * Return a pointer to the next line that isn't empty or only contains a
2444 * comment. Skips over white space.
2445 * Returns NULL if there is none.
2446 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002447 char_u *
2448peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449{
2450 int lnum = cctx->ctx_lnum;
2451
2452 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2453 {
2454 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002455 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002456
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002457 // ignore NULLs inserted for continuation lines
2458 if (line != NULL)
2459 {
2460 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002461 if (vim9_bad_comment(p))
2462 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002463 if (*p != NUL && !vim9_comment_start(p))
2464 return p;
2465 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002466 }
2467 return NULL;
2468}
2469
2470/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002471 * Called when checking for a following operator at "arg". When the rest of
2472 * the line is empty or only a comment, peek the next line. If there is a next
2473 * line return a pointer to it and set "nextp".
2474 * Otherwise skip over white space.
2475 */
2476 static char_u *
2477may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2478{
2479 char_u *p = skipwhite(arg);
2480
2481 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002482 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002483 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002484 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002485 if (*nextp != NULL)
2486 return *nextp;
2487 }
2488 return p;
2489}
2490
2491/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002492 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002493 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002494 * Returns NULL when at the end.
2495 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002496 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002499 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002500
2501 do
2502 {
2503 ++cctx->ctx_lnum;
2504 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002505 {
2506 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002507 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002508 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002509 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002510 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002511 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002512 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002513 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002514 return line;
2515}
2516
2517/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002518 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002519 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002520 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002521 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2522 */
2523 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002526 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002527 if (vim9_bad_comment(*arg))
2528 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002529 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002530 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002531 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002532
2533 if (next == NULL)
2534 return FAIL;
2535 *arg = skipwhite(next);
2536 }
2537 return OK;
2538}
2539
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002540/*
2541 * Idem, and give an error when failed.
2542 */
2543 static int
2544may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2545{
2546 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2547 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002548 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002550 return FAIL;
2551 }
2552 return OK;
2553}
2554
2555
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556// Structure passed between the compile_expr* functions to keep track of
2557// constants that have been parsed but for which no code was produced yet. If
2558// possible expressions on these constants are applied at compile time. If
2559// that is not possible, the code to push the constants needs to be generated
2560// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002561// Using 50 should be more than enough of 5 levels of ().
2562#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002564 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002566 int pp_is_const; // all generated code was constants, used for a
2567 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568} ppconst_T;
2569
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002571static int compile_expr0(char_u **arg, cctx_T *cctx);
2572static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2573
Bram Moolenaara5565e42020-05-09 15:44:01 +02002574/*
2575 * Generate a PUSH instruction for "tv".
2576 * "tv" will be consumed or cleared.
2577 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2578 */
2579 static int
2580generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2581{
2582 if (tv != NULL)
2583 {
2584 switch (tv->v_type)
2585 {
2586 case VAR_UNKNOWN:
2587 break;
2588 case VAR_BOOL:
2589 generate_PUSHBOOL(cctx, tv->vval.v_number);
2590 break;
2591 case VAR_SPECIAL:
2592 generate_PUSHSPEC(cctx, tv->vval.v_number);
2593 break;
2594 case VAR_NUMBER:
2595 generate_PUSHNR(cctx, tv->vval.v_number);
2596 break;
2597#ifdef FEAT_FLOAT
2598 case VAR_FLOAT:
2599 generate_PUSHF(cctx, tv->vval.v_float);
2600 break;
2601#endif
2602 case VAR_BLOB:
2603 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2604 tv->vval.v_blob = NULL;
2605 break;
2606 case VAR_STRING:
2607 generate_PUSHS(cctx, tv->vval.v_string);
2608 tv->vval.v_string = NULL;
2609 break;
2610 default:
2611 iemsg("constant type not supported");
2612 clear_tv(tv);
2613 return FAIL;
2614 }
2615 tv->v_type = VAR_UNKNOWN;
2616 }
2617 return OK;
2618}
2619
2620/*
2621 * Generate code for any ppconst entries.
2622 */
2623 static int
2624generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2625{
2626 int i;
2627 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002628 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002629
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002630 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002631 for (i = 0; i < ppconst->pp_used; ++i)
2632 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2633 ret = FAIL;
2634 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002635 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002636 return ret;
2637}
2638
2639/*
2640 * Clear ppconst constants. Used when failing.
2641 */
2642 static void
2643clear_ppconst(ppconst_T *ppconst)
2644{
2645 int i;
2646
2647 for (i = 0; i < ppconst->pp_used; ++i)
2648 clear_tv(&ppconst->pp_tv[i]);
2649 ppconst->pp_used = 0;
2650}
2651
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002652/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002653 * Compile getting a member from a list/dict/string/blob. Stack has the
2654 * indexable value and the index.
2655 */
2656 static int
2657compile_member(int is_slice, cctx_T *cctx)
2658{
2659 type_T **typep;
2660 garray_T *stack = &cctx->ctx_type_stack;
2661 vartype_T vtype;
2662 type_T *valtype;
2663
2664 // We can index a list and a dict. If we don't know the type
2665 // we can use the index value type.
2666 // TODO: If we don't know use an instruction to figure it out at
2667 // runtime.
2668 typep = ((type_T **)stack->ga_data) + stack->ga_len
2669 - (is_slice ? 3 : 2);
2670 vtype = (*typep)->tt_type;
2671 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2672 // If the index is a string, the variable must be a Dict.
2673 if (*typep == &t_any && valtype == &t_string)
2674 vtype = VAR_DICT;
2675 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2676 {
2677 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2678 return FAIL;
2679 if (is_slice)
2680 {
2681 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2682 if (need_type(valtype, &t_number, -2, 0, cctx,
2683 FALSE, FALSE) == FAIL)
2684 return FAIL;
2685 }
2686 }
2687
2688 if (vtype == VAR_DICT)
2689 {
2690 if (is_slice)
2691 {
2692 emsg(_(e_cannot_slice_dictionary));
2693 return FAIL;
2694 }
2695 if ((*typep)->tt_type == VAR_DICT)
2696 {
2697 *typep = (*typep)->tt_member;
2698 if (*typep == &t_unknown)
2699 // empty dict was used
2700 *typep = &t_any;
2701 }
2702 else
2703 {
2704 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2705 FALSE, FALSE) == FAIL)
2706 return FAIL;
2707 *typep = &t_any;
2708 }
2709 if (may_generate_2STRING(-1, cctx) == FAIL)
2710 return FAIL;
2711 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2712 return FAIL;
2713 }
2714 else if (vtype == VAR_STRING)
2715 {
2716 *typep = &t_string;
2717 if ((is_slice
2718 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2719 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2720 return FAIL;
2721 }
2722 else if (vtype == VAR_BLOB)
2723 {
2724 emsg("Sorry, blob index and slice not implemented yet");
2725 return FAIL;
2726 }
2727 else if (vtype == VAR_LIST || *typep == &t_any)
2728 {
2729 if (is_slice)
2730 {
2731 if (generate_instr_drop(cctx,
2732 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2733 2) == FAIL)
2734 return FAIL;
2735 }
2736 else
2737 {
2738 if ((*typep)->tt_type == VAR_LIST)
2739 {
2740 *typep = (*typep)->tt_member;
2741 if (*typep == &t_unknown)
2742 // empty list was used
2743 *typep = &t_any;
2744 }
2745 if (generate_instr_drop(cctx,
2746 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2747 return FAIL;
2748 }
2749 }
2750 else
2751 {
2752 emsg(_(e_string_list_dict_or_blob_required));
2753 return FAIL;
2754 }
2755 return OK;
2756}
2757
2758/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002759 * Generate an instruction to load script-local variable "name", without the
2760 * leading "s:".
2761 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 */
2763 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002764compile_load_scriptvar(
2765 cctx_T *cctx,
2766 char_u *name, // variable NUL terminated
2767 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002768 char_u **end, // end of variable
2769 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002771 scriptitem_T *si;
2772 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 imported_T *import;
2774
Bram Moolenaare3d46852020-08-29 13:39:17 +02002775 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2776 return FAIL;
2777 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002778 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002779 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002781 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002782 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2783 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 }
2785 if (idx >= 0)
2786 {
2787 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2788
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002789 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 current_sctx.sc_sid, idx, sv->sv_type);
2791 return OK;
2792 }
2793
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002794 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 if (import != NULL)
2796 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002797 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002798 {
2799 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002800 char_u *exp_name;
2801 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 ufunc_T *ufunc;
2803 type_T *type;
2804
2805 // Used "import * as Name", need to lookup the member.
2806 if (*p != '.')
2807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002808 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002809 return FAIL;
2810 }
2811 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002812 if (VIM_ISWHITE(*p))
2813 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002814 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002815 return FAIL;
2816 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002817
Bram Moolenaar1c991142020-07-04 13:15:31 +02002818 // isolate one name
2819 exp_name = p;
2820 while (eval_isnamec(*p))
2821 ++p;
2822 cc = *p;
2823 *p = NUL;
2824
Bram Moolenaaredba7072021-03-13 21:14:18 +01002825 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2826 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002827 *p = cc;
2828 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002829 *end = p;
2830
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002831 if (idx < 0)
2832 {
2833 if (*p == '(' && ufunc != NULL)
2834 {
2835 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2836 return OK;
2837 }
2838 return FAIL;
2839 }
2840
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2842 import->imp_sid,
2843 idx,
2844 type);
2845 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002846 else if (import->imp_funcname != NULL)
2847 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002848 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002849 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2850 import->imp_sid,
2851 import->imp_var_vals_idx,
2852 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 return OK;
2854 }
2855
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002856 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002857 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 return FAIL;
2859}
2860
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002861 static int
2862generate_funcref(cctx_T *cctx, char_u *name)
2863{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002864 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865
2866 if (ufunc == NULL)
2867 return FAIL;
2868
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002869 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002870 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2871 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002872 == FAIL)
2873 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002874 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002875}
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877/*
2878 * Compile a variable name into a load instruction.
2879 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002880 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 * When "error" is FALSE do not give an error when not found.
2882 */
2883 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002884compile_load(
2885 char_u **arg,
2886 char_u *end_arg,
2887 cctx_T *cctx,
2888 int is_expr,
2889 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890{
2891 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002892 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002893 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002895 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
2897 if (*(*arg + 1) == ':')
2898 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002899 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002900 {
2901 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
Bram Moolenaarfa596382021-04-07 21:58:16 +02002903 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002904 switch (**arg)
2905 {
2906 case 'g': isn_type = ISN_LOADGDICT; break;
2907 case 'w': isn_type = ISN_LOADWDICT; break;
2908 case 't': isn_type = ISN_LOADTDICT; break;
2909 case 'b': isn_type = ISN_LOADBDICT; break;
2910 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002911 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002912 goto theend;
2913 }
2914 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2915 goto theend;
2916 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 else
2919 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002920 isntype_T isn_type = ISN_DROP;
2921
Bram Moolenaarfa596382021-04-07 21:58:16 +02002922 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002923 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2924 if (name == NULL)
2925 return FAIL;
2926
2927 switch (**arg)
2928 {
2929 case 'v': res = generate_LOADV(cctx, name, error);
2930 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002931 case 's': if (is_expr && ASCII_ISUPPER(*name)
2932 && find_func(name, FALSE, cctx) != NULL)
2933 res = generate_funcref(cctx, name);
2934 else
2935 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002936 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002938 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002939 {
2940 if (is_expr && ASCII_ISUPPER(*name)
2941 && find_func(name, FALSE, cctx) != NULL)
2942 res = generate_funcref(cctx, name);
2943 else
2944 isn_type = ISN_LOADG;
2945 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002946 else
2947 {
2948 isn_type = ISN_LOADAUTO;
2949 vim_free(name);
2950 name = vim_strnsave(*arg, end - *arg);
2951 if (name == NULL)
2952 return FAIL;
2953 }
2954 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002955 case 'w': isn_type = ISN_LOADW; break;
2956 case 't': isn_type = ISN_LOADT; break;
2957 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002958 default: // cannot happen, just in case
2959 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 goto theend;
2961 }
2962 if (isn_type != ISN_DROP)
2963 {
2964 // Global, Buffer-local, Window-local and Tabpage-local
2965 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002966 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002967 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 }
2970 }
2971 else
2972 {
2973 size_t len = end - *arg;
2974 int idx;
2975 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002976 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
2978 name = vim_strnsave(*arg, end - *arg);
2979 if (name == NULL)
2980 return FAIL;
2981
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002982 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002984 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002985 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 }
2987 else
2988 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002989 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002990
Bram Moolenaar709664c2020-12-12 14:33:41 +01002991 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002993 type = lvar.lv_type;
2994 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002995 if (lvar.lv_from_outer != 0)
2996 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002997 else
2998 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 }
3000 else
3001 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003002 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003003 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003004 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003005 || find_imported(name, 0, cctx) != NULL)
3006 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003007
Bram Moolenaar0f769812020-09-12 18:32:34 +02003008 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003009 // uppercase letter it can be a user defined function.
3010 // generate_funcref() will fail if the function can't be found.
3011 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003012 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 }
3014 }
3015 if (gen_load)
3016 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003017 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003018 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003019 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003020 cctx->ctx_outer_used = TRUE;
3021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 }
3023
3024 *arg = end;
3025
3026theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003027 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003028 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 vim_free(name);
3030 return res;
3031}
3032
3033/*
3034 * Compile the argument expressions.
3035 * "arg" points to just after the "(" and is advanced to after the ")"
3036 */
3037 static int
3038compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3039{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003040 char_u *p = *arg;
3041 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003042 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
Bram Moolenaare6085c52020-04-12 20:19:16 +02003044 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003046 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3047 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048 if (*p == ')')
3049 {
3050 *arg = p + 1;
3051 return OK;
3052 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003053 if (must_end)
3054 {
3055 semsg(_(e_missing_comma_before_argument_str), p);
3056 return FAIL;
3057 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003058
Bram Moolenaara5565e42020-05-09 15:44:01 +02003059 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return FAIL;
3061 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003062
3063 if (*p != ',' && *skipwhite(p) == ',')
3064 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003065 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003066 p = skipwhite(p);
3067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003069 {
3070 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003071 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003072 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003073 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003074 else
3075 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003076 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003077 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003079failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003080 emsg(_(e_missing_close));
3081 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082}
3083
3084/*
3085 * Compile a function call: name(arg1, arg2)
3086 * "arg" points to "name", "arg + varlen" to the "(".
3087 * "argcount_init" is 1 for "value->method()"
3088 * Instructions:
3089 * EVAL arg1
3090 * EVAL arg2
3091 * BCALL / DCALL / UCALL
3092 */
3093 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003094compile_call(
3095 char_u **arg,
3096 size_t varlen,
3097 cctx_T *cctx,
3098 ppconst_T *ppconst,
3099 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100{
3101 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003102 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 int argcount = argcount_init;
3104 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003105 char_u fname_buf[FLEN_FIXED + 1];
3106 char_u *tofree = NULL;
3107 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003108 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003109 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003110 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
Bram Moolenaara5565e42020-05-09 15:44:01 +02003112 // we can evaluate "has('name')" at compile time
3113 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3114 {
3115 char_u *s = skipwhite(*arg + varlen + 1);
3116 typval_T argvars[2];
3117
3118 argvars[0].v_type = VAR_UNKNOWN;
3119 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003120 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003121 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003122 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003123 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003124 if (*s == ')' && argvars[0].v_type == VAR_STRING
3125 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003126 {
3127 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3128
3129 *arg = s + 1;
3130 argvars[1].v_type = VAR_UNKNOWN;
3131 tv->v_type = VAR_NUMBER;
3132 tv->vval.v_number = 0;
3133 f_has(argvars, tv);
3134 clear_tv(&argvars[0]);
3135 ++ppconst->pp_used;
3136 return OK;
3137 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003138 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003139 }
3140
3141 if (generate_ppconst(cctx, ppconst) == FAIL)
3142 return FAIL;
3143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (varlen >= sizeof(namebuf))
3145 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003146 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return FAIL;
3148 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003149 vim_strncpy(namebuf, *arg, varlen);
3150 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151
3152 *arg = skipwhite(*arg + varlen + 1);
3153 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003154 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
Bram Moolenaar03290b82020-12-19 16:30:44 +01003156 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003157 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 {
3159 int idx;
3160
3161 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003162 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003164 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003165 if (STRCMP(name, "flatten") == 0)
3166 {
3167 emsg(_(e_cannot_use_flatten_in_vim9_script));
3168 goto theend;
3169 }
3170
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003171 if (STRCMP(name, "add") == 0 && argcount == 2)
3172 {
3173 garray_T *stack = &cctx->ctx_type_stack;
3174 type_T *type = ((type_T **)stack->ga_data)[
3175 stack->ga_len - 2];
3176
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003177 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003178 if (type->tt_type == VAR_LIST)
3179 {
3180 // inline "add(list, item)" so that the type can be checked
3181 res = generate_LISTAPPEND(cctx);
3182 idx = -1;
3183 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003184 else if (type->tt_type == VAR_BLOB)
3185 {
3186 // inline "add(blob, nr)" so that the type can be checked
3187 res = generate_BLOBAPPEND(cctx);
3188 idx = -1;
3189 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003190 }
3191
3192 if (idx >= 0)
3193 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3194 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003195 else
3196 semsg(_(e_unknownfunc), namebuf);
3197 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 }
3199
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003200 // An argument or local variable can be a function reference, this
3201 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003202 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003203 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003204 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003205 // If we can find the function by name generate the right call.
3206 // Skip global functions here, a local funcref takes precedence.
3207 ufunc = find_func(name, FALSE, cctx);
3208 if (ufunc != NULL && !func_is_global(ufunc))
3209 {
3210 res = generate_CALL(cctx, ufunc, argcount);
3211 goto theend;
3212 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
3215 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003216 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003217 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003219 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003220 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003221 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003222 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003223 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003224
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003225 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003226 goto theend;
3227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228
Bram Moolenaar0f769812020-09-12 18:32:34 +02003229 // If we can find a global function by name generate the right call.
3230 if (ufunc != NULL)
3231 {
3232 res = generate_CALL(cctx, ufunc, argcount);
3233 goto theend;
3234 }
3235
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003236 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003237 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003238 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003239 res = generate_UCALL(cctx, name, argcount);
3240 else
3241 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003242
3243theend:
3244 vim_free(tofree);
3245 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246}
3247
3248// like NAMESPACE_CHAR but with 'a' and 'l'.
3249#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3250
3251/*
3252 * Find the end of a variable or function name. Unlike find_name_end() this
3253 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003254 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 * Return a pointer to just after the name. Equal to "arg" if there is no
3256 * valid name.
3257 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003258 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003259to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260{
3261 char_u *p;
3262
3263 // Quick check for valid starting character.
3264 if (!eval_isnamec1(*arg))
3265 return arg;
3266
3267 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3268 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3269 // and can be used in slice "[n:]".
3270 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003271 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3273 break;
3274 return p;
3275}
3276
3277/*
3278 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003279 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 */
3281 char_u *
3282to_name_const_end(char_u *arg)
3283{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003284 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 typval_T rettv;
3286
3287 if (p == arg && *arg == '[')
3288 {
3289
3290 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003291 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 p = arg;
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 return p;
3295}
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297/*
3298 * parse a list: [expr, expr]
3299 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003300 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 */
3302 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003303compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304{
3305 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003306 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003308 int is_const;
3309 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003311 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003313 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003314 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003315 semsg(_(e_list_end), *arg);
3316 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003317 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003318 if (*p == ',')
3319 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003320 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003321 return FAIL;
3322 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003323 if (*p == ']')
3324 {
3325 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003326 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003327 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003328 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003329 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003330 if (!is_const)
3331 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 ++count;
3333 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003334 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003336 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3337 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003338 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003339 return FAIL;
3340 }
3341 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003342 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 p = skipwhite(p);
3344 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003345 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003347 ppconst->pp_is_const = is_all_const;
3348 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349}
3350
3351/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003352 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003353 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003354 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 */
3356 static int
3357compile_lambda(char_u **arg, cctx_T *cctx)
3358{
Bram Moolenaare462f522020-12-27 14:43:30 +01003359 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 typval_T rettv;
3361 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003362 evalarg_T evalarg;
3363
3364 CLEAR_FIELD(evalarg);
3365 evalarg.eval_flags = EVAL_EVALUATE;
3366 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367
3368 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003369 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3370 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003371 {
3372 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003373 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003374 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003375
Bram Moolenaar65c44152020-12-24 15:14:01 +01003376 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003378 ++ufunc->uf_refcount;
3379 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaar65c44152020-12-24 15:14:01 +01003381 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003382 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003384 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3385 // points into it. Point to the original line to avoid a dangling pointer.
3386 if (evalarg.eval_tofree_cmdline != NULL)
3387 {
3388 size_t off = *arg - evalarg.eval_tofree_cmdline;
3389
3390 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3391 + off;
3392 }
3393
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003394 clear_evalarg(&evalarg, NULL);
3395
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003396 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003397 {
3398 // The return type will now be known.
3399 set_function_type(ufunc);
3400
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003401 // The function reference count will be 1. When the ISN_FUNCREF
3402 // instruction is deleted the reference count is decremented and the
3403 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003404 return generate_FUNCREF(cctx, ufunc);
3405 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003406
3407 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
3409}
3410
3411/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003412 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003414 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 */
3416 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003417compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418{
3419 garray_T *instr = &cctx->ctx_instr;
3420 int count = 0;
3421 dict_T *d = dict_alloc();
3422 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003423 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003424 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003425 int is_const;
3426 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427
3428 if (d == NULL)
3429 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003430 if (generate_ppconst(cctx, ppconst) == FAIL)
3431 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003432 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003434 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435
Bram Moolenaar23c55272020-06-21 16:58:13 +02003436 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003437 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003438 *arg = NULL;
3439 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003440 }
3441
3442 if (**arg == '}')
3443 break;
3444
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003445 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003447 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003449 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003450 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003451 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003454 if (isn->isn_type == ISN_PUSHNR)
3455 {
3456 char buf[NUMBUFLEN];
3457
3458 // Convert to string at compile time.
3459 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3460 isn->isn_type = ISN_PUSHS;
3461 isn->isn_arg.string = vim_strsave((char_u *)buf);
3462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 if (isn->isn_type == ISN_PUSHS)
3464 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003465 else if (may_generate_2STRING(-1, cctx) == FAIL)
3466 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003467 *arg = skipwhite(*arg);
3468 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003469 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003470 emsg(_(e_missing_matching_bracket_after_dict_key));
3471 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003472 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003473 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003475 else
3476 {
3477 // {"name": value},
3478 // {'name': value},
3479 // {name: value} use "name" as a literal key
3480 key = get_literal_key(arg);
3481 if (key == NULL)
3482 return FAIL;
3483 if (generate_PUSHS(cctx, key) == FAIL)
3484 return FAIL;
3485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486
3487 // Check for duplicate keys, if using string keys.
3488 if (key != NULL)
3489 {
3490 item = dict_find(d, key, -1);
3491 if (item != NULL)
3492 {
3493 semsg(_(e_duplicate_key), key);
3494 goto failret;
3495 }
3496 item = dictitem_alloc(key);
3497 if (item != NULL)
3498 {
3499 item->di_tv.v_type = VAR_UNKNOWN;
3500 item->di_tv.v_lock = 0;
3501 if (dict_add(d, item) == FAIL)
3502 dictitem_free(item);
3503 }
3504 }
3505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 if (**arg != ':')
3507 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003508 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003509 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003510 else
3511 semsg(_(e_missing_dict_colon), *arg);
3512 return FAIL;
3513 }
3514 whitep = *arg + 1;
3515 if (!IS_WHITE_OR_NUL(*whitep))
3516 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003517 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 return FAIL;
3519 }
3520
Bram Moolenaar23c55272020-06-21 16:58:13 +02003521 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003522 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003523 *arg = NULL;
3524 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003525 }
3526
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003527 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003529 if (!is_const)
3530 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 ++count;
3532
Bram Moolenaar2c330432020-04-13 14:41:35 +02003533 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003534 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003535 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003536 *arg = NULL;
3537 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 if (**arg == '}')
3540 break;
3541 if (**arg != ',')
3542 {
3543 semsg(_(e_missing_dict_comma), *arg);
3544 goto failret;
3545 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003546 if (IS_WHITE_OR_NUL(*whitep))
3547 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003548 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003549 return FAIL;
3550 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003551 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003552 if (!IS_WHITE_OR_NUL(*whitep))
3553 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003554 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003555 return FAIL;
3556 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003557 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 }
3559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 *arg = *arg + 1;
3561
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003563 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003564 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003565 *arg += STRLEN(*arg);
3566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 return generate_NEWDICT(cctx, count);
3570
3571failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003572 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003573 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003574 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003575 *arg = (char_u *)"";
3576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 dict_unref(d);
3578 return FAIL;
3579}
3580
3581/*
3582 * Compile "&option".
3583 */
3584 static int
3585compile_get_option(char_u **arg, cctx_T *cctx)
3586{
3587 typval_T rettv;
3588 char_u *start = *arg;
3589 int ret;
3590
3591 // parse the option and get the current value to get the type.
3592 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003593 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 if (ret == OK)
3595 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003596 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003597 char_u *name = vim_strnsave(start, *arg - start);
3598 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3599 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600
3601 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3602 vim_free(name);
3603 }
3604 clear_tv(&rettv);
3605
3606 return ret;
3607}
3608
3609/*
3610 * Compile "$VAR".
3611 */
3612 static int
3613compile_get_env(char_u **arg, cctx_T *cctx)
3614{
3615 char_u *start = *arg;
3616 int len;
3617 int ret;
3618 char_u *name;
3619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 ++*arg;
3621 len = get_env_len(arg);
3622 if (len == 0)
3623 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003624 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 return FAIL;
3626 }
3627
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003628 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 name = vim_strnsave(start, len + 1);
3630 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3631 vim_free(name);
3632 return ret;
3633}
3634
3635/*
3636 * Compile "@r".
3637 */
3638 static int
3639compile_get_register(char_u **arg, cctx_T *cctx)
3640{
3641 int ret;
3642
3643 ++*arg;
3644 if (**arg == NUL)
3645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003646 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 return FAIL;
3648 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003649 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
3651 emsg_invreg(**arg);
3652 return FAIL;
3653 }
3654 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3655 ++*arg;
3656 return ret;
3657}
3658
3659/*
3660 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003661 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 */
3663 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003664apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003666 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
3668 // this works from end to start
3669 while (p > start)
3670 {
3671 --p;
3672 if (*p == '-' || *p == '+')
3673 {
3674 // only '-' has an effect, for '+' we only check the type
3675#ifdef FEAT_FLOAT
3676 if (rettv->v_type == VAR_FLOAT)
3677 {
3678 if (*p == '-')
3679 rettv->vval.v_float = -rettv->vval.v_float;
3680 }
3681 else
3682#endif
3683 {
3684 varnumber_T val;
3685 int error = FALSE;
3686
3687 // tv_get_number_chk() accepts a string, but we don't want that
3688 // here
3689 if (check_not_string(rettv) == FAIL)
3690 return FAIL;
3691 val = tv_get_number_chk(rettv, &error);
3692 clear_tv(rettv);
3693 if (error)
3694 return FAIL;
3695 if (*p == '-')
3696 val = -val;
3697 rettv->v_type = VAR_NUMBER;
3698 rettv->vval.v_number = val;
3699 }
3700 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003701 else if (numeric_only)
3702 {
3703 ++p;
3704 break;
3705 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003706 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 {
3708 int v = tv2bool(rettv);
3709
3710 // '!' is permissive in the type.
3711 clear_tv(rettv);
3712 rettv->v_type = VAR_BOOL;
3713 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3714 }
3715 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003716 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 return OK;
3718}
3719
3720/*
3721 * Recognize v: variables that are constants and set "rettv".
3722 */
3723 static void
3724get_vim_constant(char_u **arg, typval_T *rettv)
3725{
3726 if (STRNCMP(*arg, "v:true", 6) == 0)
3727 {
3728 rettv->v_type = VAR_BOOL;
3729 rettv->vval.v_number = VVAL_TRUE;
3730 *arg += 6;
3731 }
3732 else if (STRNCMP(*arg, "v:false", 7) == 0)
3733 {
3734 rettv->v_type = VAR_BOOL;
3735 rettv->vval.v_number = VVAL_FALSE;
3736 *arg += 7;
3737 }
3738 else if (STRNCMP(*arg, "v:null", 6) == 0)
3739 {
3740 rettv->v_type = VAR_SPECIAL;
3741 rettv->vval.v_number = VVAL_NULL;
3742 *arg += 6;
3743 }
3744 else if (STRNCMP(*arg, "v:none", 6) == 0)
3745 {
3746 rettv->v_type = VAR_SPECIAL;
3747 rettv->vval.v_number = VVAL_NONE;
3748 *arg += 6;
3749 }
3750}
3751
Bram Moolenaar657137c2021-01-09 15:45:23 +01003752 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003753get_compare_type(char_u *p, int *len, int *type_is)
3754{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003755 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003756 int i;
3757
3758 switch (p[0])
3759 {
3760 case '=': if (p[1] == '=')
3761 type = EXPR_EQUAL;
3762 else if (p[1] == '~')
3763 type = EXPR_MATCH;
3764 break;
3765 case '!': if (p[1] == '=')
3766 type = EXPR_NEQUAL;
3767 else if (p[1] == '~')
3768 type = EXPR_NOMATCH;
3769 break;
3770 case '>': if (p[1] != '=')
3771 {
3772 type = EXPR_GREATER;
3773 *len = 1;
3774 }
3775 else
3776 type = EXPR_GEQUAL;
3777 break;
3778 case '<': if (p[1] != '=')
3779 {
3780 type = EXPR_SMALLER;
3781 *len = 1;
3782 }
3783 else
3784 type = EXPR_SEQUAL;
3785 break;
3786 case 'i': if (p[1] == 's')
3787 {
3788 // "is" and "isnot"; but not a prefix of a name
3789 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3790 *len = 5;
3791 i = p[*len];
3792 if (!isalnum(i) && i != '_')
3793 {
3794 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3795 *type_is = TRUE;
3796 }
3797 }
3798 break;
3799 }
3800 return type;
3801}
3802
3803/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003804 * Skip over an expression, ignoring most errors.
3805 */
3806 static void
3807skip_expr_cctx(char_u **arg, cctx_T *cctx)
3808{
3809 evalarg_T evalarg;
3810
3811 CLEAR_FIELD(evalarg);
3812 evalarg.eval_cctx = cctx;
3813 skip_expr(arg, &evalarg);
3814}
3815
3816/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003818 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 */
3820 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003821compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003823 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
3825 // this works from end to start
3826 while (p > start)
3827 {
3828 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003829 while (VIM_ISWHITE(*p))
3830 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 if (*p == '-' || *p == '+')
3832 {
3833 int negate = *p == '-';
3834 isn_T *isn;
3835
3836 // TODO: check type
3837 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3838 {
3839 --p;
3840 if (*p == '-')
3841 negate = !negate;
3842 }
3843 // only '-' has an effect, for '+' we only check the type
3844 if (negate)
3845 isn = generate_instr(cctx, ISN_NEGATENR);
3846 else
3847 isn = generate_instr(cctx, ISN_CHECKNR);
3848 if (isn == NULL)
3849 return FAIL;
3850 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003851 else if (numeric_only)
3852 {
3853 ++p;
3854 break;
3855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 else
3857 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003858 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003860 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003862 if (p[-1] == '!')
3863 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 }
3866 if (generate_2BOOL(cctx, invert) == FAIL)
3867 return FAIL;
3868 }
3869 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003870 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 return OK;
3872}
3873
3874/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003875 * Compile "(expression)": recursive!
3876 * Return FAIL/OK.
3877 */
3878 static int
3879compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3880{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003881 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003882 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003883
Bram Moolenaar24156692021-01-14 20:35:49 +01003884 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3885 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003886 if (ppconst->pp_used <= PPSIZE - 10)
3887 {
3888 ret = compile_expr1(arg, cctx, ppconst);
3889 }
3890 else
3891 {
3892 // Not enough space in ppconst, flush constants.
3893 if (generate_ppconst(cctx, ppconst) == FAIL)
3894 return FAIL;
3895 ret = compile_expr0(arg, cctx);
3896 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003897 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3898 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003899 if (**arg == ')')
3900 ++*arg;
3901 else if (ret == OK)
3902 {
3903 emsg(_(e_missing_close));
3904 ret = FAIL;
3905 }
3906 return ret;
3907}
3908
3909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003911 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 */
3913 static int
3914compile_subscript(
3915 char_u **arg,
3916 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003917 char_u *start_leader,
3918 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003919 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003921 char_u *name_start = *end_leader;
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 for (;;)
3924 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003925 char_u *p = skipwhite(*arg);
3926
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003927 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003928 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003929 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003930
3931 // If a following line starts with "->{" or "->X" advance to that
3932 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003933 // Also if a following line starts with ".x".
3934 if (next != NULL &&
3935 ((next[0] == '-' && next[1] == '>'
3936 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003937 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003938 {
3939 next = next_line_from_context(cctx, TRUE);
3940 if (next == NULL)
3941 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003942 *arg = next;
3943 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003944 }
3945 }
3946
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003947 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003948 // not a function call.
3949 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003951 garray_T *stack = &cctx->ctx_type_stack;
3952 type_T *type;
3953 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003955 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003956 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003957 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003960 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3961
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003962 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3964 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003965 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 return FAIL;
3967 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003968 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003970 char_u *pstart = p;
3971
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003972 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003974 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 // something->method()
3977 // Apply the '!', '-' and '+' first:
3978 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003979 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003982 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003983 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003984 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003985 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003986 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003987 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003988 garray_T *stack = &cctx->ctx_type_stack;
3989 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003990 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003991 int expr_isn_start = cctx->ctx_instr.ga_len;
3992 int expr_isn_end;
3993 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003994
3995 // Funcref call: list->(Refs[2])(arg)
3996 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003997 //
3998 // Fist compile the function expression.
3999 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004000 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004001
4002 // Remember the next instruction index, where the instructions
4003 // for arguments are being written.
4004 expr_isn_end = cctx->ctx_instr.ga_len;
4005
4006 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004007 if (**arg != '(')
4008 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004009 if (*skipwhite(*arg) == '(')
4010 emsg(_(e_nowhitespace));
4011 else
4012 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004013 return FAIL;
4014 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004015 *arg = skipwhite(*arg + 1);
4016 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4017 return FAIL;
4018
Bram Moolenaar2927c072021-04-05 19:41:21 +02004019 // Move the instructions for the arguments to before the
4020 // instructions of the expression and move the type of the
4021 // expression after the argument types. This is what ISN_PCALL
4022 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004023 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004024 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4025 if (arg_isn_count > 0)
4026 {
4027 int expr_isn_count = expr_isn_end - expr_isn_start;
4028 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4029
4030 if (isn == NULL)
4031 return FAIL;
4032 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4033 + expr_isn_start,
4034 sizeof(isn_T) * expr_isn_count);
4035 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4036 + expr_isn_start,
4037 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4038 sizeof(isn_T) * arg_isn_count);
4039 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4040 + expr_isn_start + arg_isn_count,
4041 isn, sizeof(isn_T) * expr_isn_count);
4042 vim_free(isn);
4043
4044 type = ((type_T **)stack->ga_data)[type_idx_start];
4045 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4046 ((type_T **)stack->ga_data) + type_idx_start + 1,
4047 sizeof(type_T *)
4048 * (stack->ga_len - type_idx_start - 1));
4049 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4050 }
4051
Bram Moolenaar7e368202020-12-25 21:56:57 +01004052 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4053 if (generate_PCALL(cctx, argcount,
4054 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 return FAIL;
4056 }
4057 else
4058 {
4059 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004060 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004061 if (!eval_isnamec1(*p))
4062 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004063 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004064 return FAIL;
4065 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004066 if (ASCII_ISALPHA(*p) && p[1] == ':')
4067 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004068 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 ;
4070 if (*p != '(')
4071 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004072 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
4075 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004080 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004083
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004084 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004085 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004086 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004087 // TODO: blob index
4088 // TODO: more arguments
4089 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004090 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004091 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004092 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004093
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004094 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004095 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004096 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004097 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004098 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004099 // missing first index is equal to zero
4100 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004101 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004102 else
4103 {
4104 if (compile_expr0(arg, cctx) == FAIL)
4105 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004106 if (**arg == ':')
4107 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004108 semsg(_(e_white_space_required_before_and_after_str_at_str),
4109 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004110 return FAIL;
4111 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004112 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004113 return FAIL;
4114 *arg = skipwhite(*arg);
4115 }
4116 if (**arg == ':')
4117 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004118 is_slice = TRUE;
4119 ++*arg;
4120 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4121 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004122 semsg(_(e_white_space_required_before_and_after_str_at_str),
4123 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004124 return FAIL;
4125 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004126 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004127 return FAIL;
4128 if (**arg == ']')
4129 // missing second index is equal to end of string
4130 generate_PUSHNR(cctx, -1);
4131 else
4132 {
4133 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004134 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004135 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004136 return FAIL;
4137 *arg = skipwhite(*arg);
4138 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
4141 if (**arg != ']')
4142 {
4143 emsg(_(e_missbrac));
4144 return FAIL;
4145 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004146 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147
Bram Moolenaare42939a2021-04-05 17:11:17 +02004148 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004149 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004151 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004153 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004154 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004155 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004156 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004157
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004158 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004159 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004160 {
4161 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004162 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004163 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004164 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004165 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 while (eval_isnamec(*p))
4167 MB_PTR_ADV(p);
4168 if (p == *arg)
4169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004170 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 return FAIL;
4172 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004173 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return FAIL;
4175 *arg = p;
4176 }
4177 else
4178 break;
4179 }
4180
4181 // TODO - see handle_subscript():
4182 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4183 // Don't do this when "Func" is already a partial that was bound
4184 // explicitly (pt_auto is FALSE).
4185
4186 return OK;
4187}
4188
4189/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004190 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4191 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004193 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 *
4196 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 */
4198
4199/*
4200 * number number constant
4201 * 0zFFFFFFFF Blob constant
4202 * "string" string constant
4203 * 'string' literal string constant
4204 * &option-name option value
4205 * @r register contents
4206 * identifier variable value
4207 * function() function call
4208 * $VAR environment variable
4209 * (expression) nested expression
4210 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004211 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 *
4213 * Also handle:
4214 * ! in front logical NOT
4215 * - in front unary minus
4216 * + in front unary plus (ignored)
4217 * trailing (arg) funcref/partial call
4218 * trailing [] subscript in String or List
4219 * trailing .name entry in Dictionary
4220 * trailing ->name() method call
4221 */
4222 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004223compile_expr7(
4224 char_u **arg,
4225 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004226 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 char_u *start_leader, *end_leader;
4229 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004230 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004231 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004233 ppconst->pp_is_const = FALSE;
4234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 /*
4236 * Skip '!', '-' and '+' characters. They are handled later.
4237 */
4238 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004239 if (eval_leader(arg, TRUE) == FAIL)
4240 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 end_leader = *arg;
4242
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004243 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 switch (**arg)
4245 {
4246 /*
4247 * Number constant.
4248 */
4249 case '0': // also for blob starting with 0z
4250 case '1':
4251 case '2':
4252 case '3':
4253 case '4':
4254 case '5':
4255 case '6':
4256 case '7':
4257 case '8':
4258 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004259 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004261 // Apply "-" and "+" just before the number now, right to
4262 // left. Matters especially when "->" follows. Stops at
4263 // '!'.
4264 if (apply_leader(rettv, TRUE,
4265 start_leader, &end_leader) == FAIL)
4266 {
4267 clear_tv(rettv);
4268 return FAIL;
4269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 break;
4271
4272 /*
4273 * String constant: "string".
4274 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004275 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 return FAIL;
4277 break;
4278
4279 /*
4280 * Literal string constant: 'str''ing'.
4281 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004282 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 return FAIL;
4284 break;
4285
4286 /*
4287 * Constant Vim variable.
4288 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 ret = NOTDONE;
4291 break;
4292
4293 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294 * "true" constant
4295 */
4296 case 't': if (STRNCMP(*arg, "true", 4) == 0
4297 && !eval_isnamec((*arg)[4]))
4298 {
4299 *arg += 4;
4300 rettv->v_type = VAR_BOOL;
4301 rettv->vval.v_number = VVAL_TRUE;
4302 }
4303 else
4304 ret = NOTDONE;
4305 break;
4306
4307 /*
4308 * "false" constant
4309 */
4310 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4311 && !eval_isnamec((*arg)[5]))
4312 {
4313 *arg += 5;
4314 rettv->v_type = VAR_BOOL;
4315 rettv->vval.v_number = VVAL_FALSE;
4316 }
4317 else
4318 ret = NOTDONE;
4319 break;
4320
4321 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004322 * "null" constant
4323 */
4324 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004325 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004326 {
4327 *arg += 4;
4328 rettv->v_type = VAR_SPECIAL;
4329 rettv->vval.v_number = VVAL_NULL;
4330 }
4331 else
4332 ret = NOTDONE;
4333 break;
4334
4335 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 * List: [expr, expr]
4337 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004338 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4339 return FAIL;
4340 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 break;
4342
4343 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 * Dictionary: {'key': val, 'key': val}
4345 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004346 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4347 return FAIL;
4348 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 break;
4350
4351 /*
4352 * Option value: &name
4353 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004354 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4355 return FAIL;
4356 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 break;
4358
4359 /*
4360 * Environment variable: $VAR.
4361 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004362 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4363 return FAIL;
4364 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 break;
4366
4367 /*
4368 * Register contents: @r.
4369 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004370 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4371 return FAIL;
4372 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 break;
4374 /*
4375 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004376 * lambda: (arg, arg) => expr
4377 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004379 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4380 ret = compile_lambda(arg, cctx);
4381 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004382 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 break;
4384
4385 default: ret = NOTDONE;
4386 break;
4387 }
4388 if (ret == FAIL)
4389 return FAIL;
4390
Bram Moolenaar1c747212020-05-09 18:28:34 +02004391 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004393 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 clear_tv(rettv);
4395 else
4396 // A constant expression can possibly be handled compile time,
4397 // return the value instead of generating code.
4398 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 else if (ret == NOTDONE)
4401 {
4402 char_u *p;
4403 int r;
4404
4405 if (!eval_isnamec1(**arg))
4406 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004407 if (!vim9_bad_comment(*arg))
4408 {
4409 if (ends_excmd(*skipwhite(*arg)))
4410 semsg(_(e_empty_expression_str), *arg);
4411 else
4412 semsg(_(e_name_expected_str), *arg);
4413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415 }
4416
4417 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004418 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 {
4421 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004423 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004424 {
4425 if (generate_ppconst(cctx, ppconst) == FAIL)
4426 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004427 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 if (r == FAIL)
4430 return FAIL;
4431 }
4432
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004433 // Handle following "[]", ".member", etc.
4434 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004435 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436 ppconst) == FAIL)
4437 return FAIL;
4438 if (ppconst->pp_used > 0)
4439 {
4440 // apply the '!', '-' and '+' before the constant
4441 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004442 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004443 return FAIL;
4444 return OK;
4445 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004446 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004448 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449}
4450
4451/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004452 * Give the "white on both sides" error, taking the operator from "p[len]".
4453 */
4454 void
4455error_white_both(char_u *op, int len)
4456{
4457 char_u buf[10];
4458
4459 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004460 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004461}
4462
4463/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004464 * <type>expr7: runtime type check / conversion
4465 */
4466 static int
4467compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4468{
4469 type_T *want_type = NULL;
4470
4471 // Recognize <type>
4472 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4473 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004474 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004475 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4476 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004477 return FAIL;
4478
4479 if (**arg != '>')
4480 {
4481 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004482 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004483 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004484 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004485 return FAIL;
4486 }
4487 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004488 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004489 return FAIL;
4490 }
4491
4492 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4493 return FAIL;
4494
4495 if (want_type != NULL)
4496 {
4497 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004498 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004499 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004500
Bram Moolenaard1103582020-08-14 22:44:25 +02004501 generate_ppconst(cctx, ppconst);
4502 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004503 where.wt_index = 0;
4504 where.wt_variable = FALSE;
4505 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004506 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004507 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004508 return FAIL;
4509 }
4510 }
4511
4512 return OK;
4513}
4514
4515/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 * * number multiplication
4517 * / number division
4518 * % number modulo
4519 */
4520 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522{
4523 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004524 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004525 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004527 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004528 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 return FAIL;
4530
4531 /*
4532 * Repeat computing, until no "*", "/" or "%" is following.
4533 */
4534 for (;;)
4535 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004536 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 if (*op != '*' && *op != '/' && *op != '%')
4538 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004539 if (next != NULL)
4540 {
4541 *arg = next_line_from_context(cctx, TRUE);
4542 op = skipwhite(*arg);
4543 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004544
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004545 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004547 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004548 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004550 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004553 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004554 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004556
4557 if (ppconst->pp_used == ppconst_used + 2
4558 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4559 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004560 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004561 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4562 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4563 varnumber_T res = 0;
4564 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004566 // both are numbers: compute the result
4567 switch (*op)
4568 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004569 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004570 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004571 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004572 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004573 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004574 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004575 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 break;
4577 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004578 if (failed)
4579 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004580 tv1->vval.v_number = res;
4581 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004582 }
4583 else
4584 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004585 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004586 generate_two_op(cctx, op);
4587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 }
4589
4590 return OK;
4591}
4592
4593/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004594 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 * - number subtraction
4596 * .. string concatenation
4597 */
4598 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600{
4601 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004602 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004604 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
4606 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004607 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 return FAIL;
4609
4610 /*
4611 * Repeat computing, until no "+", "-" or ".." is following.
4612 */
4613 for (;;)
4614 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004615 op = may_peek_next_line(cctx, *arg, &next);
4616 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 break;
4618 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004619 if (next != NULL)
4620 {
4621 *arg = next_line_from_context(cctx, TRUE);
4622 op = skipwhite(*arg);
4623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004625 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004626 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004627 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 }
4630
Bram Moolenaare0de1712020-12-02 17:36:54 +01004631 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004632 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004634 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 return FAIL;
4637
Bram Moolenaara5565e42020-05-09 15:44:01 +02004638 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004639 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004640 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4641 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4642 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4643 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4646 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004647
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004648 // concat/subtract/add constant numbers
4649 if (*op == '+')
4650 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4651 else if (*op == '-')
4652 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4653 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004654 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004655 // concatenate constant strings
4656 char_u *s1 = tv1->vval.v_string;
4657 char_u *s2 = tv2->vval.v_string;
4658 size_t len1 = STRLEN(s1);
4659
4660 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4661 if (tv1->vval.v_string == NULL)
4662 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004663 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004664 return FAIL;
4665 }
4666 mch_memmove(tv1->vval.v_string, s1, len1);
4667 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004668 vim_free(s1);
4669 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004670 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004671 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 }
4673 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004674 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004676 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004677 if (*op == '.')
4678 {
4679 if (may_generate_2STRING(-2, cctx) == FAIL
4680 || may_generate_2STRING(-1, cctx) == FAIL)
4681 return FAIL;
4682 generate_instr_drop(cctx, ISN_CONCAT, 1);
4683 }
4684 else
4685 generate_two_op(cctx, op);
4686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 }
4688
4689 return OK;
4690}
4691
4692/*
4693 * expr5a == expr5b
4694 * expr5a =~ expr5b
4695 * expr5a != expr5b
4696 * expr5a !~ expr5b
4697 * expr5a > expr5b
4698 * expr5a >= expr5b
4699 * expr5a < expr5b
4700 * expr5a <= expr5b
4701 * expr5a is expr5b
4702 * expr5a isnot expr5b
4703 *
4704 * Produces instructions:
4705 * EVAL expr5a Push result of "expr5a"
4706 * EVAL expr5b Push result of "expr5b"
4707 * COMPARE one of the compare instructions
4708 */
4709 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004712 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004714 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718
4719 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 return FAIL;
4722
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004723 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004724 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725
4726 /*
4727 * If there is a comparative operator, use it.
4728 */
4729 if (type != EXPR_UNKNOWN)
4730 {
4731 int ic = FALSE; // Default: do not ignore case
4732
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004733 if (next != NULL)
4734 {
4735 *arg = next_line_from_context(cctx, TRUE);
4736 p = skipwhite(*arg);
4737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 if (type_is && (p[len] == '?' || p[len] == '#'))
4739 {
4740 semsg(_(e_invexpr2), *arg);
4741 return FAIL;
4742 }
4743 // extra question mark appended: ignore case
4744 if (p[len] == '?')
4745 {
4746 ic = TRUE;
4747 ++len;
4748 }
4749 // extra '#' appended: match case (ignored)
4750 else if (p[len] == '#')
4751 ++len;
4752 // nothing appended: match case
4753
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004754 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004756 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 }
4759
4760 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004761 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004762 return FAIL;
4763
Bram Moolenaara5565e42020-05-09 15:44:01 +02004764 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 return FAIL;
4766
Bram Moolenaara5565e42020-05-09 15:44:01 +02004767 if (ppconst->pp_used == ppconst_used + 2)
4768 {
4769 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4770 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4771 int ret;
4772
4773 // Both sides are a constant, compute the result now.
4774 // First check for a valid combination of types, this is more
4775 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004776 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 ret = FAIL;
4778 else
4779 {
4780 ret = typval_compare(tv1, tv2, type, ic);
4781 tv1->v_type = VAR_BOOL;
4782 tv1->vval.v_number = tv1->vval.v_number
4783 ? VVAL_TRUE : VVAL_FALSE;
4784 clear_tv(tv2);
4785 --ppconst->pp_used;
4786 }
4787 return ret;
4788 }
4789
4790 generate_ppconst(cctx, ppconst);
4791 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 }
4793
4794 return OK;
4795}
4796
Bram Moolenaar7f141552020-05-09 17:35:53 +02004797static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799/*
4800 * Compile || or &&.
4801 */
4802 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004803compile_and_or(
4804 char_u **arg,
4805 cctx_T *cctx,
4806 char *op,
4807 ppconst_T *ppconst,
4808 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004810 char_u *next;
4811 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004812 int opchar = *op;
4813
4814 if (p[0] == opchar && p[1] == opchar)
4815 {
4816 garray_T *instr = &cctx->ctx_instr;
4817 garray_T end_ga;
4818
4819 /*
4820 * Repeat until there is no following "||" or "&&"
4821 */
4822 ga_init2(&end_ga, sizeof(int), 10);
4823 while (p[0] == opchar && p[1] == opchar)
4824 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004825 long start_lnum = SOURCING_LNUM;
4826 int start_ctx_lnum = cctx->ctx_lnum;
4827 int save_lnum;
4828
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004829 if (next != NULL)
4830 {
4831 *arg = next_line_from_context(cctx, TRUE);
4832 p = skipwhite(*arg);
4833 }
4834
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004835 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4836 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004837 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004838 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004839 return FAIL;
4840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004842 // TODO: use ppconst if the value is a constant and check
4843 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 generate_ppconst(cctx, ppconst);
4845
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004846 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004847 SOURCING_LNUM = start_lnum;
4848 save_lnum = cctx->ctx_lnum;
4849 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004850 if (bool_on_stack(cctx) == FAIL)
4851 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004852 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004853 ga_clear(&end_ga);
4854 return FAIL;
4855 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004856 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 if (ga_grow(&end_ga, 1) == FAIL)
4859 {
4860 ga_clear(&end_ga);
4861 return FAIL;
4862 }
4863 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4864 ++end_ga.ga_len;
4865 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004866 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867
4868 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004869 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004870 {
4871 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004872 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004873 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004874
Bram Moolenaara5565e42020-05-09 15:44:01 +02004875 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4876 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 {
4878 ga_clear(&end_ga);
4879 return FAIL;
4880 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004881
4882 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004884 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004886 // Every part must evaluate to a bool.
4887 if (bool_on_stack(cctx) == FAIL)
4888 {
4889 ga_clear(&end_ga);
4890 return FAIL;
4891 }
4892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 // Fill in the end label in all jumps.
4894 while (end_ga.ga_len > 0)
4895 {
4896 isn_T *isn;
4897
4898 --end_ga.ga_len;
4899 isn = ((isn_T *)instr->ga_data)
4900 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4901 isn->isn_arg.jump.jump_where = instr->ga_len;
4902 }
4903 ga_clear(&end_ga);
4904 }
4905
4906 return OK;
4907}
4908
4909/*
4910 * expr4a && expr4a && expr4a logical AND
4911 *
4912 * Produces instructions:
4913 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004914 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004915 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004917 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 * EVAL expr4c Push result of "expr4c"
4919 * end:
4920 */
4921 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 int ppconst_used = ppconst->pp_used;
4925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004927 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 return FAIL;
4929
4930 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932}
4933
4934/*
4935 * expr3a || expr3b || expr3c logical OR
4936 *
4937 * Produces instructions:
4938 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004939 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004940 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004942 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 * EVAL expr3c Push result of "expr3c"
4944 * end:
4945 */
4946 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004949 int ppconst_used = ppconst->pp_used;
4950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004952 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 return FAIL;
4954
4955 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004956 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957}
4958
4959/*
4960 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004962 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 * JUMP_IF_FALSE alt jump if false
4964 * EVAL expr1a
4965 * JUMP_ALWAYS end
4966 * alt: EVAL expr1b
4967 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004968 *
4969 * Toplevel expression: expr2 ?? expr1
4970 * Produces instructions:
4971 * EVAL expr2 Push result of "expr2"
4972 * JUMP_AND_KEEP_IF_TRUE end jump if true
4973 * EVAL expr1
4974 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 */
4976 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004977compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978{
4979 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004980 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004981 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982
Bram Moolenaar3988f642020-08-27 22:43:03 +02004983 // Ignore all kinds of errors when not producing code.
4984 if (cctx->ctx_skip == SKIP_YES)
4985 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004986 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004987 return OK;
4988 }
4989
Bram Moolenaar61a89812020-05-07 16:58:17 +02004990 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004991 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 return FAIL;
4993
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004994 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 if (*p == '?')
4996 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004997 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 garray_T *instr = &cctx->ctx_instr;
4999 garray_T *stack = &cctx->ctx_type_stack;
5000 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005001 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005003 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 int has_const_expr = FALSE;
5005 int const_value = FALSE;
5006 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005008 if (next != NULL)
5009 {
5010 *arg = next_line_from_context(cctx, TRUE);
5011 p = skipwhite(*arg);
5012 }
5013
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005014 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005015 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005016 semsg(_(e_white_space_required_before_and_after_str_at_str),
5017 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005018 return FAIL;
5019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020
Bram Moolenaara5565e42020-05-09 15:44:01 +02005021 if (ppconst->pp_used == ppconst_used + 1)
5022 {
5023 // the condition is a constant, we know whether the ? or the :
5024 // expression is to be evaluated.
5025 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005026 if (op_falsy)
5027 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5028 else
5029 {
5030 int error = FALSE;
5031
5032 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5033 &error);
5034 if (error)
5035 return FAIL;
5036 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005037 cctx->ctx_skip = save_skip == SKIP_YES ||
5038 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5039
5040 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5041 // "left ?? right" and "left" is truthy: produce "left"
5042 generate_ppconst(cctx, ppconst);
5043 else
5044 {
5045 clear_tv(&ppconst->pp_tv[ppconst_used]);
5046 --ppconst->pp_used;
5047 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005048 }
5049 else
5050 {
5051 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005052 if (op_falsy)
5053 end_idx = instr->ga_len;
5054 generate_JUMP(cctx, op_falsy
5055 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5056 if (op_falsy)
5057 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059
5060 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005061 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005062 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005063 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005064 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065
Bram Moolenaara5565e42020-05-09 15:44:01 +02005066 if (!has_const_expr)
5067 {
5068 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005070 if (!op_falsy)
5071 {
5072 // remember the type and drop it
5073 --stack->ga_len;
5074 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005076 end_idx = instr->ga_len;
5077 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005078
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005079 // jump here from JUMP_IF_FALSE
5080 isn = ((isn_T *)instr->ga_data) + alt_idx;
5081 isn->isn_arg.jump.jump_where = instr->ga_len;
5082 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005085 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005087 // Check for the ":".
5088 p = may_peek_next_line(cctx, *arg, &next);
5089 if (*p != ':')
5090 {
5091 emsg(_(e_missing_colon));
5092 return FAIL;
5093 }
5094 if (next != NULL)
5095 {
5096 *arg = next_line_from_context(cctx, TRUE);
5097 p = skipwhite(*arg);
5098 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005099
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005100 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5101 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005102 semsg(_(e_white_space_required_before_and_after_str_at_str),
5103 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005104 return FAIL;
5105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005107 // evaluate the third expression
5108 if (has_const_expr)
5109 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005110 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005111 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005112 return FAIL;
5113 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5114 return FAIL;
5115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaara5565e42020-05-09 15:44:01 +02005117 if (!has_const_expr)
5118 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005119 type_T **typep;
5120
Bram Moolenaara5565e42020-05-09 15:44:01 +02005121 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122
Bram Moolenaara5565e42020-05-09 15:44:01 +02005123 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005124 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5125 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005126
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005127 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005128 isn = ((isn_T *)instr->ga_data) + end_idx;
5129 isn->isn_arg.jump.jump_where = instr->ga_len;
5130 }
5131
5132 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 }
5134 return OK;
5135}
5136
5137/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005139 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5140 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005141 */
5142 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005143compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005144{
5145 ppconst_T ppconst;
5146
5147 CLEAR_FIELD(ppconst);
5148 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5149 {
5150 clear_ppconst(&ppconst);
5151 return FAIL;
5152 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005153 if (is_const != NULL)
5154 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005155 if (generate_ppconst(cctx, &ppconst) == FAIL)
5156 return FAIL;
5157 return OK;
5158}
5159
5160/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005161 * Toplevel expression.
5162 */
5163 static int
5164compile_expr0(char_u **arg, cctx_T *cctx)
5165{
5166 return compile_expr0_ext(arg, cctx, NULL);
5167}
5168
5169/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 * compile "return [expr]"
5171 */
5172 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005173compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174{
5175 char_u *p = arg;
5176 garray_T *stack = &cctx->ctx_type_stack;
5177 type_T *stack_type;
5178
5179 if (*p != NUL && *p != '|' && *p != '\n')
5180 {
5181 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005182 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 return NULL;
5184
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005185 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005186 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005187 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005188 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005189 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5190 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005191 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005192 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005193 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005194 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005195 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005196 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5197 && stack_type->tt_type != VAR_VOID
5198 && stack_type->tt_type != VAR_UNKNOWN)
5199 {
5200 emsg(_(e_returning_value_in_function_without_return_type));
5201 return NULL;
5202 }
5203 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005204 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005205 return NULL;
5206 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 }
5209 else
5210 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005211 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005212 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005213 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5214 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005216 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217 return NULL;
5218 }
5219
5220 // No argument, return zero.
5221 generate_PUSHNR(cctx, 0);
5222 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005223
5224 // Undo any command modifiers.
5225 generate_undo_cmdmods(cctx);
5226
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005227 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 return NULL;
5229
5230 // "return val | endif" is possible
5231 return skipwhite(p);
5232}
5233
5234/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005235 * Get a line from the compilation context, compatible with exarg_T getline().
5236 * Return a pointer to the line in allocated memory.
5237 * Return NULL for end-of-file or some error.
5238 */
5239 static char_u *
5240exarg_getline(
5241 int c UNUSED,
5242 void *cookie,
5243 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005244 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005245{
5246 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005247 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005248
Bram Moolenaar66250c92020-08-20 15:02:42 +02005249 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005250 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005251 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005252 return NULL;
5253 ++cctx->ctx_lnum;
5254 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5255 // Comment lines result in NULL pointers, skip them.
5256 if (p != NULL)
5257 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005258 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005259}
5260
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005261 void
5262fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5263{
5264 eap->getline = exarg_getline;
5265 eap->cookie = cctx;
5266}
5267
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268/*
5269 * Compile a nested :def command.
5270 */
5271 static char_u *
5272compile_nested_function(exarg_T *eap, cctx_T *cctx)
5273{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005274 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005275 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005276 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005277 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005278 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005279 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005280
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005281 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005282 {
5283 emsg(_(e_cannot_use_bang_with_nested_def));
5284 return NULL;
5285 }
5286
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005287 if (*name_start == '/')
5288 {
5289 name_end = skip_regexp(name_start + 1, '/', TRUE);
5290 if (*name_end == '/')
5291 ++name_end;
5292 eap->nextcmd = check_nextcmd(name_end);
5293 }
5294 if (name_end == name_start || *skipwhite(name_end) != '(')
5295 {
5296 if (!ends_excmd2(name_start, name_end))
5297 {
5298 semsg(_(e_invalid_command_str), eap->cmd);
5299 return NULL;
5300 }
5301
5302 // "def" or "def Name": list functions
5303 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5304 return NULL;
5305 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5306 }
5307
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005308 // Only g:Func() can use a namespace.
5309 if (name_start[1] == ':' && !is_global)
5310 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005311 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005312 return NULL;
5313 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005314 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005315 return NULL;
5316
Bram Moolenaar04b12692020-05-04 23:24:44 +02005317 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005318 fill_exarg_from_cctx(eap, cctx);
5319
Bram Moolenaar04b12692020-05-04 23:24:44 +02005320 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005321 lambda_name = vim_strsave(get_lambda_name());
5322 if (lambda_name == NULL)
5323 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005324 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005325
Bram Moolenaar822ba242020-05-24 23:00:18 +02005326 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005327 {
5328 r = eap->skip ? OK : FAIL;
5329 goto theend;
5330 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005331
5332 // copy over the block scope IDs before compiling
5333 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5334 {
5335 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5336
5337 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5338 if (ufunc->uf_block_ids != NULL)
5339 {
5340 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5341 sizeof(int) * block_depth);
5342 ufunc->uf_block_depth = block_depth;
5343 }
5344 }
5345
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005346 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5347 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005348 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005349 {
5350 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005351 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005352 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005353
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005354 if (is_global)
5355 {
5356 char_u *func_name = vim_strnsave(name_start + 2,
5357 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005358
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005359 if (func_name == NULL)
5360 r = FAIL;
5361 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005362 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005363 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005364 lambda_name = NULL;
5365 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005366 }
5367 else
5368 {
5369 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005370 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005371 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005372
Bram Moolenaareef21022020-08-01 22:16:43 +02005373 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005374 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005375 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005376 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005377 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5378 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005379 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005380
5381theend:
5382 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005383 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005384}
5385
5386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 * Return the length of an assignment operator, or zero if there isn't one.
5388 */
5389 int
5390assignment_len(char_u *p, int *heredoc)
5391{
5392 if (*p == '=')
5393 {
5394 if (p[1] == '<' && p[2] == '<')
5395 {
5396 *heredoc = TRUE;
5397 return 3;
5398 }
5399 return 1;
5400 }
5401 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5402 return 2;
5403 if (STRNCMP(p, "..=", 3) == 0)
5404 return 3;
5405 return 0;
5406}
5407
5408// words that cannot be used as a variable
5409static char *reserved[] = {
5410 "true",
5411 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005412 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 NULL
5414};
5415
Bram Moolenaar752fc692021-01-04 21:57:11 +01005416// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005417typedef enum {
5418 dest_local,
5419 dest_option,
5420 dest_env,
5421 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005422 dest_buffer,
5423 dest_window,
5424 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005425 dest_vimvar,
5426 dest_script,
5427 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005428 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005429} assign_dest_T;
5430
Bram Moolenaar752fc692021-01-04 21:57:11 +01005431// Used by compile_lhs() to store information about the LHS of an assignment
5432// and one argument of ":unlet" with an index.
5433typedef struct {
5434 assign_dest_T lhs_dest; // type of destination
5435
5436 char_u *lhs_name; // allocated name including
5437 // "[expr]" or ".name".
5438 size_t lhs_varlen; // length of the variable without
5439 // "[expr]" or ".name"
5440 char_u *lhs_dest_end; // end of the destination, including
5441 // "[expr]" or ".name".
5442
5443 int lhs_has_index; // has "[expr]" or ".name"
5444
5445 int lhs_new_local; // create new local variable
5446 int lhs_opt_flags; // for when destination is an option
5447 int lhs_vimvaridx; // for when destination is a v:var
5448
5449 lvar_T lhs_local_lvar; // used for existing local destination
5450 lvar_T lhs_arg_lvar; // used for argument destination
5451 lvar_T *lhs_lvar; // points to destination lvar
5452 int lhs_scriptvar_sid;
5453 int lhs_scriptvar_idx;
5454
5455 int lhs_has_type; // type was specified
5456 type_T *lhs_type;
5457 type_T *lhs_member_type;
5458} lhs_T;
5459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005460/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005461 * Generate the load instruction for "name".
5462 */
5463 static void
5464generate_loadvar(
5465 cctx_T *cctx,
5466 assign_dest_T dest,
5467 char_u *name,
5468 lvar_T *lvar,
5469 type_T *type)
5470{
5471 switch (dest)
5472 {
5473 case dest_option:
5474 // TODO: check the option exists
5475 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5476 break;
5477 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005478 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5479 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5480 else
5481 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005482 break;
5483 case dest_buffer:
5484 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5485 break;
5486 case dest_window:
5487 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5488 break;
5489 case dest_tab:
5490 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5491 break;
5492 case dest_script:
5493 compile_load_scriptvar(cctx,
5494 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5495 break;
5496 case dest_env:
5497 // Include $ in the name here
5498 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5499 break;
5500 case dest_reg:
5501 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5502 break;
5503 case dest_vimvar:
5504 generate_LOADV(cctx, name + 2, TRUE);
5505 break;
5506 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005507 if (lvar->lv_from_outer > 0)
5508 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5509 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005510 else
5511 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5512 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005513 case dest_expr:
5514 // list or dict value should already be on the stack.
5515 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005516 }
5517}
5518
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005519/*
5520 * Skip over "[expr]" or ".member".
5521 * Does not check for any errors.
5522 */
5523 static char_u *
5524skip_index(char_u *start)
5525{
5526 char_u *p = start;
5527
5528 if (*p == '[')
5529 {
5530 p = skipwhite(p + 1);
5531 (void)skip_expr(&p, NULL);
5532 p = skipwhite(p);
5533 if (*p == ']')
5534 return p + 1;
5535 return p;
5536 }
5537 // if (*p == '.')
5538 return to_name_end(p + 1, TRUE);
5539}
5540
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005541 void
5542vim9_declare_error(char_u *name)
5543{
5544 char *scope = "";
5545
5546 switch (*name)
5547 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005548 case 'g': scope = _("global"); break;
5549 case 'b': scope = _("buffer"); break;
5550 case 'w': scope = _("window"); break;
5551 case 't': scope = _("tab"); break;
5552 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005553 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005554 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005555 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005556 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005557 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005558 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005559 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005560 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005561 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005562}
5563
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005564/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005565 * For one assignment figure out the type of destination. Return it in "dest".
5566 * When not recognized "dest" is not set.
5567 * For an option "opt_flags" is set.
5568 * For a v:var "vimvaridx" is set.
5569 * "type" is set to the destination type if known, unchanted otherwise.
5570 * Return FAIL if an error message was given.
5571 */
5572 static int
5573get_var_dest(
5574 char_u *name,
5575 assign_dest_T *dest,
5576 int cmdidx,
5577 int *opt_flags,
5578 int *vimvaridx,
5579 type_T **type,
5580 cctx_T *cctx)
5581{
5582 char_u *p;
5583
5584 if (*name == '&')
5585 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005586 int cc;
5587 long numval;
5588 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005589
5590 *dest = dest_option;
5591 if (cmdidx == CMD_final || cmdidx == CMD_const)
5592 {
5593 emsg(_(e_const_option));
5594 return FAIL;
5595 }
5596 p = name;
5597 p = find_option_end(&p, opt_flags);
5598 if (p == NULL)
5599 {
5600 // cannot happen?
5601 emsg(_(e_letunexp));
5602 return FAIL;
5603 }
5604 cc = *p;
5605 *p = NUL;
5606 opt_type = get_option_value(skip_option_env_lead(name),
5607 &numval, NULL, *opt_flags);
5608 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005609 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005610 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005611 case gov_unknown:
5612 semsg(_(e_unknown_option), name);
5613 return FAIL;
5614 case gov_string:
5615 case gov_hidden_string:
5616 *type = &t_string;
5617 break;
5618 case gov_bool:
5619 case gov_hidden_bool:
5620 *type = &t_bool;
5621 break;
5622 case gov_number:
5623 case gov_hidden_number:
5624 *type = &t_number;
5625 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005626 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005627 }
5628 else if (*name == '$')
5629 {
5630 *dest = dest_env;
5631 *type = &t_string;
5632 }
5633 else if (*name == '@')
5634 {
5635 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5636 {
5637 emsg_invreg(name[1]);
5638 return FAIL;
5639 }
5640 *dest = dest_reg;
5641 *type = &t_string;
5642 }
5643 else if (STRNCMP(name, "g:", 2) == 0)
5644 {
5645 *dest = dest_global;
5646 }
5647 else if (STRNCMP(name, "b:", 2) == 0)
5648 {
5649 *dest = dest_buffer;
5650 }
5651 else if (STRNCMP(name, "w:", 2) == 0)
5652 {
5653 *dest = dest_window;
5654 }
5655 else if (STRNCMP(name, "t:", 2) == 0)
5656 {
5657 *dest = dest_tab;
5658 }
5659 else if (STRNCMP(name, "v:", 2) == 0)
5660 {
5661 typval_T *vtv;
5662 int di_flags;
5663
5664 *vimvaridx = find_vim_var(name + 2, &di_flags);
5665 if (*vimvaridx < 0)
5666 {
5667 semsg(_(e_variable_not_found_str), name);
5668 return FAIL;
5669 }
5670 // We use the current value of "sandbox" here, is that OK?
5671 if (var_check_ro(di_flags, name, FALSE))
5672 return FAIL;
5673 *dest = dest_vimvar;
5674 vtv = get_vim_var_tv(*vimvaridx);
5675 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5676 }
5677 return OK;
5678}
5679
5680/*
5681 * Generate a STORE instruction for "dest", not being "dest_local".
5682 * Return FAIL when out of memory.
5683 */
5684 static int
5685generate_store_var(
5686 cctx_T *cctx,
5687 assign_dest_T dest,
5688 int opt_flags,
5689 int vimvaridx,
5690 int scriptvar_idx,
5691 int scriptvar_sid,
5692 type_T *type,
5693 char_u *name)
5694{
5695 switch (dest)
5696 {
5697 case dest_option:
5698 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5699 opt_flags);
5700 case dest_global:
5701 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005702 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5703 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005704 case dest_buffer:
5705 // include b: with the name, easier to execute that way
5706 return generate_STORE(cctx, ISN_STOREB, 0, name);
5707 case dest_window:
5708 // include w: with the name, easier to execute that way
5709 return generate_STORE(cctx, ISN_STOREW, 0, name);
5710 case dest_tab:
5711 // include t: with the name, easier to execute that way
5712 return generate_STORE(cctx, ISN_STORET, 0, name);
5713 case dest_env:
5714 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5715 case dest_reg:
5716 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5717 case dest_vimvar:
5718 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5719 case dest_script:
5720 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005721 // "s:" may be included in the name.
5722 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005723 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005724 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5725 scriptvar_sid, scriptvar_idx, type);
5726 case dest_local:
5727 case dest_expr:
5728 // cannot happen
5729 break;
5730 }
5731 return FAIL;
5732}
5733
Bram Moolenaar752fc692021-01-04 21:57:11 +01005734 static int
5735is_decl_command(int cmdidx)
5736{
5737 return cmdidx == CMD_let || cmdidx == CMD_var
5738 || cmdidx == CMD_final || cmdidx == CMD_const;
5739}
5740
5741/*
5742 * Figure out the LHS type and other properties for an assignment or one item
5743 * of ":unlet" with an index.
5744 * Returns OK or FAIL.
5745 */
5746 static int
5747compile_lhs(
5748 char_u *var_start,
5749 lhs_T *lhs,
5750 int cmdidx,
5751 int heredoc,
5752 int oplen,
5753 cctx_T *cctx)
5754{
5755 char_u *var_end;
5756 int is_decl = is_decl_command(cmdidx);
5757
5758 CLEAR_POINTER(lhs);
5759 lhs->lhs_dest = dest_local;
5760 lhs->lhs_vimvaridx = -1;
5761 lhs->lhs_scriptvar_idx = -1;
5762
5763 // "dest_end" is the end of the destination, including "[expr]" or
5764 // ".name".
5765 // "var_end" is the end of the variable/option/etc. name.
5766 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5767 if (*var_start == '@')
5768 var_end = var_start + 2;
5769 else
5770 {
5771 // skip over the leading "&", "&l:", "&g:" and "$"
5772 var_end = skip_option_env_lead(var_start);
5773 var_end = to_name_end(var_end, TRUE);
5774 }
5775
5776 // "a: type" is declaring variable "a" with a type, not dict "a:".
5777 if (is_decl && lhs->lhs_dest_end == var_start + 2
5778 && lhs->lhs_dest_end[-1] == ':')
5779 --lhs->lhs_dest_end;
5780 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5781 --var_end;
5782
5783 // compute the length of the destination without "[expr]" or ".name"
5784 lhs->lhs_varlen = var_end - var_start;
5785 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5786 if (lhs->lhs_name == NULL)
5787 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005788
5789 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5790 // Something follows after the variable: "var[idx]" or "var.key".
5791 lhs->lhs_has_index = TRUE;
5792
Bram Moolenaar752fc692021-01-04 21:57:11 +01005793 if (heredoc)
5794 lhs->lhs_type = &t_list_string;
5795 else
5796 lhs->lhs_type = &t_any;
5797
5798 if (cctx->ctx_skip != SKIP_YES)
5799 {
5800 int declare_error = FALSE;
5801
5802 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5803 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5804 &lhs->lhs_type, cctx) == FAIL)
5805 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005806 if (lhs->lhs_dest != dest_local
5807 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005808 {
5809 // Specific kind of variable recognized.
5810 declare_error = is_decl;
5811 }
5812 else
5813 {
5814 int idx;
5815
5816 // No specific kind of variable recognized, just a name.
5817 for (idx = 0; reserved[idx] != NULL; ++idx)
5818 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5819 {
5820 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5821 return FAIL;
5822 }
5823
5824
5825 if (lookup_local(var_start, lhs->lhs_varlen,
5826 &lhs->lhs_local_lvar, cctx) == OK)
5827 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5828 else
5829 {
5830 CLEAR_FIELD(lhs->lhs_arg_lvar);
5831 if (arg_exists(var_start, lhs->lhs_varlen,
5832 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5833 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5834 {
5835 if (is_decl)
5836 {
5837 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5838 return FAIL;
5839 }
5840 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5841 }
5842 }
5843 if (lhs->lhs_lvar != NULL)
5844 {
5845 if (is_decl)
5846 {
5847 semsg(_(e_variable_already_declared), lhs->lhs_name);
5848 return FAIL;
5849 }
5850 }
5851 else
5852 {
5853 int script_namespace = lhs->lhs_varlen > 1
5854 && STRNCMP(var_start, "s:", 2) == 0;
5855 int script_var = (script_namespace
5856 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005857 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005858 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005859 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005860 imported_T *import =
5861 find_imported(var_start, lhs->lhs_varlen, cctx);
5862
5863 if (script_namespace || script_var || import != NULL)
5864 {
5865 char_u *rawname = lhs->lhs_name
5866 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5867
5868 if (is_decl)
5869 {
5870 if (script_namespace)
5871 semsg(_(e_cannot_declare_script_variable_in_function),
5872 lhs->lhs_name);
5873 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005874 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005875 lhs->lhs_name);
5876 return FAIL;
5877 }
5878 else if (cctx->ctx_ufunc->uf_script_ctx_version
5879 == SCRIPT_VERSION_VIM9
5880 && script_namespace
5881 && !script_var && import == NULL)
5882 {
5883 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5884 return FAIL;
5885 }
5886
5887 lhs->lhs_dest = dest_script;
5888
5889 // existing script-local variables should have a type
5890 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5891 if (import != NULL)
5892 lhs->lhs_scriptvar_sid = import->imp_sid;
5893 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5894 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005895 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005896 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005897 lhs->lhs_scriptvar_sid, rawname,
5898 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5899 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005900 if (lhs->lhs_scriptvar_idx >= 0)
5901 {
5902 scriptitem_T *si = SCRIPT_ITEM(
5903 lhs->lhs_scriptvar_sid);
5904 svar_T *sv =
5905 ((svar_T *)si->sn_var_vals.ga_data)
5906 + lhs->lhs_scriptvar_idx;
5907 lhs->lhs_type = sv->sv_type;
5908 }
5909 }
5910 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005911 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005912 == FAIL)
5913 return FAIL;
5914 }
5915 }
5916
5917 if (declare_error)
5918 {
5919 vim9_declare_error(lhs->lhs_name);
5920 return FAIL;
5921 }
5922 }
5923
5924 // handle "a:name" as a name, not index "name" on "a"
5925 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5926 var_end = lhs->lhs_dest_end;
5927
5928 if (lhs->lhs_dest != dest_option)
5929 {
5930 if (is_decl && *var_end == ':')
5931 {
5932 char_u *p;
5933
5934 // parse optional type: "let var: type = expr"
5935 if (!VIM_ISWHITE(var_end[1]))
5936 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005937 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005938 return FAIL;
5939 }
5940 p = skipwhite(var_end + 1);
5941 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5942 if (lhs->lhs_type == NULL)
5943 return FAIL;
5944 lhs->lhs_has_type = TRUE;
5945 }
5946 else if (lhs->lhs_lvar != NULL)
5947 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5948 }
5949
Bram Moolenaare42939a2021-04-05 17:11:17 +02005950 if (oplen == 3 && !heredoc
5951 && lhs->lhs_dest != dest_global
5952 && !lhs->lhs_has_index
5953 && lhs->lhs_type->tt_type != VAR_STRING
5954 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005955 {
5956 emsg(_(e_can_only_concatenate_to_string));
5957 return FAIL;
5958 }
5959
5960 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5961 && cctx->ctx_skip != SKIP_YES)
5962 {
5963 if (oplen > 1 && !heredoc)
5964 {
5965 // +=, /=, etc. require an existing variable
5966 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5967 return FAIL;
5968 }
5969 if (!is_decl)
5970 {
5971 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5972 return FAIL;
5973 }
5974
Bram Moolenaar3f327882021-03-17 20:56:38 +01005975 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005976 if ((lhs->lhs_type->tt_type == VAR_FUNC
5977 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005978 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005979 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005980
5981 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005982 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5983 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5984 if (lhs->lhs_lvar == NULL)
5985 return FAIL;
5986 lhs->lhs_new_local = TRUE;
5987 }
5988
5989 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005990 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005991 {
5992 // Something follows after the variable: "var[idx]" or "var.key".
5993 // TODO: should we also handle "->func()" here?
5994 if (is_decl)
5995 {
5996 emsg(_(e_cannot_use_index_when_declaring_variable));
5997 return FAIL;
5998 }
5999
6000 if (var_start[lhs->lhs_varlen] == '['
6001 || var_start[lhs->lhs_varlen] == '.')
6002 {
6003 char_u *after = var_start + lhs->lhs_varlen;
6004 char_u *p;
6005
6006 // Only the last index is used below, if there are others
6007 // before it generate code for the expression. Thus for
6008 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6009 for (;;)
6010 {
6011 p = skip_index(after);
6012 if (*p != '[' && *p != '.')
6013 break;
6014 after = p;
6015 }
6016 if (after > var_start + lhs->lhs_varlen)
6017 {
6018 lhs->lhs_varlen = after - var_start;
6019 lhs->lhs_dest = dest_expr;
6020 // We don't know the type before evaluating the expression,
6021 // use "any" until then.
6022 lhs->lhs_type = &t_any;
6023 }
6024
Bram Moolenaar752fc692021-01-04 21:57:11 +01006025 if (lhs->lhs_type->tt_member == NULL)
6026 lhs->lhs_member_type = &t_any;
6027 else
6028 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6029 }
6030 else
6031 {
6032 semsg("Not supported yet: %s", var_start);
6033 return FAIL;
6034 }
6035 }
6036 return OK;
6037}
6038
6039/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006040 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6041 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006042 */
6043 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006044compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006045 char_u *var_start,
6046 lhs_T *lhs,
6047 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006048 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006049 cctx_T *cctx)
6050{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006051 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006052 char_u *p;
6053 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006054
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 p = var_start + varlen;
6056 if (*p == '[')
6057 {
6058 p = skipwhite(p + 1);
6059 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006060
6061 if (r == OK && *skipwhite(p) == ':')
6062 {
6063 // unlet var[idx : idx]
6064 if (is_assign)
6065 {
6066 semsg(_(e_cannot_use_range_with_assignment_str), p);
6067 return FAIL;
6068 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006069 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006070 p = skipwhite(p);
6071 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6072 {
6073 semsg(_(e_white_space_required_before_and_after_str_at_str),
6074 ":", p);
6075 return FAIL;
6076 }
6077 p = skipwhite(p + 1);
6078 r = compile_expr0(&p, cctx);
6079 }
6080
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081 if (r == OK && *skipwhite(p) != ']')
6082 {
6083 // this should not happen
6084 emsg(_(e_missbrac));
6085 r = FAIL;
6086 }
6087 }
6088 else // if (*p == '.')
6089 {
6090 char_u *key_end = to_name_end(p + 1, TRUE);
6091 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6092
6093 r = generate_PUSHS(cctx, key);
6094 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006095 return r;
6096}
6097
6098/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006099 * For a LHS with an index, load the variable to be indexed.
6100 */
6101 static int
6102compile_load_lhs(
6103 lhs_T *lhs,
6104 char_u *var_start,
6105 type_T *rhs_type,
6106 cctx_T *cctx)
6107{
6108 if (lhs->lhs_dest == dest_expr)
6109 {
6110 size_t varlen = lhs->lhs_varlen;
6111 int c = var_start[varlen];
6112 char_u *p = var_start;
6113 garray_T *stack = &cctx->ctx_type_stack;
6114
6115 // Evaluate "ll[expr]" of "ll[expr][idx]"
6116 var_start[varlen] = NUL;
6117 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6118 {
6119 // this should not happen
6120 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006121 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006122 return FAIL;
6123 }
6124 var_start[varlen] = c;
6125
6126 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6127 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6128 // now we can properly check the type
6129 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6130 && rhs_type != &t_void
6131 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6132 FALSE, FALSE) == FAIL)
6133 return FAIL;
6134 }
6135 else
6136 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6137 lhs->lhs_lvar, lhs->lhs_type);
6138 return OK;
6139}
6140
6141/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006142 * Assignment to a list or dict member, or ":unlet" for the item, using the
6143 * information in "lhs".
6144 * Returns OK or FAIL.
6145 */
6146 static int
6147compile_assign_unlet(
6148 char_u *var_start,
6149 lhs_T *lhs,
6150 int is_assign,
6151 type_T *rhs_type,
6152 cctx_T *cctx)
6153{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006154 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006155 garray_T *stack = &cctx->ctx_type_stack;
6156 int range = FALSE;
6157
6158 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006159 return FAIL;
6160
6161 if (lhs->lhs_type == &t_any)
6162 {
6163 // Index on variable of unknown type: check at runtime.
6164 dest_type = VAR_ANY;
6165 }
6166 else
6167 {
6168 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006169 if (dest_type == VAR_DICT && range)
6170 {
6171 emsg(e_cannot_use_range_with_dictionary);
6172 return FAIL;
6173 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006174 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6175 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006176 if (dest_type == VAR_LIST)
6177 {
6178 if (range
6179 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006180 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006181 return FAIL;
6182 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6183 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6184 return FAIL;
6185 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006186 }
6187
6188 // Load the dict or list. On the stack we then have:
6189 // - value (for assignment, not for :unlet)
6190 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006191 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006193 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6194 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006195
6196 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6197 {
6198 if (is_assign)
6199 {
6200 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6201
6202 if (isn == NULL)
6203 return FAIL;
6204 isn->isn_arg.vartype = dest_type;
6205 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006206 else if (range)
6207 {
6208 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6209 return FAIL;
6210 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 else
6212 {
6213 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6214 return FAIL;
6215 }
6216 }
6217 else
6218 {
6219 emsg(_(e_indexable_type_required));
6220 return FAIL;
6221 }
6222
6223 return OK;
6224}
6225
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006226/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006227 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006228 * "let name"
6229 * "var name = expr"
6230 * "final name = expr"
6231 * "const name = expr"
6232 * "name = expr"
6233 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006234 * Return NULL for an error.
6235 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 */
6237 static char_u *
6238compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6239{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006241 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006242 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006243 char_u *ret = NULL;
6244 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006245 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006248 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 int oplen = 0;
6251 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006252 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006254 int is_decl = is_decl_command(cmdidx);
6255 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006256 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006258 // Skip over the "var" or "[var, var]" to get to any "=".
6259 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6260 if (p == NULL)
6261 return *arg == '[' ? arg : NULL;
6262
6263 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006265 // TODO: should we allow this, and figure out type inference from list
6266 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006267 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 return NULL;
6269 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006270 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 sp = p;
6273 p = skipwhite(p);
6274 op = p;
6275 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006276
6277 if (var_count > 0 && oplen == 0)
6278 // can be something like "[1, 2]->func()"
6279 return arg;
6280
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006281 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006283 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006284 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006285 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 if (heredoc)
6288 {
6289 list_T *l;
6290 listitem_T *li;
6291
6292 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006293 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006295 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006296 if (l == NULL)
6297 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
Bram Moolenaar078269b2020-09-21 20:35:55 +02006299 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006301 // Push each line and the create the list.
6302 FOR_ALL_LIST_ITEMS(l, li)
6303 {
6304 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6305 li->li_tv.vval.v_string = NULL;
6306 }
6307 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 list_free(l);
6310 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006313 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006315 char_u *wp;
6316
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 // for "[var, var] = expr" evaluate the expression here, loop over the
6318 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006319 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006320
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006321 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006322 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006323 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006324 if (compile_expr0(&p, cctx) == FAIL)
6325 return NULL;
6326 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006328 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006330 type_T *stacktype;
6331
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006332 stacktype = stack->ga_len == 0 ? &t_void
6333 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006334 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006336 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006337 goto theend;
6338 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006339 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006340 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006341 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006342 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006343 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6344 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006345 if (stacktype->tt_member != NULL)
6346 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006347 }
6348 }
6349
6350 /*
6351 * Loop over variables in "[var, var] = expr".
6352 * For "var = expr" and "let var: type" this is done only once.
6353 */
6354 if (var_count > 0)
6355 var_start = skipwhite(arg + 1); // skip over the "["
6356 else
6357 var_start = arg;
6358 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6359 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006360 int instr_count = -1;
6361
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 vim_free(lhs.lhs_name);
6363
6364 /*
6365 * Figure out the LHS type and other properties.
6366 */
6367 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6368 goto theend;
6369
6370 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006371 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006373 goto theend;
6374 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006375 if (!is_decl && lhs.lhs_lvar != NULL
6376 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006377 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006378 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006379 goto theend;
6380 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006381
6382 if (!heredoc)
6383 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006384 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006385 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006386 if (oplen > 0 && var_count == 0)
6387 {
6388 // skip over the "=" and the expression
6389 p = skipwhite(op + oplen);
6390 compile_expr0(&p, cctx);
6391 }
6392 }
6393 else if (oplen > 0)
6394 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006395 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006396 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006397
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006398 // For "var = expr" evaluate the expression.
6399 if (var_count == 0)
6400 {
6401 int r;
6402
6403 // for "+=", "*=", "..=" etc. first load the current value
6404 if (*op != '=')
6405 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006406 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006407
Bram Moolenaar752fc692021-01-04 21:57:11 +01006408 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006409 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006410 int range = FALSE;
6411
6412 // Get member from list or dict. First compile the
6413 // index value.
6414 if (compile_assign_index(var_start, &lhs,
6415 TRUE, &range, cctx) == FAIL)
6416 goto theend;
6417
6418 // Get the member.
6419 if (compile_member(FALSE, cctx) == FAIL)
6420 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006421 }
6422 }
6423
6424 // Compile the expression. Temporarily hide the new local
6425 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006426 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006427 --cctx->ctx_locals.ga_len;
6428 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006429 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006430 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006431 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006432 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006433 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006434 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006435 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006436 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006438 ++cctx->ctx_locals.ga_len;
6439 if (r == FAIL)
6440 goto theend;
6441 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006442 else if (semicolon && var_idx == var_count - 1)
6443 {
6444 // For "[var; var] = expr" get the rest of the list
6445 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6446 goto theend;
6447 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006448 else
6449 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006450 // For "[var, var] = expr" get the "var_idx" item from the
6451 // list.
6452 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006453 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006455
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006456 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006457 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006459 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006460 if ((rhs_type->tt_type == VAR_FUNC
6461 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006462 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006463 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006464 goto theend;
6465
Bram Moolenaar752fc692021-01-04 21:57:11 +01006466 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006467 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006468 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006469 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006470 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006471 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006472 }
6473 else
6474 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006475 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006476 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006477 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006478 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006479 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006481 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006483 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006485 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006486 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006487 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006488 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006490
Bram Moolenaar77709b12021-04-03 21:01:01 +02006491 // Without operator check type here, otherwise below.
6492 // Use the line number of the assignment.
6493 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 if (lhs.lhs_has_index)
6495 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006496 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006497 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006498 goto theend;
6499 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006500 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006501 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006502 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006503 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006504 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006505 else if (cmdidx == CMD_final)
6506 {
6507 emsg(_(e_final_requires_a_value));
6508 goto theend;
6509 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006510 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006512 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006513 goto theend;
6514 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006515 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006516 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006517 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006518 goto theend;
6519 }
6520 else
6521 {
6522 // variables are always initialized
6523 if (ga_grow(instr, 1) == FAIL)
6524 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006525 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006526 {
6527 case VAR_BOOL:
6528 generate_PUSHBOOL(cctx, VVAL_FALSE);
6529 break;
6530 case VAR_FLOAT:
6531#ifdef FEAT_FLOAT
6532 generate_PUSHF(cctx, 0.0);
6533#endif
6534 break;
6535 case VAR_STRING:
6536 generate_PUSHS(cctx, NULL);
6537 break;
6538 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006539 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006540 break;
6541 case VAR_FUNC:
6542 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6543 break;
6544 case VAR_LIST:
6545 generate_NEWLIST(cctx, 0);
6546 break;
6547 case VAR_DICT:
6548 generate_NEWDICT(cctx, 0);
6549 break;
6550 case VAR_JOB:
6551 generate_PUSHJOB(cctx, NULL);
6552 break;
6553 case VAR_CHANNEL:
6554 generate_PUSHCHANNEL(cctx, NULL);
6555 break;
6556 case VAR_NUMBER:
6557 case VAR_UNKNOWN:
6558 case VAR_ANY:
6559 case VAR_PARTIAL:
6560 case VAR_VOID:
6561 case VAR_SPECIAL: // cannot happen
6562 generate_PUSHNR(cctx, 0);
6563 break;
6564 }
6565 }
6566 if (var_count == 0)
6567 end = p;
6568 }
6569
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006570 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006571 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006572 break;
6573
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006574 if (oplen > 0 && *op != '=')
6575 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006576 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006577 type_T *stacktype;
6578
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006579 if (*op == '.')
6580 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006581 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006582 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006583 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006584 if (
6585#ifdef FEAT_FLOAT
6586 // If variable is float operation with number is OK.
6587 !(expected == &t_float && stacktype == &t_number) &&
6588#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006589 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006590 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006591 goto theend;
6592
6593 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006594 {
6595 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6596 goto theend;
6597 }
6598 else if (*op == '+')
6599 {
6600 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006601 operator_type(lhs.lhs_member_type, stacktype),
6602 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006603 goto theend;
6604 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006605 else if (generate_two_op(cctx, op) == FAIL)
6606 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006610 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006611 // Use the info in "lhs" to store the value at the index in the
6612 // list or dict.
6613 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6614 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006615 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006616 }
6617 else
6618 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006619 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006620 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006621 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006622 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006623 generate_LOCKCONST(cctx);
6624
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006625 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 && (lhs.lhs_type->tt_type == VAR_DICT
6627 || lhs.lhs_type->tt_type == VAR_LIST)
6628 && lhs.lhs_type->tt_member != NULL
6629 && lhs.lhs_type->tt_member != &t_any
6630 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006631 // Set the type in the list or dict, so that it can be checked,
6632 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006633 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006634
Bram Moolenaar752fc692021-01-04 21:57:11 +01006635 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006637 if (generate_store_var(cctx, lhs.lhs_dest,
6638 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6639 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6640 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006641 goto theend;
6642 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006643 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006644 {
6645 isn_T *isn = ((isn_T *)instr->ga_data)
6646 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006647
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006648 // optimization: turn "var = 123" from ISN_PUSHNR +
6649 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006650 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006651 && instr->ga_len == instr_count + 1
6652 && isn->isn_type == ISN_PUSHNR)
6653 {
6654 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006655
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006656 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006657 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006658 isn->isn_arg.storenr.stnr_val = val;
6659 if (stack->ga_len > 0)
6660 --stack->ga_len;
6661 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006662 else if (lhs.lhs_lvar->lv_from_outer > 0)
6663 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6664 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006665 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006666 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006667 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006668 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006669
6670 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006671 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006672 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006673
6674 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006675 if (var_count > 0 && !semicolon)
6676 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006677 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006678 goto theend;
6679 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680
Bram Moolenaarb2097502020-07-19 17:17:02 +02006681 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682
6683theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006684 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006685 return ret;
6686}
6687
6688/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006689 * Check for an assignment at "eap->cmd", compile it if found.
6690 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6691 */
6692 static int
6693may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6694{
6695 char_u *pskip;
6696 char_u *p;
6697
6698 // Assuming the command starts with a variable or function name,
6699 // find what follows.
6700 // Skip over "var.member", "var[idx]" and the like.
6701 // Also "&opt = val", "$ENV = val" and "@r = val".
6702 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6703 ? eap->cmd + 1 : eap->cmd;
6704 p = to_name_end(pskip, TRUE);
6705 if (p > eap->cmd && *p != NUL)
6706 {
6707 char_u *var_end;
6708 int oplen;
6709 int heredoc;
6710
6711 if (eap->cmd[0] == '@')
6712 var_end = eap->cmd + 2;
6713 else
6714 var_end = find_name_end(pskip, NULL, NULL,
6715 FNE_CHECK_START | FNE_INCL_BR);
6716 oplen = assignment_len(skipwhite(var_end), &heredoc);
6717 if (oplen > 0)
6718 {
6719 size_t len = p - eap->cmd;
6720
6721 // Recognize an assignment if we recognize the variable
6722 // name:
6723 // "g:var = expr"
6724 // "local = expr" where "local" is a local var.
6725 // "script = expr" where "script" is a script-local var.
6726 // "import = expr" where "import" is an imported var
6727 // "&opt = expr"
6728 // "$ENV = expr"
6729 // "@r = expr"
6730 if (*eap->cmd == '&'
6731 || *eap->cmd == '$'
6732 || *eap->cmd == '@'
6733 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006734 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006735 {
6736 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6737 if (*line == NULL || *line == eap->cmd)
6738 return FAIL;
6739 return OK;
6740 }
6741 }
6742 }
6743
6744 if (*eap->cmd == '[')
6745 {
6746 // [var, var] = expr
6747 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6748 if (*line == NULL)
6749 return FAIL;
6750 if (*line != eap->cmd)
6751 return OK;
6752 }
6753 return NOTDONE;
6754}
6755
6756/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006757 * Check if "name" can be "unlet".
6758 */
6759 int
6760check_vim9_unlet(char_u *name)
6761{
6762 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6763 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006764 // "unlet s:var" is allowed in legacy script.
6765 if (*name == 's' && !script_is_vim9())
6766 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006767 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006768 return FAIL;
6769 }
6770 return OK;
6771}
6772
6773/*
6774 * Callback passed to ex_unletlock().
6775 */
6776 static int
6777compile_unlet(
6778 lval_T *lvp,
6779 char_u *name_end,
6780 exarg_T *eap,
6781 int deep UNUSED,
6782 void *coookie)
6783{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006784 cctx_T *cctx = coookie;
6785 char_u *p = lvp->ll_name;
6786 int cc = *name_end;
6787 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006788
Bram Moolenaar752fc692021-01-04 21:57:11 +01006789 if (cctx->ctx_skip == SKIP_YES)
6790 return OK;
6791
6792 *name_end = NUL;
6793 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006794 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006795 // :unlet $ENV_VAR
6796 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6797 }
6798 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6799 {
6800 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006801
Bram Moolenaar752fc692021-01-04 21:57:11 +01006802 // This is similar to assigning: lookup the list/dict, compile the
6803 // idx/key. Then instead of storing the value unlet the item.
6804 // unlet {list}[idx]
6805 // unlet {dict}[key] dict.key
6806 //
6807 // Figure out the LHS type and other properties.
6808 //
6809 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6810
6811 // : unlet an indexed item
6812 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006813 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006814 iemsg("called compile_lhs() without an index");
6815 ret = FAIL;
6816 }
6817 else
6818 {
6819 // Use the info in "lhs" to unlet the item at the index in the
6820 // list or dict.
6821 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006822 }
6823
Bram Moolenaar752fc692021-01-04 21:57:11 +01006824 vim_free(lhs.lhs_name);
6825 }
6826 else if (check_vim9_unlet(p) == FAIL)
6827 {
6828 ret = FAIL;
6829 }
6830 else
6831 {
6832 // Normal name. Only supports g:, w:, t: and b: namespaces.
6833 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006834 }
6835
Bram Moolenaar752fc692021-01-04 21:57:11 +01006836 *name_end = cc;
6837 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006838}
6839
6840/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006841 * Callback passed to ex_unletlock().
6842 */
6843 static int
6844compile_lock_unlock(
6845 lval_T *lvp,
6846 char_u *name_end,
6847 exarg_T *eap,
6848 int deep UNUSED,
6849 void *coookie)
6850{
6851 cctx_T *cctx = coookie;
6852 int cc = *name_end;
6853 char_u *p = lvp->ll_name;
6854 int ret = OK;
6855 size_t len;
6856 char_u *buf;
6857
6858 if (cctx->ctx_skip == SKIP_YES)
6859 return OK;
6860
6861 // Cannot use :lockvar and :unlockvar on local variables.
6862 if (p[1] != ':')
6863 {
6864 char_u *end = skip_var_one(p, FALSE);
6865
6866 if (lookup_local(p, end - p, NULL, cctx) == OK)
6867 {
6868 emsg(_(e_cannot_lock_unlock_local_variable));
6869 return FAIL;
6870 }
6871 }
6872
6873 // Checking is done at runtime.
6874 *name_end = NUL;
6875 len = name_end - p + 20;
6876 buf = alloc(len);
6877 if (buf == NULL)
6878 ret = FAIL;
6879 else
6880 {
6881 vim_snprintf((char *)buf, len, "%s %s",
6882 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6883 p);
6884 ret = generate_EXEC(cctx, buf);
6885
6886 vim_free(buf);
6887 *name_end = cc;
6888 }
6889 return ret;
6890}
6891
6892/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006893 * compile "unlet var", "lock var" and "unlock var"
6894 * "arg" points to "var".
6895 */
6896 static char_u *
6897compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6898{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006899 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6900 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6901 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006902 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6903}
6904
6905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 * Compile an :import command.
6907 */
6908 static char_u *
6909compile_import(char_u *arg, cctx_T *cctx)
6910{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006911 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912}
6913
6914/*
6915 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6916 */
6917 static int
6918compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6919{
6920 garray_T *instr = &cctx->ctx_instr;
6921 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6922
6923 if (endlabel == NULL)
6924 return FAIL;
6925 endlabel->el_next = *el;
6926 *el = endlabel;
6927 endlabel->el_end_label = instr->ga_len;
6928
6929 generate_JUMP(cctx, when, 0);
6930 return OK;
6931}
6932
6933 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006934compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935{
6936 garray_T *instr = &cctx->ctx_instr;
6937
6938 while (*el != NULL)
6939 {
6940 endlabel_T *cur = (*el);
6941 isn_T *isn;
6942
6943 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006944 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945 *el = cur->el_next;
6946 vim_free(cur);
6947 }
6948}
6949
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006950 static void
6951compile_free_jump_to_end(endlabel_T **el)
6952{
6953 while (*el != NULL)
6954 {
6955 endlabel_T *cur = (*el);
6956
6957 *el = cur->el_next;
6958 vim_free(cur);
6959 }
6960}
6961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962/*
6963 * Create a new scope and set up the generic items.
6964 */
6965 static scope_T *
6966new_scope(cctx_T *cctx, scopetype_T type)
6967{
6968 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6969
6970 if (scope == NULL)
6971 return NULL;
6972 scope->se_outer = cctx->ctx_scope;
6973 cctx->ctx_scope = scope;
6974 scope->se_type = type;
6975 scope->se_local_count = cctx->ctx_locals.ga_len;
6976 return scope;
6977}
6978
6979/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006980 * Free the current scope and go back to the outer scope.
6981 */
6982 static void
6983drop_scope(cctx_T *cctx)
6984{
6985 scope_T *scope = cctx->ctx_scope;
6986
6987 if (scope == NULL)
6988 {
6989 iemsg("calling drop_scope() without a scope");
6990 return;
6991 }
6992 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006993 switch (scope->se_type)
6994 {
6995 case IF_SCOPE:
6996 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6997 case FOR_SCOPE:
6998 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6999 case WHILE_SCOPE:
7000 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7001 case TRY_SCOPE:
7002 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7003 case NO_SCOPE:
7004 case BLOCK_SCOPE:
7005 break;
7006 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007007 vim_free(scope);
7008}
7009
7010/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 * compile "if expr"
7012 *
7013 * "if expr" Produces instructions:
7014 * EVAL expr Push result of "expr"
7015 * JUMP_IF_FALSE end
7016 * ... body ...
7017 * end:
7018 *
7019 * "if expr | else" Produces instructions:
7020 * EVAL expr Push result of "expr"
7021 * JUMP_IF_FALSE else
7022 * ... body ...
7023 * JUMP_ALWAYS end
7024 * else:
7025 * ... body ...
7026 * end:
7027 *
7028 * "if expr1 | elseif expr2 | else" Produces instructions:
7029 * EVAL expr Push result of "expr"
7030 * JUMP_IF_FALSE elseif
7031 * ... body ...
7032 * JUMP_ALWAYS end
7033 * elseif:
7034 * EVAL expr Push result of "expr"
7035 * JUMP_IF_FALSE else
7036 * ... body ...
7037 * JUMP_ALWAYS end
7038 * else:
7039 * ... body ...
7040 * end:
7041 */
7042 static char_u *
7043compile_if(char_u *arg, cctx_T *cctx)
7044{
7045 char_u *p = arg;
7046 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007047 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007049 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007050 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051
Bram Moolenaara5565e42020-05-09 15:44:01 +02007052 CLEAR_FIELD(ppconst);
7053 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007054 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007055 clear_ppconst(&ppconst);
7056 return NULL;
7057 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007058 if (!ends_excmd2(arg, skipwhite(p)))
7059 {
7060 semsg(_(e_trailing_arg), p);
7061 return NULL;
7062 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007063 if (cctx->ctx_skip == SKIP_YES)
7064 clear_ppconst(&ppconst);
7065 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007066 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007067 int error = FALSE;
7068 int v;
7069
Bram Moolenaara5565e42020-05-09 15:44:01 +02007070 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007071 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007072 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007073 if (error)
7074 return NULL;
7075 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007076 }
7077 else
7078 {
7079 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007080 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007081 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007082 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007083 if (bool_on_stack(cctx) == FAIL)
7084 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086
Bram Moolenaara91a7132021-03-25 21:12:15 +01007087 // CMDMOD_REV must come before the jump
7088 generate_undo_cmdmods(cctx);
7089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 scope = new_scope(cctx, IF_SCOPE);
7091 if (scope == NULL)
7092 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007093 scope->se_skip_save = skip_save;
7094 // "is_had_return" will be reset if any block does not end in :return
7095 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007097 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007098 {
7099 // "where" is set when ":elseif", "else" or ":endif" is found
7100 scope->se_u.se_if.is_if_label = instr->ga_len;
7101 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7102 }
7103 else
7104 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105
Bram Moolenaarced68a02021-01-24 17:53:47 +01007106#ifdef FEAT_PROFILE
7107 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7108 && skip_save != SKIP_YES)
7109 {
7110 // generated a profile start, need to generate a profile end, since it
7111 // won't be done after returning
7112 cctx->ctx_skip = SKIP_NOT;
7113 generate_instr(cctx, ISN_PROF_END);
7114 cctx->ctx_skip = SKIP_YES;
7115 }
7116#endif
7117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007118 return p;
7119}
7120
7121 static char_u *
7122compile_elseif(char_u *arg, cctx_T *cctx)
7123{
7124 char_u *p = arg;
7125 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007126 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007127 isn_T *isn;
7128 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007129 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007130 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131
7132 if (scope == NULL || scope->se_type != IF_SCOPE)
7133 {
7134 emsg(_(e_elseif_without_if));
7135 return NULL;
7136 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007137 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007138 if (!cctx->ctx_had_return)
7139 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007140
Bram Moolenaarced68a02021-01-24 17:53:47 +01007141 if (cctx->ctx_skip == SKIP_NOT)
7142 {
7143 // previous block was executed, this one and following will not
7144 cctx->ctx_skip = SKIP_YES;
7145 scope->se_u.se_if.is_seen_skip_not = TRUE;
7146 }
7147 if (scope->se_u.se_if.is_seen_skip_not)
7148 {
7149 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007150 // Do not count the "elseif" for profiling and cmdmod
7151 instr->ga_len = current_instr_idx(cctx);
7152
Bram Moolenaarced68a02021-01-24 17:53:47 +01007153 skip_expr_cctx(&p, cctx);
7154 return p;
7155 }
7156
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007157 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007158 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007159 int moved_cmdmod = FALSE;
7160
7161 // Move any CMDMOD instruction to after the jump
7162 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7163 {
7164 if (ga_grow(instr, 1) == FAIL)
7165 return NULL;
7166 ((isn_T *)instr->ga_data)[instr->ga_len] =
7167 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7168 --instr->ga_len;
7169 moved_cmdmod = TRUE;
7170 }
7171
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007172 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007173 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007174 return NULL;
7175 // previous "if" or "elseif" jumps here
7176 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7177 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007178 if (moved_cmdmod)
7179 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007181
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007182 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007183 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007184 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007185 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007186 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007187#ifdef FEAT_PROFILE
7188 if (cctx->ctx_profiling)
7189 {
7190 // the previous block was skipped, need to profile this line
7191 generate_instr(cctx, ISN_PROF_START);
7192 instr_count = instr->ga_len;
7193 }
7194#endif
7195 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007196 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007197 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007198 clear_ppconst(&ppconst);
7199 return NULL;
7200 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007201 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007202 if (!ends_excmd2(arg, skipwhite(p)))
7203 {
7204 semsg(_(e_trailing_arg), p);
7205 return NULL;
7206 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007207 if (scope->se_skip_save == SKIP_YES)
7208 clear_ppconst(&ppconst);
7209 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007210 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007211 int error = FALSE;
7212 int v;
7213
Bram Moolenaar7f141552020-05-09 17:35:53 +02007214 // The expression results in a constant.
7215 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007216 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7217 if (error)
7218 return NULL;
7219 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007220 clear_ppconst(&ppconst);
7221 scope->se_u.se_if.is_if_label = -1;
7222 }
7223 else
7224 {
7225 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007226 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007227 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007228 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007229 if (bool_on_stack(cctx) == FAIL)
7230 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231
Bram Moolenaara91a7132021-03-25 21:12:15 +01007232 // CMDMOD_REV must come before the jump
7233 generate_undo_cmdmods(cctx);
7234
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007235 // "where" is set when ":elseif", "else" or ":endif" is found
7236 scope->se_u.se_if.is_if_label = instr->ga_len;
7237 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239
7240 return p;
7241}
7242
7243 static char_u *
7244compile_else(char_u *arg, cctx_T *cctx)
7245{
7246 char_u *p = arg;
7247 garray_T *instr = &cctx->ctx_instr;
7248 isn_T *isn;
7249 scope_T *scope = cctx->ctx_scope;
7250
7251 if (scope == NULL || scope->se_type != IF_SCOPE)
7252 {
7253 emsg(_(e_else_without_if));
7254 return NULL;
7255 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007256 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007257 if (!cctx->ctx_had_return)
7258 scope->se_u.se_if.is_had_return = FALSE;
7259 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260
Bram Moolenaarced68a02021-01-24 17:53:47 +01007261#ifdef FEAT_PROFILE
7262 if (cctx->ctx_profiling)
7263 {
7264 if (cctx->ctx_skip == SKIP_NOT
7265 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7266 .isn_type == ISN_PROF_START)
7267 // the previous block was executed, do not count "else" for profiling
7268 --instr->ga_len;
7269 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7270 {
7271 // the previous block was not executed, this one will, do count the
7272 // "else" for profiling
7273 cctx->ctx_skip = SKIP_NOT;
7274 generate_instr(cctx, ISN_PROF_END);
7275 generate_instr(cctx, ISN_PROF_START);
7276 cctx->ctx_skip = SKIP_YES;
7277 }
7278 }
7279#endif
7280
7281 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007282 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007283 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007284 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007285 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007286 if (!cctx->ctx_had_return
7287 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7288 JUMP_ALWAYS, cctx) == FAIL)
7289 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007290 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007291
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007292 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007293 {
7294 if (scope->se_u.se_if.is_if_label >= 0)
7295 {
7296 // previous "if" or "elseif" jumps here
7297 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7298 isn->isn_arg.jump.jump_where = instr->ga_len;
7299 scope->se_u.se_if.is_if_label = -1;
7300 }
7301 }
7302
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007303 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007304 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7305 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007306
7307 return p;
7308}
7309
7310 static char_u *
7311compile_endif(char_u *arg, cctx_T *cctx)
7312{
7313 scope_T *scope = cctx->ctx_scope;
7314 ifscope_T *ifscope;
7315 garray_T *instr = &cctx->ctx_instr;
7316 isn_T *isn;
7317
Bram Moolenaarfa984412021-03-25 22:15:28 +01007318 if (misplaced_cmdmod(cctx))
7319 return NULL;
7320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321 if (scope == NULL || scope->se_type != IF_SCOPE)
7322 {
7323 emsg(_(e_endif_without_if));
7324 return NULL;
7325 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007326 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007327 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007328 if (!cctx->ctx_had_return)
7329 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007330
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007331 if (scope->se_u.se_if.is_if_label >= 0)
7332 {
7333 // previous "if" or "elseif" jumps here
7334 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7335 isn->isn_arg.jump.jump_where = instr->ga_len;
7336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007338 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007339
7340#ifdef FEAT_PROFILE
7341 // even when skipping we count the endif as executed, unless the block it's
7342 // in is skipped
7343 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7344 && scope->se_skip_save != SKIP_YES)
7345 {
7346 cctx->ctx_skip = SKIP_NOT;
7347 generate_instr(cctx, ISN_PROF_START);
7348 }
7349#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007350 cctx->ctx_skip = scope->se_skip_save;
7351
7352 // If all the blocks end in :return and there is an :else then the
7353 // had_return flag is set.
7354 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007356 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 return arg;
7358}
7359
7360/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007361 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 *
7363 * Produces instructions:
7364 * PUSHNR -1
7365 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007366 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7368 * - if beyond end, jump to "end"
7369 * - otherwise get item from list and push it
7370 * STORE var Store item in "var"
7371 * ... body ...
7372 * JUMP top Jump back to repeat
7373 * end: DROP Drop the result of "expr"
7374 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007375 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7376 * UNPACK 2 Split item in 2
7377 * STORE var1 Store item in "var1"
7378 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 */
7380 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007381compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007383 char_u *arg;
7384 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007385 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007387 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007388 int var_count = 0;
7389 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 garray_T *stack = &cctx->ctx_type_stack;
7392 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007393 lvar_T *loop_lvar; // loop iteration variable
7394 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007395 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007396 type_T *item_type = &t_any;
7397 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398
Bram Moolenaar792f7862020-11-23 08:31:18 +01007399 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007400 if (p == NULL)
7401 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007402 if (var_count == 0)
7403 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404
7405 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007406 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007407 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7408 return NULL;
7409 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 {
7411 emsg(_(e_missing_in));
7412 return NULL;
7413 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007414 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007415 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7416 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007418 scope = new_scope(cctx, FOR_SCOPE);
7419 if (scope == NULL)
7420 return NULL;
7421
Bram Moolenaar792f7862020-11-23 08:31:18 +01007422 // Reserve a variable to store the loop iteration counter and initialize it
7423 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007424 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7425 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007426 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007427 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007428 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007430 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007431 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432
7433 // compile "expr", it remains on the stack until "endfor"
7434 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007435 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007436 {
7437 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007439 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007440 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007442 // If we know the type of "var" and it is a not a list or string we can
7443 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007445 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7446 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007448 // TODO: support Blob
7449 semsg(_(e_for_loop_on_str_not_supported),
7450 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007451 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452 return NULL;
7453 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007454
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007455 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007456 {
7457 if (var_count == 1)
7458 item_type = vartype->tt_member;
7459 else if (vartype->tt_member->tt_type == VAR_LIST
7460 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7461 item_type = vartype->tt_member->tt_member;
7462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463
Bram Moolenaara91a7132021-03-25 21:12:15 +01007464 // CMDMOD_REV must come before the FOR instruction
7465 generate_undo_cmdmods(cctx);
7466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007467 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007468 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007469 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007470
Bram Moolenaar792f7862020-11-23 08:31:18 +01007471 arg = arg_start;
7472 if (var_count > 1)
7473 {
7474 generate_UNPACK(cctx, var_count, semicolon);
7475 arg = skipwhite(arg + 1); // skip white after '['
7476
7477 // the list item is replaced by a number of items
7478 if (ga_grow(stack, var_count - 1) == FAIL)
7479 {
7480 drop_scope(cctx);
7481 return NULL;
7482 }
7483 --stack->ga_len;
7484 for (idx = 0; idx < var_count; ++idx)
7485 {
7486 ((type_T **)stack->ga_data)[stack->ga_len] =
7487 (semicolon && idx == 0) ? vartype : item_type;
7488 ++stack->ga_len;
7489 }
7490 }
7491
7492 for (idx = 0; idx < var_count; ++idx)
7493 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007494 assign_dest_T dest = dest_local;
7495 int opt_flags = 0;
7496 int vimvaridx = -1;
7497 type_T *type = &t_any;
7498
7499 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007500 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007501 name = vim_strnsave(arg, varlen);
7502 if (name == NULL)
7503 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007504
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007505 // TODO: script var not supported?
7506 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7507 &vimvaridx, &type, cctx) == FAIL)
7508 goto failed;
7509 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007510 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007511 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7512 0, 0, type, name) == FAIL)
7513 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007514 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007515 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007516 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007517 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007518 {
7519 semsg(_(e_variable_already_declared), arg);
7520 goto failed;
7521 }
7522
Bram Moolenaarea870692020-12-02 14:24:30 +01007523 if (STRNCMP(name, "s:", 2) == 0)
7524 {
7525 semsg(_(e_cannot_declare_script_variable_in_function), name);
7526 goto failed;
7527 }
7528
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007529 // Reserve a variable to store "var".
7530 // TODO: check for type
7531 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7532 if (var_lvar == NULL)
7533 // out of memory or used as an argument
7534 goto failed;
7535
7536 if (semicolon && idx == var_count - 1)
7537 var_lvar->lv_type = vartype;
7538 else
7539 var_lvar->lv_type = item_type;
7540 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7541 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007542
Bram Moolenaar036d0712021-01-17 20:23:38 +01007543 if (*p == ':')
7544 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007545 if (*p == ',' || *p == ';')
7546 ++p;
7547 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007548 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007549 }
7550
7551 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007552
7553failed:
7554 vim_free(name);
7555 drop_scope(cctx);
7556 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557}
7558
7559/*
7560 * compile "endfor"
7561 */
7562 static char_u *
7563compile_endfor(char_u *arg, cctx_T *cctx)
7564{
7565 garray_T *instr = &cctx->ctx_instr;
7566 scope_T *scope = cctx->ctx_scope;
7567 forscope_T *forscope;
7568 isn_T *isn;
7569
Bram Moolenaarfa984412021-03-25 22:15:28 +01007570 if (misplaced_cmdmod(cctx))
7571 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 if (scope == NULL || scope->se_type != FOR_SCOPE)
7574 {
7575 emsg(_(e_for));
7576 return NULL;
7577 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007578 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007579 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007580 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581
7582 // At end of ":for" scope jump back to the FOR instruction.
7583 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7584
7585 // Fill in the "end" label in the FOR statement so it can jump here
7586 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7587 isn->isn_arg.forloop.for_end = instr->ga_len;
7588
7589 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007590 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591
7592 // Below the ":for" scope drop the "expr" list from the stack.
7593 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7594 return NULL;
7595
7596 vim_free(scope);
7597
7598 return arg;
7599}
7600
7601/*
7602 * compile "while expr"
7603 *
7604 * Produces instructions:
7605 * top: EVAL expr Push result of "expr"
7606 * JUMP_IF_FALSE end jump if false
7607 * ... body ...
7608 * JUMP top Jump back to repeat
7609 * end:
7610 *
7611 */
7612 static char_u *
7613compile_while(char_u *arg, cctx_T *cctx)
7614{
7615 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 scope_T *scope;
7617
7618 scope = new_scope(cctx, WHILE_SCOPE);
7619 if (scope == NULL)
7620 return NULL;
7621
Bram Moolenaara91a7132021-03-25 21:12:15 +01007622 // "endwhile" jumps back here, one before when profiling or using cmdmods
7623 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624
7625 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007626 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007628 if (!ends_excmd2(arg, skipwhite(p)))
7629 {
7630 semsg(_(e_trailing_arg), p);
7631 return NULL;
7632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007633
Bram Moolenaar13106602020-10-04 16:06:05 +02007634 if (bool_on_stack(cctx) == FAIL)
7635 return FAIL;
7636
Bram Moolenaara91a7132021-03-25 21:12:15 +01007637 // CMDMOD_REV must come before the jump
7638 generate_undo_cmdmods(cctx);
7639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007641 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 JUMP_IF_FALSE, cctx) == FAIL)
7643 return FAIL;
7644
7645 return p;
7646}
7647
7648/*
7649 * compile "endwhile"
7650 */
7651 static char_u *
7652compile_endwhile(char_u *arg, cctx_T *cctx)
7653{
7654 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007655 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007656
Bram Moolenaarfa984412021-03-25 22:15:28 +01007657 if (misplaced_cmdmod(cctx))
7658 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7660 {
7661 emsg(_(e_while));
7662 return NULL;
7663 }
7664 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007665 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666
Bram Moolenaarf002a412021-01-24 13:34:18 +01007667#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007668 // count the endwhile before jumping
7669 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007670#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007673 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007674
7675 // Fill in the "end" label in the WHILE statement so it can jump here.
7676 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007677 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7678 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007679
7680 vim_free(scope);
7681
7682 return arg;
7683}
7684
7685/*
7686 * compile "continue"
7687 */
7688 static char_u *
7689compile_continue(char_u *arg, cctx_T *cctx)
7690{
7691 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007692 int try_scopes = 0;
7693 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694
7695 for (;;)
7696 {
7697 if (scope == NULL)
7698 {
7699 emsg(_(e_continue));
7700 return NULL;
7701 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007702 if (scope->se_type == FOR_SCOPE)
7703 {
7704 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007706 }
7707 if (scope->se_type == WHILE_SCOPE)
7708 {
7709 loop_label = scope->se_u.se_while.ws_top_label;
7710 break;
7711 }
7712 if (scope->se_type == TRY_SCOPE)
7713 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714 scope = scope->se_outer;
7715 }
7716
Bram Moolenaarc150c092021-02-13 15:02:46 +01007717 if (try_scopes > 0)
7718 // Inside one or more try/catch blocks we first need to jump to the
7719 // "finally" or "endtry" to cleanup.
7720 generate_TRYCONT(cctx, try_scopes, loop_label);
7721 else
7722 // Jump back to the FOR or WHILE instruction.
7723 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 return arg;
7726}
7727
7728/*
7729 * compile "break"
7730 */
7731 static char_u *
7732compile_break(char_u *arg, cctx_T *cctx)
7733{
7734 scope_T *scope = cctx->ctx_scope;
7735 endlabel_T **el;
7736
7737 for (;;)
7738 {
7739 if (scope == NULL)
7740 {
7741 emsg(_(e_break));
7742 return NULL;
7743 }
7744 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7745 break;
7746 scope = scope->se_outer;
7747 }
7748
7749 // Jump to the end of the FOR or WHILE loop.
7750 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007751 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007753 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7755 return FAIL;
7756
7757 return arg;
7758}
7759
7760/*
7761 * compile "{" start of block
7762 */
7763 static char_u *
7764compile_block(char_u *arg, cctx_T *cctx)
7765{
7766 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7767 return NULL;
7768 return skipwhite(arg + 1);
7769}
7770
7771/*
7772 * compile end of block: drop one scope
7773 */
7774 static void
7775compile_endblock(cctx_T *cctx)
7776{
7777 scope_T *scope = cctx->ctx_scope;
7778
7779 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007780 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 vim_free(scope);
7782}
7783
7784/*
7785 * compile "try"
7786 * Creates a new scope for the try-endtry, pointing to the first catch and
7787 * finally.
7788 * Creates another scope for the "try" block itself.
7789 * TRY instruction sets up exception handling at runtime.
7790 *
7791 * "try"
7792 * TRY -> catch1, -> finally push trystack entry
7793 * ... try block
7794 * "throw {exception}"
7795 * EVAL {exception}
7796 * THROW create exception
7797 * ... try block
7798 * " catch {expr}"
7799 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007800 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007801 * EVAL {expr}
7802 * MATCH
7803 * JUMP nomatch -> catch2
7804 * CATCH remove exception
7805 * ... catch block
7806 * " catch"
7807 * JUMP -> finally
7808 * catch2: CATCH remove exception
7809 * ... catch block
7810 * " finally"
7811 * finally:
7812 * ... finally block
7813 * " endtry"
7814 * ENDTRY pop trystack entry, may rethrow
7815 */
7816 static char_u *
7817compile_try(char_u *arg, cctx_T *cctx)
7818{
7819 garray_T *instr = &cctx->ctx_instr;
7820 scope_T *try_scope;
7821 scope_T *scope;
7822
Bram Moolenaarfa984412021-03-25 22:15:28 +01007823 if (misplaced_cmdmod(cctx))
7824 return NULL;
7825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826 // scope that holds the jumps that go to catch/finally/endtry
7827 try_scope = new_scope(cctx, TRY_SCOPE);
7828 if (try_scope == NULL)
7829 return NULL;
7830
Bram Moolenaar69f70502021-01-01 16:10:46 +01007831 if (cctx->ctx_skip != SKIP_YES)
7832 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007833 isn_T *isn;
7834
7835 // "try_catch" is set when the first ":catch" is found or when no catch
7836 // is found and ":finally" is found.
7837 // "try_finally" is set when ":finally" is found
7838 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007839 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007840 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7841 return NULL;
7842 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7843 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007844 return NULL;
7845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007846
7847 // scope for the try block itself
7848 scope = new_scope(cctx, BLOCK_SCOPE);
7849 if (scope == NULL)
7850 return NULL;
7851
7852 return arg;
7853}
7854
7855/*
7856 * compile "catch {expr}"
7857 */
7858 static char_u *
7859compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7860{
7861 scope_T *scope = cctx->ctx_scope;
7862 garray_T *instr = &cctx->ctx_instr;
7863 char_u *p;
7864 isn_T *isn;
7865
Bram Moolenaarfa984412021-03-25 22:15:28 +01007866 if (misplaced_cmdmod(cctx))
7867 return NULL;
7868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869 // end block scope from :try or :catch
7870 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7871 compile_endblock(cctx);
7872 scope = cctx->ctx_scope;
7873
7874 // Error if not in a :try scope
7875 if (scope == NULL || scope->se_type != TRY_SCOPE)
7876 {
7877 emsg(_(e_catch));
7878 return NULL;
7879 }
7880
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007881 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007883 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884 return NULL;
7885 }
7886
Bram Moolenaar69f70502021-01-01 16:10:46 +01007887 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007888 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007889#ifdef FEAT_PROFILE
7890 // the profile-start should be after the jump
7891 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7892 .isn_type == ISN_PROF_START)
7893 --instr->ga_len;
7894#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007895 // Jump from end of previous block to :finally or :endtry
7896 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7897 JUMP_ALWAYS, cctx) == FAIL)
7898 return NULL;
7899
7900 // End :try or :catch scope: set value in ISN_TRY instruction
7901 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007902 if (isn->isn_arg.try.try_ref->try_catch == 0)
7903 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007904 if (scope->se_u.se_try.ts_catch_label != 0)
7905 {
7906 // Previous catch without match jumps here
7907 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7908 isn->isn_arg.jump.jump_where = instr->ga_len;
7909 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007910#ifdef FEAT_PROFILE
7911 if (cctx->ctx_profiling)
7912 {
7913 // a "throw" that jumps here needs to be counted
7914 generate_instr(cctx, ISN_PROF_END);
7915 // the "catch" is also counted
7916 generate_instr(cctx, ISN_PROF_START);
7917 }
7918#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 }
7920
7921 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007922 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007923 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007924 scope->se_u.se_try.ts_caught_all = TRUE;
7925 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007926 }
7927 else
7928 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007929 char_u *end;
7930 char_u *pat;
7931 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007932 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007933 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007935 // Push v:exception, push {expr} and MATCH
7936 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7937
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007938 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007939 if (*end != *p)
7940 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007941 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007942 vim_free(tofree);
7943 return FAIL;
7944 }
7945 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007946 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007947 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007948 len = (int)(end - tofree);
7949 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007950 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007951 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007952 if (pat == NULL)
7953 return FAIL;
7954 if (generate_PUSHS(cctx, pat) == FAIL)
7955 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7958 return NULL;
7959
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007960 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7962 return NULL;
7963 }
7964
Bram Moolenaar69f70502021-01-01 16:10:46 +01007965 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966 return NULL;
7967
7968 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7969 return NULL;
7970 return p;
7971}
7972
7973 static char_u *
7974compile_finally(char_u *arg, cctx_T *cctx)
7975{
7976 scope_T *scope = cctx->ctx_scope;
7977 garray_T *instr = &cctx->ctx_instr;
7978 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007979 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007980
Bram Moolenaarfa984412021-03-25 22:15:28 +01007981 if (misplaced_cmdmod(cctx))
7982 return NULL;
7983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 // end block scope from :try or :catch
7985 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7986 compile_endblock(cctx);
7987 scope = cctx->ctx_scope;
7988
7989 // Error if not in a :try scope
7990 if (scope == NULL || scope->se_type != TRY_SCOPE)
7991 {
7992 emsg(_(e_finally));
7993 return NULL;
7994 }
7995
7996 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007997 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007998 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999 {
8000 emsg(_(e_finally_dup));
8001 return NULL;
8002 }
8003
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008004 this_instr = instr->ga_len;
8005#ifdef FEAT_PROFILE
8006 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8007 .isn_type == ISN_PROF_START)
8008 // jump to the profile start of the "finally"
8009 --this_instr;
8010#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008012 // Fill in the "end" label in jumps at the end of the blocks.
8013 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8014 this_instr, cctx);
8015
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008016 // If there is no :catch then an exception jumps to :finally.
8017 if (isn->isn_arg.try.try_ref->try_catch == 0)
8018 isn->isn_arg.try.try_ref->try_catch = this_instr;
8019 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008020 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021 {
8022 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008023 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008024 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008025 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008027 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8028 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 // TODO: set index in ts_finally_label jumps
8031
8032 return arg;
8033}
8034
8035 static char_u *
8036compile_endtry(char_u *arg, cctx_T *cctx)
8037{
8038 scope_T *scope = cctx->ctx_scope;
8039 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008040 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041
Bram Moolenaarfa984412021-03-25 22:15:28 +01008042 if (misplaced_cmdmod(cctx))
8043 return NULL;
8044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008045 // end block scope from :catch or :finally
8046 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8047 compile_endblock(cctx);
8048 scope = cctx->ctx_scope;
8049
8050 // Error if not in a :try scope
8051 if (scope == NULL || scope->se_type != TRY_SCOPE)
8052 {
8053 if (scope == NULL)
8054 emsg(_(e_no_endtry));
8055 else if (scope->se_type == WHILE_SCOPE)
8056 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008057 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 emsg(_(e_endfor));
8059 else
8060 emsg(_(e_endif));
8061 return NULL;
8062 }
8063
Bram Moolenaarc150c092021-02-13 15:02:46 +01008064 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008065 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008067 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8068 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008069 {
8070 emsg(_(e_missing_catch_or_finally));
8071 return NULL;
8072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008074#ifdef FEAT_PROFILE
8075 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8076 .isn_type == ISN_PROF_START)
8077 // move the profile start after "endtry" so that it's not counted when
8078 // the exception is rethrown.
8079 --instr->ga_len;
8080#endif
8081
Bram Moolenaar69f70502021-01-01 16:10:46 +01008082 // Fill in the "end" label in jumps at the end of the blocks, if not
8083 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008084 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8085 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008086
Bram Moolenaar69f70502021-01-01 16:10:46 +01008087 if (scope->se_u.se_try.ts_catch_label != 0)
8088 {
8089 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008090 isn_T *isn = ((isn_T *)instr->ga_data)
8091 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008092 isn->isn_arg.jump.jump_where = instr->ga_len;
8093 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008094 }
8095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 compile_endblock(cctx);
8097
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008098 if (cctx->ctx_skip != SKIP_YES)
8099 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008100 // End :catch or :finally scope: set instruction index in ISN_TRY
8101 // instruction
8102 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008103 if (cctx->ctx_skip != SKIP_YES
8104 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8105 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008106#ifdef FEAT_PROFILE
8107 if (cctx->ctx_profiling)
8108 generate_instr(cctx, ISN_PROF_START);
8109#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008111 return arg;
8112}
8113
8114/*
8115 * compile "throw {expr}"
8116 */
8117 static char_u *
8118compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8119{
8120 char_u *p = skipwhite(arg);
8121
Bram Moolenaara5565e42020-05-09 15:44:01 +02008122 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008124 if (cctx->ctx_skip == SKIP_YES)
8125 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008126 if (may_generate_2STRING(-1, cctx) == FAIL)
8127 return NULL;
8128 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8129 return NULL;
8130
8131 return p;
8132}
8133
8134/*
8135 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008136 * compile "echomsg expr"
8137 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008138 * compile "execute expr"
8139 */
8140 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008141compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008142{
8143 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008144 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008145 int count = 0;
8146
8147 for (;;)
8148 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008149 if (ends_excmd2(prev, p))
8150 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008151 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008152 return NULL;
8153 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008154 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008155 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008156 }
8157
Bram Moolenaare4984292020-12-13 14:19:25 +01008158 if (count > 0)
8159 {
8160 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8161 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8162 else if (cmdidx == CMD_execute)
8163 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8164 else if (cmdidx == CMD_echomsg)
8165 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8166 else
8167 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008169 return p;
8170}
8171
8172/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008173 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008174 * instruction to compute it and return OK.
8175 * Otherwise return FAIL, the caller must deal with any range.
8176 */
8177 static int
8178compile_variable_range(exarg_T *eap, cctx_T *cctx)
8179{
8180 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8181 char_u *p = skipdigits(eap->cmd);
8182
8183 if (p == range_end)
8184 return FAIL;
8185 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8186}
8187
8188/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008189 * :put r
8190 * :put ={expr}
8191 */
8192 static char_u *
8193compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8194{
8195 char_u *line = arg;
8196 linenr_T lnum;
8197 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008198 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008199
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008200 eap->regname = *line;
8201
8202 if (eap->regname == '=')
8203 {
8204 char_u *p = line + 1;
8205
8206 if (compile_expr0(&p, cctx) == FAIL)
8207 return NULL;
8208 line = p;
8209 }
8210 else if (eap->regname != NUL)
8211 ++line;
8212
Bram Moolenaar08597872020-12-10 19:43:40 +01008213 if (compile_variable_range(eap, cctx) == OK)
8214 {
8215 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8216 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008217 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008218 {
8219 // Either no range or a number.
8220 // "errormsg" will not be set because the range is ADDR_LINES.
8221 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008222 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008223 return NULL;
8224 if (eap->addr_count == 0)
8225 lnum = -1;
8226 else
8227 lnum = eap->line2;
8228 if (above)
8229 --lnum;
8230 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008231
8232 generate_PUT(cctx, eap->regname, lnum);
8233 return line;
8234}
8235
8236/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008237 * A command that is not compiled, execute with legacy code.
8238 */
8239 static char_u *
8240compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8241{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008242 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008243 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008244 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008245
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008246 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008247 goto theend;
8248
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008249 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008250 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008251 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008252 int usefilter = FALSE;
8253
8254 has_expr = argt & (EX_XFILE | EX_EXPAND);
8255
8256 // If the command can be followed by a bar, find the bar and truncate
8257 // it, so that the following command can be compiled.
8258 // The '|' is overwritten with a NUL, it is put back below.
8259 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8260 && *eap->arg == '!')
8261 // :w !filter or :r !filter or :r! filter
8262 usefilter = TRUE;
8263 if ((argt & EX_TRLBAR) && !usefilter)
8264 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008265 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008266 separate_nextcmd(eap);
8267 if (eap->nextcmd != NULL)
8268 nextcmd = eap->nextcmd;
8269 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008270 else if (eap->cmdidx == CMD_wincmd)
8271 {
8272 p = eap->arg;
8273 if (*p != NUL)
8274 ++p;
8275 if (*p == 'g' || *p == Ctrl_G)
8276 ++p;
8277 p = skipwhite(p);
8278 if (*p == '|')
8279 {
8280 *p = NUL;
8281 nextcmd = p + 1;
8282 }
8283 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008284 }
8285
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008286 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8287 {
8288 // expand filename in "syntax include [@group] filename"
8289 has_expr = TRUE;
8290 eap->arg = skipwhite(eap->arg + 7);
8291 if (*eap->arg == '@')
8292 eap->arg = skiptowhite(eap->arg);
8293 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008294
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008295 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8296 && STRLEN(eap->arg) > 4)
8297 {
8298 int delim = *eap->arg;
8299
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008300 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008301 if (*p == delim)
8302 {
8303 eap->arg = p + 1;
8304 has_expr = TRUE;
8305 }
8306 }
8307
Bram Moolenaarecac5912021-01-05 19:23:28 +01008308 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8309 {
8310 // TODO: should only expand when appropriate for the command
8311 eap->arg = skiptowhite(eap->arg);
8312 has_expr = TRUE;
8313 }
8314
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008315 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008316 {
8317 int count = 0;
8318 char_u *start = skipwhite(line);
8319
8320 // :cmd xxx`=expr1`yyy`=expr2`zzz
8321 // PUSHS ":cmd xxx"
8322 // eval expr1
8323 // PUSHS "yyy"
8324 // eval expr2
8325 // PUSHS "zzz"
8326 // EXECCONCAT 5
8327 for (;;)
8328 {
8329 if (p > start)
8330 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008331 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008332 ++count;
8333 }
8334 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008335 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008336 return NULL;
8337 may_generate_2STRING(-1, cctx);
8338 ++count;
8339 p = skipwhite(p);
8340 if (*p != '`')
8341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008342 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008343 return NULL;
8344 }
8345 start = p + 1;
8346
8347 p = (char_u *)strstr((char *)start, "`=");
8348 if (p == NULL)
8349 {
8350 if (*skipwhite(start) != NUL)
8351 {
8352 generate_PUSHS(cctx, vim_strsave(start));
8353 ++count;
8354 }
8355 break;
8356 }
8357 }
8358 generate_EXECCONCAT(cctx, count);
8359 }
8360 else
8361 generate_EXEC(cctx, line);
8362
8363theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008364 if (*nextcmd != NUL)
8365 {
8366 // the parser expects a pointer to the bar, put it back
8367 --nextcmd;
8368 *nextcmd = '|';
8369 }
8370
8371 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008372}
8373
8374/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008375 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008376 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008377 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008378 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008379add_def_function(ufunc_T *ufunc)
8380{
8381 dfunc_T *dfunc;
8382
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008383 if (def_functions.ga_len == 0)
8384 {
8385 // The first position is not used, so that a zero uf_dfunc_idx means it
8386 // wasn't set.
8387 if (ga_grow(&def_functions, 1) == FAIL)
8388 return FAIL;
8389 ++def_functions.ga_len;
8390 }
8391
Bram Moolenaar09689a02020-05-09 22:50:08 +02008392 // Add the function to "def_functions".
8393 if (ga_grow(&def_functions, 1) == FAIL)
8394 return FAIL;
8395 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8396 CLEAR_POINTER(dfunc);
8397 dfunc->df_idx = def_functions.ga_len;
8398 ufunc->uf_dfunc_idx = dfunc->df_idx;
8399 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008400 dfunc->df_name = vim_strsave(ufunc->uf_name);
8401 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008402 ++def_functions.ga_len;
8403 return OK;
8404}
8405
8406/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 * After ex_function() has collected all the function lines: parse and compile
8408 * the lines into instructions.
8409 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008410 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8411 * the return statement (used for lambda). When uf_ret_type is already set
8412 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008413 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008414 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008415 * This can be used recursively through compile_lambda(), which may reallocate
8416 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008417 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008419 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008420compile_def_function(
8421 ufunc_T *ufunc,
8422 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008423 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008424 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 char_u *line = NULL;
8427 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429 cctx_T cctx;
8430 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008431 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008432 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 int ret = FAIL;
8434 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008435 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008436 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008437 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008438#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008439 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008440#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008442 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008443 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008444 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008446 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8447 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008448 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008450 else
8451 {
8452 if (add_def_function(ufunc) == FAIL)
8453 return FAIL;
8454 new_def_function = TRUE;
8455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456
Bram Moolenaar985116a2020-07-12 17:31:09 +02008457 ufunc->uf_def_status = UF_COMPILING;
8458
Bram Moolenaara80faa82020-04-12 19:37:17 +02008459 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008460
Bram Moolenaarf002a412021-01-24 13:34:18 +01008461#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008462 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008463#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008464 cctx.ctx_ufunc = ufunc;
8465 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008466 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8468 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8469 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8470 cctx.ctx_type_list = &ufunc->uf_type_list;
8471 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8472 instr = &cctx.ctx_instr;
8473
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008474 // Set the context to the function, it may be compiled when called from
8475 // another script. Set the script version to the most modern one.
8476 // The line number will be set in next_line_from_context().
8477 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8479
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008480 // Make sure error messages are OK.
8481 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8482 if (do_estack_push)
8483 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008484 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008485
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008486 if (ufunc->uf_def_args.ga_len > 0)
8487 {
8488 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008489 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008490 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008491 int i;
8492 char_u *arg;
8493 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008494 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008495
8496 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008497 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008498 for (i = 0; i < count; ++i)
8499 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008500 garray_T *stack = &cctx.ctx_type_stack;
8501 type_T *val_type;
8502 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008503 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008504 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008505 int jump_instr_idx = instr->ga_len;
8506 isn_T *isn;
8507
8508 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8509 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8510 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008511
8512 // Make sure later arguments are not found.
8513 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008514
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008515 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008516 r = compile_expr0(&arg, &cctx);
8517
8518 ufunc->uf_args.ga_len = uf_args_len;
8519 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008520 goto erret;
8521
8522 // If no type specified use the type of the default value.
8523 // Otherwise check that the default value type matches the
8524 // specified type.
8525 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008526 where.wt_index = arg_idx + 1;
8527 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008528 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008529 {
8530 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008531 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008532 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008533 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008534 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008535 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008536
8537 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008538 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008539
8540 // set instruction index in JUMP_IF_ARG_SET to here
8541 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8542 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008543 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008544
8545 if (did_set_arg_type)
8546 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008547 }
8548
8549 /*
8550 * Loop over all the lines of the function and generate instructions.
8551 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 for (;;)
8553 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008554 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008555 int starts_with_colon = FALSE;
8556 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008557 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008558
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008559 // Bail out on the first error to avoid a flood of errors and report
8560 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008561 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008562 goto erret;
8563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008564 if (line != NULL && *line == '|')
8565 // the line continues after a '|'
8566 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008567 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008568 && !(*line == '#' && (line == cctx.ctx_line_start
8569 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008571 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 goto erret;
8573 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008574 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8575 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008576 else
8577 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008578 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008579 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008580 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008581 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008582#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008583 if (cctx.ctx_skip != SKIP_YES)
8584 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008585#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008588 }
8589
Bram Moolenaara80faa82020-04-12 19:37:17 +02008590 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 ea.cmdlinep = &line;
8592 ea.cmd = skipwhite(line);
8593
Bram Moolenaarb2049902021-01-24 12:53:53 +01008594 if (*ea.cmd == '#')
8595 {
8596 // "#" starts a comment
8597 line = (char_u *)"";
8598 continue;
8599 }
8600
Bram Moolenaarf002a412021-01-24 13:34:18 +01008601#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008602 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8603 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008604 {
8605 may_generate_prof_end(&cctx, prof_lnum);
8606
8607 prof_lnum = cctx.ctx_lnum;
8608 generate_instr(&cctx, ISN_PROF_START);
8609 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008610#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008611
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008612 // Some things can be recognized by the first character.
8613 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008614 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008615 case '}':
8616 {
8617 // "}" ends a block scope
8618 scopetype_T stype = cctx.ctx_scope == NULL
8619 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008621 if (stype == BLOCK_SCOPE)
8622 {
8623 compile_endblock(&cctx);
8624 line = ea.cmd;
8625 }
8626 else
8627 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008628 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008629 goto erret;
8630 }
8631 if (line != NULL)
8632 line = skipwhite(ea.cmd + 1);
8633 continue;
8634 }
8635
8636 case '{':
8637 // "{" starts a block scope
8638 // "{'a': 1}->func() is something else
8639 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8640 {
8641 line = compile_block(ea.cmd, &cctx);
8642 continue;
8643 }
8644 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008645 }
8646
8647 /*
8648 * COMMAND MODIFIERS
8649 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008650 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008651 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8652 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653 {
8654 if (errormsg != NULL)
8655 goto erret;
8656 // empty line or comment
8657 line = (char_u *)"";
8658 continue;
8659 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008660 generate_cmdmods(&cctx, &local_cmdmod);
8661 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008663 // Check if there was a colon after the last command modifier or before
8664 // the current position.
8665 for (p = ea.cmd; p >= line; --p)
8666 {
8667 if (*p == ':')
8668 starts_with_colon = TRUE;
8669 if (p < ea.cmd && !VIM_ISWHITE(*p))
8670 break;
8671 }
8672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008674 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008675 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008676 {
8677 if (*ea.cmd == '(')
8678 // not for "call()"
8679 ea.cmd = p;
8680 else
8681 ea.cmd = skipwhite(ea.cmd);
8682 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008683
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008684 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008685 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008686 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008687
Bram Moolenaar17126b12021-01-07 22:03:02 +01008688 // Check for assignment after command modifiers.
8689 assign = may_compile_assignment(&ea, &line, &cctx);
8690 if (assign == OK)
8691 goto nextline;
8692 if (assign == FAIL)
8693 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694 }
8695
8696 /*
8697 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008698 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008699 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008700 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008701 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008702 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008703 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008704 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008705 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008706 if (!starts_with_colon)
8707 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008708 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008709 goto erret;
8710 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008711 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008712 if (ends_excmd2(line, ea.cmd))
8713 {
8714 // A range without a command: jump to the line.
8715 // TODO: compile to a more efficient command, possibly
8716 // calling parse_cmd_address().
8717 ea.cmdidx = CMD_SIZE;
8718 line = compile_exec(line, &ea, &cctx);
8719 goto nextline;
8720 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008721 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008722 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008723 p = find_ex_command(&ea, NULL, starts_with_colon
8724 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008725
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008726 if (p == NULL)
8727 {
8728 if (cctx.ctx_skip != SKIP_YES)
8729 emsg(_(e_ambiguous_use_of_user_defined_command));
8730 goto erret;
8731 }
8732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008733 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8734 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008735 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008736 {
8737 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008738 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008739 }
8740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008742 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008743 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008744 // CMD_var cannot happen, compile_assignment() above would be
8745 // used. Most likely an assignment to a non-existing variable.
8746 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008747 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008748 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008749 }
8750
Bram Moolenaar3988f642020-08-27 22:43:03 +02008751 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008752 && ea.cmdidx != CMD_elseif
8753 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008754 && ea.cmdidx != CMD_endif
8755 && ea.cmdidx != CMD_endfor
8756 && ea.cmdidx != CMD_endwhile
8757 && ea.cmdidx != CMD_catch
8758 && ea.cmdidx != CMD_finally
8759 && ea.cmdidx != CMD_endtry)
8760 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008761 emsg(_(e_unreachable_code_after_return));
8762 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008763 }
8764
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008765 p = skipwhite(p);
8766 if (ea.cmdidx != CMD_SIZE
8767 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8768 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008769 if (ea.cmdidx >= 0)
8770 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008771 if ((ea.argt & EX_BANG) && *p == '!')
8772 {
8773 ea.forceit = TRUE;
8774 p = skipwhite(p + 1);
8775 }
8776 }
8777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 switch (ea.cmdidx)
8779 {
8780 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008781 ea.arg = p;
8782 line = compile_nested_function(&ea, &cctx);
8783 break;
8784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008785 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008786 // TODO: should we allow this, e.g. to declare a global
8787 // function?
8788 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 goto erret;
8790
8791 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008792 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008793 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008794 break;
8795
8796 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008797 emsg(_(e_cannot_use_let_in_vim9_script));
8798 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008799 case CMD_var:
8800 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 case CMD_const:
8802 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008803 if (line == p)
8804 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 break;
8806
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008807 case CMD_unlet:
8808 case CMD_unlockvar:
8809 case CMD_lockvar:
8810 line = compile_unletlock(p, &ea, &cctx);
8811 break;
8812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813 case CMD_import:
8814 line = compile_import(p, &cctx);
8815 break;
8816
8817 case CMD_if:
8818 line = compile_if(p, &cctx);
8819 break;
8820 case CMD_elseif:
8821 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008822 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823 break;
8824 case CMD_else:
8825 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008826 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008827 break;
8828 case CMD_endif:
8829 line = compile_endif(p, &cctx);
8830 break;
8831
8832 case CMD_while:
8833 line = compile_while(p, &cctx);
8834 break;
8835 case CMD_endwhile:
8836 line = compile_endwhile(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
8840 case CMD_for:
8841 line = compile_for(p, &cctx);
8842 break;
8843 case CMD_endfor:
8844 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008845 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008846 break;
8847 case CMD_continue:
8848 line = compile_continue(p, &cctx);
8849 break;
8850 case CMD_break:
8851 line = compile_break(p, &cctx);
8852 break;
8853
8854 case CMD_try:
8855 line = compile_try(p, &cctx);
8856 break;
8857 case CMD_catch:
8858 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008859 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008860 break;
8861 case CMD_finally:
8862 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008863 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008864 break;
8865 case CMD_endtry:
8866 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008867 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008868 break;
8869 case CMD_throw:
8870 line = compile_throw(p, &cctx);
8871 break;
8872
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008873 case CMD_eval:
8874 if (compile_expr0(&p, &cctx) == FAIL)
8875 goto erret;
8876
Bram Moolenaar3988f642020-08-27 22:43:03 +02008877 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008878 generate_instr_drop(&cctx, ISN_DROP, 1);
8879
8880 line = skipwhite(p);
8881 break;
8882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008883 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008884 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008885 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008886 case CMD_echomsg:
8887 case CMD_echoerr:
8888 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008889 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008890
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008891 case CMD_put:
8892 ea.cmd = cmd;
8893 line = compile_put(p, &ea, &cctx);
8894 break;
8895
Bram Moolenaar3988f642020-08-27 22:43:03 +02008896 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008897
Bram Moolenaarae616492020-07-28 20:07:27 +02008898 case CMD_append:
8899 case CMD_change:
8900 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008901 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008902 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008903 case CMD_xit:
8904 not_in_vim9(&ea);
8905 goto erret;
8906
Bram Moolenaar002262f2020-07-08 17:47:57 +02008907 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008908 if (cctx.ctx_skip != SKIP_YES)
8909 {
8910 semsg(_(e_invalid_command_str), ea.cmd);
8911 goto erret;
8912 }
8913 // We don't check for a next command here.
8914 line = (char_u *)"";
8915 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008917 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008918 if (cctx.ctx_skip == SKIP_YES)
8919 {
8920 // We don't check for a next command here.
8921 line = (char_u *)"";
8922 }
8923 else
8924 {
8925 // Not recognized, execute with do_cmdline_cmd().
8926 ea.arg = p;
8927 line = compile_exec(line, &ea, &cctx);
8928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008929 break;
8930 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008931nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008932 if (line == NULL)
8933 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008934 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008935
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008936 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008937 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008939 if (cctx.ctx_type_stack.ga_len < 0)
8940 {
8941 iemsg("Type stack underflow");
8942 goto erret;
8943 }
8944 }
8945
8946 if (cctx.ctx_scope != NULL)
8947 {
8948 if (cctx.ctx_scope->se_type == IF_SCOPE)
8949 emsg(_(e_endif));
8950 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8951 emsg(_(e_endwhile));
8952 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8953 emsg(_(e_endfor));
8954 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008955 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 goto erret;
8957 }
8958
Bram Moolenaarefd88552020-06-18 20:50:10 +02008959 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008960 {
8961 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8962 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008963 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008964 goto erret;
8965 }
8966
8967 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008968 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008969 }
8970
Bram Moolenaar599410c2021-04-10 14:03:43 +02008971 // When compiled with ":silent!" and there was an error don't consider the
8972 // function compiled.
8973 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008974 {
8975 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8976 + ufunc->uf_dfunc_idx;
8977 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008978 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008979#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008980 if (cctx.ctx_profiling)
8981 {
8982 dfunc->df_instr_prof = instr->ga_data;
8983 dfunc->df_instr_prof_count = instr->ga_len;
8984 }
8985 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008986#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008987 {
8988 dfunc->df_instr = instr->ga_data;
8989 dfunc->df_instr_count = instr->ga_len;
8990 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008991 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008992 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008993 if (cctx.ctx_outer_used)
8994 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008995 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008997
8998 ret = OK;
8999
9000erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009001 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009002 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009003 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009004 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9005 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009006
9007 for (idx = 0; idx < instr->ga_len; ++idx)
9008 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009009 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009010 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009011
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009012 // If using the last entry in the table and it was added above, we
9013 // might as well remove it.
9014 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009015 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009016 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009017 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009018 ufunc->uf_dfunc_idx = 0;
9019 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009020 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009021
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009022 while (cctx.ctx_scope != NULL)
9023 drop_scope(&cctx);
9024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009025 if (errormsg != NULL)
9026 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009027 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009028 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009029 }
9030
9031 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009032 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009033 if (do_estack_push)
9034 estack_pop();
9035
Bram Moolenaar20431c92020-03-20 18:39:46 +01009036 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009037 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009038 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009039 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009040}
9041
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009042 void
9043set_function_type(ufunc_T *ufunc)
9044{
9045 int varargs = ufunc->uf_va_name != NULL;
9046 int argcount = ufunc->uf_args.ga_len;
9047
9048 // Create a type for the function, with the return type and any
9049 // argument types.
9050 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9051 // The type is included in "tt_args".
9052 if (argcount > 0 || varargs)
9053 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009054 if (ufunc->uf_type_list.ga_itemsize == 0)
9055 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009056 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9057 argcount, &ufunc->uf_type_list);
9058 // Add argument types to the function type.
9059 if (func_type_add_arg_types(ufunc->uf_func_type,
9060 argcount + varargs,
9061 &ufunc->uf_type_list) == FAIL)
9062 return;
9063 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9064 ufunc->uf_func_type->tt_min_argcount =
9065 argcount - ufunc->uf_def_args.ga_len;
9066 if (ufunc->uf_arg_types == NULL)
9067 {
9068 int i;
9069
9070 // lambda does not have argument types.
9071 for (i = 0; i < argcount; ++i)
9072 ufunc->uf_func_type->tt_args[i] = &t_any;
9073 }
9074 else
9075 mch_memmove(ufunc->uf_func_type->tt_args,
9076 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9077 if (varargs)
9078 {
9079 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009080 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009081 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9082 }
9083 }
9084 else
9085 // No arguments, can use a predefined type.
9086 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9087 argcount, &ufunc->uf_type_list);
9088}
9089
9090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009091/*
9092 * Delete an instruction, free what it contains.
9093 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009094 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009095delete_instr(isn_T *isn)
9096{
9097 switch (isn->isn_type)
9098 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009099 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009100 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009101 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009102 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009103 case ISN_LOADENV:
9104 case ISN_LOADG:
9105 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009106 case ISN_LOADT:
9107 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009108 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009109 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009110 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009111 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009112 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009113 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009114 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009115 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009116 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009117 case ISN_STOREW:
9118 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009119 vim_free(isn->isn_arg.string);
9120 break;
9121
9122 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009123 case ISN_STORES:
9124 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009125 break;
9126
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009127 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009128 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009129 vim_free(isn->isn_arg.unlet.ul_name);
9130 break;
9131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009132 case ISN_STOREOPT:
9133 vim_free(isn->isn_arg.storeopt.so_name);
9134 break;
9135
9136 case ISN_PUSHBLOB: // push blob isn_arg.blob
9137 blob_unref(isn->isn_arg.blob);
9138 break;
9139
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009140 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009142 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009143#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009144 break;
9145
9146 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009148 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009149#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009150 break;
9151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009152 case ISN_UCALL:
9153 vim_free(isn->isn_arg.ufunc.cuf_name);
9154 break;
9155
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009156 case ISN_FUNCREF:
9157 {
9158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9159 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009160 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009161
Bram Moolenaar077a4232020-12-22 18:33:27 +01009162 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9163 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009164 }
9165 break;
9166
9167 case ISN_DCALL:
9168 {
9169 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9170 + isn->isn_arg.dfunc.cdf_idx;
9171
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009172 if (dfunc->df_ufunc != NULL
9173 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009174 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009175 }
9176 break;
9177
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009178 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009179 {
9180 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9181 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9182
9183 if (ufunc != NULL)
9184 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009185 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009186 func_ptr_unref(ufunc);
9187 }
9188
9189 vim_free(lambda);
9190 vim_free(isn->isn_arg.newfunc.nf_global);
9191 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009192 break;
9193
Bram Moolenaar5e654232020-09-16 15:22:00 +02009194 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009195 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009196 free_type(isn->isn_arg.type.ct_type);
9197 break;
9198
Bram Moolenaar02194d22020-10-24 23:08:38 +02009199 case ISN_CMDMOD:
9200 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9201 ->cmod_filter_regmatch.regprog);
9202 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9203 break;
9204
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009205 case ISN_LOADSCRIPT:
9206 case ISN_STORESCRIPT:
9207 vim_free(isn->isn_arg.script.scriptref);
9208 break;
9209
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009210 case ISN_TRY:
9211 vim_free(isn->isn_arg.try.try_ref);
9212 break;
9213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009214 case ISN_2BOOL:
9215 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009216 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009217 case ISN_ADDBLOB:
9218 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009219 case ISN_ANYINDEX:
9220 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009221 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009222 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009223 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009224 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009226 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009227 case ISN_COMPAREANY:
9228 case ISN_COMPAREBLOB:
9229 case ISN_COMPAREBOOL:
9230 case ISN_COMPAREDICT:
9231 case ISN_COMPAREFLOAT:
9232 case ISN_COMPAREFUNC:
9233 case ISN_COMPARELIST:
9234 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 case ISN_COMPARESPECIAL:
9236 case ISN_COMPARESTRING:
9237 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009238 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009239 case ISN_DROP:
9240 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009241 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009242 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009243 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009244 case ISN_EXECCONCAT:
9245 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009246 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009247 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009248 case ISN_GETITEM:
9249 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009250 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009251 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009252 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009253 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009255 case ISN_LOADBDICT:
9256 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009257 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009259 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009261 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009262 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009263 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009264 case ISN_NEGATENR:
9265 case ISN_NEWDICT:
9266 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009267 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009268 case ISN_OPFLOAT:
9269 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009270 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009271 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009272 case ISN_PROF_END:
9273 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009274 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 case ISN_PUSHF:
9276 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009277 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009278 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009280 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009281 case ISN_SHUFFLE:
9282 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009283 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009284 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009285 case ISN_STORENR:
9286 case ISN_STOREOUTER:
9287 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009288 case ISN_STOREV:
9289 case ISN_STRINDEX:
9290 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009291 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009292 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009293 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009294 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009295 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296 // nothing allocated
9297 break;
9298 }
9299}
9300
9301/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009302 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009303 */
9304 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009305delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009306{
9307 int idx;
9308
9309 ga_clear(&dfunc->df_def_args_isn);
9310
9311 if (dfunc->df_instr != NULL)
9312 {
9313 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9314 delete_instr(dfunc->df_instr + idx);
9315 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009316 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009317 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009318#ifdef FEAT_PROFILE
9319 if (dfunc->df_instr_prof != NULL)
9320 {
9321 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9322 delete_instr(dfunc->df_instr_prof + idx);
9323 VIM_CLEAR(dfunc->df_instr_prof);
9324 dfunc->df_instr_prof = NULL;
9325 }
9326#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009327
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009328 if (mark_deleted)
9329 dfunc->df_deleted = TRUE;
9330 if (dfunc->df_ufunc != NULL)
9331 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009332}
9333
9334/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009335 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009336 * function, unless another user function still uses it.
9337 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009338 */
9339 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009340unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009341{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009342 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009343 {
9344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9345 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009346
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009347 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009348 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009349 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009350 ufunc->uf_dfunc_idx = 0;
9351 if (dfunc->df_ufunc == ufunc)
9352 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009353 }
9354}
9355
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009356/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009357 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009358 */
9359 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009360link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009361{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009362 if (ufunc->uf_dfunc_idx > 0)
9363 {
9364 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9365 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009366
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009367 ++dfunc->df_refcount;
9368 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009369}
9370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009372/*
9373 * Free all functions defined with ":def".
9374 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009375 void
9376free_def_functions(void)
9377{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009378 int idx;
9379
9380 for (idx = 0; idx < def_functions.ga_len; ++idx)
9381 {
9382 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9383
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009384 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009385 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009386 }
9387
9388 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009389}
9390#endif
9391
9392
9393#endif // FEAT_EVAL