blob: 15d2c30e94ab520e46a869208af2f523263511d4 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaarda479c72021-04-10 21:01:38 +0200425 // underscore argument is OK
426 if (len == 1 && *p == '_')
427 return OK;
428
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200429 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100430 {
431 if (is_arg)
432 semsg(_(e_argument_already_declared_in_script_str), p);
433 else
434 semsg(_(e_variable_already_declared_in_script_str), p);
435 return FAIL;
436 }
437
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100439 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100440 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200441 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200442 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200443 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100444 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200445 // A local or script-local function can shadow a global function.
446 if (ufunc == NULL || !func_is_global(ufunc)
447 || (p[0] == 'g' && p[1] == ':'))
448 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100449 if (is_arg)
450 semsg(_(e_argument_name_shadows_existing_variable_str), p);
451 else
452 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200453 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200454 return FAIL;
455 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100456 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200457 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100458 return OK;
459}
460
Bram Moolenaar65b95452020-07-19 14:03:09 +0200461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462/////////////////////////////////////////////////////////////////////
463// Following generate_ functions expect the caller to call ga_grow().
464
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200465#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
466#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Generate an instruction without arguments.
470 * Returns a pointer to the new instruction, NULL if failed.
471 */
472 static isn_T *
473generate_instr(cctx_T *cctx, isntype_T isn_type)
474{
475 garray_T *instr = &cctx->ctx_instr;
476 isn_T *isn;
477
Bram Moolenaar080457c2020-03-03 21:53:32 +0100478 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 if (ga_grow(instr, 1) == FAIL)
480 return NULL;
481 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
482 isn->isn_type = isn_type;
483 isn->isn_lnum = cctx->ctx_lnum + 1;
484 ++instr->ga_len;
485
486 return isn;
487}
488
489/*
490 * Generate an instruction without arguments.
491 * "drop" will be removed from the stack.
492 * Returns a pointer to the new instruction, NULL if failed.
493 */
494 static isn_T *
495generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
496{
497 garray_T *stack = &cctx->ctx_type_stack;
498
Bram Moolenaar080457c2020-03-03 21:53:32 +0100499 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 stack->ga_len -= drop;
501 return generate_instr(cctx, isn_type);
502}
503
504/*
505 * Generate instruction "isn_type" and put "type" on the type stack.
506 */
507 static isn_T *
508generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
509{
510 isn_T *isn;
511 garray_T *stack = &cctx->ctx_type_stack;
512
513 if ((isn = generate_instr(cctx, isn_type)) == NULL)
514 return NULL;
515
516 if (ga_grow(stack, 1) == FAIL)
517 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200518 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 ++stack->ga_len;
520
521 return isn;
522}
523
524/*
525 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200526 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 */
528 static int
529may_generate_2STRING(int offset, cctx_T *cctx)
530{
531 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200532 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100534 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100536 RETURN_OK_IF_SKIP(cctx);
537 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200538 switch ((*type)->tt_type)
539 {
540 // nothing to be done
541 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100542
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200543 // conversion possible
544 case VAR_SPECIAL:
545 case VAR_BOOL:
546 case VAR_NUMBER:
547 case VAR_FLOAT:
548 break;
549
550 // conversion possible (with runtime check)
551 case VAR_ANY:
552 case VAR_UNKNOWN:
553 isntype = ISN_2STRING_ANY;
554 break;
555
556 // conversion not possible
557 case VAR_VOID:
558 case VAR_BLOB:
559 case VAR_FUNC:
560 case VAR_PARTIAL:
561 case VAR_LIST:
562 case VAR_DICT:
563 case VAR_JOB:
564 case VAR_CHANNEL:
565 to_string_error((*type)->tt_type);
566 return FAIL;
567 }
568
569 *type = &t_string;
570 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return FAIL;
572 isn->isn_arg.number = offset;
573
574 return OK;
575}
576
577 static int
578check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
579{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200580 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200582 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 {
584 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200585 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200587 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 }
590 return OK;
591}
592
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200593 static int
594generate_add_instr(
595 cctx_T *cctx,
596 vartype_T vartype,
597 type_T *type1,
598 type_T *type2)
599{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100600 garray_T *stack = &cctx->ctx_type_stack;
601 isn_T *isn = generate_instr_drop(cctx,
602 vartype == VAR_NUMBER ? ISN_OPNR
603 : vartype == VAR_LIST ? ISN_ADDLIST
604 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100606 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200607#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100608 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200609
610 if (vartype != VAR_LIST && vartype != VAR_BLOB
611 && type1->tt_type != VAR_ANY
612 && type2->tt_type != VAR_ANY
613 && check_number_or_float(
614 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
615 return FAIL;
616
617 if (isn != NULL)
618 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100619
620 // When concatenating two lists with different member types the member type
621 // becomes "any".
622 if (vartype == VAR_LIST
623 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
624 && type1->tt_member != type2->tt_member)
625 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
626
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200627 return isn == NULL ? FAIL : OK;
628}
629
630/*
631 * Get the type to use for an instruction for an operation on "type1" and
632 * "type2". If they are matching use a type-specific instruction. Otherwise
633 * fall back to runtime type checking.
634 */
635 static vartype_T
636operator_type(type_T *type1, type_T *type2)
637{
638 if (type1->tt_type == type2->tt_type
639 && (type1->tt_type == VAR_NUMBER
640 || type1->tt_type == VAR_LIST
641#ifdef FEAT_FLOAT
642 || type1->tt_type == VAR_FLOAT
643#endif
644 || type1->tt_type == VAR_BLOB))
645 return type1->tt_type;
646 return VAR_ANY;
647}
648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649/*
650 * Generate an instruction with two arguments. The instruction depends on the
651 * type of the arguments.
652 */
653 static int
654generate_two_op(cctx_T *cctx, char_u *op)
655{
656 garray_T *stack = &cctx->ctx_type_stack;
657 type_T *type1;
658 type_T *type2;
659 vartype_T vartype;
660 isn_T *isn;
661
Bram Moolenaar080457c2020-03-03 21:53:32 +0100662 RETURN_OK_IF_SKIP(cctx);
663
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200664 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
666 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668
669 switch (*op)
670 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200671 case '+':
672 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 break;
675
676 case '-':
677 case '*':
678 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
679 op) == FAIL)
680 return FAIL;
681 if (vartype == VAR_NUMBER)
682 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
683#ifdef FEAT_FLOAT
684 else if (vartype == VAR_FLOAT)
685 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
686#endif
687 else
688 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
689 if (isn != NULL)
690 isn->isn_arg.op.op_type = *op == '*'
691 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
692 break;
693
Bram Moolenaar4c683752020-04-05 21:38:23 +0200694 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200696 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 && type2->tt_type != VAR_NUMBER))
698 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200699 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 return FAIL;
701 }
702 isn = generate_instr_drop(cctx,
703 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
704 if (isn != NULL)
705 isn->isn_arg.op.op_type = EXPR_REM;
706 break;
707 }
708
709 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200710 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 {
712 type_T *type = &t_any;
713
714#ifdef FEAT_FLOAT
715 // float+number and number+float results in float
716 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
717 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
718 type = &t_float;
719#endif
720 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
721 }
722
723 return OK;
724}
725
726/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 * Get the instruction to use for comparing "type1" with "type2"
728 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100731get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732{
733 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
Bram Moolenaar4c683752020-04-05 21:38:23 +0200735 if (type1 == VAR_UNKNOWN)
736 type1 = VAR_ANY;
737 if (type2 == VAR_UNKNOWN)
738 type2 = VAR_ANY;
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 if (type1 == type2)
741 {
742 switch (type1)
743 {
744 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
745 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
746 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
747 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
748 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
749 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
750 case VAR_LIST: isntype = ISN_COMPARELIST; break;
751 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
752 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 default: isntype = ISN_COMPAREANY; break;
754 }
755 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200756 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100758 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 isntype = ISN_COMPAREANY;
760
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 && (isntype == ISN_COMPAREBOOL
763 || isntype == ISN_COMPARESPECIAL
764 || isntype == ISN_COMPARENR
765 || isntype == ISN_COMPAREFLOAT))
766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200767 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200769 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 }
771 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100772 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
774 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
776 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 && (type1 == VAR_BLOB || type2 == VAR_BLOB
778 || type1 == VAR_LIST || type2 == VAR_LIST))))
779 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200780 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200782 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200784 return isntype;
785}
786
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200787 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100788check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200789{
790 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
791 return FAIL;
792 return OK;
793}
794
Bram Moolenaara5565e42020-05-09 15:44:01 +0200795/*
796 * Generate an ISN_COMPARE* instruction with a boolean result.
797 */
798 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100799generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200800{
801 isntype_T isntype;
802 isn_T *isn;
803 garray_T *stack = &cctx->ctx_type_stack;
804 vartype_T type1;
805 vartype_T type2;
806
807 RETURN_OK_IF_SKIP(cctx);
808
809 // Get the known type of the two items on the stack. If they are matching
810 // use a type-specific instruction. Otherwise fall back to runtime type
811 // checking.
812 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
813 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100814 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200815 if (isntype == ISN_DROP)
816 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817
818 if ((isn = generate_instr(cctx, isntype)) == NULL)
819 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100820 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 isn->isn_arg.op.op_ic = ic;
822
823 // takes two arguments, puts one bool back
824 if (stack->ga_len >= 2)
825 {
826 --stack->ga_len;
827 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
828 }
829
830 return OK;
831}
832
833/*
834 * Generate an ISN_2BOOL instruction.
835 */
836 static int
837generate_2BOOL(cctx_T *cctx, int invert)
838{
839 isn_T *isn;
840 garray_T *stack = &cctx->ctx_type_stack;
841
Bram Moolenaar080457c2020-03-03 21:53:32 +0100842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
844 return FAIL;
845 isn->isn_arg.number = invert;
846
847 // type becomes bool
848 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
849
850 return OK;
851}
852
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200853/*
854 * Generate an ISN_COND2BOOL instruction.
855 */
856 static int
857generate_COND2BOOL(cctx_T *cctx)
858{
859 isn_T *isn;
860 garray_T *stack = &cctx->ctx_type_stack;
861
862 RETURN_OK_IF_SKIP(cctx);
863 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
864 return FAIL;
865
866 // type becomes bool
867 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
868
869 return OK;
870}
871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200873generate_TYPECHECK(
874 cctx_T *cctx,
875 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100876 int offset,
877 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878{
879 isn_T *isn;
880 garray_T *stack = &cctx->ctx_type_stack;
881
Bram Moolenaar080457c2020-03-03 21:53:32 +0100882 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
884 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100886 isn->isn_arg.type.ct_off = (int8_T)offset;
887 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888
Bram Moolenaar5e654232020-09-16 15:22:00 +0200889 // type becomes expected
890 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
892 return OK;
893}
894
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100895 static int
896generate_SETTYPE(
897 cctx_T *cctx,
898 type_T *expected)
899{
900 isn_T *isn;
901
902 RETURN_OK_IF_SKIP(cctx);
903 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
904 return FAIL;
905 isn->isn_arg.type.ct_type = alloc_type(expected);
906 return OK;
907}
908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
911 * used. Return FALSE if the types will never match.
912 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100913 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200914use_typecheck(type_T *actual, type_T *expected)
915{
916 if (actual->tt_type == VAR_ANY
917 || actual->tt_type == VAR_UNKNOWN
918 || (actual->tt_type == VAR_FUNC
919 && (expected->tt_type == VAR_FUNC
920 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100921 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
922 && ((actual->tt_member == &t_void)
923 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200924 return TRUE;
925 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
926 && actual->tt_type == expected->tt_type)
927 // This takes care of a nested list or dict.
928 return use_typecheck(actual->tt_member, expected->tt_member);
929 return FALSE;
930}
931
932/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200933 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200934 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 * - "actual" is a type that can be "expected" type: add a runtime check; or
936 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200937 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
938 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200939 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100940 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200941need_type(
942 type_T *actual,
943 type_T *expected,
944 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100945 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200946 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200947 int silent,
948 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200949{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100950 where_T where;
951
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200952 if (expected == &t_bool && actual != &t_bool
953 && (actual->tt_flags & TTFLAG_BOOL_OK))
954 {
955 // Using "0", "1" or the result of an expression with "&&" or "||" as a
956 // boolean is OK but requires a conversion.
957 generate_2BOOL(cctx, FALSE);
958 return OK;
959 }
960
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100961 where.wt_index = arg_idx;
962 where.wt_variable = FALSE;
963 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200964 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200965
966 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200967 // If it's a constant a runtime check makes no sense.
968 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200969 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100970 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200971 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200973
974 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100975 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200977}
978
979/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100980 * Check that the top of the type stack has a type that can be used as a
981 * condition. Give an error and return FAIL if not.
982 */
983 static int
984bool_on_stack(cctx_T *cctx)
985{
986 garray_T *stack = &cctx->ctx_type_stack;
987 type_T *type;
988
989 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
990 if (type == &t_bool)
991 return OK;
992
993 if (type == &t_any || type == &t_number)
994 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
995 // This requires a runtime type check.
996 return generate_COND2BOOL(cctx);
997
Bram Moolenaar351ead02021-01-16 16:07:01 +0100998 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100999}
1000
1001/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 * Generate an ISN_PUSHNR instruction.
1003 */
1004 static int
1005generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1006{
1007 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001008 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009
Bram Moolenaar080457c2020-03-03 21:53:32 +01001010 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1012 return FAIL;
1013 isn->isn_arg.number = number;
1014
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001015 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001016 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001017 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 return OK;
1019}
1020
1021/*
1022 * Generate an ISN_PUSHBOOL instruction.
1023 */
1024 static int
1025generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1026{
1027 isn_T *isn;
1028
Bram Moolenaar080457c2020-03-03 21:53:32 +01001029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.number = number;
1033
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_PUSHSPEC instruction.
1039 */
1040 static int
1041generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1042{
1043 isn_T *isn;
1044
Bram Moolenaar080457c2020-03-03 21:53:32 +01001045 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1047 return FAIL;
1048 isn->isn_arg.number = number;
1049
1050 return OK;
1051}
1052
1053#ifdef FEAT_FLOAT
1054/*
1055 * Generate an ISN_PUSHF instruction.
1056 */
1057 static int
1058generate_PUSHF(cctx_T *cctx, float_T fnumber)
1059{
1060 isn_T *isn;
1061
Bram Moolenaar080457c2020-03-03 21:53:32 +01001062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.fnumber = fnumber;
1066
1067 return OK;
1068}
1069#endif
1070
1071/*
1072 * Generate an ISN_PUSHS instruction.
1073 * Consumes "str".
1074 */
1075 static int
1076generate_PUSHS(cctx_T *cctx, char_u *str)
1077{
1078 isn_T *isn;
1079
Bram Moolenaar508b5612021-01-02 18:17:26 +01001080 if (cctx->ctx_skip == SKIP_YES)
1081 {
1082 vim_free(str);
1083 return OK;
1084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1086 return FAIL;
1087 isn->isn_arg.string = str;
1088
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001093 * Generate an ISN_PUSHCHANNEL instruction.
1094 * Consumes "channel".
1095 */
1096 static int
1097generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1098{
1099 isn_T *isn;
1100
Bram Moolenaar080457c2020-03-03 21:53:32 +01001101 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001102 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1103 return FAIL;
1104 isn->isn_arg.channel = channel;
1105
1106 return OK;
1107}
1108
1109/*
1110 * Generate an ISN_PUSHJOB instruction.
1111 * Consumes "job".
1112 */
1113 static int
1114generate_PUSHJOB(cctx_T *cctx, job_T *job)
1115{
1116 isn_T *isn;
1117
Bram Moolenaar080457c2020-03-03 21:53:32 +01001118 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001119 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001120 return FAIL;
1121 isn->isn_arg.job = job;
1122
1123 return OK;
1124}
1125
1126/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 * Generate an ISN_PUSHBLOB instruction.
1128 * Consumes "blob".
1129 */
1130 static int
1131generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1132{
1133 isn_T *isn;
1134
Bram Moolenaar080457c2020-03-03 21:53:32 +01001135 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1137 return FAIL;
1138 isn->isn_arg.blob = blob;
1139
1140 return OK;
1141}
1142
1143/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001144 * Generate an ISN_PUSHFUNC instruction with name "name".
1145 * Consumes "name".
1146 */
1147 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001148generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001149{
1150 isn_T *isn;
1151
Bram Moolenaar080457c2020-03-03 21:53:32 +01001152 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001153 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001154 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001155 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001156
1157 return OK;
1158}
1159
1160/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001161 * Generate an ISN_GETITEM instruction with "index".
1162 */
1163 static int
1164generate_GETITEM(cctx_T *cctx, int index)
1165{
1166 isn_T *isn;
1167 garray_T *stack = &cctx->ctx_type_stack;
1168 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1169 type_T *item_type = &t_any;
1170
1171 RETURN_OK_IF_SKIP(cctx);
1172
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001173 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001174 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 emsg(_(e_listreq));
1177 return FAIL;
1178 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001179 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001180 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1181 return FAIL;
1182 isn->isn_arg.number = index;
1183
1184 // add the item type to the type stack
1185 if (ga_grow(stack, 1) == FAIL)
1186 return FAIL;
1187 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1188 ++stack->ga_len;
1189 return OK;
1190}
1191
1192/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001193 * Generate an ISN_SLICE instruction with "count".
1194 */
1195 static int
1196generate_SLICE(cctx_T *cctx, int count)
1197{
1198 isn_T *isn;
1199
1200 RETURN_OK_IF_SKIP(cctx);
1201 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1202 return FAIL;
1203 isn->isn_arg.number = count;
1204 return OK;
1205}
1206
1207/*
1208 * Generate an ISN_CHECKLEN instruction with "min_len".
1209 */
1210 static int
1211generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1212{
1213 isn_T *isn;
1214
1215 RETURN_OK_IF_SKIP(cctx);
1216
1217 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1218 return FAIL;
1219 isn->isn_arg.checklen.cl_min_len = min_len;
1220 isn->isn_arg.checklen.cl_more_OK = more_OK;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 * Generate an ISN_STORE instruction.
1227 */
1228 static int
1229generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1235 return FAIL;
1236 if (name != NULL)
1237 isn->isn_arg.string = vim_strsave(name);
1238 else
1239 isn->isn_arg.number = idx;
1240
1241 return OK;
1242}
1243
1244/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001245 * Generate an ISN_STOREOUTER instruction.
1246 */
1247 static int
1248generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1249{
1250 isn_T *isn;
1251
1252 RETURN_OK_IF_SKIP(cctx);
1253 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1254 return FAIL;
1255 isn->isn_arg.outer.outer_idx = idx;
1256 isn->isn_arg.outer.outer_depth = level;
1257
1258 return OK;
1259}
1260
1261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1263 */
1264 static int
1265generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1266{
1267 isn_T *isn;
1268
Bram Moolenaar080457c2020-03-03 21:53:32 +01001269 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1271 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001272 isn->isn_arg.storenr.stnr_idx = idx;
1273 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
1275 return OK;
1276}
1277
1278/*
1279 * Generate an ISN_STOREOPT instruction
1280 */
1281 static int
1282generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1283{
1284 isn_T *isn;
1285
Bram Moolenaar080457c2020-03-03 21:53:32 +01001286 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001287 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 return FAIL;
1289 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1290 isn->isn_arg.storeopt.so_flags = opt_flags;
1291
1292 return OK;
1293}
1294
1295/*
1296 * Generate an ISN_LOAD or similar instruction.
1297 */
1298 static int
1299generate_LOAD(
1300 cctx_T *cctx,
1301 isntype_T isn_type,
1302 int idx,
1303 char_u *name,
1304 type_T *type)
1305{
1306 isn_T *isn;
1307
Bram Moolenaar080457c2020-03-03 21:53:32 +01001308 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1310 return FAIL;
1311 if (name != NULL)
1312 isn->isn_arg.string = vim_strsave(name);
1313 else
1314 isn->isn_arg.number = idx;
1315
1316 return OK;
1317}
1318
1319/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001320 * Generate an ISN_LOADOUTER instruction
1321 */
1322 static int
1323generate_LOADOUTER(
1324 cctx_T *cctx,
1325 int idx,
1326 int nesting,
1327 type_T *type)
1328{
1329 isn_T *isn;
1330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1333 return FAIL;
1334 isn->isn_arg.outer.outer_idx = idx;
1335 isn->isn_arg.outer.outer_depth = nesting;
1336
1337 return OK;
1338}
1339
1340/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001341 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 */
1343 static int
1344generate_LOADV(
1345 cctx_T *cctx,
1346 char_u *name,
1347 int error)
1348{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001349 int di_flags;
1350 int vidx = find_vim_var(name, &di_flags);
1351 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352
Bram Moolenaar080457c2020-03-03 21:53:32 +01001353 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 if (vidx < 0)
1355 {
1356 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001357 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001358 return FAIL;
1359 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001362 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363}
1364
1365/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366 * Generate an ISN_UNLET instruction.
1367 */
1368 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001369generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001370{
1371 isn_T *isn;
1372
1373 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001374 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001375 return FAIL;
1376 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1377 isn->isn_arg.unlet.ul_forceit = forceit;
1378
1379 return OK;
1380}
1381
1382/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001383 * Generate an ISN_LOCKCONST instruction.
1384 */
1385 static int
1386generate_LOCKCONST(cctx_T *cctx)
1387{
1388 isn_T *isn;
1389
1390 RETURN_OK_IF_SKIP(cctx);
1391 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1392 return FAIL;
1393 return OK;
1394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Generate an ISN_LOADS instruction.
1398 */
1399 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001402 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001404 int sid,
1405 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406{
1407 isn_T *isn;
1408
Bram Moolenaar080457c2020-03-03 21:53:32 +01001409 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001410 if (isn_type == ISN_LOADS)
1411 isn = generate_instr_type(cctx, isn_type, type);
1412 else
1413 isn = generate_instr_drop(cctx, isn_type, 1);
1414 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001416 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1417 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418
1419 return OK;
1420}
1421
1422/*
1423 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1424 */
1425 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001426generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 cctx_T *cctx,
1428 isntype_T isn_type,
1429 int sid,
1430 int idx,
1431 type_T *type)
1432{
1433 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001434 scriptref_T *sref;
1435 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
Bram Moolenaar080457c2020-03-03 21:53:32 +01001437 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 if (isn_type == ISN_LOADSCRIPT)
1439 isn = generate_instr_type(cctx, isn_type, type);
1440 else
1441 isn = generate_instr_drop(cctx, isn_type, 1);
1442 if (isn == NULL)
1443 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001444
1445 // This requires three arguments, which doesn't fit in an instruction, thus
1446 // we need to allocate a struct for this.
1447 sref = ALLOC_ONE(scriptref_T);
1448 if (sref == NULL)
1449 return FAIL;
1450 isn->isn_arg.script.scriptref = sref;
1451 sref->sref_sid = sid;
1452 sref->sref_idx = idx;
1453 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001454 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 return OK;
1456}
1457
1458/*
1459 * Generate an ISN_NEWLIST instruction.
1460 */
1461 static int
1462generate_NEWLIST(cctx_T *cctx, int count)
1463{
1464 isn_T *isn;
1465 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 type_T *type;
1467 type_T *member;
1468
Bram Moolenaar080457c2020-03-03 21:53:32 +01001469 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1471 return FAIL;
1472 isn->isn_arg.number = count;
1473
Bram Moolenaar127542b2020-08-09 17:22:04 +02001474 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001475 if (count == 0)
1476 member = &t_void;
1477 else
1478 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001479 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1480 cctx->ctx_type_list);
1481 type = get_list_type(member, cctx->ctx_type_list);
1482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 // drop the value types
1484 stack->ga_len -= count;
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // add the list type to the type stack
1487 if (ga_grow(stack, 1) == FAIL)
1488 return FAIL;
1489 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1490 ++stack->ga_len;
1491
1492 return OK;
1493}
1494
1495/*
1496 * Generate an ISN_NEWDICT instruction.
1497 */
1498 static int
1499generate_NEWDICT(cctx_T *cctx, int count)
1500{
1501 isn_T *isn;
1502 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 type_T *type;
1504 type_T *member;
1505
Bram Moolenaar080457c2020-03-03 21:53:32 +01001506 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1508 return FAIL;
1509 isn->isn_arg.number = count;
1510
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001511 if (count == 0)
1512 member = &t_void;
1513 else
1514 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001515 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1516 cctx->ctx_type_list);
1517 type = get_dict_type(member, cctx->ctx_type_list);
1518
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 // drop the key and value types
1520 stack->ga_len -= 2 * count;
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // add the dict type to the type stack
1523 if (ga_grow(stack, 1) == FAIL)
1524 return FAIL;
1525 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1526 ++stack->ga_len;
1527
1528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_FUNCREF instruction.
1533 */
1534 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001535generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539
Bram Moolenaar080457c2020-03-03 21:53:32 +01001540 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1542 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001543 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001544 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001546 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001547 // the nested context, including this one.
1548 if (ufunc->uf_flags & FC_CLOSURE)
1549 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if (ga_grow(stack, 1) == FAIL)
1552 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001553 ((type_T **)stack->ga_data)[stack->ga_len] =
1554 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 ++stack->ga_len;
1556
1557 return OK;
1558}
1559
1560/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001561 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001562 * "lambda_name" and "func_name" must be in allocated memory and will be
1563 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 */
1565 static int
1566generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1567{
1568 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001569
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001570 if (cctx->ctx_skip == SKIP_YES)
1571 {
1572 vim_free(lambda_name);
1573 vim_free(func_name);
1574 return OK;
1575 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 {
1578 vim_free(lambda_name);
1579 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001580 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001581 }
1582 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 isn->isn_arg.newfunc.nf_global = func_name;
1584
1585 return OK;
1586}
1587
1588/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001589 * Generate an ISN_DEF instruction: list functions
1590 */
1591 static int
1592generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1593{
1594 isn_T *isn;
1595
1596 RETURN_OK_IF_SKIP(cctx);
1597 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1598 return FAIL;
1599 if (len > 0)
1600 {
1601 isn->isn_arg.string = vim_strnsave(name, len);
1602 if (isn->isn_arg.string == NULL)
1603 return FAIL;
1604 }
1605 return OK;
1606}
1607
1608/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 * Generate an ISN_JUMP instruction.
1610 */
1611 static int
1612generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1613{
1614 isn_T *isn;
1615 garray_T *stack = &cctx->ctx_type_stack;
1616
Bram Moolenaar080457c2020-03-03 21:53:32 +01001617 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1619 return FAIL;
1620 isn->isn_arg.jump.jump_when = when;
1621 isn->isn_arg.jump.jump_where = where;
1622
1623 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1624 --stack->ga_len;
1625
1626 return OK;
1627}
1628
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001629/*
1630 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1631 */
1632 static int
1633generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1634{
1635 isn_T *isn;
1636
1637 RETURN_OK_IF_SKIP(cctx);
1638 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1641 // jump_where is set later
1642 return OK;
1643}
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 static int
1646generate_FOR(cctx_T *cctx, int loop_idx)
1647{
1648 isn_T *isn;
1649 garray_T *stack = &cctx->ctx_type_stack;
1650
Bram Moolenaar080457c2020-03-03 21:53:32 +01001651 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.forloop.for_idx = loop_idx;
1655
1656 if (ga_grow(stack, 1) == FAIL)
1657 return FAIL;
1658 // type doesn't matter, will be stored next
1659 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1660 ++stack->ga_len;
1661
1662 return OK;
1663}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001664/*
1665 * Generate an ISN_TRYCONT instruction.
1666 */
1667 static int
1668generate_TRYCONT(cctx_T *cctx, int levels, int where)
1669{
1670 isn_T *isn;
1671
1672 RETURN_OK_IF_SKIP(cctx);
1673 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1674 return FAIL;
1675 isn->isn_arg.trycont.tct_levels = levels;
1676 isn->isn_arg.trycont.tct_where = where;
1677
1678 return OK;
1679}
1680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681
1682/*
1683 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 * Return FAIL if the number of arguments is wrong.
1686 */
1687 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001688generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689{
1690 isn_T *isn;
1691 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001692 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001693 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001694 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695
Bram Moolenaar080457c2020-03-03 21:53:32 +01001696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 argoff = check_internal_func(func_idx, argcount);
1698 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 return FAIL;
1700
Bram Moolenaar389df252020-07-09 21:20:47 +02001701 if (method_call && argoff > 1)
1702 {
1703 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1704 return FAIL;
1705 isn->isn_arg.shuffle.shfl_item = argcount;
1706 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1707 }
1708
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001709 if (argcount > 0)
1710 {
1711 // Check the types of the arguments.
1712 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001713 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1714 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001716 if (internal_func_is_map(func_idx))
1717 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001718 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1721 return FAIL;
1722 isn->isn_arg.bfunc.cbf_idx = func_idx;
1723 isn->isn_arg.bfunc.cbf_argcount = argcount;
1724
Bram Moolenaar94738d82020-10-21 14:25:07 +02001725 // Drop the argument types and push the return type.
1726 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 if (ga_grow(stack, 1) == FAIL)
1728 return FAIL;
1729 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001730 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001731 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733 if (maptype != NULL && maptype->tt_member != NULL
1734 && maptype->tt_member != &t_any)
1735 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001736 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 return OK;
1739}
1740
1741/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001742 * Generate an ISN_LISTAPPEND instruction. Works like add().
1743 * Argument count is already checked.
1744 */
1745 static int
1746generate_LISTAPPEND(cctx_T *cctx)
1747{
1748 garray_T *stack = &cctx->ctx_type_stack;
1749 type_T *list_type;
1750 type_T *item_type;
1751 type_T *expected;
1752
1753 // Caller already checked that list_type is a list.
1754 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1755 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1756 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001757 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001758 return FAIL;
1759
1760 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1761 return FAIL;
1762
1763 --stack->ga_len; // drop the argument
1764 return OK;
1765}
1766
1767/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001768 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1769 * Argument count is already checked.
1770 */
1771 static int
1772generate_BLOBAPPEND(cctx_T *cctx)
1773{
1774 garray_T *stack = &cctx->ctx_type_stack;
1775 type_T *item_type;
1776
1777 // Caller already checked that blob_type is a blob.
1778 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001779 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001780 return FAIL;
1781
1782 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1783 return FAIL;
1784
1785 --stack->ga_len; // drop the argument
1786 return OK;
1787}
1788
1789/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 * Return TRUE if "ufunc" should be compiled, taking into account whether
1791 * "profile" indicates profiling is to be done.
1792 */
1793 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001794func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001795{
1796 switch (ufunc->uf_def_status)
1797 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001798 case UF_TO_BE_COMPILED:
1799 return TRUE;
1800
Bram Moolenaarb2049902021-01-24 12:53:53 +01001801 case UF_COMPILED:
1802 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001803#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001804 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1805 + ufunc->uf_dfunc_idx;
1806
1807 return profile ? dfunc->df_instr_prof == NULL
1808 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001809#else
1810 break;
1811#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001812 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001813
1814 case UF_NOT_COMPILED:
1815 case UF_COMPILE_ERROR:
1816 case UF_COMPILING:
1817 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001818 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001819 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001820}
1821
1822/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 * Generate an ISN_DCALL or ISN_UCALL instruction.
1824 * Return FAIL if the number of arguments is wrong.
1825 */
1826 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001827generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828{
1829 isn_T *isn;
1830 garray_T *stack = &cctx->ctx_type_stack;
1831 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001832 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833
Bram Moolenaar080457c2020-03-03 21:53:32 +01001834 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 if (argcount > regular_args && !has_varargs(ufunc))
1836 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001837 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 return FAIL;
1839 }
1840 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1841 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001842 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 return FAIL;
1844 }
1845
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001846 if (ufunc->uf_def_status != UF_NOT_COMPILED
1847 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001848 {
1849 int i;
1850
1851 for (i = 0; i < argcount; ++i)
1852 {
1853 type_T *expected;
1854 type_T *actual;
1855
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001856 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1857 if (actual == &t_special
1858 && i >= regular_args - ufunc->uf_def_args.ga_len)
1859 {
1860 // assume v:none used for default argument value
1861 continue;
1862 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001863 if (i < regular_args)
1864 {
1865 if (ufunc->uf_arg_types == NULL)
1866 continue;
1867 expected = ufunc->uf_arg_types[i];
1868 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001869 else if (ufunc->uf_va_type == NULL
1870 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001871 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001872 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 else
1874 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001875 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001876 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001877 {
1878 arg_type_mismatch(expected, actual, i + 1);
1879 return FAIL;
1880 }
1881 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001882 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001883 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001884 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001885 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001886 }
1887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001889 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001890 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001892 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 {
1894 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1895 isn->isn_arg.dfunc.cdf_argcount = argcount;
1896 }
1897 else
1898 {
1899 // A user function may be deleted and redefined later, can't use the
1900 // ufunc pointer, need to look it up again at runtime.
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903 }
1904
1905 stack->ga_len -= argcount; // drop the arguments
1906 if (ga_grow(stack, 1) == FAIL)
1907 return FAIL;
1908 // add return value
1909 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1910 ++stack->ga_len;
1911
1912 return OK;
1913}
1914
1915/*
1916 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1917 */
1918 static int
1919generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1920{
1921 isn_T *isn;
1922 garray_T *stack = &cctx->ctx_type_stack;
1923
Bram Moolenaar080457c2020-03-03 21:53:32 +01001924 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1926 return FAIL;
1927 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1928 isn->isn_arg.ufunc.cuf_argcount = argcount;
1929
1930 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001931 if (ga_grow(stack, 1) == FAIL)
1932 return FAIL;
1933 // add return value
1934 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1935 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
1937 return OK;
1938}
1939
1940/*
1941 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001942 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 */
1944 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001945generate_PCALL(
1946 cctx_T *cctx,
1947 int argcount,
1948 char_u *name,
1949 type_T *type,
1950 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951{
1952 isn_T *isn;
1953 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaar080457c2020-03-03 21:53:32 +01001956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001957
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001958 if (type->tt_type == VAR_ANY)
1959 ret_type = &t_any;
1960 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001961 {
1962 if (type->tt_argcount != -1)
1963 {
1964 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1965
1966 if (argcount < type->tt_min_argcount - varargs)
1967 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001968 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001969 return FAIL;
1970 }
1971 if (!varargs && argcount > type->tt_argcount)
1972 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001973 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 return FAIL;
1975 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001976 if (type->tt_args != NULL)
1977 {
1978 int i;
1979
1980 for (i = 0; i < argcount; ++i)
1981 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02001982 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001983 type_T *actual = ((type_T **)stack->ga_data)[
1984 stack->ga_len + offset];
1985 type_T *expected;
1986
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001987 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001988 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001989 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001990 else if (i >= type->tt_min_argcount
1991 && actual == &t_special)
1992 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001993 else
1994 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001995 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001996 cctx, TRUE, FALSE) == FAIL)
1997 {
1998 arg_type_mismatch(expected, actual, i + 1);
1999 return FAIL;
2000 }
2001 }
2002 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002003 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002004 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002005 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002006 else
2007 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002008 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002009 return FAIL;
2010 }
2011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2013 return FAIL;
2014 isn->isn_arg.pfunc.cpf_top = at_top;
2015 isn->isn_arg.pfunc.cpf_argcount = argcount;
2016
2017 stack->ga_len -= argcount; // drop the arguments
2018
2019 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002022 // If partial is above the arguments it must be cleared and replaced with
2023 // the return value.
2024 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2025 return FAIL;
2026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 return OK;
2028}
2029
2030/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002031 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 */
2033 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002034generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035{
2036 isn_T *isn;
2037 garray_T *stack = &cctx->ctx_type_stack;
2038 type_T *type;
2039
Bram Moolenaar080457c2020-03-03 21:53:32 +01002040 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002041 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002043 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002045 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002047 if (type->tt_type != VAR_DICT && type != &t_any)
2048 {
2049 emsg(_(e_dictreq));
2050 return FAIL;
2051 }
2052 // change dict type to dict member type
2053 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002054 {
2055 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2056 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058
2059 return OK;
2060}
2061
2062/*
2063 * Generate an ISN_ECHO instruction.
2064 */
2065 static int
2066generate_ECHO(cctx_T *cctx, int with_white, int count)
2067{
2068 isn_T *isn;
2069
Bram Moolenaar080457c2020-03-03 21:53:32 +01002070 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2072 return FAIL;
2073 isn->isn_arg.echo.echo_with_white = with_white;
2074 isn->isn_arg.echo.echo_count = count;
2075
2076 return OK;
2077}
2078
Bram Moolenaarad39c092020-02-26 18:23:43 +01002079/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002080 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002081 */
2082 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084{
2085 isn_T *isn;
2086
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002087 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002088 return FAIL;
2089 isn->isn_arg.number = count;
2090
2091 return OK;
2092}
2093
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002094/*
2095 * Generate an ISN_PUT instruction.
2096 */
2097 static int
2098generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2099{
2100 isn_T *isn;
2101
2102 RETURN_OK_IF_SKIP(cctx);
2103 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.put.put_regname = regname;
2106 isn->isn_arg.put.put_lnum = lnum;
2107 return OK;
2108}
2109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 static int
2111generate_EXEC(cctx_T *cctx, char_u *line)
2112{
2113 isn_T *isn;
2114
Bram Moolenaar080457c2020-03-03 21:53:32 +01002115 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2117 return FAIL;
2118 isn->isn_arg.string = vim_strsave(line);
2119 return OK;
2120}
2121
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002122 static int
2123generate_EXECCONCAT(cctx_T *cctx, int count)
2124{
2125 isn_T *isn;
2126
2127 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2128 return FAIL;
2129 isn->isn_arg.number = count;
2130 return OK;
2131}
2132
Bram Moolenaar08597872020-12-10 19:43:40 +01002133/*
2134 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2135 */
2136 static int
2137generate_RANGE(cctx_T *cctx, char_u *range)
2138{
2139 isn_T *isn;
2140 garray_T *stack = &cctx->ctx_type_stack;
2141
2142 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2143 return FAIL;
2144 isn->isn_arg.string = range;
2145
2146 if (ga_grow(stack, 1) == FAIL)
2147 return FAIL;
2148 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2149 ++stack->ga_len;
2150 return OK;
2151}
2152
Bram Moolenaar792f7862020-11-23 08:31:18 +01002153 static int
2154generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2155{
2156 isn_T *isn;
2157
2158 RETURN_OK_IF_SKIP(cctx);
2159 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2160 return FAIL;
2161 isn->isn_arg.unpack.unp_count = var_count;
2162 isn->isn_arg.unpack.unp_semicolon = semicolon;
2163 return OK;
2164}
2165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002167 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 */
2169 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002170generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171{
2172 isn_T *isn;
2173
Bram Moolenaarfa984412021-03-25 22:15:28 +01002174 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 cctx->ctx_has_cmdmod = TRUE;
2177
2178 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002179 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002180 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2181 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2182 return FAIL;
2183 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002184 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002185 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002186 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002187
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002188 return OK;
2189}
2190
2191 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002192generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002194 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2195 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002196 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002197 return OK;
2198}
2199
Bram Moolenaarfa984412021-03-25 22:15:28 +01002200 static int
2201misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002202{
2203 garray_T *instr = &cctx->ctx_instr;
2204
Bram Moolenaara91a7132021-03-25 21:12:15 +01002205 if (cctx->ctx_has_cmdmod
2206 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2207 == ISN_CMDMOD)
2208 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002209 emsg(_(e_misplaced_command_modifier));
2210 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002211 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002212 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002213}
2214
2215/*
2216 * Get the index of the current instruction.
2217 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2218 */
2219 static int
2220current_instr_idx(cctx_T *cctx)
2221{
2222 garray_T *instr = &cctx->ctx_instr;
2223 int idx = instr->ga_len;
2224
2225 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_CMDMOD)
2227 --idx;
2228#ifdef FEAT_PROFILE
2229 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2230 .isn_type == ISN_PROF_START)
2231 --idx;
2232#endif
2233 return idx;
2234}
2235
Bram Moolenaarf002a412021-01-24 13:34:18 +01002236#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 static void
2238may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2239{
2240 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002241 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002242}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002243#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002244
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002245/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002247 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002249 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002250reserve_local(
2251 cctx_T *cctx,
2252 char_u *name,
2253 size_t len,
2254 int isConst,
2255 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 lvar_T *lvar;
2258
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002259 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002261 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 }
2264
2265 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 return NULL;
2267 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002268 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002270 // Every local variable uses the next entry on the stack. We could re-use
2271 // the last ones when leaving a scope, but then variables used in a closure
2272 // might get overwritten. To keep things simple do not re-use stack
2273 // entries. This is less efficient, but memory is cheap these days.
2274 lvar->lv_idx = cctx->ctx_locals_count++;
2275
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002276 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 lvar->lv_const = isConst;
2278 lvar->lv_type = type;
2279
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002280 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281}
2282
2283/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002284 * Remove local variables above "new_top".
2285 */
2286 static void
2287unwind_locals(cctx_T *cctx, int new_top)
2288{
2289 if (cctx->ctx_locals.ga_len > new_top)
2290 {
2291 int idx;
2292 lvar_T *lvar;
2293
2294 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2295 {
2296 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2297 vim_free(lvar->lv_name);
2298 }
2299 }
2300 cctx->ctx_locals.ga_len = new_top;
2301}
2302
2303/*
2304 * Free all local variables.
2305 */
2306 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002307free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002308{
2309 unwind_locals(cctx, 0);
2310 ga_clear(&cctx->ctx_locals);
2311}
2312
2313/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002314 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2315 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2316 * error if the variable was defined with :const.
2317 */
2318 static int
2319check_item_writable(svar_T *sv, int check_writable, char_u *name)
2320{
2321 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2322 || (check_writable == ASSIGN_FINAL
2323 && sv->sv_const == ASSIGN_CONST))
2324 {
2325 semsg(_(e_readonlyvar), name);
2326 return FAIL;
2327 }
2328 return OK;
2329}
2330
2331/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002333 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 * Returns the index in "sn_var_vals" if found.
2335 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002336 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 */
2338 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002339get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340{
2341 hashtab_T *ht;
2342 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002343 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002344 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 int idx;
2346
Bram Moolenaare3d46852020-08-29 13:39:17 +02002347 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002349 if (sid == current_sctx.sc_sid)
2350 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002351 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002352
2353 if (sav == NULL)
2354 return -2;
2355 idx = sav->sav_var_vals_idx;
2356 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002357 if (check_item_writable(sv, check_writable, name) == FAIL)
2358 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002359 return idx;
2360 }
2361
2362 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 ht = &SCRIPT_VARS(sid);
2364 di = find_var_in_ht(ht, 0, name, TRUE);
2365 if (di == NULL)
2366 return -2;
2367
2368 // Now find the svar_T index in sn_var_vals.
2369 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2370 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002371 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (sv->sv_tv == &di->di_tv)
2373 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002374 if (check_item_writable(sv, check_writable, name) == FAIL)
2375 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 return idx;
2377 }
2378 }
2379 return -1;
2380}
2381
2382/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002383 * Find "name" in imported items of the current script or in "cctx" if not
2384 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 */
2386 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002387find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 int idx;
2390
Bram Moolenaare3d46852020-08-29 13:39:17 +02002391 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002392 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 if (cctx != NULL)
2394 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2395 {
2396 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2397 + idx;
2398
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002399 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2400 : STRLEN(import->imp_name) == len
2401 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return import;
2403 }
2404
Bram Moolenaarefa94442020-08-08 22:16:00 +02002405 return find_imported_in_script(name, len, current_sctx.sc_sid);
2406}
2407
2408 imported_T *
2409find_imported_in_script(char_u *name, size_t len, int sid)
2410{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002411 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002412 int idx;
2413
Bram Moolenaare3d46852020-08-29 13:39:17 +02002414 if (!SCRIPT_ID_VALID(sid))
2415 return NULL;
2416 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2418 {
2419 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2420
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002421 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2422 : STRLEN(import->imp_name) == len
2423 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 return import;
2425 }
2426 return NULL;
2427}
2428
2429/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002430 * Free all imported variables.
2431 */
2432 static void
2433free_imported(cctx_T *cctx)
2434{
2435 int idx;
2436
2437 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2438 {
2439 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2440
2441 vim_free(import->imp_name);
2442 }
2443 ga_clear(&cctx->ctx_imports);
2444}
2445
2446/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002447 * Return a pointer to the next line that isn't empty or only contains a
2448 * comment. Skips over white space.
2449 * Returns NULL if there is none.
2450 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002451 char_u *
2452peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002453{
2454 int lnum = cctx->ctx_lnum;
2455
2456 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2457 {
2458 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002459 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002460
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002461 // ignore NULLs inserted for continuation lines
2462 if (line != NULL)
2463 {
2464 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002465 if (vim9_bad_comment(p))
2466 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002467 if (*p != NUL && !vim9_comment_start(p))
2468 return p;
2469 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002470 }
2471 return NULL;
2472}
2473
2474/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002475 * Called when checking for a following operator at "arg". When the rest of
2476 * the line is empty or only a comment, peek the next line. If there is a next
2477 * line return a pointer to it and set "nextp".
2478 * Otherwise skip over white space.
2479 */
2480 static char_u *
2481may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2482{
2483 char_u *p = skipwhite(arg);
2484
2485 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002486 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002487 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002488 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002489 if (*nextp != NULL)
2490 return *nextp;
2491 }
2492 return p;
2493}
2494
2495/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002496 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498 * Returns NULL when at the end.
2499 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002500 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002501next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002502{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002503 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002504
2505 do
2506 {
2507 ++cctx->ctx_lnum;
2508 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002509 {
2510 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002511 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002512 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002513 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002514 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002515 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002516 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002517 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002518 return line;
2519}
2520
2521/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002522 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002523 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2526 */
2527 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002528may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002529{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002530 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002531 if (vim9_bad_comment(*arg))
2532 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002533 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002534 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002535 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002536
2537 if (next == NULL)
2538 return FAIL;
2539 *arg = skipwhite(next);
2540 }
2541 return OK;
2542}
2543
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002544/*
2545 * Idem, and give an error when failed.
2546 */
2547 static int
2548may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2549{
2550 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2551 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002552 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002553 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002554 return FAIL;
2555 }
2556 return OK;
2557}
2558
2559
Bram Moolenaara5565e42020-05-09 15:44:01 +02002560// Structure passed between the compile_expr* functions to keep track of
2561// constants that have been parsed but for which no code was produced yet. If
2562// possible expressions on these constants are applied at compile time. If
2563// that is not possible, the code to push the constants needs to be generated
2564// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002565// Using 50 should be more than enough of 5 levels of ().
2566#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002567typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002568 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002569 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570 int pp_is_const; // all generated code was constants, used for a
2571 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002572} ppconst_T;
2573
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002574static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002575static int compile_expr0(char_u **arg, cctx_T *cctx);
2576static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2577
Bram Moolenaara5565e42020-05-09 15:44:01 +02002578/*
2579 * Generate a PUSH instruction for "tv".
2580 * "tv" will be consumed or cleared.
2581 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2582 */
2583 static int
2584generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2585{
2586 if (tv != NULL)
2587 {
2588 switch (tv->v_type)
2589 {
2590 case VAR_UNKNOWN:
2591 break;
2592 case VAR_BOOL:
2593 generate_PUSHBOOL(cctx, tv->vval.v_number);
2594 break;
2595 case VAR_SPECIAL:
2596 generate_PUSHSPEC(cctx, tv->vval.v_number);
2597 break;
2598 case VAR_NUMBER:
2599 generate_PUSHNR(cctx, tv->vval.v_number);
2600 break;
2601#ifdef FEAT_FLOAT
2602 case VAR_FLOAT:
2603 generate_PUSHF(cctx, tv->vval.v_float);
2604 break;
2605#endif
2606 case VAR_BLOB:
2607 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2608 tv->vval.v_blob = NULL;
2609 break;
2610 case VAR_STRING:
2611 generate_PUSHS(cctx, tv->vval.v_string);
2612 tv->vval.v_string = NULL;
2613 break;
2614 default:
2615 iemsg("constant type not supported");
2616 clear_tv(tv);
2617 return FAIL;
2618 }
2619 tv->v_type = VAR_UNKNOWN;
2620 }
2621 return OK;
2622}
2623
2624/*
2625 * Generate code for any ppconst entries.
2626 */
2627 static int
2628generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2629{
2630 int i;
2631 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002632 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002633
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002634 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002635 for (i = 0; i < ppconst->pp_used; ++i)
2636 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2637 ret = FAIL;
2638 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002639 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002640 return ret;
2641}
2642
2643/*
2644 * Clear ppconst constants. Used when failing.
2645 */
2646 static void
2647clear_ppconst(ppconst_T *ppconst)
2648{
2649 int i;
2650
2651 for (i = 0; i < ppconst->pp_used; ++i)
2652 clear_tv(&ppconst->pp_tv[i]);
2653 ppconst->pp_used = 0;
2654}
2655
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002656/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002657 * Compile getting a member from a list/dict/string/blob. Stack has the
2658 * indexable value and the index.
2659 */
2660 static int
2661compile_member(int is_slice, cctx_T *cctx)
2662{
2663 type_T **typep;
2664 garray_T *stack = &cctx->ctx_type_stack;
2665 vartype_T vtype;
2666 type_T *valtype;
2667
2668 // We can index a list and a dict. If we don't know the type
2669 // we can use the index value type.
2670 // TODO: If we don't know use an instruction to figure it out at
2671 // runtime.
2672 typep = ((type_T **)stack->ga_data) + stack->ga_len
2673 - (is_slice ? 3 : 2);
2674 vtype = (*typep)->tt_type;
2675 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2676 // If the index is a string, the variable must be a Dict.
2677 if (*typep == &t_any && valtype == &t_string)
2678 vtype = VAR_DICT;
2679 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2680 {
2681 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2682 return FAIL;
2683 if (is_slice)
2684 {
2685 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2686 if (need_type(valtype, &t_number, -2, 0, cctx,
2687 FALSE, FALSE) == FAIL)
2688 return FAIL;
2689 }
2690 }
2691
2692 if (vtype == VAR_DICT)
2693 {
2694 if (is_slice)
2695 {
2696 emsg(_(e_cannot_slice_dictionary));
2697 return FAIL;
2698 }
2699 if ((*typep)->tt_type == VAR_DICT)
2700 {
2701 *typep = (*typep)->tt_member;
2702 if (*typep == &t_unknown)
2703 // empty dict was used
2704 *typep = &t_any;
2705 }
2706 else
2707 {
2708 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2709 FALSE, FALSE) == FAIL)
2710 return FAIL;
2711 *typep = &t_any;
2712 }
2713 if (may_generate_2STRING(-1, cctx) == FAIL)
2714 return FAIL;
2715 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2716 return FAIL;
2717 }
2718 else if (vtype == VAR_STRING)
2719 {
2720 *typep = &t_string;
2721 if ((is_slice
2722 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2723 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2724 return FAIL;
2725 }
2726 else if (vtype == VAR_BLOB)
2727 {
2728 emsg("Sorry, blob index and slice not implemented yet");
2729 return FAIL;
2730 }
2731 else if (vtype == VAR_LIST || *typep == &t_any)
2732 {
2733 if (is_slice)
2734 {
2735 if (generate_instr_drop(cctx,
2736 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2737 2) == FAIL)
2738 return FAIL;
2739 }
2740 else
2741 {
2742 if ((*typep)->tt_type == VAR_LIST)
2743 {
2744 *typep = (*typep)->tt_member;
2745 if (*typep == &t_unknown)
2746 // empty list was used
2747 *typep = &t_any;
2748 }
2749 if (generate_instr_drop(cctx,
2750 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2751 return FAIL;
2752 }
2753 }
2754 else
2755 {
2756 emsg(_(e_string_list_dict_or_blob_required));
2757 return FAIL;
2758 }
2759 return OK;
2760}
2761
2762/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002763 * Generate an instruction to load script-local variable "name", without the
2764 * leading "s:".
2765 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 */
2767 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002768compile_load_scriptvar(
2769 cctx_T *cctx,
2770 char_u *name, // variable NUL terminated
2771 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002772 char_u **end, // end of variable
2773 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002775 scriptitem_T *si;
2776 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 imported_T *import;
2778
Bram Moolenaare3d46852020-08-29 13:39:17 +02002779 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2780 return FAIL;
2781 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002782 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002783 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002785 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002786 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2787 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 }
2789 if (idx >= 0)
2790 {
2791 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2792
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002793 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 current_sctx.sc_sid, idx, sv->sv_type);
2795 return OK;
2796 }
2797
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002798 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 if (import != NULL)
2800 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002801 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 {
2803 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002804 char_u *exp_name;
2805 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002806 ufunc_T *ufunc;
2807 type_T *type;
2808
2809 // Used "import * as Name", need to lookup the member.
2810 if (*p != '.')
2811 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002812 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002813 return FAIL;
2814 }
2815 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002816 if (VIM_ISWHITE(*p))
2817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002818 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002819 return FAIL;
2820 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002821
Bram Moolenaar1c991142020-07-04 13:15:31 +02002822 // isolate one name
2823 exp_name = p;
2824 while (eval_isnamec(*p))
2825 ++p;
2826 cc = *p;
2827 *p = NUL;
2828
Bram Moolenaaredba7072021-03-13 21:14:18 +01002829 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2830 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002831 *p = cc;
2832 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002833 *end = p;
2834
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002835 if (idx < 0)
2836 {
2837 if (*p == '(' && ufunc != NULL)
2838 {
2839 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2840 return OK;
2841 }
2842 return FAIL;
2843 }
2844
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002845 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2846 import->imp_sid,
2847 idx,
2848 type);
2849 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002850 else if (import->imp_funcname != NULL)
2851 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002852 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002853 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2854 import->imp_sid,
2855 import->imp_var_vals_idx,
2856 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 return OK;
2858 }
2859
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002860 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002861 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 return FAIL;
2863}
2864
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865 static int
2866generate_funcref(cctx_T *cctx, char_u *name)
2867{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002868 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002869
2870 if (ufunc == NULL)
2871 return FAIL;
2872
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002873 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002874 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2875 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002876 == FAIL)
2877 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002878 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002879}
2880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881/*
2882 * Compile a variable name into a load instruction.
2883 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002884 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 * When "error" is FALSE do not give an error when not found.
2886 */
2887 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002888compile_load(
2889 char_u **arg,
2890 char_u *end_arg,
2891 cctx_T *cctx,
2892 int is_expr,
2893 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
2895 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002896 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002897 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002899 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
2901 if (*(*arg + 1) == ':')
2902 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002903 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002904 {
2905 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906
Bram Moolenaarfa596382021-04-07 21:58:16 +02002907 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002908 switch (**arg)
2909 {
2910 case 'g': isn_type = ISN_LOADGDICT; break;
2911 case 'w': isn_type = ISN_LOADWDICT; break;
2912 case 't': isn_type = ISN_LOADTDICT; break;
2913 case 'b': isn_type = ISN_LOADBDICT; break;
2914 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002915 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002916 goto theend;
2917 }
2918 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2919 goto theend;
2920 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 else
2923 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002924 isntype_T isn_type = ISN_DROP;
2925
Bram Moolenaarfa596382021-04-07 21:58:16 +02002926 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002927 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2928 if (name == NULL)
2929 return FAIL;
2930
2931 switch (**arg)
2932 {
2933 case 'v': res = generate_LOADV(cctx, name, error);
2934 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002935 case 's': if (is_expr && ASCII_ISUPPER(*name)
2936 && find_func(name, FALSE, cctx) != NULL)
2937 res = generate_funcref(cctx, name);
2938 else
2939 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002940 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002941 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002942 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002943 {
2944 if (is_expr && ASCII_ISUPPER(*name)
2945 && find_func(name, FALSE, cctx) != NULL)
2946 res = generate_funcref(cctx, name);
2947 else
2948 isn_type = ISN_LOADG;
2949 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002950 else
2951 {
2952 isn_type = ISN_LOADAUTO;
2953 vim_free(name);
2954 name = vim_strnsave(*arg, end - *arg);
2955 if (name == NULL)
2956 return FAIL;
2957 }
2958 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002959 case 'w': isn_type = ISN_LOADW; break;
2960 case 't': isn_type = ISN_LOADT; break;
2961 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002962 default: // cannot happen, just in case
2963 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002964 goto theend;
2965 }
2966 if (isn_type != ISN_DROP)
2967 {
2968 // Global, Buffer-local, Window-local and Tabpage-local
2969 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002970 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002971 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 }
2974 }
2975 else
2976 {
2977 size_t len = end - *arg;
2978 int idx;
2979 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002980 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981
2982 name = vim_strnsave(*arg, end - *arg);
2983 if (name == NULL)
2984 return FAIL;
2985
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002986 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002988 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002989 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 }
2991 else
2992 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002993 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002994
Bram Moolenaar709664c2020-12-12 14:33:41 +01002995 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002997 type = lvar.lv_type;
2998 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002999 if (lvar.lv_from_outer != 0)
3000 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003001 else
3002 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
3004 else
3005 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003006 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003007 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003008 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003009 || find_imported(name, 0, cctx) != NULL)
3010 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003011
Bram Moolenaar0f769812020-09-12 18:32:34 +02003012 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003013 // uppercase letter it can be a user defined function.
3014 // generate_funcref() will fail if the function can't be found.
3015 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003016 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 }
3018 }
3019 if (gen_load)
3020 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003021 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003022 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003023 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003024 cctx->ctx_outer_used = TRUE;
3025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 }
3027
3028 *arg = end;
3029
3030theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003031 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003032 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 vim_free(name);
3034 return res;
3035}
3036
3037/*
3038 * Compile the argument expressions.
3039 * "arg" points to just after the "(" and is advanced to after the ")"
3040 */
3041 static int
3042compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3043{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003044 char_u *p = *arg;
3045 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003046 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003050 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3051 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003052 if (*p == ')')
3053 {
3054 *arg = p + 1;
3055 return OK;
3056 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003057 if (must_end)
3058 {
3059 semsg(_(e_missing_comma_before_argument_str), p);
3060 return FAIL;
3061 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003062
Bram Moolenaara5565e42020-05-09 15:44:01 +02003063 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 return FAIL;
3065 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003066
3067 if (*p != ',' && *skipwhite(p) == ',')
3068 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003069 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003070 p = skipwhite(p);
3071 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003073 {
3074 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003075 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003076 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003077 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003078 else
3079 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003080 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003081 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003083failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003084 emsg(_(e_missing_close));
3085 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086}
3087
3088/*
3089 * Compile a function call: name(arg1, arg2)
3090 * "arg" points to "name", "arg + varlen" to the "(".
3091 * "argcount_init" is 1 for "value->method()"
3092 * Instructions:
3093 * EVAL arg1
3094 * EVAL arg2
3095 * BCALL / DCALL / UCALL
3096 */
3097 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003098compile_call(
3099 char_u **arg,
3100 size_t varlen,
3101 cctx_T *cctx,
3102 ppconst_T *ppconst,
3103 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104{
3105 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003106 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 int argcount = argcount_init;
3108 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003109 char_u fname_buf[FLEN_FIXED + 1];
3110 char_u *tofree = NULL;
3111 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003112 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003113 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003114 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115
Bram Moolenaara5565e42020-05-09 15:44:01 +02003116 // we can evaluate "has('name')" at compile time
3117 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3118 {
3119 char_u *s = skipwhite(*arg + varlen + 1);
3120 typval_T argvars[2];
3121
3122 argvars[0].v_type = VAR_UNKNOWN;
3123 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003124 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003125 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003126 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003127 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003128 if (*s == ')' && argvars[0].v_type == VAR_STRING
3129 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003130 {
3131 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3132
3133 *arg = s + 1;
3134 argvars[1].v_type = VAR_UNKNOWN;
3135 tv->v_type = VAR_NUMBER;
3136 tv->vval.v_number = 0;
3137 f_has(argvars, tv);
3138 clear_tv(&argvars[0]);
3139 ++ppconst->pp_used;
3140 return OK;
3141 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003142 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003143 }
3144
3145 if (generate_ppconst(cctx, ppconst) == FAIL)
3146 return FAIL;
3147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (varlen >= sizeof(namebuf))
3149 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 return FAIL;
3152 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003153 vim_strncpy(namebuf, *arg, varlen);
3154 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 *arg = skipwhite(*arg + varlen + 1);
3157 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003158 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
Bram Moolenaar03290b82020-12-19 16:30:44 +01003160 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003161 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 {
3163 int idx;
3164
3165 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003166 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003168 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003169 if (STRCMP(name, "flatten") == 0)
3170 {
3171 emsg(_(e_cannot_use_flatten_in_vim9_script));
3172 goto theend;
3173 }
3174
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003175 if (STRCMP(name, "add") == 0 && argcount == 2)
3176 {
3177 garray_T *stack = &cctx->ctx_type_stack;
3178 type_T *type = ((type_T **)stack->ga_data)[
3179 stack->ga_len - 2];
3180
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003181 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003182 if (type->tt_type == VAR_LIST)
3183 {
3184 // inline "add(list, item)" so that the type can be checked
3185 res = generate_LISTAPPEND(cctx);
3186 idx = -1;
3187 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003188 else if (type->tt_type == VAR_BLOB)
3189 {
3190 // inline "add(blob, nr)" so that the type can be checked
3191 res = generate_BLOBAPPEND(cctx);
3192 idx = -1;
3193 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003194 }
3195
3196 if (idx >= 0)
3197 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3198 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003199 else
3200 semsg(_(e_unknownfunc), namebuf);
3201 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003204 // An argument or local variable can be a function reference, this
3205 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003206 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003207 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003208 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003209 // If we can find the function by name generate the right call.
3210 // Skip global functions here, a local funcref takes precedence.
3211 ufunc = find_func(name, FALSE, cctx);
3212 if (ufunc != NULL && !func_is_global(ufunc))
3213 {
3214 res = generate_CALL(cctx, ufunc, argcount);
3215 goto theend;
3216 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003220 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003221 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003223 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003224 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003225 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003226 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003227 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003228
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003229 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003230 goto theend;
3231 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232
Bram Moolenaar0f769812020-09-12 18:32:34 +02003233 // If we can find a global function by name generate the right call.
3234 if (ufunc != NULL)
3235 {
3236 res = generate_CALL(cctx, ufunc, argcount);
3237 goto theend;
3238 }
3239
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003240 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003241 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003242 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003243 res = generate_UCALL(cctx, name, argcount);
3244 else
3245 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003246
3247theend:
3248 vim_free(tofree);
3249 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250}
3251
3252// like NAMESPACE_CHAR but with 'a' and 'l'.
3253#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3254
3255/*
3256 * Find the end of a variable or function name. Unlike find_name_end() this
3257 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003258 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 * Return a pointer to just after the name. Equal to "arg" if there is no
3260 * valid name.
3261 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003262 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003263to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264{
3265 char_u *p;
3266
3267 // Quick check for valid starting character.
3268 if (!eval_isnamec1(*arg))
3269 return arg;
3270
3271 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3272 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3273 // and can be used in slice "[n:]".
3274 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003275 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3277 break;
3278 return p;
3279}
3280
3281/*
3282 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003283 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 */
3285 char_u *
3286to_name_const_end(char_u *arg)
3287{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003288 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 typval_T rettv;
3290
3291 if (p == arg && *arg == '[')
3292 {
3293
3294 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003295 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 p = arg;
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 return p;
3299}
3300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301/*
3302 * parse a list: [expr, expr]
3303 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003304 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 */
3306 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003307compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308{
3309 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003310 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003312 int is_const;
3313 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003315 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003317 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003318 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003319 semsg(_(e_list_end), *arg);
3320 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003321 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003322 if (*p == ',')
3323 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003324 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003325 return FAIL;
3326 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003327 if (*p == ']')
3328 {
3329 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003330 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003331 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003332 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003333 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003334 if (!is_const)
3335 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 ++count;
3337 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003338 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003340 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3341 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003342 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003343 return FAIL;
3344 }
3345 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003346 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 p = skipwhite(p);
3348 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003349 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003351 ppconst->pp_is_const = is_all_const;
3352 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353}
3354
3355/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003356 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003357 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003358 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 */
3360 static int
3361compile_lambda(char_u **arg, cctx_T *cctx)
3362{
Bram Moolenaare462f522020-12-27 14:43:30 +01003363 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 typval_T rettv;
3365 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003366 evalarg_T evalarg;
3367
3368 CLEAR_FIELD(evalarg);
3369 evalarg.eval_flags = EVAL_EVALUATE;
3370 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371
3372 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003373 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3374 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003375 {
3376 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003377 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003378 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003379
Bram Moolenaar65c44152020-12-24 15:14:01 +01003380 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003382 ++ufunc->uf_refcount;
3383 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384
Bram Moolenaar65c44152020-12-24 15:14:01 +01003385 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003386 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003388 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3389 // points into it. Point to the original line to avoid a dangling pointer.
3390 if (evalarg.eval_tofree_cmdline != NULL)
3391 {
3392 size_t off = *arg - evalarg.eval_tofree_cmdline;
3393
3394 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3395 + off;
3396 }
3397
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003398 clear_evalarg(&evalarg, NULL);
3399
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003400 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003401 {
3402 // The return type will now be known.
3403 set_function_type(ufunc);
3404
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003405 // The function reference count will be 1. When the ISN_FUNCREF
3406 // instruction is deleted the reference count is decremented and the
3407 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003408 return generate_FUNCREF(cctx, ufunc);
3409 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003410
3411 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 return FAIL;
3413}
3414
3415/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003416 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003418 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 */
3420 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003421compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422{
3423 garray_T *instr = &cctx->ctx_instr;
3424 int count = 0;
3425 dict_T *d = dict_alloc();
3426 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003427 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003428 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003429 int is_const;
3430 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431
3432 if (d == NULL)
3433 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003434 if (generate_ppconst(cctx, ppconst) == FAIL)
3435 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003436 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003438 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439
Bram Moolenaar23c55272020-06-21 16:58:13 +02003440 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003441 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003442 *arg = NULL;
3443 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003444 }
3445
3446 if (**arg == '}')
3447 break;
3448
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003449 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003451 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003453 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003454 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003455 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003458 if (isn->isn_type == ISN_PUSHNR)
3459 {
3460 char buf[NUMBUFLEN];
3461
3462 // Convert to string at compile time.
3463 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3464 isn->isn_type = ISN_PUSHS;
3465 isn->isn_arg.string = vim_strsave((char_u *)buf);
3466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 if (isn->isn_type == ISN_PUSHS)
3468 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003469 else if (may_generate_2STRING(-1, cctx) == FAIL)
3470 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003471 *arg = skipwhite(*arg);
3472 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003473 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003474 emsg(_(e_missing_matching_bracket_after_dict_key));
3475 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003476 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003477 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003479 else
3480 {
3481 // {"name": value},
3482 // {'name': value},
3483 // {name: value} use "name" as a literal key
3484 key = get_literal_key(arg);
3485 if (key == NULL)
3486 return FAIL;
3487 if (generate_PUSHS(cctx, key) == FAIL)
3488 return FAIL;
3489 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
3491 // Check for duplicate keys, if using string keys.
3492 if (key != NULL)
3493 {
3494 item = dict_find(d, key, -1);
3495 if (item != NULL)
3496 {
3497 semsg(_(e_duplicate_key), key);
3498 goto failret;
3499 }
3500 item = dictitem_alloc(key);
3501 if (item != NULL)
3502 {
3503 item->di_tv.v_type = VAR_UNKNOWN;
3504 item->di_tv.v_lock = 0;
3505 if (dict_add(d, item) == FAIL)
3506 dictitem_free(item);
3507 }
3508 }
3509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 if (**arg != ':')
3511 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003512 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003513 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003514 else
3515 semsg(_(e_missing_dict_colon), *arg);
3516 return FAIL;
3517 }
3518 whitep = *arg + 1;
3519 if (!IS_WHITE_OR_NUL(*whitep))
3520 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003521 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 return FAIL;
3523 }
3524
Bram Moolenaar23c55272020-06-21 16:58:13 +02003525 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003526 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003527 *arg = NULL;
3528 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003529 }
3530
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003531 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003533 if (!is_const)
3534 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 ++count;
3536
Bram Moolenaar2c330432020-04-13 14:41:35 +02003537 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003538 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003539 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003540 *arg = NULL;
3541 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (**arg == '}')
3544 break;
3545 if (**arg != ',')
3546 {
3547 semsg(_(e_missing_dict_comma), *arg);
3548 goto failret;
3549 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003550 if (IS_WHITE_OR_NUL(*whitep))
3551 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003552 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003553 return FAIL;
3554 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003555 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003556 if (!IS_WHITE_OR_NUL(*whitep))
3557 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003558 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003559 return FAIL;
3560 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003561 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 }
3563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 *arg = *arg + 1;
3565
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003566 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003567 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003568 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003569 *arg += STRLEN(*arg);
3570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003572 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 return generate_NEWDICT(cctx, count);
3574
3575failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003576 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003577 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003578 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003579 *arg = (char_u *)"";
3580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 dict_unref(d);
3582 return FAIL;
3583}
3584
3585/*
3586 * Compile "&option".
3587 */
3588 static int
3589compile_get_option(char_u **arg, cctx_T *cctx)
3590{
3591 typval_T rettv;
3592 char_u *start = *arg;
3593 int ret;
3594
3595 // parse the option and get the current value to get the type.
3596 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003597 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (ret == OK)
3599 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003600 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003601 char_u *name = vim_strnsave(start, *arg - start);
3602 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3603 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604
3605 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3606 vim_free(name);
3607 }
3608 clear_tv(&rettv);
3609
3610 return ret;
3611}
3612
3613/*
3614 * Compile "$VAR".
3615 */
3616 static int
3617compile_get_env(char_u **arg, cctx_T *cctx)
3618{
3619 char_u *start = *arg;
3620 int len;
3621 int ret;
3622 char_u *name;
3623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 ++*arg;
3625 len = get_env_len(arg);
3626 if (len == 0)
3627 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003628 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 return FAIL;
3630 }
3631
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003632 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 name = vim_strnsave(start, len + 1);
3634 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3635 vim_free(name);
3636 return ret;
3637}
3638
3639/*
3640 * Compile "@r".
3641 */
3642 static int
3643compile_get_register(char_u **arg, cctx_T *cctx)
3644{
3645 int ret;
3646
3647 ++*arg;
3648 if (**arg == NUL)
3649 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003650 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 return FAIL;
3652 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003653 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 {
3655 emsg_invreg(**arg);
3656 return FAIL;
3657 }
3658 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3659 ++*arg;
3660 return ret;
3661}
3662
3663/*
3664 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003665 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 */
3667 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003668apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003670 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671
3672 // this works from end to start
3673 while (p > start)
3674 {
3675 --p;
3676 if (*p == '-' || *p == '+')
3677 {
3678 // only '-' has an effect, for '+' we only check the type
3679#ifdef FEAT_FLOAT
3680 if (rettv->v_type == VAR_FLOAT)
3681 {
3682 if (*p == '-')
3683 rettv->vval.v_float = -rettv->vval.v_float;
3684 }
3685 else
3686#endif
3687 {
3688 varnumber_T val;
3689 int error = FALSE;
3690
3691 // tv_get_number_chk() accepts a string, but we don't want that
3692 // here
3693 if (check_not_string(rettv) == FAIL)
3694 return FAIL;
3695 val = tv_get_number_chk(rettv, &error);
3696 clear_tv(rettv);
3697 if (error)
3698 return FAIL;
3699 if (*p == '-')
3700 val = -val;
3701 rettv->v_type = VAR_NUMBER;
3702 rettv->vval.v_number = val;
3703 }
3704 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003705 else if (numeric_only)
3706 {
3707 ++p;
3708 break;
3709 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003710 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 {
3712 int v = tv2bool(rettv);
3713
3714 // '!' is permissive in the type.
3715 clear_tv(rettv);
3716 rettv->v_type = VAR_BOOL;
3717 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3718 }
3719 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003720 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 return OK;
3722}
3723
3724/*
3725 * Recognize v: variables that are constants and set "rettv".
3726 */
3727 static void
3728get_vim_constant(char_u **arg, typval_T *rettv)
3729{
3730 if (STRNCMP(*arg, "v:true", 6) == 0)
3731 {
3732 rettv->v_type = VAR_BOOL;
3733 rettv->vval.v_number = VVAL_TRUE;
3734 *arg += 6;
3735 }
3736 else if (STRNCMP(*arg, "v:false", 7) == 0)
3737 {
3738 rettv->v_type = VAR_BOOL;
3739 rettv->vval.v_number = VVAL_FALSE;
3740 *arg += 7;
3741 }
3742 else if (STRNCMP(*arg, "v:null", 6) == 0)
3743 {
3744 rettv->v_type = VAR_SPECIAL;
3745 rettv->vval.v_number = VVAL_NULL;
3746 *arg += 6;
3747 }
3748 else if (STRNCMP(*arg, "v:none", 6) == 0)
3749 {
3750 rettv->v_type = VAR_SPECIAL;
3751 rettv->vval.v_number = VVAL_NONE;
3752 *arg += 6;
3753 }
3754}
3755
Bram Moolenaar657137c2021-01-09 15:45:23 +01003756 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003757get_compare_type(char_u *p, int *len, int *type_is)
3758{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003759 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003760 int i;
3761
3762 switch (p[0])
3763 {
3764 case '=': if (p[1] == '=')
3765 type = EXPR_EQUAL;
3766 else if (p[1] == '~')
3767 type = EXPR_MATCH;
3768 break;
3769 case '!': if (p[1] == '=')
3770 type = EXPR_NEQUAL;
3771 else if (p[1] == '~')
3772 type = EXPR_NOMATCH;
3773 break;
3774 case '>': if (p[1] != '=')
3775 {
3776 type = EXPR_GREATER;
3777 *len = 1;
3778 }
3779 else
3780 type = EXPR_GEQUAL;
3781 break;
3782 case '<': if (p[1] != '=')
3783 {
3784 type = EXPR_SMALLER;
3785 *len = 1;
3786 }
3787 else
3788 type = EXPR_SEQUAL;
3789 break;
3790 case 'i': if (p[1] == 's')
3791 {
3792 // "is" and "isnot"; but not a prefix of a name
3793 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3794 *len = 5;
3795 i = p[*len];
3796 if (!isalnum(i) && i != '_')
3797 {
3798 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3799 *type_is = TRUE;
3800 }
3801 }
3802 break;
3803 }
3804 return type;
3805}
3806
3807/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003808 * Skip over an expression, ignoring most errors.
3809 */
3810 static void
3811skip_expr_cctx(char_u **arg, cctx_T *cctx)
3812{
3813 evalarg_T evalarg;
3814
3815 CLEAR_FIELD(evalarg);
3816 evalarg.eval_cctx = cctx;
3817 skip_expr(arg, &evalarg);
3818}
3819
3820/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003822 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 */
3824 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003825compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003827 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
3829 // this works from end to start
3830 while (p > start)
3831 {
3832 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003833 while (VIM_ISWHITE(*p))
3834 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 if (*p == '-' || *p == '+')
3836 {
3837 int negate = *p == '-';
3838 isn_T *isn;
3839
3840 // TODO: check type
3841 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3842 {
3843 --p;
3844 if (*p == '-')
3845 negate = !negate;
3846 }
3847 // only '-' has an effect, for '+' we only check the type
3848 if (negate)
3849 isn = generate_instr(cctx, ISN_NEGATENR);
3850 else
3851 isn = generate_instr(cctx, ISN_CHECKNR);
3852 if (isn == NULL)
3853 return FAIL;
3854 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003855 else if (numeric_only)
3856 {
3857 ++p;
3858 break;
3859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 else
3861 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003862 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003864 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003866 if (p[-1] == '!')
3867 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
3870 if (generate_2BOOL(cctx, invert) == FAIL)
3871 return FAIL;
3872 }
3873 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003874 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 return OK;
3876}
3877
3878/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003879 * Compile "(expression)": recursive!
3880 * Return FAIL/OK.
3881 */
3882 static int
3883compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3884{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003885 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003886 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003887
Bram Moolenaar24156692021-01-14 20:35:49 +01003888 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3889 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003890 if (ppconst->pp_used <= PPSIZE - 10)
3891 {
3892 ret = compile_expr1(arg, cctx, ppconst);
3893 }
3894 else
3895 {
3896 // Not enough space in ppconst, flush constants.
3897 if (generate_ppconst(cctx, ppconst) == FAIL)
3898 return FAIL;
3899 ret = compile_expr0(arg, cctx);
3900 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003901 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3902 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003903 if (**arg == ')')
3904 ++*arg;
3905 else if (ret == OK)
3906 {
3907 emsg(_(e_missing_close));
3908 ret = FAIL;
3909 }
3910 return ret;
3911}
3912
3913/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003915 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 */
3917 static int
3918compile_subscript(
3919 char_u **arg,
3920 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003921 char_u *start_leader,
3922 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003923 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003925 char_u *name_start = *end_leader;
3926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 for (;;)
3928 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003929 char_u *p = skipwhite(*arg);
3930
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003931 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003932 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003933 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003934
3935 // If a following line starts with "->{" or "->X" advance to that
3936 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003937 // Also if a following line starts with ".x".
3938 if (next != NULL &&
3939 ((next[0] == '-' && next[1] == '>'
3940 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003941 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003942 {
3943 next = next_line_from_context(cctx, TRUE);
3944 if (next == NULL)
3945 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003946 *arg = next;
3947 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003948 }
3949 }
3950
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003951 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003952 // not a function call.
3953 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003955 garray_T *stack = &cctx->ctx_type_stack;
3956 type_T *type;
3957 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003959 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003960 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003961 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003964 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3965
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003966 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3968 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003969 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
3971 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003972 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003974 char_u *pstart = p;
3975
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003976 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003977 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003978 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 // something->method()
3981 // Apply the '!', '-' and '+' first:
3982 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003983 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003986 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003987 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003988 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003989 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003990 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003991 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003992 garray_T *stack = &cctx->ctx_type_stack;
3993 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003994 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003995 int expr_isn_start = cctx->ctx_instr.ga_len;
3996 int expr_isn_end;
3997 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003998
3999 // Funcref call: list->(Refs[2])(arg)
4000 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02004001 //
4002 // Fist compile the function expression.
4003 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004004 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004005
4006 // Remember the next instruction index, where the instructions
4007 // for arguments are being written.
4008 expr_isn_end = cctx->ctx_instr.ga_len;
4009
4010 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004011 if (**arg != '(')
4012 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004013 if (*skipwhite(*arg) == '(')
4014 emsg(_(e_nowhitespace));
4015 else
4016 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004017 return FAIL;
4018 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004019 *arg = skipwhite(*arg + 1);
4020 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4021 return FAIL;
4022
Bram Moolenaar2927c072021-04-05 19:41:21 +02004023 // Move the instructions for the arguments to before the
4024 // instructions of the expression and move the type of the
4025 // expression after the argument types. This is what ISN_PCALL
4026 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004027 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004028 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4029 if (arg_isn_count > 0)
4030 {
4031 int expr_isn_count = expr_isn_end - expr_isn_start;
4032 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4033
4034 if (isn == NULL)
4035 return FAIL;
4036 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4037 + expr_isn_start,
4038 sizeof(isn_T) * expr_isn_count);
4039 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4040 + expr_isn_start,
4041 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4042 sizeof(isn_T) * arg_isn_count);
4043 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4044 + expr_isn_start + arg_isn_count,
4045 isn, sizeof(isn_T) * expr_isn_count);
4046 vim_free(isn);
4047
4048 type = ((type_T **)stack->ga_data)[type_idx_start];
4049 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4050 ((type_T **)stack->ga_data) + type_idx_start + 1,
4051 sizeof(type_T *)
4052 * (stack->ga_len - type_idx_start - 1));
4053 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4054 }
4055
Bram Moolenaar7e368202020-12-25 21:56:57 +01004056 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4057 if (generate_PCALL(cctx, argcount,
4058 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 return FAIL;
4060 }
4061 else
4062 {
4063 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004064 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004065 if (!eval_isnamec1(*p))
4066 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004067 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004068 return FAIL;
4069 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004070 if (ASCII_ISALPHA(*p) && p[1] == ':')
4071 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004072 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 ;
4074 if (*p != '(')
4075 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004076 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004080 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 return FAIL;
4082 }
4083 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004084 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004086 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004087
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004088 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004089 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004090 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004091 // TODO: blob index
4092 // TODO: more arguments
4093 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004094 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004095 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004096 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004097
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004098 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004099 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004100 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004101 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004102 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004103 // missing first index is equal to zero
4104 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004105 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004106 else
4107 {
4108 if (compile_expr0(arg, cctx) == FAIL)
4109 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004110 if (**arg == ':')
4111 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004112 semsg(_(e_white_space_required_before_and_after_str_at_str),
4113 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004114 return FAIL;
4115 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004116 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004117 return FAIL;
4118 *arg = skipwhite(*arg);
4119 }
4120 if (**arg == ':')
4121 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004122 is_slice = TRUE;
4123 ++*arg;
4124 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4125 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004126 semsg(_(e_white_space_required_before_and_after_str_at_str),
4127 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004128 return FAIL;
4129 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004130 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004131 return FAIL;
4132 if (**arg == ']')
4133 // missing second index is equal to end of string
4134 generate_PUSHNR(cctx, -1);
4135 else
4136 {
4137 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004138 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004139 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004140 return FAIL;
4141 *arg = skipwhite(*arg);
4142 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144
4145 if (**arg != ']')
4146 {
4147 emsg(_(e_missbrac));
4148 return FAIL;
4149 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004150 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
Bram Moolenaare42939a2021-04-05 17:11:17 +02004152 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004153 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004155 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004157 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004158 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004159 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004160 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004161
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004162 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004163 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004164 {
4165 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004166 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004167 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004168 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004169 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 while (eval_isnamec(*p))
4171 MB_PTR_ADV(p);
4172 if (p == *arg)
4173 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004174 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 return FAIL;
4176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004177 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 return FAIL;
4179 *arg = p;
4180 }
4181 else
4182 break;
4183 }
4184
4185 // TODO - see handle_subscript():
4186 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4187 // Don't do this when "Func" is already a partial that was bound
4188 // explicitly (pt_auto is FALSE).
4189
4190 return OK;
4191}
4192
4193/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004194 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4195 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004197 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004198 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 *
4200 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 */
4202
4203/*
4204 * number number constant
4205 * 0zFFFFFFFF Blob constant
4206 * "string" string constant
4207 * 'string' literal string constant
4208 * &option-name option value
4209 * @r register contents
4210 * identifier variable value
4211 * function() function call
4212 * $VAR environment variable
4213 * (expression) nested expression
4214 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004215 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 *
4217 * Also handle:
4218 * ! in front logical NOT
4219 * - in front unary minus
4220 * + in front unary plus (ignored)
4221 * trailing (arg) funcref/partial call
4222 * trailing [] subscript in String or List
4223 * trailing .name entry in Dictionary
4224 * trailing ->name() method call
4225 */
4226 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004227compile_expr7(
4228 char_u **arg,
4229 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004230 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 char_u *start_leader, *end_leader;
4233 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004234 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004235 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004237 ppconst->pp_is_const = FALSE;
4238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 /*
4240 * Skip '!', '-' and '+' characters. They are handled later.
4241 */
4242 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004243 if (eval_leader(arg, TRUE) == FAIL)
4244 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 end_leader = *arg;
4246
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004247 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 switch (**arg)
4249 {
4250 /*
4251 * Number constant.
4252 */
4253 case '0': // also for blob starting with 0z
4254 case '1':
4255 case '2':
4256 case '3':
4257 case '4':
4258 case '5':
4259 case '6':
4260 case '7':
4261 case '8':
4262 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004263 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004265 // Apply "-" and "+" just before the number now, right to
4266 // left. Matters especially when "->" follows. Stops at
4267 // '!'.
4268 if (apply_leader(rettv, TRUE,
4269 start_leader, &end_leader) == FAIL)
4270 {
4271 clear_tv(rettv);
4272 return FAIL;
4273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 break;
4275
4276 /*
4277 * String constant: "string".
4278 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004279 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 return FAIL;
4281 break;
4282
4283 /*
4284 * Literal string constant: 'str''ing'.
4285 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004286 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 return FAIL;
4288 break;
4289
4290 /*
4291 * Constant Vim variable.
4292 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004293 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 ret = NOTDONE;
4295 break;
4296
4297 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004298 * "true" constant
4299 */
4300 case 't': if (STRNCMP(*arg, "true", 4) == 0
4301 && !eval_isnamec((*arg)[4]))
4302 {
4303 *arg += 4;
4304 rettv->v_type = VAR_BOOL;
4305 rettv->vval.v_number = VVAL_TRUE;
4306 }
4307 else
4308 ret = NOTDONE;
4309 break;
4310
4311 /*
4312 * "false" constant
4313 */
4314 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4315 && !eval_isnamec((*arg)[5]))
4316 {
4317 *arg += 5;
4318 rettv->v_type = VAR_BOOL;
4319 rettv->vval.v_number = VVAL_FALSE;
4320 }
4321 else
4322 ret = NOTDONE;
4323 break;
4324
4325 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004326 * "null" constant
4327 */
4328 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004329 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004330 {
4331 *arg += 4;
4332 rettv->v_type = VAR_SPECIAL;
4333 rettv->vval.v_number = VVAL_NULL;
4334 }
4335 else
4336 ret = NOTDONE;
4337 break;
4338
4339 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 * List: [expr, expr]
4341 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004342 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4343 return FAIL;
4344 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 break;
4346
4347 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 * Dictionary: {'key': val, 'key': val}
4349 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004350 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4351 return FAIL;
4352 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 break;
4354
4355 /*
4356 * Option value: &name
4357 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004358 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4359 return FAIL;
4360 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 break;
4362
4363 /*
4364 * Environment variable: $VAR.
4365 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004366 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4367 return FAIL;
4368 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 break;
4370
4371 /*
4372 * Register contents: @r.
4373 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004374 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4375 return FAIL;
4376 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 break;
4378 /*
4379 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004380 * lambda: (arg, arg) => expr
4381 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004383 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4384 ret = compile_lambda(arg, cctx);
4385 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004386 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 break;
4388
4389 default: ret = NOTDONE;
4390 break;
4391 }
4392 if (ret == FAIL)
4393 return FAIL;
4394
Bram Moolenaar1c747212020-05-09 18:28:34 +02004395 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004397 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004398 clear_tv(rettv);
4399 else
4400 // A constant expression can possibly be handled compile time,
4401 // return the value instead of generating code.
4402 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
4404 else if (ret == NOTDONE)
4405 {
4406 char_u *p;
4407 int r;
4408
4409 if (!eval_isnamec1(**arg))
4410 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004411 if (!vim9_bad_comment(*arg))
4412 {
4413 if (ends_excmd(*skipwhite(*arg)))
4414 semsg(_(e_empty_expression_str), *arg);
4415 else
4416 semsg(_(e_name_expected_str), *arg);
4417 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 return FAIL;
4419 }
4420
4421 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004422 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004423 if (p - *arg == (size_t)1 && **arg == '_')
4424 {
4425 emsg(_(e_cannot_use_underscore_here));
4426 return FAIL;
4427 }
4428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 {
4431 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 {
4435 if (generate_ppconst(cctx, ppconst) == FAIL)
4436 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004437 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004438 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 if (r == FAIL)
4440 return FAIL;
4441 }
4442
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004443 // Handle following "[]", ".member", etc.
4444 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004445 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004446 ppconst) == FAIL)
4447 return FAIL;
4448 if (ppconst->pp_used > 0)
4449 {
4450 // apply the '!', '-' and '+' before the constant
4451 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004452 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 return FAIL;
4454 return OK;
4455 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004456 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004458 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459}
4460
4461/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004462 * Give the "white on both sides" error, taking the operator from "p[len]".
4463 */
4464 void
4465error_white_both(char_u *op, int len)
4466{
4467 char_u buf[10];
4468
4469 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004470 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004471}
4472
4473/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004474 * <type>expr7: runtime type check / conversion
4475 */
4476 static int
4477compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4478{
4479 type_T *want_type = NULL;
4480
4481 // Recognize <type>
4482 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4483 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004484 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004485 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4486 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004487 return FAIL;
4488
4489 if (**arg != '>')
4490 {
4491 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004492 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004493 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004494 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004495 return FAIL;
4496 }
4497 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004498 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004499 return FAIL;
4500 }
4501
4502 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4503 return FAIL;
4504
4505 if (want_type != NULL)
4506 {
4507 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004508 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004509 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004510
Bram Moolenaard1103582020-08-14 22:44:25 +02004511 generate_ppconst(cctx, ppconst);
4512 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004513 where.wt_index = 0;
4514 where.wt_variable = FALSE;
4515 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004516 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004517 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004518 return FAIL;
4519 }
4520 }
4521
4522 return OK;
4523}
4524
4525/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 * * number multiplication
4527 * / number division
4528 * % number modulo
4529 */
4530 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004531compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532{
4533 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004534 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004535 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004537 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004538 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 return FAIL;
4540
4541 /*
4542 * Repeat computing, until no "*", "/" or "%" is following.
4543 */
4544 for (;;)
4545 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004546 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 if (*op != '*' && *op != '/' && *op != '%')
4548 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004549 if (next != NULL)
4550 {
4551 *arg = next_line_from_context(cctx, TRUE);
4552 op = skipwhite(*arg);
4553 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004554
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004555 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004557 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004558 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004560 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004561 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004563 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004564 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004566
4567 if (ppconst->pp_used == ppconst_used + 2
4568 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4569 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004570 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004571 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4572 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4573 varnumber_T res = 0;
4574 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 // both are numbers: compute the result
4577 switch (*op)
4578 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004579 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004580 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004581 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004582 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004583 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004584 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004585 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004586 break;
4587 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004588 if (failed)
4589 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004590 tv1->vval.v_number = res;
4591 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004592 }
4593 else
4594 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004595 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004596 generate_two_op(cctx, op);
4597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
4599
4600 return OK;
4601}
4602
4603/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004604 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 * - number subtraction
4606 * .. string concatenation
4607 */
4608 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004609compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610{
4611 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004614 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615
4616 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004617 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 return FAIL;
4619
4620 /*
4621 * Repeat computing, until no "+", "-" or ".." is following.
4622 */
4623 for (;;)
4624 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004625 op = may_peek_next_line(cctx, *arg, &next);
4626 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 break;
4628 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004629 if (next != NULL)
4630 {
4631 *arg = next_line_from_context(cctx, TRUE);
4632 op = skipwhite(*arg);
4633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004635 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004637 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004638 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640
Bram Moolenaare0de1712020-12-02 17:36:54 +01004641 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004644 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004646 return FAIL;
4647
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004649 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004650 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4651 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4652 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4653 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4656 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004657
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004658 // concat/subtract/add constant numbers
4659 if (*op == '+')
4660 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4661 else if (*op == '-')
4662 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4663 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004664 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004665 // concatenate constant strings
4666 char_u *s1 = tv1->vval.v_string;
4667 char_u *s2 = tv2->vval.v_string;
4668 size_t len1 = STRLEN(s1);
4669
4670 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4671 if (tv1->vval.v_string == NULL)
4672 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004673 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004674 return FAIL;
4675 }
4676 mch_memmove(tv1->vval.v_string, s1, len1);
4677 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004678 vim_free(s1);
4679 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004680 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 }
4683 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004684 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004686 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004687 if (*op == '.')
4688 {
4689 if (may_generate_2STRING(-2, cctx) == FAIL
4690 || may_generate_2STRING(-1, cctx) == FAIL)
4691 return FAIL;
4692 generate_instr_drop(cctx, ISN_CONCAT, 1);
4693 }
4694 else
4695 generate_two_op(cctx, op);
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 }
4698
4699 return OK;
4700}
4701
4702/*
4703 * expr5a == expr5b
4704 * expr5a =~ expr5b
4705 * expr5a != expr5b
4706 * expr5a !~ expr5b
4707 * expr5a > expr5b
4708 * expr5a >= expr5b
4709 * expr5a < expr5b
4710 * expr5a <= expr5b
4711 * expr5a is expr5b
4712 * expr5a isnot expr5b
4713 *
4714 * Produces instructions:
4715 * EVAL expr5a Push result of "expr5a"
4716 * EVAL expr5b Push result of "expr5b"
4717 * COMPARE one of the compare instructions
4718 */
4719 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004722 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004724 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728
4729 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 return FAIL;
4732
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004733 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004734 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735
4736 /*
4737 * If there is a comparative operator, use it.
4738 */
4739 if (type != EXPR_UNKNOWN)
4740 {
4741 int ic = FALSE; // Default: do not ignore case
4742
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004743 if (next != NULL)
4744 {
4745 *arg = next_line_from_context(cctx, TRUE);
4746 p = skipwhite(*arg);
4747 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 if (type_is && (p[len] == '?' || p[len] == '#'))
4749 {
4750 semsg(_(e_invexpr2), *arg);
4751 return FAIL;
4752 }
4753 // extra question mark appended: ignore case
4754 if (p[len] == '?')
4755 {
4756 ic = TRUE;
4757 ++len;
4758 }
4759 // extra '#' appended: match case (ignored)
4760 else if (p[len] == '#')
4761 ++len;
4762 // nothing appended: match case
4763
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004764 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004766 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004767 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 }
4769
4770 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004771 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004772 return FAIL;
4773
Bram Moolenaara5565e42020-05-09 15:44:01 +02004774 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 return FAIL;
4776
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (ppconst->pp_used == ppconst_used + 2)
4778 {
4779 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4780 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4781 int ret;
4782
4783 // Both sides are a constant, compute the result now.
4784 // First check for a valid combination of types, this is more
4785 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004786 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004787 ret = FAIL;
4788 else
4789 {
4790 ret = typval_compare(tv1, tv2, type, ic);
4791 tv1->v_type = VAR_BOOL;
4792 tv1->vval.v_number = tv1->vval.v_number
4793 ? VVAL_TRUE : VVAL_FALSE;
4794 clear_tv(tv2);
4795 --ppconst->pp_used;
4796 }
4797 return ret;
4798 }
4799
4800 generate_ppconst(cctx, ppconst);
4801 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 }
4803
4804 return OK;
4805}
4806
Bram Moolenaar7f141552020-05-09 17:35:53 +02004807static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809/*
4810 * Compile || or &&.
4811 */
4812 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004813compile_and_or(
4814 char_u **arg,
4815 cctx_T *cctx,
4816 char *op,
4817 ppconst_T *ppconst,
4818 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004819{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004820 char_u *next;
4821 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 int opchar = *op;
4823
4824 if (p[0] == opchar && p[1] == opchar)
4825 {
4826 garray_T *instr = &cctx->ctx_instr;
4827 garray_T end_ga;
4828
4829 /*
4830 * Repeat until there is no following "||" or "&&"
4831 */
4832 ga_init2(&end_ga, sizeof(int), 10);
4833 while (p[0] == opchar && p[1] == opchar)
4834 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004835 long start_lnum = SOURCING_LNUM;
4836 int start_ctx_lnum = cctx->ctx_lnum;
4837 int save_lnum;
4838
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004839 if (next != NULL)
4840 {
4841 *arg = next_line_from_context(cctx, TRUE);
4842 p = skipwhite(*arg);
4843 }
4844
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004845 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4846 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004847 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004848 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004849 return FAIL;
4850 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004852 // TODO: use ppconst if the value is a constant and check
4853 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 generate_ppconst(cctx, ppconst);
4855
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004856 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004857 SOURCING_LNUM = start_lnum;
4858 save_lnum = cctx->ctx_lnum;
4859 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004860 if (bool_on_stack(cctx) == FAIL)
4861 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004862 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004863 ga_clear(&end_ga);
4864 return FAIL;
4865 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004866 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 if (ga_grow(&end_ga, 1) == FAIL)
4869 {
4870 ga_clear(&end_ga);
4871 return FAIL;
4872 }
4873 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4874 ++end_ga.ga_len;
4875 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004876 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
4878 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004879 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004880 {
4881 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004882 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004883 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004884
Bram Moolenaara5565e42020-05-09 15:44:01 +02004885 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4886 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
4888 ga_clear(&end_ga);
4889 return FAIL;
4890 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004891
4892 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004896 // Every part must evaluate to a bool.
4897 if (bool_on_stack(cctx) == FAIL)
4898 {
4899 ga_clear(&end_ga);
4900 return FAIL;
4901 }
4902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 // Fill in the end label in all jumps.
4904 while (end_ga.ga_len > 0)
4905 {
4906 isn_T *isn;
4907
4908 --end_ga.ga_len;
4909 isn = ((isn_T *)instr->ga_data)
4910 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4911 isn->isn_arg.jump.jump_where = instr->ga_len;
4912 }
4913 ga_clear(&end_ga);
4914 }
4915
4916 return OK;
4917}
4918
4919/*
4920 * expr4a && expr4a && expr4a logical AND
4921 *
4922 * Produces instructions:
4923 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004924 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004925 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004927 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 * EVAL expr4c Push result of "expr4c"
4929 * end:
4930 */
4931 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004934 int ppconst_used = ppconst->pp_used;
4935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 return FAIL;
4939
4940 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942}
4943
4944/*
4945 * expr3a || expr3b || expr3c logical OR
4946 *
4947 * Produces instructions:
4948 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004949 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004950 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004952 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 * EVAL expr3c Push result of "expr3c"
4954 * end:
4955 */
4956 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004957compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004959 int ppconst_used = ppconst->pp_used;
4960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 return FAIL;
4964
4965 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004966 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967}
4968
4969/*
4970 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004972 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 * JUMP_IF_FALSE alt jump if false
4974 * EVAL expr1a
4975 * JUMP_ALWAYS end
4976 * alt: EVAL expr1b
4977 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004978 *
4979 * Toplevel expression: expr2 ?? expr1
4980 * Produces instructions:
4981 * EVAL expr2 Push result of "expr2"
4982 * JUMP_AND_KEEP_IF_TRUE end jump if true
4983 * EVAL expr1
4984 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 */
4986 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004987compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988{
4989 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004990 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004991 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992
Bram Moolenaar3988f642020-08-27 22:43:03 +02004993 // Ignore all kinds of errors when not producing code.
4994 if (cctx->ctx_skip == SKIP_YES)
4995 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004996 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004997 return OK;
4998 }
4999
Bram Moolenaar61a89812020-05-07 16:58:17 +02005000 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005001 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 return FAIL;
5003
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005004 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 if (*p == '?')
5006 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005007 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 garray_T *instr = &cctx->ctx_instr;
5009 garray_T *stack = &cctx->ctx_type_stack;
5010 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005011 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005013 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005014 int has_const_expr = FALSE;
5015 int const_value = FALSE;
5016 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005017
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005018 if (next != NULL)
5019 {
5020 *arg = next_line_from_context(cctx, TRUE);
5021 p = skipwhite(*arg);
5022 }
5023
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005024 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005025 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005026 semsg(_(e_white_space_required_before_and_after_str_at_str),
5027 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005028 return FAIL;
5029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005030
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 if (ppconst->pp_used == ppconst_used + 1)
5032 {
5033 // the condition is a constant, we know whether the ? or the :
5034 // expression is to be evaluated.
5035 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005036 if (op_falsy)
5037 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5038 else
5039 {
5040 int error = FALSE;
5041
5042 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5043 &error);
5044 if (error)
5045 return FAIL;
5046 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005047 cctx->ctx_skip = save_skip == SKIP_YES ||
5048 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5049
5050 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5051 // "left ?? right" and "left" is truthy: produce "left"
5052 generate_ppconst(cctx, ppconst);
5053 else
5054 {
5055 clear_tv(&ppconst->pp_tv[ppconst_used]);
5056 --ppconst->pp_used;
5057 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005058 }
5059 else
5060 {
5061 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005062 if (op_falsy)
5063 end_idx = instr->ga_len;
5064 generate_JUMP(cctx, op_falsy
5065 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5066 if (op_falsy)
5067 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069
5070 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005071 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005072 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005073 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005074 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
Bram Moolenaara5565e42020-05-09 15:44:01 +02005076 if (!has_const_expr)
5077 {
5078 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005080 if (!op_falsy)
5081 {
5082 // remember the type and drop it
5083 --stack->ga_len;
5084 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005086 end_idx = instr->ga_len;
5087 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005088
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005089 // jump here from JUMP_IF_FALSE
5090 isn = ((isn_T *)instr->ga_data) + alt_idx;
5091 isn->isn_arg.jump.jump_where = instr->ga_len;
5092 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005095 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005097 // Check for the ":".
5098 p = may_peek_next_line(cctx, *arg, &next);
5099 if (*p != ':')
5100 {
5101 emsg(_(e_missing_colon));
5102 return FAIL;
5103 }
5104 if (next != NULL)
5105 {
5106 *arg = next_line_from_context(cctx, TRUE);
5107 p = skipwhite(*arg);
5108 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005109
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005110 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5111 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005112 semsg(_(e_white_space_required_before_and_after_str_at_str),
5113 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005114 return FAIL;
5115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005117 // evaluate the third expression
5118 if (has_const_expr)
5119 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005120 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005121 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005122 return FAIL;
5123 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5124 return FAIL;
5125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127 if (!has_const_expr)
5128 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005129 type_T **typep;
5130
Bram Moolenaara5565e42020-05-09 15:44:01 +02005131 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132
Bram Moolenaara5565e42020-05-09 15:44:01 +02005133 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005134 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5135 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005136
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005137 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138 isn = ((isn_T *)instr->ga_data) + end_idx;
5139 isn->isn_arg.jump.jump_where = instr->ga_len;
5140 }
5141
5142 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 }
5144 return OK;
5145}
5146
5147/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005148 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005149 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5150 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005151 */
5152 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005153compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005154{
5155 ppconst_T ppconst;
5156
5157 CLEAR_FIELD(ppconst);
5158 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5159 {
5160 clear_ppconst(&ppconst);
5161 return FAIL;
5162 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005163 if (is_const != NULL)
5164 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005165 if (generate_ppconst(cctx, &ppconst) == FAIL)
5166 return FAIL;
5167 return OK;
5168}
5169
5170/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005171 * Toplevel expression.
5172 */
5173 static int
5174compile_expr0(char_u **arg, cctx_T *cctx)
5175{
5176 return compile_expr0_ext(arg, cctx, NULL);
5177}
5178
5179/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 * compile "return [expr]"
5181 */
5182 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005183compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184{
5185 char_u *p = arg;
5186 garray_T *stack = &cctx->ctx_type_stack;
5187 type_T *stack_type;
5188
5189 if (*p != NUL && *p != '|' && *p != '\n')
5190 {
5191 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005192 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 return NULL;
5194
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005195 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005196 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005197 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005198 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005199 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5200 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005201 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005202 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005203 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005204 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005205 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005206 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5207 && stack_type->tt_type != VAR_VOID
5208 && stack_type->tt_type != VAR_UNKNOWN)
5209 {
5210 emsg(_(e_returning_value_in_function_without_return_type));
5211 return NULL;
5212 }
5213 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005214 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005215 return NULL;
5216 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219 else
5220 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005221 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005222 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005223 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5224 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005226 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 return NULL;
5228 }
5229
5230 // No argument, return zero.
5231 generate_PUSHNR(cctx, 0);
5232 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005233
5234 // Undo any command modifiers.
5235 generate_undo_cmdmods(cctx);
5236
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005237 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 return NULL;
5239
5240 // "return val | endif" is possible
5241 return skipwhite(p);
5242}
5243
5244/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005245 * Get a line from the compilation context, compatible with exarg_T getline().
5246 * Return a pointer to the line in allocated memory.
5247 * Return NULL for end-of-file or some error.
5248 */
5249 static char_u *
5250exarg_getline(
5251 int c UNUSED,
5252 void *cookie,
5253 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005254 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005255{
5256 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005257 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005258
Bram Moolenaar66250c92020-08-20 15:02:42 +02005259 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005260 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005261 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005262 return NULL;
5263 ++cctx->ctx_lnum;
5264 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5265 // Comment lines result in NULL pointers, skip them.
5266 if (p != NULL)
5267 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005268 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005269}
5270
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005271 void
5272fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5273{
5274 eap->getline = exarg_getline;
5275 eap->cookie = cctx;
5276}
5277
Bram Moolenaar04b12692020-05-04 23:24:44 +02005278/*
5279 * Compile a nested :def command.
5280 */
5281 static char_u *
5282compile_nested_function(exarg_T *eap, cctx_T *cctx)
5283{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005284 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005285 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005286 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005287 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005288 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005289 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005290
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005291 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005292 {
5293 emsg(_(e_cannot_use_bang_with_nested_def));
5294 return NULL;
5295 }
5296
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005297 if (*name_start == '/')
5298 {
5299 name_end = skip_regexp(name_start + 1, '/', TRUE);
5300 if (*name_end == '/')
5301 ++name_end;
5302 eap->nextcmd = check_nextcmd(name_end);
5303 }
5304 if (name_end == name_start || *skipwhite(name_end) != '(')
5305 {
5306 if (!ends_excmd2(name_start, name_end))
5307 {
5308 semsg(_(e_invalid_command_str), eap->cmd);
5309 return NULL;
5310 }
5311
5312 // "def" or "def Name": list functions
5313 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5314 return NULL;
5315 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5316 }
5317
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005318 // Only g:Func() can use a namespace.
5319 if (name_start[1] == ':' && !is_global)
5320 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005321 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005322 return NULL;
5323 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005324 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005325 return NULL;
5326
Bram Moolenaar04b12692020-05-04 23:24:44 +02005327 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005328 fill_exarg_from_cctx(eap, cctx);
5329
Bram Moolenaar04b12692020-05-04 23:24:44 +02005330 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005331 lambda_name = vim_strsave(get_lambda_name());
5332 if (lambda_name == NULL)
5333 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005334 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005335
Bram Moolenaar822ba242020-05-24 23:00:18 +02005336 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005337 {
5338 r = eap->skip ? OK : FAIL;
5339 goto theend;
5340 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005341
5342 // copy over the block scope IDs before compiling
5343 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5344 {
5345 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5346
5347 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5348 if (ufunc->uf_block_ids != NULL)
5349 {
5350 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5351 sizeof(int) * block_depth);
5352 ufunc->uf_block_depth = block_depth;
5353 }
5354 }
5355
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005356 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5357 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005358 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005359 {
5360 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005361 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005362 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005363
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005364 if (is_global)
5365 {
5366 char_u *func_name = vim_strnsave(name_start + 2,
5367 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005368
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005369 if (func_name == NULL)
5370 r = FAIL;
5371 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005372 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005373 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005374 lambda_name = NULL;
5375 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005376 }
5377 else
5378 {
5379 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005380 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005381 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005382
Bram Moolenaareef21022020-08-01 22:16:43 +02005383 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005384 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005385 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005386 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005387 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5388 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005389 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005390
5391theend:
5392 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005393 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005394}
5395
5396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 * Return the length of an assignment operator, or zero if there isn't one.
5398 */
5399 int
5400assignment_len(char_u *p, int *heredoc)
5401{
5402 if (*p == '=')
5403 {
5404 if (p[1] == '<' && p[2] == '<')
5405 {
5406 *heredoc = TRUE;
5407 return 3;
5408 }
5409 return 1;
5410 }
5411 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5412 return 2;
5413 if (STRNCMP(p, "..=", 3) == 0)
5414 return 3;
5415 return 0;
5416}
5417
5418// words that cannot be used as a variable
5419static char *reserved[] = {
5420 "true",
5421 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005422 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 NULL
5424};
5425
Bram Moolenaar752fc692021-01-04 21:57:11 +01005426// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005427typedef enum {
5428 dest_local,
5429 dest_option,
5430 dest_env,
5431 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005432 dest_buffer,
5433 dest_window,
5434 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005435 dest_vimvar,
5436 dest_script,
5437 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005438 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005439} assign_dest_T;
5440
Bram Moolenaar752fc692021-01-04 21:57:11 +01005441// Used by compile_lhs() to store information about the LHS of an assignment
5442// and one argument of ":unlet" with an index.
5443typedef struct {
5444 assign_dest_T lhs_dest; // type of destination
5445
5446 char_u *lhs_name; // allocated name including
5447 // "[expr]" or ".name".
5448 size_t lhs_varlen; // length of the variable without
5449 // "[expr]" or ".name"
5450 char_u *lhs_dest_end; // end of the destination, including
5451 // "[expr]" or ".name".
5452
5453 int lhs_has_index; // has "[expr]" or ".name"
5454
5455 int lhs_new_local; // create new local variable
5456 int lhs_opt_flags; // for when destination is an option
5457 int lhs_vimvaridx; // for when destination is a v:var
5458
5459 lvar_T lhs_local_lvar; // used for existing local destination
5460 lvar_T lhs_arg_lvar; // used for argument destination
5461 lvar_T *lhs_lvar; // points to destination lvar
5462 int lhs_scriptvar_sid;
5463 int lhs_scriptvar_idx;
5464
5465 int lhs_has_type; // type was specified
5466 type_T *lhs_type;
5467 type_T *lhs_member_type;
5468} lhs_T;
5469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005470/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005471 * Generate the load instruction for "name".
5472 */
5473 static void
5474generate_loadvar(
5475 cctx_T *cctx,
5476 assign_dest_T dest,
5477 char_u *name,
5478 lvar_T *lvar,
5479 type_T *type)
5480{
5481 switch (dest)
5482 {
5483 case dest_option:
5484 // TODO: check the option exists
5485 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5486 break;
5487 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005488 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5489 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5490 else
5491 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005492 break;
5493 case dest_buffer:
5494 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5495 break;
5496 case dest_window:
5497 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5498 break;
5499 case dest_tab:
5500 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5501 break;
5502 case dest_script:
5503 compile_load_scriptvar(cctx,
5504 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5505 break;
5506 case dest_env:
5507 // Include $ in the name here
5508 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5509 break;
5510 case dest_reg:
5511 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5512 break;
5513 case dest_vimvar:
5514 generate_LOADV(cctx, name + 2, TRUE);
5515 break;
5516 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005517 if (lvar->lv_from_outer > 0)
5518 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5519 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005520 else
5521 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5522 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005523 case dest_expr:
5524 // list or dict value should already be on the stack.
5525 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005526 }
5527}
5528
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005529/*
5530 * Skip over "[expr]" or ".member".
5531 * Does not check for any errors.
5532 */
5533 static char_u *
5534skip_index(char_u *start)
5535{
5536 char_u *p = start;
5537
5538 if (*p == '[')
5539 {
5540 p = skipwhite(p + 1);
5541 (void)skip_expr(&p, NULL);
5542 p = skipwhite(p);
5543 if (*p == ']')
5544 return p + 1;
5545 return p;
5546 }
5547 // if (*p == '.')
5548 return to_name_end(p + 1, TRUE);
5549}
5550
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005551 void
5552vim9_declare_error(char_u *name)
5553{
5554 char *scope = "";
5555
5556 switch (*name)
5557 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005558 case 'g': scope = _("global"); break;
5559 case 'b': scope = _("buffer"); break;
5560 case 'w': scope = _("window"); break;
5561 case 't': scope = _("tab"); break;
5562 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005563 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005564 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005565 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005566 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005567 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005568 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005569 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005570 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005571 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005572}
5573
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005574/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005575 * For one assignment figure out the type of destination. Return it in "dest".
5576 * When not recognized "dest" is not set.
5577 * For an option "opt_flags" is set.
5578 * For a v:var "vimvaridx" is set.
5579 * "type" is set to the destination type if known, unchanted otherwise.
5580 * Return FAIL if an error message was given.
5581 */
5582 static int
5583get_var_dest(
5584 char_u *name,
5585 assign_dest_T *dest,
5586 int cmdidx,
5587 int *opt_flags,
5588 int *vimvaridx,
5589 type_T **type,
5590 cctx_T *cctx)
5591{
5592 char_u *p;
5593
5594 if (*name == '&')
5595 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005596 int cc;
5597 long numval;
5598 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005599
5600 *dest = dest_option;
5601 if (cmdidx == CMD_final || cmdidx == CMD_const)
5602 {
5603 emsg(_(e_const_option));
5604 return FAIL;
5605 }
5606 p = name;
5607 p = find_option_end(&p, opt_flags);
5608 if (p == NULL)
5609 {
5610 // cannot happen?
5611 emsg(_(e_letunexp));
5612 return FAIL;
5613 }
5614 cc = *p;
5615 *p = NUL;
5616 opt_type = get_option_value(skip_option_env_lead(name),
5617 &numval, NULL, *opt_flags);
5618 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005619 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005620 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005621 case gov_unknown:
5622 semsg(_(e_unknown_option), name);
5623 return FAIL;
5624 case gov_string:
5625 case gov_hidden_string:
5626 *type = &t_string;
5627 break;
5628 case gov_bool:
5629 case gov_hidden_bool:
5630 *type = &t_bool;
5631 break;
5632 case gov_number:
5633 case gov_hidden_number:
5634 *type = &t_number;
5635 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005636 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005637 }
5638 else if (*name == '$')
5639 {
5640 *dest = dest_env;
5641 *type = &t_string;
5642 }
5643 else if (*name == '@')
5644 {
5645 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5646 {
5647 emsg_invreg(name[1]);
5648 return FAIL;
5649 }
5650 *dest = dest_reg;
5651 *type = &t_string;
5652 }
5653 else if (STRNCMP(name, "g:", 2) == 0)
5654 {
5655 *dest = dest_global;
5656 }
5657 else if (STRNCMP(name, "b:", 2) == 0)
5658 {
5659 *dest = dest_buffer;
5660 }
5661 else if (STRNCMP(name, "w:", 2) == 0)
5662 {
5663 *dest = dest_window;
5664 }
5665 else if (STRNCMP(name, "t:", 2) == 0)
5666 {
5667 *dest = dest_tab;
5668 }
5669 else if (STRNCMP(name, "v:", 2) == 0)
5670 {
5671 typval_T *vtv;
5672 int di_flags;
5673
5674 *vimvaridx = find_vim_var(name + 2, &di_flags);
5675 if (*vimvaridx < 0)
5676 {
5677 semsg(_(e_variable_not_found_str), name);
5678 return FAIL;
5679 }
5680 // We use the current value of "sandbox" here, is that OK?
5681 if (var_check_ro(di_flags, name, FALSE))
5682 return FAIL;
5683 *dest = dest_vimvar;
5684 vtv = get_vim_var_tv(*vimvaridx);
5685 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5686 }
5687 return OK;
5688}
5689
5690/*
5691 * Generate a STORE instruction for "dest", not being "dest_local".
5692 * Return FAIL when out of memory.
5693 */
5694 static int
5695generate_store_var(
5696 cctx_T *cctx,
5697 assign_dest_T dest,
5698 int opt_flags,
5699 int vimvaridx,
5700 int scriptvar_idx,
5701 int scriptvar_sid,
5702 type_T *type,
5703 char_u *name)
5704{
5705 switch (dest)
5706 {
5707 case dest_option:
5708 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5709 opt_flags);
5710 case dest_global:
5711 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005712 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5713 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005714 case dest_buffer:
5715 // include b: with the name, easier to execute that way
5716 return generate_STORE(cctx, ISN_STOREB, 0, name);
5717 case dest_window:
5718 // include w: with the name, easier to execute that way
5719 return generate_STORE(cctx, ISN_STOREW, 0, name);
5720 case dest_tab:
5721 // include t: with the name, easier to execute that way
5722 return generate_STORE(cctx, ISN_STORET, 0, name);
5723 case dest_env:
5724 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5725 case dest_reg:
5726 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5727 case dest_vimvar:
5728 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5729 case dest_script:
5730 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005731 // "s:" may be included in the name.
5732 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005733 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005734 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5735 scriptvar_sid, scriptvar_idx, type);
5736 case dest_local:
5737 case dest_expr:
5738 // cannot happen
5739 break;
5740 }
5741 return FAIL;
5742}
5743
Bram Moolenaar752fc692021-01-04 21:57:11 +01005744 static int
5745is_decl_command(int cmdidx)
5746{
5747 return cmdidx == CMD_let || cmdidx == CMD_var
5748 || cmdidx == CMD_final || cmdidx == CMD_const;
5749}
5750
5751/*
5752 * Figure out the LHS type and other properties for an assignment or one item
5753 * of ":unlet" with an index.
5754 * Returns OK or FAIL.
5755 */
5756 static int
5757compile_lhs(
5758 char_u *var_start,
5759 lhs_T *lhs,
5760 int cmdidx,
5761 int heredoc,
5762 int oplen,
5763 cctx_T *cctx)
5764{
5765 char_u *var_end;
5766 int is_decl = is_decl_command(cmdidx);
5767
5768 CLEAR_POINTER(lhs);
5769 lhs->lhs_dest = dest_local;
5770 lhs->lhs_vimvaridx = -1;
5771 lhs->lhs_scriptvar_idx = -1;
5772
5773 // "dest_end" is the end of the destination, including "[expr]" or
5774 // ".name".
5775 // "var_end" is the end of the variable/option/etc. name.
5776 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5777 if (*var_start == '@')
5778 var_end = var_start + 2;
5779 else
5780 {
5781 // skip over the leading "&", "&l:", "&g:" and "$"
5782 var_end = skip_option_env_lead(var_start);
5783 var_end = to_name_end(var_end, TRUE);
5784 }
5785
5786 // "a: type" is declaring variable "a" with a type, not dict "a:".
5787 if (is_decl && lhs->lhs_dest_end == var_start + 2
5788 && lhs->lhs_dest_end[-1] == ':')
5789 --lhs->lhs_dest_end;
5790 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5791 --var_end;
5792
5793 // compute the length of the destination without "[expr]" or ".name"
5794 lhs->lhs_varlen = var_end - var_start;
5795 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5796 if (lhs->lhs_name == NULL)
5797 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005798
5799 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5800 // Something follows after the variable: "var[idx]" or "var.key".
5801 lhs->lhs_has_index = TRUE;
5802
Bram Moolenaar752fc692021-01-04 21:57:11 +01005803 if (heredoc)
5804 lhs->lhs_type = &t_list_string;
5805 else
5806 lhs->lhs_type = &t_any;
5807
5808 if (cctx->ctx_skip != SKIP_YES)
5809 {
5810 int declare_error = FALSE;
5811
5812 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5813 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5814 &lhs->lhs_type, cctx) == FAIL)
5815 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005816 if (lhs->lhs_dest != dest_local
5817 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005818 {
5819 // Specific kind of variable recognized.
5820 declare_error = is_decl;
5821 }
5822 else
5823 {
5824 int idx;
5825
5826 // No specific kind of variable recognized, just a name.
5827 for (idx = 0; reserved[idx] != NULL; ++idx)
5828 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5829 {
5830 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5831 return FAIL;
5832 }
5833
5834
5835 if (lookup_local(var_start, lhs->lhs_varlen,
5836 &lhs->lhs_local_lvar, cctx) == OK)
5837 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5838 else
5839 {
5840 CLEAR_FIELD(lhs->lhs_arg_lvar);
5841 if (arg_exists(var_start, lhs->lhs_varlen,
5842 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5843 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5844 {
5845 if (is_decl)
5846 {
5847 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5848 return FAIL;
5849 }
5850 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5851 }
5852 }
5853 if (lhs->lhs_lvar != NULL)
5854 {
5855 if (is_decl)
5856 {
5857 semsg(_(e_variable_already_declared), lhs->lhs_name);
5858 return FAIL;
5859 }
5860 }
5861 else
5862 {
5863 int script_namespace = lhs->lhs_varlen > 1
5864 && STRNCMP(var_start, "s:", 2) == 0;
5865 int script_var = (script_namespace
5866 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005867 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005868 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005869 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005870 imported_T *import =
5871 find_imported(var_start, lhs->lhs_varlen, cctx);
5872
5873 if (script_namespace || script_var || import != NULL)
5874 {
5875 char_u *rawname = lhs->lhs_name
5876 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5877
5878 if (is_decl)
5879 {
5880 if (script_namespace)
5881 semsg(_(e_cannot_declare_script_variable_in_function),
5882 lhs->lhs_name);
5883 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005884 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005885 lhs->lhs_name);
5886 return FAIL;
5887 }
5888 else if (cctx->ctx_ufunc->uf_script_ctx_version
5889 == SCRIPT_VERSION_VIM9
5890 && script_namespace
5891 && !script_var && import == NULL)
5892 {
5893 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5894 return FAIL;
5895 }
5896
5897 lhs->lhs_dest = dest_script;
5898
5899 // existing script-local variables should have a type
5900 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5901 if (import != NULL)
5902 lhs->lhs_scriptvar_sid = import->imp_sid;
5903 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5904 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005905 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005907 lhs->lhs_scriptvar_sid, rawname,
5908 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5909 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005910 if (lhs->lhs_scriptvar_idx >= 0)
5911 {
5912 scriptitem_T *si = SCRIPT_ITEM(
5913 lhs->lhs_scriptvar_sid);
5914 svar_T *sv =
5915 ((svar_T *)si->sn_var_vals.ga_data)
5916 + lhs->lhs_scriptvar_idx;
5917 lhs->lhs_type = sv->sv_type;
5918 }
5919 }
5920 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005921 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005922 == FAIL)
5923 return FAIL;
5924 }
5925 }
5926
5927 if (declare_error)
5928 {
5929 vim9_declare_error(lhs->lhs_name);
5930 return FAIL;
5931 }
5932 }
5933
5934 // handle "a:name" as a name, not index "name" on "a"
5935 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5936 var_end = lhs->lhs_dest_end;
5937
5938 if (lhs->lhs_dest != dest_option)
5939 {
5940 if (is_decl && *var_end == ':')
5941 {
5942 char_u *p;
5943
5944 // parse optional type: "let var: type = expr"
5945 if (!VIM_ISWHITE(var_end[1]))
5946 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005947 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005948 return FAIL;
5949 }
5950 p = skipwhite(var_end + 1);
5951 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5952 if (lhs->lhs_type == NULL)
5953 return FAIL;
5954 lhs->lhs_has_type = TRUE;
5955 }
5956 else if (lhs->lhs_lvar != NULL)
5957 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5958 }
5959
Bram Moolenaare42939a2021-04-05 17:11:17 +02005960 if (oplen == 3 && !heredoc
5961 && lhs->lhs_dest != dest_global
5962 && !lhs->lhs_has_index
5963 && lhs->lhs_type->tt_type != VAR_STRING
5964 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005965 {
5966 emsg(_(e_can_only_concatenate_to_string));
5967 return FAIL;
5968 }
5969
5970 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5971 && cctx->ctx_skip != SKIP_YES)
5972 {
5973 if (oplen > 1 && !heredoc)
5974 {
5975 // +=, /=, etc. require an existing variable
5976 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5977 return FAIL;
5978 }
5979 if (!is_decl)
5980 {
5981 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5982 return FAIL;
5983 }
5984
Bram Moolenaar3f327882021-03-17 20:56:38 +01005985 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005986 if ((lhs->lhs_type->tt_type == VAR_FUNC
5987 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005988 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005989 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005990
5991 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005992 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5993 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5994 if (lhs->lhs_lvar == NULL)
5995 return FAIL;
5996 lhs->lhs_new_local = TRUE;
5997 }
5998
5999 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01006000 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006001 {
6002 // Something follows after the variable: "var[idx]" or "var.key".
6003 // TODO: should we also handle "->func()" here?
6004 if (is_decl)
6005 {
6006 emsg(_(e_cannot_use_index_when_declaring_variable));
6007 return FAIL;
6008 }
6009
6010 if (var_start[lhs->lhs_varlen] == '['
6011 || var_start[lhs->lhs_varlen] == '.')
6012 {
6013 char_u *after = var_start + lhs->lhs_varlen;
6014 char_u *p;
6015
6016 // Only the last index is used below, if there are others
6017 // before it generate code for the expression. Thus for
6018 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6019 for (;;)
6020 {
6021 p = skip_index(after);
6022 if (*p != '[' && *p != '.')
6023 break;
6024 after = p;
6025 }
6026 if (after > var_start + lhs->lhs_varlen)
6027 {
6028 lhs->lhs_varlen = after - var_start;
6029 lhs->lhs_dest = dest_expr;
6030 // We don't know the type before evaluating the expression,
6031 // use "any" until then.
6032 lhs->lhs_type = &t_any;
6033 }
6034
Bram Moolenaar752fc692021-01-04 21:57:11 +01006035 if (lhs->lhs_type->tt_member == NULL)
6036 lhs->lhs_member_type = &t_any;
6037 else
6038 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6039 }
6040 else
6041 {
6042 semsg("Not supported yet: %s", var_start);
6043 return FAIL;
6044 }
6045 }
6046 return OK;
6047}
6048
6049/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006050 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6051 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006052 */
6053 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006054compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 char_u *var_start,
6056 lhs_T *lhs,
6057 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006058 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006059 cctx_T *cctx)
6060{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006062 char_u *p;
6063 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 p = var_start + varlen;
6066 if (*p == '[')
6067 {
6068 p = skipwhite(p + 1);
6069 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006070
6071 if (r == OK && *skipwhite(p) == ':')
6072 {
6073 // unlet var[idx : idx]
6074 if (is_assign)
6075 {
6076 semsg(_(e_cannot_use_range_with_assignment_str), p);
6077 return FAIL;
6078 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006079 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006080 p = skipwhite(p);
6081 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6082 {
6083 semsg(_(e_white_space_required_before_and_after_str_at_str),
6084 ":", p);
6085 return FAIL;
6086 }
6087 p = skipwhite(p + 1);
6088 r = compile_expr0(&p, cctx);
6089 }
6090
Bram Moolenaar752fc692021-01-04 21:57:11 +01006091 if (r == OK && *skipwhite(p) != ']')
6092 {
6093 // this should not happen
6094 emsg(_(e_missbrac));
6095 r = FAIL;
6096 }
6097 }
6098 else // if (*p == '.')
6099 {
6100 char_u *key_end = to_name_end(p + 1, TRUE);
6101 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6102
6103 r = generate_PUSHS(cctx, key);
6104 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006105 return r;
6106}
6107
6108/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006109 * For a LHS with an index, load the variable to be indexed.
6110 */
6111 static int
6112compile_load_lhs(
6113 lhs_T *lhs,
6114 char_u *var_start,
6115 type_T *rhs_type,
6116 cctx_T *cctx)
6117{
6118 if (lhs->lhs_dest == dest_expr)
6119 {
6120 size_t varlen = lhs->lhs_varlen;
6121 int c = var_start[varlen];
6122 char_u *p = var_start;
6123 garray_T *stack = &cctx->ctx_type_stack;
6124
6125 // Evaluate "ll[expr]" of "ll[expr][idx]"
6126 var_start[varlen] = NUL;
6127 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6128 {
6129 // this should not happen
6130 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006131 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006132 return FAIL;
6133 }
6134 var_start[varlen] = c;
6135
6136 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6137 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6138 // now we can properly check the type
6139 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6140 && rhs_type != &t_void
6141 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6142 FALSE, FALSE) == FAIL)
6143 return FAIL;
6144 }
6145 else
6146 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6147 lhs->lhs_lvar, lhs->lhs_type);
6148 return OK;
6149}
6150
6151/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006152 * Assignment to a list or dict member, or ":unlet" for the item, using the
6153 * information in "lhs".
6154 * Returns OK or FAIL.
6155 */
6156 static int
6157compile_assign_unlet(
6158 char_u *var_start,
6159 lhs_T *lhs,
6160 int is_assign,
6161 type_T *rhs_type,
6162 cctx_T *cctx)
6163{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006164 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006165 garray_T *stack = &cctx->ctx_type_stack;
6166 int range = FALSE;
6167
6168 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 return FAIL;
6170
6171 if (lhs->lhs_type == &t_any)
6172 {
6173 // Index on variable of unknown type: check at runtime.
6174 dest_type = VAR_ANY;
6175 }
6176 else
6177 {
6178 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006179 if (dest_type == VAR_DICT && range)
6180 {
6181 emsg(e_cannot_use_range_with_dictionary);
6182 return FAIL;
6183 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6185 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006186 if (dest_type == VAR_LIST)
6187 {
6188 if (range
6189 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006190 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006191 return FAIL;
6192 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6193 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6194 return FAIL;
6195 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 }
6197
6198 // Load the dict or list. On the stack we then have:
6199 // - value (for assignment, not for :unlet)
6200 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006201 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006202 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006203 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6204 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205
6206 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6207 {
6208 if (is_assign)
6209 {
6210 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6211
6212 if (isn == NULL)
6213 return FAIL;
6214 isn->isn_arg.vartype = dest_type;
6215 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006216 else if (range)
6217 {
6218 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6219 return FAIL;
6220 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221 else
6222 {
6223 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6224 return FAIL;
6225 }
6226 }
6227 else
6228 {
6229 emsg(_(e_indexable_type_required));
6230 return FAIL;
6231 }
6232
6233 return OK;
6234}
6235
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006236/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006237 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006238 * "let name"
6239 * "var name = expr"
6240 * "final name = expr"
6241 * "const name = expr"
6242 * "name = expr"
6243 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006244 * Return NULL for an error.
6245 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006246 */
6247 static char_u *
6248compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6249{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006250 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006252 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 char_u *ret = NULL;
6254 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006255 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006258 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 int oplen = 0;
6261 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006262 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006264 int is_decl = is_decl_command(cmdidx);
6265 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006266 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268 // Skip over the "var" or "[var, var]" to get to any "=".
6269 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6270 if (p == NULL)
6271 return *arg == '[' ? arg : NULL;
6272
6273 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006275 // TODO: should we allow this, and figure out type inference from list
6276 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006277 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 return NULL;
6279 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006280 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282 sp = p;
6283 p = skipwhite(p);
6284 op = p;
6285 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006286
6287 if (var_count > 0 && oplen == 0)
6288 // can be something like "[1, 2]->func()"
6289 return arg;
6290
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006291 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006293 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006294 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006295 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 if (heredoc)
6298 {
6299 list_T *l;
6300 listitem_T *li;
6301
6302 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006303 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006305 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006306 if (l == NULL)
6307 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
Bram Moolenaar078269b2020-09-21 20:35:55 +02006309 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006311 // Push each line and the create the list.
6312 FOR_ALL_LIST_ITEMS(l, li)
6313 {
6314 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6315 li->li_tv.vval.v_string = NULL;
6316 }
6317 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 list_free(l);
6320 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006321 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006323 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006325 char_u *wp;
6326
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006327 // for "[var, var] = expr" evaluate the expression here, loop over the
6328 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006329 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006330
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006331 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006332 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006333 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006334 if (compile_expr0(&p, cctx) == FAIL)
6335 return NULL;
6336 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006337
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006338 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006340 type_T *stacktype;
6341
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006342 stacktype = stack->ga_len == 0 ? &t_void
6343 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006344 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006345 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006346 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006347 goto theend;
6348 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006349 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006350 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006351 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006352 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006353 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6354 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006355 if (stacktype->tt_member != NULL)
6356 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006357 }
6358 }
6359
6360 /*
6361 * Loop over variables in "[var, var] = expr".
6362 * For "var = expr" and "let var: type" this is done only once.
6363 */
6364 if (var_count > 0)
6365 var_start = skipwhite(arg + 1); // skip over the "["
6366 else
6367 var_start = arg;
6368 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6369 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006370 int instr_count = -1;
6371
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02006372 if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
6373 {
6374 // Ignore underscore in "[a, _, b] = list".
6375 if (var_count > 0)
6376 {
6377 var_start = skipwhite(var_start + 2);
6378 continue;
6379 }
6380 emsg(_(e_cannot_use_underscore_here));
6381 goto theend;
6382 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006383 vim_free(lhs.lhs_name);
6384
6385 /*
6386 * Figure out the LHS type and other properties.
6387 */
6388 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6389 goto theend;
6390
6391 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006392 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006393 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006394 goto theend;
6395 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006396 if (!is_decl && lhs.lhs_lvar != NULL
6397 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006398 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006399 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006400 goto theend;
6401 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006402
6403 if (!heredoc)
6404 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006405 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006406 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006407 if (oplen > 0 && var_count == 0)
6408 {
6409 // skip over the "=" and the expression
6410 p = skipwhite(op + oplen);
6411 compile_expr0(&p, cctx);
6412 }
6413 }
6414 else if (oplen > 0)
6415 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006416 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006417 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006418
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006419 // For "var = expr" evaluate the expression.
6420 if (var_count == 0)
6421 {
6422 int r;
6423
6424 // for "+=", "*=", "..=" etc. first load the current value
6425 if (*op != '=')
6426 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006427 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006428
Bram Moolenaar752fc692021-01-04 21:57:11 +01006429 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006430 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006431 int range = FALSE;
6432
6433 // Get member from list or dict. First compile the
6434 // index value.
6435 if (compile_assign_index(var_start, &lhs,
6436 TRUE, &range, cctx) == FAIL)
6437 goto theend;
6438
6439 // Get the member.
6440 if (compile_member(FALSE, cctx) == FAIL)
6441 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006442 }
6443 }
6444
6445 // Compile the expression. Temporarily hide the new local
6446 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006447 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006448 --cctx->ctx_locals.ga_len;
6449 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006450 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006451 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006452 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006453 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006454 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006455 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006456 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006457 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006458 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006459 ++cctx->ctx_locals.ga_len;
6460 if (r == FAIL)
6461 goto theend;
6462 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006463 else if (semicolon && var_idx == var_count - 1)
6464 {
6465 // For "[var; var] = expr" get the rest of the list
6466 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6467 goto theend;
6468 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006469 else
6470 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006471 // For "[var, var] = expr" get the "var_idx" item from the
6472 // list.
6473 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006474 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006475 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006476
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006477 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006478 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006479 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006480 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006481 if ((rhs_type->tt_type == VAR_FUNC
6482 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006483 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006485 goto theend;
6486
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006488 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006489 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006490 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006491 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006492 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493 }
6494 else
6495 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006496 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006497 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006498 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006500 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006501 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006502 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006504 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006506 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006507 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006508 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006509 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006510 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006511
Bram Moolenaar77709b12021-04-03 21:01:01 +02006512 // Without operator check type here, otherwise below.
6513 // Use the line number of the assignment.
6514 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006515 if (lhs.lhs_has_index)
6516 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006517 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006518 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006519 goto theend;
6520 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006521 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006523 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006524 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006525 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006526 else if (cmdidx == CMD_final)
6527 {
6528 emsg(_(e_final_requires_a_value));
6529 goto theend;
6530 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006531 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006533 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006534 goto theend;
6535 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006536 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006537 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006538 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006539 goto theend;
6540 }
6541 else
6542 {
6543 // variables are always initialized
6544 if (ga_grow(instr, 1) == FAIL)
6545 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006546 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006547 {
6548 case VAR_BOOL:
6549 generate_PUSHBOOL(cctx, VVAL_FALSE);
6550 break;
6551 case VAR_FLOAT:
6552#ifdef FEAT_FLOAT
6553 generate_PUSHF(cctx, 0.0);
6554#endif
6555 break;
6556 case VAR_STRING:
6557 generate_PUSHS(cctx, NULL);
6558 break;
6559 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006560 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006561 break;
6562 case VAR_FUNC:
6563 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6564 break;
6565 case VAR_LIST:
6566 generate_NEWLIST(cctx, 0);
6567 break;
6568 case VAR_DICT:
6569 generate_NEWDICT(cctx, 0);
6570 break;
6571 case VAR_JOB:
6572 generate_PUSHJOB(cctx, NULL);
6573 break;
6574 case VAR_CHANNEL:
6575 generate_PUSHCHANNEL(cctx, NULL);
6576 break;
6577 case VAR_NUMBER:
6578 case VAR_UNKNOWN:
6579 case VAR_ANY:
6580 case VAR_PARTIAL:
6581 case VAR_VOID:
6582 case VAR_SPECIAL: // cannot happen
6583 generate_PUSHNR(cctx, 0);
6584 break;
6585 }
6586 }
6587 if (var_count == 0)
6588 end = p;
6589 }
6590
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006591 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006592 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006593 break;
6594
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006595 if (oplen > 0 && *op != '=')
6596 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006597 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006598 type_T *stacktype;
6599
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006600 if (*op == '.')
6601 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006602 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006603 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006604 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006605 if (
6606#ifdef FEAT_FLOAT
6607 // If variable is float operation with number is OK.
6608 !(expected == &t_float && stacktype == &t_number) &&
6609#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006610 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006611 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006612 goto theend;
6613
6614 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006615 {
6616 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6617 goto theend;
6618 }
6619 else if (*op == '+')
6620 {
6621 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 operator_type(lhs.lhs_member_type, stacktype),
6623 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006624 goto theend;
6625 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006626 else if (generate_two_op(cctx, op) == FAIL)
6627 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006629
Bram Moolenaar752fc692021-01-04 21:57:11 +01006630 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006631 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 // Use the info in "lhs" to store the value at the index in the
6633 // list or dict.
6634 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6635 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006636 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006637 }
6638 else
6639 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006641 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006642 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006643 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006644 generate_LOCKCONST(cctx);
6645
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006646 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006647 && (lhs.lhs_type->tt_type == VAR_DICT
6648 || lhs.lhs_type->tt_type == VAR_LIST)
6649 && lhs.lhs_type->tt_member != NULL
6650 && lhs.lhs_type->tt_member != &t_any
6651 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006652 // Set the type in the list or dict, so that it can be checked,
6653 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006655
Bram Moolenaar752fc692021-01-04 21:57:11 +01006656 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006657 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006658 if (generate_store_var(cctx, lhs.lhs_dest,
6659 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6660 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6661 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006662 goto theend;
6663 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006664 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006665 {
6666 isn_T *isn = ((isn_T *)instr->ga_data)
6667 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006668
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006669 // optimization: turn "var = 123" from ISN_PUSHNR +
6670 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006671 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006672 && instr->ga_len == instr_count + 1
6673 && isn->isn_type == ISN_PUSHNR)
6674 {
6675 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006676
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006677 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006678 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006679 isn->isn_arg.storenr.stnr_val = val;
6680 if (stack->ga_len > 0)
6681 --stack->ga_len;
6682 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006683 else if (lhs.lhs_lvar->lv_from_outer > 0)
6684 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6685 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006686 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006687 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006688 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006689 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006690
6691 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006692 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006694
6695 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006696 if (var_count > 0 && !semicolon)
6697 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006698 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006699 goto theend;
6700 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006701
Bram Moolenaarb2097502020-07-19 17:17:02 +02006702 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006703
6704theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006705 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706 return ret;
6707}
6708
6709/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006710 * Check for an assignment at "eap->cmd", compile it if found.
6711 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6712 */
6713 static int
6714may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6715{
6716 char_u *pskip;
6717 char_u *p;
6718
6719 // Assuming the command starts with a variable or function name,
6720 // find what follows.
6721 // Skip over "var.member", "var[idx]" and the like.
6722 // Also "&opt = val", "$ENV = val" and "@r = val".
6723 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6724 ? eap->cmd + 1 : eap->cmd;
6725 p = to_name_end(pskip, TRUE);
6726 if (p > eap->cmd && *p != NUL)
6727 {
6728 char_u *var_end;
6729 int oplen;
6730 int heredoc;
6731
6732 if (eap->cmd[0] == '@')
6733 var_end = eap->cmd + 2;
6734 else
6735 var_end = find_name_end(pskip, NULL, NULL,
6736 FNE_CHECK_START | FNE_INCL_BR);
6737 oplen = assignment_len(skipwhite(var_end), &heredoc);
6738 if (oplen > 0)
6739 {
6740 size_t len = p - eap->cmd;
6741
6742 // Recognize an assignment if we recognize the variable
6743 // name:
6744 // "g:var = expr"
6745 // "local = expr" where "local" is a local var.
6746 // "script = expr" where "script" is a script-local var.
6747 // "import = expr" where "import" is an imported var
6748 // "&opt = expr"
6749 // "$ENV = expr"
6750 // "@r = expr"
6751 if (*eap->cmd == '&'
6752 || *eap->cmd == '$'
6753 || *eap->cmd == '@'
6754 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006755 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006756 {
6757 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6758 if (*line == NULL || *line == eap->cmd)
6759 return FAIL;
6760 return OK;
6761 }
6762 }
6763 }
6764
6765 if (*eap->cmd == '[')
6766 {
6767 // [var, var] = expr
6768 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6769 if (*line == NULL)
6770 return FAIL;
6771 if (*line != eap->cmd)
6772 return OK;
6773 }
6774 return NOTDONE;
6775}
6776
6777/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006778 * Check if "name" can be "unlet".
6779 */
6780 int
6781check_vim9_unlet(char_u *name)
6782{
6783 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6784 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006785 // "unlet s:var" is allowed in legacy script.
6786 if (*name == 's' && !script_is_vim9())
6787 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006788 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006789 return FAIL;
6790 }
6791 return OK;
6792}
6793
6794/*
6795 * Callback passed to ex_unletlock().
6796 */
6797 static int
6798compile_unlet(
6799 lval_T *lvp,
6800 char_u *name_end,
6801 exarg_T *eap,
6802 int deep UNUSED,
6803 void *coookie)
6804{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006805 cctx_T *cctx = coookie;
6806 char_u *p = lvp->ll_name;
6807 int cc = *name_end;
6808 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006809
Bram Moolenaar752fc692021-01-04 21:57:11 +01006810 if (cctx->ctx_skip == SKIP_YES)
6811 return OK;
6812
6813 *name_end = NUL;
6814 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006815 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006816 // :unlet $ENV_VAR
6817 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6818 }
6819 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6820 {
6821 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006822
Bram Moolenaar752fc692021-01-04 21:57:11 +01006823 // This is similar to assigning: lookup the list/dict, compile the
6824 // idx/key. Then instead of storing the value unlet the item.
6825 // unlet {list}[idx]
6826 // unlet {dict}[key] dict.key
6827 //
6828 // Figure out the LHS type and other properties.
6829 //
6830 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6831
6832 // : unlet an indexed item
6833 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006834 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006835 iemsg("called compile_lhs() without an index");
6836 ret = FAIL;
6837 }
6838 else
6839 {
6840 // Use the info in "lhs" to unlet the item at the index in the
6841 // list or dict.
6842 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006843 }
6844
Bram Moolenaar752fc692021-01-04 21:57:11 +01006845 vim_free(lhs.lhs_name);
6846 }
6847 else if (check_vim9_unlet(p) == FAIL)
6848 {
6849 ret = FAIL;
6850 }
6851 else
6852 {
6853 // Normal name. Only supports g:, w:, t: and b: namespaces.
6854 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006855 }
6856
Bram Moolenaar752fc692021-01-04 21:57:11 +01006857 *name_end = cc;
6858 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006859}
6860
6861/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006862 * Callback passed to ex_unletlock().
6863 */
6864 static int
6865compile_lock_unlock(
6866 lval_T *lvp,
6867 char_u *name_end,
6868 exarg_T *eap,
6869 int deep UNUSED,
6870 void *coookie)
6871{
6872 cctx_T *cctx = coookie;
6873 int cc = *name_end;
6874 char_u *p = lvp->ll_name;
6875 int ret = OK;
6876 size_t len;
6877 char_u *buf;
6878
6879 if (cctx->ctx_skip == SKIP_YES)
6880 return OK;
6881
6882 // Cannot use :lockvar and :unlockvar on local variables.
6883 if (p[1] != ':')
6884 {
6885 char_u *end = skip_var_one(p, FALSE);
6886
6887 if (lookup_local(p, end - p, NULL, cctx) == OK)
6888 {
6889 emsg(_(e_cannot_lock_unlock_local_variable));
6890 return FAIL;
6891 }
6892 }
6893
6894 // Checking is done at runtime.
6895 *name_end = NUL;
6896 len = name_end - p + 20;
6897 buf = alloc(len);
6898 if (buf == NULL)
6899 ret = FAIL;
6900 else
6901 {
6902 vim_snprintf((char *)buf, len, "%s %s",
6903 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6904 p);
6905 ret = generate_EXEC(cctx, buf);
6906
6907 vim_free(buf);
6908 *name_end = cc;
6909 }
6910 return ret;
6911}
6912
6913/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006914 * compile "unlet var", "lock var" and "unlock var"
6915 * "arg" points to "var".
6916 */
6917 static char_u *
6918compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6919{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006920 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6921 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6922 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006923 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6924}
6925
6926/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 * Compile an :import command.
6928 */
6929 static char_u *
6930compile_import(char_u *arg, cctx_T *cctx)
6931{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006932 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933}
6934
6935/*
6936 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6937 */
6938 static int
6939compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6940{
6941 garray_T *instr = &cctx->ctx_instr;
6942 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6943
6944 if (endlabel == NULL)
6945 return FAIL;
6946 endlabel->el_next = *el;
6947 *el = endlabel;
6948 endlabel->el_end_label = instr->ga_len;
6949
6950 generate_JUMP(cctx, when, 0);
6951 return OK;
6952}
6953
6954 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006955compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956{
6957 garray_T *instr = &cctx->ctx_instr;
6958
6959 while (*el != NULL)
6960 {
6961 endlabel_T *cur = (*el);
6962 isn_T *isn;
6963
6964 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006965 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966 *el = cur->el_next;
6967 vim_free(cur);
6968 }
6969}
6970
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006971 static void
6972compile_free_jump_to_end(endlabel_T **el)
6973{
6974 while (*el != NULL)
6975 {
6976 endlabel_T *cur = (*el);
6977
6978 *el = cur->el_next;
6979 vim_free(cur);
6980 }
6981}
6982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006983/*
6984 * Create a new scope and set up the generic items.
6985 */
6986 static scope_T *
6987new_scope(cctx_T *cctx, scopetype_T type)
6988{
6989 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6990
6991 if (scope == NULL)
6992 return NULL;
6993 scope->se_outer = cctx->ctx_scope;
6994 cctx->ctx_scope = scope;
6995 scope->se_type = type;
6996 scope->se_local_count = cctx->ctx_locals.ga_len;
6997 return scope;
6998}
6999
7000/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007001 * Free the current scope and go back to the outer scope.
7002 */
7003 static void
7004drop_scope(cctx_T *cctx)
7005{
7006 scope_T *scope = cctx->ctx_scope;
7007
7008 if (scope == NULL)
7009 {
7010 iemsg("calling drop_scope() without a scope");
7011 return;
7012 }
7013 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007014 switch (scope->se_type)
7015 {
7016 case IF_SCOPE:
7017 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7018 case FOR_SCOPE:
7019 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7020 case WHILE_SCOPE:
7021 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7022 case TRY_SCOPE:
7023 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7024 case NO_SCOPE:
7025 case BLOCK_SCOPE:
7026 break;
7027 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007028 vim_free(scope);
7029}
7030
7031/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 * compile "if expr"
7033 *
7034 * "if expr" Produces instructions:
7035 * EVAL expr Push result of "expr"
7036 * JUMP_IF_FALSE end
7037 * ... body ...
7038 * end:
7039 *
7040 * "if expr | else" Produces instructions:
7041 * EVAL expr Push result of "expr"
7042 * JUMP_IF_FALSE else
7043 * ... body ...
7044 * JUMP_ALWAYS end
7045 * else:
7046 * ... body ...
7047 * end:
7048 *
7049 * "if expr1 | elseif expr2 | else" Produces instructions:
7050 * EVAL expr Push result of "expr"
7051 * JUMP_IF_FALSE elseif
7052 * ... body ...
7053 * JUMP_ALWAYS end
7054 * elseif:
7055 * EVAL expr Push result of "expr"
7056 * JUMP_IF_FALSE else
7057 * ... body ...
7058 * JUMP_ALWAYS end
7059 * else:
7060 * ... body ...
7061 * end:
7062 */
7063 static char_u *
7064compile_if(char_u *arg, cctx_T *cctx)
7065{
7066 char_u *p = arg;
7067 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007068 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007070 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007071 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072
Bram Moolenaara5565e42020-05-09 15:44:01 +02007073 CLEAR_FIELD(ppconst);
7074 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007075 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007076 clear_ppconst(&ppconst);
7077 return NULL;
7078 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007079 if (!ends_excmd2(arg, skipwhite(p)))
7080 {
7081 semsg(_(e_trailing_arg), p);
7082 return NULL;
7083 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007084 if (cctx->ctx_skip == SKIP_YES)
7085 clear_ppconst(&ppconst);
7086 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007087 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007088 int error = FALSE;
7089 int v;
7090
Bram Moolenaara5565e42020-05-09 15:44:01 +02007091 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007092 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007093 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007094 if (error)
7095 return NULL;
7096 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007097 }
7098 else
7099 {
7100 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007101 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007102 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007103 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007104 if (bool_on_stack(cctx) == FAIL)
7105 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
Bram Moolenaara91a7132021-03-25 21:12:15 +01007108 // CMDMOD_REV must come before the jump
7109 generate_undo_cmdmods(cctx);
7110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007111 scope = new_scope(cctx, IF_SCOPE);
7112 if (scope == NULL)
7113 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007114 scope->se_skip_save = skip_save;
7115 // "is_had_return" will be reset if any block does not end in :return
7116 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007117
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007118 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007119 {
7120 // "where" is set when ":elseif", "else" or ":endif" is found
7121 scope->se_u.se_if.is_if_label = instr->ga_len;
7122 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7123 }
7124 else
7125 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126
Bram Moolenaarced68a02021-01-24 17:53:47 +01007127#ifdef FEAT_PROFILE
7128 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7129 && skip_save != SKIP_YES)
7130 {
7131 // generated a profile start, need to generate a profile end, since it
7132 // won't be done after returning
7133 cctx->ctx_skip = SKIP_NOT;
7134 generate_instr(cctx, ISN_PROF_END);
7135 cctx->ctx_skip = SKIP_YES;
7136 }
7137#endif
7138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007139 return p;
7140}
7141
7142 static char_u *
7143compile_elseif(char_u *arg, cctx_T *cctx)
7144{
7145 char_u *p = arg;
7146 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007147 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 isn_T *isn;
7149 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007150 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007151 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152
7153 if (scope == NULL || scope->se_type != IF_SCOPE)
7154 {
7155 emsg(_(e_elseif_without_if));
7156 return NULL;
7157 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007158 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007159 if (!cctx->ctx_had_return)
7160 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161
Bram Moolenaarced68a02021-01-24 17:53:47 +01007162 if (cctx->ctx_skip == SKIP_NOT)
7163 {
7164 // previous block was executed, this one and following will not
7165 cctx->ctx_skip = SKIP_YES;
7166 scope->se_u.se_if.is_seen_skip_not = TRUE;
7167 }
7168 if (scope->se_u.se_if.is_seen_skip_not)
7169 {
7170 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007171 // Do not count the "elseif" for profiling and cmdmod
7172 instr->ga_len = current_instr_idx(cctx);
7173
Bram Moolenaarced68a02021-01-24 17:53:47 +01007174 skip_expr_cctx(&p, cctx);
7175 return p;
7176 }
7177
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007178 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007179 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007180 int moved_cmdmod = FALSE;
7181
7182 // Move any CMDMOD instruction to after the jump
7183 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7184 {
7185 if (ga_grow(instr, 1) == FAIL)
7186 return NULL;
7187 ((isn_T *)instr->ga_data)[instr->ga_len] =
7188 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7189 --instr->ga_len;
7190 moved_cmdmod = TRUE;
7191 }
7192
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007193 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007195 return NULL;
7196 // previous "if" or "elseif" jumps here
7197 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7198 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007199 if (moved_cmdmod)
7200 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007203 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007204 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007205 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007206 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007207 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007208#ifdef FEAT_PROFILE
7209 if (cctx->ctx_profiling)
7210 {
7211 // the previous block was skipped, need to profile this line
7212 generate_instr(cctx, ISN_PROF_START);
7213 instr_count = instr->ga_len;
7214 }
7215#endif
7216 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007217 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007218 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007219 clear_ppconst(&ppconst);
7220 return NULL;
7221 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007222 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007223 if (!ends_excmd2(arg, skipwhite(p)))
7224 {
7225 semsg(_(e_trailing_arg), p);
7226 return NULL;
7227 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007228 if (scope->se_skip_save == SKIP_YES)
7229 clear_ppconst(&ppconst);
7230 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007231 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007232 int error = FALSE;
7233 int v;
7234
Bram Moolenaar7f141552020-05-09 17:35:53 +02007235 // The expression results in a constant.
7236 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007237 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7238 if (error)
7239 return NULL;
7240 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007241 clear_ppconst(&ppconst);
7242 scope->se_u.se_if.is_if_label = -1;
7243 }
7244 else
7245 {
7246 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007247 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007248 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007249 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007250 if (bool_on_stack(cctx) == FAIL)
7251 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252
Bram Moolenaara91a7132021-03-25 21:12:15 +01007253 // CMDMOD_REV must come before the jump
7254 generate_undo_cmdmods(cctx);
7255
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007256 // "where" is set when ":elseif", "else" or ":endif" is found
7257 scope->se_u.se_if.is_if_label = instr->ga_len;
7258 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260
7261 return p;
7262}
7263
7264 static char_u *
7265compile_else(char_u *arg, cctx_T *cctx)
7266{
7267 char_u *p = arg;
7268 garray_T *instr = &cctx->ctx_instr;
7269 isn_T *isn;
7270 scope_T *scope = cctx->ctx_scope;
7271
7272 if (scope == NULL || scope->se_type != IF_SCOPE)
7273 {
7274 emsg(_(e_else_without_if));
7275 return NULL;
7276 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007277 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007278 if (!cctx->ctx_had_return)
7279 scope->se_u.se_if.is_had_return = FALSE;
7280 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281
Bram Moolenaarced68a02021-01-24 17:53:47 +01007282#ifdef FEAT_PROFILE
7283 if (cctx->ctx_profiling)
7284 {
7285 if (cctx->ctx_skip == SKIP_NOT
7286 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7287 .isn_type == ISN_PROF_START)
7288 // the previous block was executed, do not count "else" for profiling
7289 --instr->ga_len;
7290 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7291 {
7292 // the previous block was not executed, this one will, do count the
7293 // "else" for profiling
7294 cctx->ctx_skip = SKIP_NOT;
7295 generate_instr(cctx, ISN_PROF_END);
7296 generate_instr(cctx, ISN_PROF_START);
7297 cctx->ctx_skip = SKIP_YES;
7298 }
7299 }
7300#endif
7301
7302 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007303 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007304 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007305 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007306 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007307 if (!cctx->ctx_had_return
7308 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7309 JUMP_ALWAYS, cctx) == FAIL)
7310 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007311 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007312
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007313 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007314 {
7315 if (scope->se_u.se_if.is_if_label >= 0)
7316 {
7317 // previous "if" or "elseif" jumps here
7318 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7319 isn->isn_arg.jump.jump_where = instr->ga_len;
7320 scope->se_u.se_if.is_if_label = -1;
7321 }
7322 }
7323
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007324 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007325 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007327
7328 return p;
7329}
7330
7331 static char_u *
7332compile_endif(char_u *arg, cctx_T *cctx)
7333{
7334 scope_T *scope = cctx->ctx_scope;
7335 ifscope_T *ifscope;
7336 garray_T *instr = &cctx->ctx_instr;
7337 isn_T *isn;
7338
Bram Moolenaarfa984412021-03-25 22:15:28 +01007339 if (misplaced_cmdmod(cctx))
7340 return NULL;
7341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 if (scope == NULL || scope->se_type != IF_SCOPE)
7343 {
7344 emsg(_(e_endif_without_if));
7345 return NULL;
7346 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007347 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007348 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007349 if (!cctx->ctx_had_return)
7350 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007352 if (scope->se_u.se_if.is_if_label >= 0)
7353 {
7354 // previous "if" or "elseif" jumps here
7355 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7356 isn->isn_arg.jump.jump_where = instr->ga_len;
7357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007359 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007360
7361#ifdef FEAT_PROFILE
7362 // even when skipping we count the endif as executed, unless the block it's
7363 // in is skipped
7364 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7365 && scope->se_skip_save != SKIP_YES)
7366 {
7367 cctx->ctx_skip = SKIP_NOT;
7368 generate_instr(cctx, ISN_PROF_START);
7369 }
7370#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007371 cctx->ctx_skip = scope->se_skip_save;
7372
7373 // If all the blocks end in :return and there is an :else then the
7374 // had_return flag is set.
7375 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007377 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 return arg;
7379}
7380
7381/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007382 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383 *
7384 * Produces instructions:
7385 * PUSHNR -1
7386 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007387 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7389 * - if beyond end, jump to "end"
7390 * - otherwise get item from list and push it
7391 * STORE var Store item in "var"
7392 * ... body ...
7393 * JUMP top Jump back to repeat
7394 * end: DROP Drop the result of "expr"
7395 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007396 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7397 * UNPACK 2 Split item in 2
7398 * STORE var1 Store item in "var1"
7399 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 */
7401 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007402compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007404 char_u *arg;
7405 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007406 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007407 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007408 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007409 int var_count = 0;
7410 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007411 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 garray_T *stack = &cctx->ctx_type_stack;
7413 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007414 lvar_T *loop_lvar; // loop iteration variable
7415 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007417 type_T *item_type = &t_any;
7418 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007419
Bram Moolenaar792f7862020-11-23 08:31:18 +01007420 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007421 if (p == NULL)
7422 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007423 if (var_count == 0)
7424 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425
7426 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007427 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007428 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7429 return NULL;
7430 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 {
7432 emsg(_(e_missing_in));
7433 return NULL;
7434 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007435 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007436 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7437 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 scope = new_scope(cctx, FOR_SCOPE);
7440 if (scope == NULL)
7441 return NULL;
7442
Bram Moolenaar792f7862020-11-23 08:31:18 +01007443 // Reserve a variable to store the loop iteration counter and initialize it
7444 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007445 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7446 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007447 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007448 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007449 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007451 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007452 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453
7454 // compile "expr", it remains on the stack until "endfor"
7455 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007456 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007457 {
7458 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007460 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007461 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007463 // If we know the type of "var" and it is a not a list or string we can
7464 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007466 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7467 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007469 // TODO: support Blob
7470 semsg(_(e_for_loop_on_str_not_supported),
7471 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007472 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 return NULL;
7474 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007475
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007476 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007477 {
7478 if (var_count == 1)
7479 item_type = vartype->tt_member;
7480 else if (vartype->tt_member->tt_type == VAR_LIST
7481 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7482 item_type = vartype->tt_member->tt_member;
7483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484
Bram Moolenaara91a7132021-03-25 21:12:15 +01007485 // CMDMOD_REV must come before the FOR instruction
7486 generate_undo_cmdmods(cctx);
7487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007489 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007490 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491
Bram Moolenaar792f7862020-11-23 08:31:18 +01007492 arg = arg_start;
7493 if (var_count > 1)
7494 {
7495 generate_UNPACK(cctx, var_count, semicolon);
7496 arg = skipwhite(arg + 1); // skip white after '['
7497
7498 // the list item is replaced by a number of items
7499 if (ga_grow(stack, var_count - 1) == FAIL)
7500 {
7501 drop_scope(cctx);
7502 return NULL;
7503 }
7504 --stack->ga_len;
7505 for (idx = 0; idx < var_count; ++idx)
7506 {
7507 ((type_T **)stack->ga_data)[stack->ga_len] =
7508 (semicolon && idx == 0) ? vartype : item_type;
7509 ++stack->ga_len;
7510 }
7511 }
7512
7513 for (idx = 0; idx < var_count; ++idx)
7514 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007515 assign_dest_T dest = dest_local;
7516 int opt_flags = 0;
7517 int vimvaridx = -1;
7518 type_T *type = &t_any;
7519
7520 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007521 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007522 name = vim_strnsave(arg, varlen);
7523 if (name == NULL)
7524 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007525
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007526 // TODO: script var not supported?
7527 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7528 &vimvaridx, &type, cctx) == FAIL)
7529 goto failed;
7530 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007531 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007532 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7533 0, 0, type, name) == FAIL)
7534 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007535 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007536 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007537 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007538 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007539 {
7540 semsg(_(e_variable_already_declared), arg);
7541 goto failed;
7542 }
7543
Bram Moolenaarea870692020-12-02 14:24:30 +01007544 if (STRNCMP(name, "s:", 2) == 0)
7545 {
7546 semsg(_(e_cannot_declare_script_variable_in_function), name);
7547 goto failed;
7548 }
7549
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007550 // Reserve a variable to store "var".
7551 // TODO: check for type
7552 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7553 if (var_lvar == NULL)
7554 // out of memory or used as an argument
7555 goto failed;
7556
7557 if (semicolon && idx == var_count - 1)
7558 var_lvar->lv_type = vartype;
7559 else
7560 var_lvar->lv_type = item_type;
7561 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7562 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007563
Bram Moolenaar036d0712021-01-17 20:23:38 +01007564 if (*p == ':')
7565 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007566 if (*p == ',' || *p == ';')
7567 ++p;
7568 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007569 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007570 }
7571
7572 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007573
7574failed:
7575 vim_free(name);
7576 drop_scope(cctx);
7577 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007578}
7579
7580/*
7581 * compile "endfor"
7582 */
7583 static char_u *
7584compile_endfor(char_u *arg, cctx_T *cctx)
7585{
7586 garray_T *instr = &cctx->ctx_instr;
7587 scope_T *scope = cctx->ctx_scope;
7588 forscope_T *forscope;
7589 isn_T *isn;
7590
Bram Moolenaarfa984412021-03-25 22:15:28 +01007591 if (misplaced_cmdmod(cctx))
7592 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007594 if (scope == NULL || scope->se_type != FOR_SCOPE)
7595 {
7596 emsg(_(e_for));
7597 return NULL;
7598 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007599 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007601 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
7603 // At end of ":for" scope jump back to the FOR instruction.
7604 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7605
7606 // Fill in the "end" label in the FOR statement so it can jump here
7607 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7608 isn->isn_arg.forloop.for_end = instr->ga_len;
7609
7610 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007611 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007612
7613 // Below the ":for" scope drop the "expr" list from the stack.
7614 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7615 return NULL;
7616
7617 vim_free(scope);
7618
7619 return arg;
7620}
7621
7622/*
7623 * compile "while expr"
7624 *
7625 * Produces instructions:
7626 * top: EVAL expr Push result of "expr"
7627 * JUMP_IF_FALSE end jump if false
7628 * ... body ...
7629 * JUMP top Jump back to repeat
7630 * end:
7631 *
7632 */
7633 static char_u *
7634compile_while(char_u *arg, cctx_T *cctx)
7635{
7636 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637 scope_T *scope;
7638
7639 scope = new_scope(cctx, WHILE_SCOPE);
7640 if (scope == NULL)
7641 return NULL;
7642
Bram Moolenaara91a7132021-03-25 21:12:15 +01007643 // "endwhile" jumps back here, one before when profiling or using cmdmods
7644 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007645
7646 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007647 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007649 if (!ends_excmd2(arg, skipwhite(p)))
7650 {
7651 semsg(_(e_trailing_arg), p);
7652 return NULL;
7653 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654
Bram Moolenaar13106602020-10-04 16:06:05 +02007655 if (bool_on_stack(cctx) == FAIL)
7656 return FAIL;
7657
Bram Moolenaara91a7132021-03-25 21:12:15 +01007658 // CMDMOD_REV must come before the jump
7659 generate_undo_cmdmods(cctx);
7660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007662 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 JUMP_IF_FALSE, cctx) == FAIL)
7664 return FAIL;
7665
7666 return p;
7667}
7668
7669/*
7670 * compile "endwhile"
7671 */
7672 static char_u *
7673compile_endwhile(char_u *arg, cctx_T *cctx)
7674{
7675 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007676 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677
Bram Moolenaarfa984412021-03-25 22:15:28 +01007678 if (misplaced_cmdmod(cctx))
7679 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007680 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7681 {
7682 emsg(_(e_while));
7683 return NULL;
7684 }
7685 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007686 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007687
Bram Moolenaarf002a412021-01-24 13:34:18 +01007688#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007689 // count the endwhile before jumping
7690 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007691#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007694 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007695
7696 // Fill in the "end" label in the WHILE statement so it can jump here.
7697 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007698 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7699 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007700
7701 vim_free(scope);
7702
7703 return arg;
7704}
7705
7706/*
7707 * compile "continue"
7708 */
7709 static char_u *
7710compile_continue(char_u *arg, cctx_T *cctx)
7711{
7712 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007713 int try_scopes = 0;
7714 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007715
7716 for (;;)
7717 {
7718 if (scope == NULL)
7719 {
7720 emsg(_(e_continue));
7721 return NULL;
7722 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007723 if (scope->se_type == FOR_SCOPE)
7724 {
7725 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007726 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007727 }
7728 if (scope->se_type == WHILE_SCOPE)
7729 {
7730 loop_label = scope->se_u.se_while.ws_top_label;
7731 break;
7732 }
7733 if (scope->se_type == TRY_SCOPE)
7734 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 scope = scope->se_outer;
7736 }
7737
Bram Moolenaarc150c092021-02-13 15:02:46 +01007738 if (try_scopes > 0)
7739 // Inside one or more try/catch blocks we first need to jump to the
7740 // "finally" or "endtry" to cleanup.
7741 generate_TRYCONT(cctx, try_scopes, loop_label);
7742 else
7743 // Jump back to the FOR or WHILE instruction.
7744 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007746 return arg;
7747}
7748
7749/*
7750 * compile "break"
7751 */
7752 static char_u *
7753compile_break(char_u *arg, cctx_T *cctx)
7754{
7755 scope_T *scope = cctx->ctx_scope;
7756 endlabel_T **el;
7757
7758 for (;;)
7759 {
7760 if (scope == NULL)
7761 {
7762 emsg(_(e_break));
7763 return NULL;
7764 }
7765 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7766 break;
7767 scope = scope->se_outer;
7768 }
7769
7770 // Jump to the end of the FOR or WHILE loop.
7771 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007772 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007774 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007775 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7776 return FAIL;
7777
7778 return arg;
7779}
7780
7781/*
7782 * compile "{" start of block
7783 */
7784 static char_u *
7785compile_block(char_u *arg, cctx_T *cctx)
7786{
7787 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7788 return NULL;
7789 return skipwhite(arg + 1);
7790}
7791
7792/*
7793 * compile end of block: drop one scope
7794 */
7795 static void
7796compile_endblock(cctx_T *cctx)
7797{
7798 scope_T *scope = cctx->ctx_scope;
7799
7800 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007801 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007802 vim_free(scope);
7803}
7804
7805/*
7806 * compile "try"
7807 * Creates a new scope for the try-endtry, pointing to the first catch and
7808 * finally.
7809 * Creates another scope for the "try" block itself.
7810 * TRY instruction sets up exception handling at runtime.
7811 *
7812 * "try"
7813 * TRY -> catch1, -> finally push trystack entry
7814 * ... try block
7815 * "throw {exception}"
7816 * EVAL {exception}
7817 * THROW create exception
7818 * ... try block
7819 * " catch {expr}"
7820 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007821 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007822 * EVAL {expr}
7823 * MATCH
7824 * JUMP nomatch -> catch2
7825 * CATCH remove exception
7826 * ... catch block
7827 * " catch"
7828 * JUMP -> finally
7829 * catch2: CATCH remove exception
7830 * ... catch block
7831 * " finally"
7832 * finally:
7833 * ... finally block
7834 * " endtry"
7835 * ENDTRY pop trystack entry, may rethrow
7836 */
7837 static char_u *
7838compile_try(char_u *arg, cctx_T *cctx)
7839{
7840 garray_T *instr = &cctx->ctx_instr;
7841 scope_T *try_scope;
7842 scope_T *scope;
7843
Bram Moolenaarfa984412021-03-25 22:15:28 +01007844 if (misplaced_cmdmod(cctx))
7845 return NULL;
7846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007847 // scope that holds the jumps that go to catch/finally/endtry
7848 try_scope = new_scope(cctx, TRY_SCOPE);
7849 if (try_scope == NULL)
7850 return NULL;
7851
Bram Moolenaar69f70502021-01-01 16:10:46 +01007852 if (cctx->ctx_skip != SKIP_YES)
7853 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007854 isn_T *isn;
7855
7856 // "try_catch" is set when the first ":catch" is found or when no catch
7857 // is found and ":finally" is found.
7858 // "try_finally" is set when ":finally" is found
7859 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007860 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007861 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7862 return NULL;
7863 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7864 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007865 return NULL;
7866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867
7868 // scope for the try block itself
7869 scope = new_scope(cctx, BLOCK_SCOPE);
7870 if (scope == NULL)
7871 return NULL;
7872
7873 return arg;
7874}
7875
7876/*
7877 * compile "catch {expr}"
7878 */
7879 static char_u *
7880compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7881{
7882 scope_T *scope = cctx->ctx_scope;
7883 garray_T *instr = &cctx->ctx_instr;
7884 char_u *p;
7885 isn_T *isn;
7886
Bram Moolenaarfa984412021-03-25 22:15:28 +01007887 if (misplaced_cmdmod(cctx))
7888 return NULL;
7889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 // end block scope from :try or :catch
7891 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7892 compile_endblock(cctx);
7893 scope = cctx->ctx_scope;
7894
7895 // Error if not in a :try scope
7896 if (scope == NULL || scope->se_type != TRY_SCOPE)
7897 {
7898 emsg(_(e_catch));
7899 return NULL;
7900 }
7901
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007902 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007903 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007904 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 return NULL;
7906 }
7907
Bram Moolenaar69f70502021-01-01 16:10:46 +01007908 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007909 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007910#ifdef FEAT_PROFILE
7911 // the profile-start should be after the jump
7912 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7913 .isn_type == ISN_PROF_START)
7914 --instr->ga_len;
7915#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007916 // Jump from end of previous block to :finally or :endtry
7917 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7918 JUMP_ALWAYS, cctx) == FAIL)
7919 return NULL;
7920
7921 // End :try or :catch scope: set value in ISN_TRY instruction
7922 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007923 if (isn->isn_arg.try.try_ref->try_catch == 0)
7924 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007925 if (scope->se_u.se_try.ts_catch_label != 0)
7926 {
7927 // Previous catch without match jumps here
7928 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7929 isn->isn_arg.jump.jump_where = instr->ga_len;
7930 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007931#ifdef FEAT_PROFILE
7932 if (cctx->ctx_profiling)
7933 {
7934 // a "throw" that jumps here needs to be counted
7935 generate_instr(cctx, ISN_PROF_END);
7936 // the "catch" is also counted
7937 generate_instr(cctx, ISN_PROF_START);
7938 }
7939#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 }
7941
7942 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007943 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007945 scope->se_u.se_try.ts_caught_all = TRUE;
7946 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007947 }
7948 else
7949 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007950 char_u *end;
7951 char_u *pat;
7952 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007953 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007954 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956 // Push v:exception, push {expr} and MATCH
7957 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7958
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007959 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007960 if (*end != *p)
7961 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007962 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007963 vim_free(tofree);
7964 return FAIL;
7965 }
7966 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007967 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007968 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007969 len = (int)(end - tofree);
7970 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007971 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007972 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007973 if (pat == NULL)
7974 return FAIL;
7975 if (generate_PUSHS(cctx, pat) == FAIL)
7976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7979 return NULL;
7980
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007981 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7983 return NULL;
7984 }
7985
Bram Moolenaar69f70502021-01-01 16:10:46 +01007986 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007987 return NULL;
7988
7989 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7990 return NULL;
7991 return p;
7992}
7993
7994 static char_u *
7995compile_finally(char_u *arg, cctx_T *cctx)
7996{
7997 scope_T *scope = cctx->ctx_scope;
7998 garray_T *instr = &cctx->ctx_instr;
7999 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008000 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001
Bram Moolenaarfa984412021-03-25 22:15:28 +01008002 if (misplaced_cmdmod(cctx))
8003 return NULL;
8004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008005 // end block scope from :try or :catch
8006 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8007 compile_endblock(cctx);
8008 scope = cctx->ctx_scope;
8009
8010 // Error if not in a :try scope
8011 if (scope == NULL || scope->se_type != TRY_SCOPE)
8012 {
8013 emsg(_(e_finally));
8014 return NULL;
8015 }
8016
8017 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008018 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008019 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008020 {
8021 emsg(_(e_finally_dup));
8022 return NULL;
8023 }
8024
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008025 this_instr = instr->ga_len;
8026#ifdef FEAT_PROFILE
8027 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8028 .isn_type == ISN_PROF_START)
8029 // jump to the profile start of the "finally"
8030 --this_instr;
8031#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008033 // Fill in the "end" label in jumps at the end of the blocks.
8034 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8035 this_instr, cctx);
8036
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008037 // If there is no :catch then an exception jumps to :finally.
8038 if (isn->isn_arg.try.try_ref->try_catch == 0)
8039 isn->isn_arg.try.try_ref->try_catch = this_instr;
8040 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008041 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008042 {
8043 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008044 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008045 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008046 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008047 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008048 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8049 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051 // TODO: set index in ts_finally_label jumps
8052
8053 return arg;
8054}
8055
8056 static char_u *
8057compile_endtry(char_u *arg, cctx_T *cctx)
8058{
8059 scope_T *scope = cctx->ctx_scope;
8060 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008061 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008062
Bram Moolenaarfa984412021-03-25 22:15:28 +01008063 if (misplaced_cmdmod(cctx))
8064 return NULL;
8065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066 // end block scope from :catch or :finally
8067 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8068 compile_endblock(cctx);
8069 scope = cctx->ctx_scope;
8070
8071 // Error if not in a :try scope
8072 if (scope == NULL || scope->se_type != TRY_SCOPE)
8073 {
8074 if (scope == NULL)
8075 emsg(_(e_no_endtry));
8076 else if (scope->se_type == WHILE_SCOPE)
8077 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008078 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 emsg(_(e_endfor));
8080 else
8081 emsg(_(e_endif));
8082 return NULL;
8083 }
8084
Bram Moolenaarc150c092021-02-13 15:02:46 +01008085 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008086 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008088 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8089 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008090 {
8091 emsg(_(e_missing_catch_or_finally));
8092 return NULL;
8093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008095#ifdef FEAT_PROFILE
8096 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8097 .isn_type == ISN_PROF_START)
8098 // move the profile start after "endtry" so that it's not counted when
8099 // the exception is rethrown.
8100 --instr->ga_len;
8101#endif
8102
Bram Moolenaar69f70502021-01-01 16:10:46 +01008103 // Fill in the "end" label in jumps at the end of the blocks, if not
8104 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008105 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8106 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107
Bram Moolenaar69f70502021-01-01 16:10:46 +01008108 if (scope->se_u.se_try.ts_catch_label != 0)
8109 {
8110 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008111 isn_T *isn = ((isn_T *)instr->ga_data)
8112 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008113 isn->isn_arg.jump.jump_where = instr->ga_len;
8114 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008115 }
8116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 compile_endblock(cctx);
8118
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008119 if (cctx->ctx_skip != SKIP_YES)
8120 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008121 // End :catch or :finally scope: set instruction index in ISN_TRY
8122 // instruction
8123 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008124 if (cctx->ctx_skip != SKIP_YES
8125 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8126 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008127#ifdef FEAT_PROFILE
8128 if (cctx->ctx_profiling)
8129 generate_instr(cctx, ISN_PROF_START);
8130#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008132 return arg;
8133}
8134
8135/*
8136 * compile "throw {expr}"
8137 */
8138 static char_u *
8139compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8140{
8141 char_u *p = skipwhite(arg);
8142
Bram Moolenaara5565e42020-05-09 15:44:01 +02008143 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008144 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008145 if (cctx->ctx_skip == SKIP_YES)
8146 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147 if (may_generate_2STRING(-1, cctx) == FAIL)
8148 return NULL;
8149 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8150 return NULL;
8151
8152 return p;
8153}
8154
8155/*
8156 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008157 * compile "echomsg expr"
8158 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008159 * compile "execute expr"
8160 */
8161 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008162compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008163{
8164 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008165 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008166 int count = 0;
8167
8168 for (;;)
8169 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008170 if (ends_excmd2(prev, p))
8171 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008172 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008173 return NULL;
8174 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008175 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008176 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008177 }
8178
Bram Moolenaare4984292020-12-13 14:19:25 +01008179 if (count > 0)
8180 {
8181 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8182 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8183 else if (cmdidx == CMD_execute)
8184 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8185 else if (cmdidx == CMD_echomsg)
8186 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8187 else
8188 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8189 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008190 return p;
8191}
8192
8193/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008194 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008195 * instruction to compute it and return OK.
8196 * Otherwise return FAIL, the caller must deal with any range.
8197 */
8198 static int
8199compile_variable_range(exarg_T *eap, cctx_T *cctx)
8200{
8201 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8202 char_u *p = skipdigits(eap->cmd);
8203
8204 if (p == range_end)
8205 return FAIL;
8206 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8207}
8208
8209/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008210 * :put r
8211 * :put ={expr}
8212 */
8213 static char_u *
8214compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8215{
8216 char_u *line = arg;
8217 linenr_T lnum;
8218 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008219 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008220
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008221 eap->regname = *line;
8222
8223 if (eap->regname == '=')
8224 {
8225 char_u *p = line + 1;
8226
8227 if (compile_expr0(&p, cctx) == FAIL)
8228 return NULL;
8229 line = p;
8230 }
8231 else if (eap->regname != NUL)
8232 ++line;
8233
Bram Moolenaar08597872020-12-10 19:43:40 +01008234 if (compile_variable_range(eap, cctx) == OK)
8235 {
8236 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8237 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008238 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008239 {
8240 // Either no range or a number.
8241 // "errormsg" will not be set because the range is ADDR_LINES.
8242 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008243 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008244 return NULL;
8245 if (eap->addr_count == 0)
8246 lnum = -1;
8247 else
8248 lnum = eap->line2;
8249 if (above)
8250 --lnum;
8251 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008252
8253 generate_PUT(cctx, eap->regname, lnum);
8254 return line;
8255}
8256
8257/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008258 * A command that is not compiled, execute with legacy code.
8259 */
8260 static char_u *
8261compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8262{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008263 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008264 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008265 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008266
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008267 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008268 goto theend;
8269
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008270 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008271 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008272 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008273 int usefilter = FALSE;
8274
8275 has_expr = argt & (EX_XFILE | EX_EXPAND);
8276
8277 // If the command can be followed by a bar, find the bar and truncate
8278 // it, so that the following command can be compiled.
8279 // The '|' is overwritten with a NUL, it is put back below.
8280 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8281 && *eap->arg == '!')
8282 // :w !filter or :r !filter or :r! filter
8283 usefilter = TRUE;
8284 if ((argt & EX_TRLBAR) && !usefilter)
8285 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008286 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008287 separate_nextcmd(eap);
8288 if (eap->nextcmd != NULL)
8289 nextcmd = eap->nextcmd;
8290 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008291 else if (eap->cmdidx == CMD_wincmd)
8292 {
8293 p = eap->arg;
8294 if (*p != NUL)
8295 ++p;
8296 if (*p == 'g' || *p == Ctrl_G)
8297 ++p;
8298 p = skipwhite(p);
8299 if (*p == '|')
8300 {
8301 *p = NUL;
8302 nextcmd = p + 1;
8303 }
8304 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008305 }
8306
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008307 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8308 {
8309 // expand filename in "syntax include [@group] filename"
8310 has_expr = TRUE;
8311 eap->arg = skipwhite(eap->arg + 7);
8312 if (*eap->arg == '@')
8313 eap->arg = skiptowhite(eap->arg);
8314 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008315
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008316 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8317 && STRLEN(eap->arg) > 4)
8318 {
8319 int delim = *eap->arg;
8320
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008321 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008322 if (*p == delim)
8323 {
8324 eap->arg = p + 1;
8325 has_expr = TRUE;
8326 }
8327 }
8328
Bram Moolenaarecac5912021-01-05 19:23:28 +01008329 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8330 {
8331 // TODO: should only expand when appropriate for the command
8332 eap->arg = skiptowhite(eap->arg);
8333 has_expr = TRUE;
8334 }
8335
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008336 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008337 {
8338 int count = 0;
8339 char_u *start = skipwhite(line);
8340
8341 // :cmd xxx`=expr1`yyy`=expr2`zzz
8342 // PUSHS ":cmd xxx"
8343 // eval expr1
8344 // PUSHS "yyy"
8345 // eval expr2
8346 // PUSHS "zzz"
8347 // EXECCONCAT 5
8348 for (;;)
8349 {
8350 if (p > start)
8351 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008352 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008353 ++count;
8354 }
8355 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008356 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008357 return NULL;
8358 may_generate_2STRING(-1, cctx);
8359 ++count;
8360 p = skipwhite(p);
8361 if (*p != '`')
8362 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008363 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008364 return NULL;
8365 }
8366 start = p + 1;
8367
8368 p = (char_u *)strstr((char *)start, "`=");
8369 if (p == NULL)
8370 {
8371 if (*skipwhite(start) != NUL)
8372 {
8373 generate_PUSHS(cctx, vim_strsave(start));
8374 ++count;
8375 }
8376 break;
8377 }
8378 }
8379 generate_EXECCONCAT(cctx, count);
8380 }
8381 else
8382 generate_EXEC(cctx, line);
8383
8384theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008385 if (*nextcmd != NUL)
8386 {
8387 // the parser expects a pointer to the bar, put it back
8388 --nextcmd;
8389 *nextcmd = '|';
8390 }
8391
8392 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008393}
8394
8395/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008396 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008397 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008398 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008399 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008400add_def_function(ufunc_T *ufunc)
8401{
8402 dfunc_T *dfunc;
8403
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008404 if (def_functions.ga_len == 0)
8405 {
8406 // The first position is not used, so that a zero uf_dfunc_idx means it
8407 // wasn't set.
8408 if (ga_grow(&def_functions, 1) == FAIL)
8409 return FAIL;
8410 ++def_functions.ga_len;
8411 }
8412
Bram Moolenaar09689a02020-05-09 22:50:08 +02008413 // Add the function to "def_functions".
8414 if (ga_grow(&def_functions, 1) == FAIL)
8415 return FAIL;
8416 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8417 CLEAR_POINTER(dfunc);
8418 dfunc->df_idx = def_functions.ga_len;
8419 ufunc->uf_dfunc_idx = dfunc->df_idx;
8420 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008421 dfunc->df_name = vim_strsave(ufunc->uf_name);
8422 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008423 ++def_functions.ga_len;
8424 return OK;
8425}
8426
8427/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 * After ex_function() has collected all the function lines: parse and compile
8429 * the lines into instructions.
8430 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008431 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8432 * the return statement (used for lambda). When uf_ret_type is already set
8433 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008434 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008435 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008436 * This can be used recursively through compile_lambda(), which may reallocate
8437 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008438 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008440 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008441compile_def_function(
8442 ufunc_T *ufunc,
8443 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008444 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008445 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 char_u *line = NULL;
8448 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 cctx_T cctx;
8451 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008452 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008453 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008454 int ret = FAIL;
8455 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008456 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008457 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008458 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008459#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008460 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008461#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008463 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008464 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008465 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008466 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008467 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8468 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008469 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008470 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008471 else
8472 {
8473 if (add_def_function(ufunc) == FAIL)
8474 return FAIL;
8475 new_def_function = TRUE;
8476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477
Bram Moolenaar985116a2020-07-12 17:31:09 +02008478 ufunc->uf_def_status = UF_COMPILING;
8479
Bram Moolenaara80faa82020-04-12 19:37:17 +02008480 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008481
Bram Moolenaarf002a412021-01-24 13:34:18 +01008482#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008483 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008484#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008485 cctx.ctx_ufunc = ufunc;
8486 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008487 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008488 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8489 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8490 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8491 cctx.ctx_type_list = &ufunc->uf_type_list;
8492 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8493 instr = &cctx.ctx_instr;
8494
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008495 // Set the context to the function, it may be compiled when called from
8496 // another script. Set the script version to the most modern one.
8497 // The line number will be set in next_line_from_context().
8498 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8500
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008501 // Make sure error messages are OK.
8502 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8503 if (do_estack_push)
8504 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008505 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008506
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008507 if (ufunc->uf_def_args.ga_len > 0)
8508 {
8509 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008510 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008511 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008512 int i;
8513 char_u *arg;
8514 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008515 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008516
8517 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008518 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008519 for (i = 0; i < count; ++i)
8520 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008521 garray_T *stack = &cctx.ctx_type_stack;
8522 type_T *val_type;
8523 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008524 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008525 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008526 int jump_instr_idx = instr->ga_len;
8527 isn_T *isn;
8528
8529 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8530 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8531 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008532
8533 // Make sure later arguments are not found.
8534 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008535
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008536 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008537 r = compile_expr0(&arg, &cctx);
8538
8539 ufunc->uf_args.ga_len = uf_args_len;
8540 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008541 goto erret;
8542
8543 // If no type specified use the type of the default value.
8544 // Otherwise check that the default value type matches the
8545 // specified type.
8546 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008547 where.wt_index = arg_idx + 1;
8548 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008549 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008550 {
8551 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008552 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008553 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008554 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008555 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008556 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008557
8558 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008559 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008560
8561 // set instruction index in JUMP_IF_ARG_SET to here
8562 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8563 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008564 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008565
8566 if (did_set_arg_type)
8567 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008568 }
8569
8570 /*
8571 * Loop over all the lines of the function and generate instructions.
8572 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 for (;;)
8574 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008575 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008576 int starts_with_colon = FALSE;
8577 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008578 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008579
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008580 // Bail out on the first error to avoid a flood of errors and report
8581 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008582 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008583 goto erret;
8584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008585 if (line != NULL && *line == '|')
8586 // the line continues after a '|'
8587 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008588 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008589 && !(*line == '#' && (line == cctx.ctx_line_start
8590 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008592 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 goto erret;
8594 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008595 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8596 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 else
8598 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008599 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008600 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008601 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008602 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008603#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008604 if (cctx.ctx_skip != SKIP_YES)
8605 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008606#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008608 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 }
8610
Bram Moolenaara80faa82020-04-12 19:37:17 +02008611 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 ea.cmdlinep = &line;
8613 ea.cmd = skipwhite(line);
8614
Bram Moolenaarb2049902021-01-24 12:53:53 +01008615 if (*ea.cmd == '#')
8616 {
8617 // "#" starts a comment
8618 line = (char_u *)"";
8619 continue;
8620 }
8621
Bram Moolenaarf002a412021-01-24 13:34:18 +01008622#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008623 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8624 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008625 {
8626 may_generate_prof_end(&cctx, prof_lnum);
8627
8628 prof_lnum = cctx.ctx_lnum;
8629 generate_instr(&cctx, ISN_PROF_START);
8630 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008631#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008632
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008633 // Some things can be recognized by the first character.
8634 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008636 case '}':
8637 {
8638 // "}" ends a block scope
8639 scopetype_T stype = cctx.ctx_scope == NULL
8640 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008642 if (stype == BLOCK_SCOPE)
8643 {
8644 compile_endblock(&cctx);
8645 line = ea.cmd;
8646 }
8647 else
8648 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008649 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008650 goto erret;
8651 }
8652 if (line != NULL)
8653 line = skipwhite(ea.cmd + 1);
8654 continue;
8655 }
8656
8657 case '{':
8658 // "{" starts a block scope
8659 // "{'a': 1}->func() is something else
8660 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8661 {
8662 line = compile_block(ea.cmd, &cctx);
8663 continue;
8664 }
8665 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008666 }
8667
8668 /*
8669 * COMMAND MODIFIERS
8670 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008671 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008672 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8673 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674 {
8675 if (errormsg != NULL)
8676 goto erret;
8677 // empty line or comment
8678 line = (char_u *)"";
8679 continue;
8680 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008681 generate_cmdmods(&cctx, &local_cmdmod);
8682 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008683
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008684 // Check if there was a colon after the last command modifier or before
8685 // the current position.
8686 for (p = ea.cmd; p >= line; --p)
8687 {
8688 if (*p == ':')
8689 starts_with_colon = TRUE;
8690 if (p < ea.cmd && !VIM_ISWHITE(*p))
8691 break;
8692 }
8693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008695 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008697 {
8698 if (*ea.cmd == '(')
8699 // not for "call()"
8700 ea.cmd = p;
8701 else
8702 ea.cmd = skipwhite(ea.cmd);
8703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008704
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008705 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008706 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008707 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008708
Bram Moolenaar17126b12021-01-07 22:03:02 +01008709 // Check for assignment after command modifiers.
8710 assign = may_compile_assignment(&ea, &line, &cctx);
8711 if (assign == OK)
8712 goto nextline;
8713 if (assign == FAIL)
8714 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 }
8716
8717 /*
8718 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008719 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008720 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008721 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008722 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008723 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008724 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008725 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008726 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008727 if (!starts_with_colon)
8728 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008729 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008730 goto erret;
8731 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008732 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008733 if (ends_excmd2(line, ea.cmd))
8734 {
8735 // A range without a command: jump to the line.
8736 // TODO: compile to a more efficient command, possibly
8737 // calling parse_cmd_address().
8738 ea.cmdidx = CMD_SIZE;
8739 line = compile_exec(line, &ea, &cctx);
8740 goto nextline;
8741 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008742 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008743 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008744 p = find_ex_command(&ea, NULL, starts_with_colon
8745 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008746
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008747 if (p == NULL)
8748 {
8749 if (cctx.ctx_skip != SKIP_YES)
8750 emsg(_(e_ambiguous_use_of_user_defined_command));
8751 goto erret;
8752 }
8753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8755 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008756 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008757 {
8758 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008759 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008760 }
8761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008762 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008763 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008764 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008765 // CMD_var cannot happen, compile_assignment() above would be
8766 // used. Most likely an assignment to a non-existing variable.
8767 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008768 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008770 }
8771
Bram Moolenaar3988f642020-08-27 22:43:03 +02008772 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008773 && ea.cmdidx != CMD_elseif
8774 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008775 && ea.cmdidx != CMD_endif
8776 && ea.cmdidx != CMD_endfor
8777 && ea.cmdidx != CMD_endwhile
8778 && ea.cmdidx != CMD_catch
8779 && ea.cmdidx != CMD_finally
8780 && ea.cmdidx != CMD_endtry)
8781 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008782 emsg(_(e_unreachable_code_after_return));
8783 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008784 }
8785
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008786 p = skipwhite(p);
8787 if (ea.cmdidx != CMD_SIZE
8788 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8789 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008790 if (ea.cmdidx >= 0)
8791 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008792 if ((ea.argt & EX_BANG) && *p == '!')
8793 {
8794 ea.forceit = TRUE;
8795 p = skipwhite(p + 1);
8796 }
8797 }
8798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799 switch (ea.cmdidx)
8800 {
8801 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008802 ea.arg = p;
8803 line = compile_nested_function(&ea, &cctx);
8804 break;
8805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008807 // TODO: should we allow this, e.g. to declare a global
8808 // function?
8809 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008810 goto erret;
8811
8812 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008813 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008814 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008815 break;
8816
8817 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008818 emsg(_(e_cannot_use_let_in_vim9_script));
8819 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008820 case CMD_var:
8821 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008822 case CMD_const:
8823 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008824 if (line == p)
8825 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008826 break;
8827
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008828 case CMD_unlet:
8829 case CMD_unlockvar:
8830 case CMD_lockvar:
8831 line = compile_unletlock(p, &ea, &cctx);
8832 break;
8833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 case CMD_import:
8835 line = compile_import(p, &cctx);
8836 break;
8837
8838 case CMD_if:
8839 line = compile_if(p, &cctx);
8840 break;
8841 case CMD_elseif:
8842 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008843 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008844 break;
8845 case CMD_else:
8846 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008847 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848 break;
8849 case CMD_endif:
8850 line = compile_endif(p, &cctx);
8851 break;
8852
8853 case CMD_while:
8854 line = compile_while(p, &cctx);
8855 break;
8856 case CMD_endwhile:
8857 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008858 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008859 break;
8860
8861 case CMD_for:
8862 line = compile_for(p, &cctx);
8863 break;
8864 case CMD_endfor:
8865 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008866 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008867 break;
8868 case CMD_continue:
8869 line = compile_continue(p, &cctx);
8870 break;
8871 case CMD_break:
8872 line = compile_break(p, &cctx);
8873 break;
8874
8875 case CMD_try:
8876 line = compile_try(p, &cctx);
8877 break;
8878 case CMD_catch:
8879 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008880 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008881 break;
8882 case CMD_finally:
8883 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008884 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008885 break;
8886 case CMD_endtry:
8887 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008888 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889 break;
8890 case CMD_throw:
8891 line = compile_throw(p, &cctx);
8892 break;
8893
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008894 case CMD_eval:
8895 if (compile_expr0(&p, &cctx) == FAIL)
8896 goto erret;
8897
Bram Moolenaar3988f642020-08-27 22:43:03 +02008898 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008899 generate_instr_drop(&cctx, ISN_DROP, 1);
8900
8901 line = skipwhite(p);
8902 break;
8903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008906 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008907 case CMD_echomsg:
8908 case CMD_echoerr:
8909 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008910 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008912 case CMD_put:
8913 ea.cmd = cmd;
8914 line = compile_put(p, &ea, &cctx);
8915 break;
8916
Bram Moolenaar3988f642020-08-27 22:43:03 +02008917 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008918
Bram Moolenaarae616492020-07-28 20:07:27 +02008919 case CMD_append:
8920 case CMD_change:
8921 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008922 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008923 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008924 case CMD_xit:
8925 not_in_vim9(&ea);
8926 goto erret;
8927
Bram Moolenaar002262f2020-07-08 17:47:57 +02008928 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008929 if (cctx.ctx_skip != SKIP_YES)
8930 {
8931 semsg(_(e_invalid_command_str), ea.cmd);
8932 goto erret;
8933 }
8934 // We don't check for a next command here.
8935 line = (char_u *)"";
8936 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008939 if (cctx.ctx_skip == SKIP_YES)
8940 {
8941 // We don't check for a next command here.
8942 line = (char_u *)"";
8943 }
8944 else
8945 {
8946 // Not recognized, execute with do_cmdline_cmd().
8947 ea.arg = p;
8948 line = compile_exec(line, &ea, &cctx);
8949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008950 break;
8951 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008952nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008953 if (line == NULL)
8954 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008955 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008957 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008958 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008960 if (cctx.ctx_type_stack.ga_len < 0)
8961 {
8962 iemsg("Type stack underflow");
8963 goto erret;
8964 }
8965 }
8966
8967 if (cctx.ctx_scope != NULL)
8968 {
8969 if (cctx.ctx_scope->se_type == IF_SCOPE)
8970 emsg(_(e_endif));
8971 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8972 emsg(_(e_endwhile));
8973 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8974 emsg(_(e_endfor));
8975 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008976 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008977 goto erret;
8978 }
8979
Bram Moolenaarefd88552020-06-18 20:50:10 +02008980 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008981 {
8982 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008984 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985 goto erret;
8986 }
8987
8988 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008989 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008990 }
8991
Bram Moolenaar599410c2021-04-10 14:03:43 +02008992 // When compiled with ":silent!" and there was an error don't consider the
8993 // function compiled.
8994 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008995 {
8996 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8997 + ufunc->uf_dfunc_idx;
8998 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008999 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01009000#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01009001 if (cctx.ctx_profiling)
9002 {
9003 dfunc->df_instr_prof = instr->ga_data;
9004 dfunc->df_instr_prof_count = instr->ga_len;
9005 }
9006 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01009007#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01009008 {
9009 dfunc->df_instr = instr->ga_data;
9010 dfunc->df_instr_count = instr->ga_len;
9011 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009012 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009013 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009014 if (cctx.ctx_outer_used)
9015 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009016 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018
9019 ret = OK;
9020
9021erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009022 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009023 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009024 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009025 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9026 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009027
9028 for (idx = 0; idx < instr->ga_len; ++idx)
9029 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009030 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009031 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009032
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009033 // If using the last entry in the table and it was added above, we
9034 // might as well remove it.
9035 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009036 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009037 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009038 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009039 ufunc->uf_dfunc_idx = 0;
9040 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009041 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009042
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009043 while (cctx.ctx_scope != NULL)
9044 drop_scope(&cctx);
9045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009046 if (errormsg != NULL)
9047 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009048 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009049 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009050 }
9051
9052 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009053 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009054 if (do_estack_push)
9055 estack_pop();
9056
Bram Moolenaar20431c92020-03-20 18:39:46 +01009057 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009058 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009059 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009060 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009061}
9062
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009063 void
9064set_function_type(ufunc_T *ufunc)
9065{
9066 int varargs = ufunc->uf_va_name != NULL;
9067 int argcount = ufunc->uf_args.ga_len;
9068
9069 // Create a type for the function, with the return type and any
9070 // argument types.
9071 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9072 // The type is included in "tt_args".
9073 if (argcount > 0 || varargs)
9074 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009075 if (ufunc->uf_type_list.ga_itemsize == 0)
9076 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009077 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9078 argcount, &ufunc->uf_type_list);
9079 // Add argument types to the function type.
9080 if (func_type_add_arg_types(ufunc->uf_func_type,
9081 argcount + varargs,
9082 &ufunc->uf_type_list) == FAIL)
9083 return;
9084 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9085 ufunc->uf_func_type->tt_min_argcount =
9086 argcount - ufunc->uf_def_args.ga_len;
9087 if (ufunc->uf_arg_types == NULL)
9088 {
9089 int i;
9090
9091 // lambda does not have argument types.
9092 for (i = 0; i < argcount; ++i)
9093 ufunc->uf_func_type->tt_args[i] = &t_any;
9094 }
9095 else
9096 mch_memmove(ufunc->uf_func_type->tt_args,
9097 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9098 if (varargs)
9099 {
9100 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009101 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009102 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9103 }
9104 }
9105 else
9106 // No arguments, can use a predefined type.
9107 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9108 argcount, &ufunc->uf_type_list);
9109}
9110
9111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009112/*
9113 * Delete an instruction, free what it contains.
9114 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009115 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009116delete_instr(isn_T *isn)
9117{
9118 switch (isn->isn_type)
9119 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009120 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009121 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009122 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009123 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009124 case ISN_LOADENV:
9125 case ISN_LOADG:
9126 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009127 case ISN_LOADT:
9128 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009129 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009130 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009131 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009132 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009133 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009134 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009135 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009136 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009137 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009138 case ISN_STOREW:
9139 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009140 vim_free(isn->isn_arg.string);
9141 break;
9142
9143 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009144 case ISN_STORES:
9145 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009146 break;
9147
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009148 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009149 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009150 vim_free(isn->isn_arg.unlet.ul_name);
9151 break;
9152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009153 case ISN_STOREOPT:
9154 vim_free(isn->isn_arg.storeopt.so_name);
9155 break;
9156
9157 case ISN_PUSHBLOB: // push blob isn_arg.blob
9158 blob_unref(isn->isn_arg.blob);
9159 break;
9160
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009161 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009162#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009163 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009164#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009165 break;
9166
9167 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009169 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009170#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009171 break;
9172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009173 case ISN_UCALL:
9174 vim_free(isn->isn_arg.ufunc.cuf_name);
9175 break;
9176
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009177 case ISN_FUNCREF:
9178 {
9179 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9180 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009181 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009182
Bram Moolenaar077a4232020-12-22 18:33:27 +01009183 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9184 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009185 }
9186 break;
9187
9188 case ISN_DCALL:
9189 {
9190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9191 + isn->isn_arg.dfunc.cdf_idx;
9192
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009193 if (dfunc->df_ufunc != NULL
9194 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009195 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009196 }
9197 break;
9198
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009199 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009200 {
9201 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9202 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9203
9204 if (ufunc != NULL)
9205 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009206 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009207 func_ptr_unref(ufunc);
9208 }
9209
9210 vim_free(lambda);
9211 vim_free(isn->isn_arg.newfunc.nf_global);
9212 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009213 break;
9214
Bram Moolenaar5e654232020-09-16 15:22:00 +02009215 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009216 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009217 free_type(isn->isn_arg.type.ct_type);
9218 break;
9219
Bram Moolenaar02194d22020-10-24 23:08:38 +02009220 case ISN_CMDMOD:
9221 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9222 ->cmod_filter_regmatch.regprog);
9223 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9224 break;
9225
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009226 case ISN_LOADSCRIPT:
9227 case ISN_STORESCRIPT:
9228 vim_free(isn->isn_arg.script.scriptref);
9229 break;
9230
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009231 case ISN_TRY:
9232 vim_free(isn->isn_arg.try.try_ref);
9233 break;
9234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009235 case ISN_2BOOL:
9236 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009237 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 case ISN_ADDBLOB:
9239 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009240 case ISN_ANYINDEX:
9241 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009243 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009244 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009245 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009247 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009248 case ISN_COMPAREANY:
9249 case ISN_COMPAREBLOB:
9250 case ISN_COMPAREBOOL:
9251 case ISN_COMPAREDICT:
9252 case ISN_COMPAREFLOAT:
9253 case ISN_COMPAREFUNC:
9254 case ISN_COMPARELIST:
9255 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009256 case ISN_COMPARESPECIAL:
9257 case ISN_COMPARESTRING:
9258 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009259 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009260 case ISN_DROP:
9261 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009262 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009263 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009264 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009265 case ISN_EXECCONCAT:
9266 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009267 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009268 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009269 case ISN_GETITEM:
9270 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009271 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009272 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009273 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009274 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009276 case ISN_LOADBDICT:
9277 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009278 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009279 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009280 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009282 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009283 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009284 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009285 case ISN_NEGATENR:
9286 case ISN_NEWDICT:
9287 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009289 case ISN_OPFLOAT:
9290 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009291 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009292 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009293 case ISN_PROF_END:
9294 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009295 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009296 case ISN_PUSHF:
9297 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009298 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009299 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009300 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009301 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009302 case ISN_SHUFFLE:
9303 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009304 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009305 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009306 case ISN_STORENR:
9307 case ISN_STOREOUTER:
9308 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009309 case ISN_STOREV:
9310 case ISN_STRINDEX:
9311 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009312 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009313 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009314 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009315 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009316 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009317 // nothing allocated
9318 break;
9319 }
9320}
9321
9322/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009323 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009324 */
9325 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009326delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009327{
9328 int idx;
9329
9330 ga_clear(&dfunc->df_def_args_isn);
9331
9332 if (dfunc->df_instr != NULL)
9333 {
9334 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9335 delete_instr(dfunc->df_instr + idx);
9336 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009337 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009338 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009339#ifdef FEAT_PROFILE
9340 if (dfunc->df_instr_prof != NULL)
9341 {
9342 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9343 delete_instr(dfunc->df_instr_prof + idx);
9344 VIM_CLEAR(dfunc->df_instr_prof);
9345 dfunc->df_instr_prof = NULL;
9346 }
9347#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009348
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009349 if (mark_deleted)
9350 dfunc->df_deleted = TRUE;
9351 if (dfunc->df_ufunc != NULL)
9352 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009353}
9354
9355/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009356 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009357 * function, unless another user function still uses it.
9358 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009359 */
9360 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009361unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009362{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009363 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 {
9365 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9366 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009367
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009368 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009369 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009370 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009371 ufunc->uf_dfunc_idx = 0;
9372 if (dfunc->df_ufunc == ufunc)
9373 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009374 }
9375}
9376
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009377/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009378 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009379 */
9380 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009381link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009382{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009383 if (ufunc->uf_dfunc_idx > 0)
9384 {
9385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9386 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009387
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009388 ++dfunc->df_refcount;
9389 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009390}
9391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009392#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009393/*
9394 * Free all functions defined with ":def".
9395 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009396 void
9397free_def_functions(void)
9398{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009399 int idx;
9400
9401 for (idx = 0; idx < def_functions.ga_len; ++idx)
9402 {
9403 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9404
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009405 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009406 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009407 }
9408
9409 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009410}
9411#endif
9412
9413
9414#endif // FEAT_EVAL