blob: c4b5d3c1c0f88a85c7218a0dedf6507ede354534 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200425 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100426 {
427 if (is_arg)
428 semsg(_(e_argument_already_declared_in_script_str), p);
429 else
430 semsg(_(e_variable_already_declared_in_script_str), p);
431 return FAIL;
432 }
433
Bram Moolenaarad486a02020-08-01 23:22:18 +0200434 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100435 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100436 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200437 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200439 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200441 // A local or script-local function can shadow a global function.
442 if (ufunc == NULL || !func_is_global(ufunc)
443 || (p[0] == 'g' && p[1] == ':'))
444 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 if (is_arg)
446 semsg(_(e_argument_name_shadows_existing_variable_str), p);
447 else
448 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200449 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200450 return FAIL;
451 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100452 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200453 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100454 return OK;
455}
456
Bram Moolenaar65b95452020-07-19 14:03:09 +0200457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458/////////////////////////////////////////////////////////////////////
459// Following generate_ functions expect the caller to call ga_grow().
460
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200461#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
462#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464/*
465 * Generate an instruction without arguments.
466 * Returns a pointer to the new instruction, NULL if failed.
467 */
468 static isn_T *
469generate_instr(cctx_T *cctx, isntype_T isn_type)
470{
471 garray_T *instr = &cctx->ctx_instr;
472 isn_T *isn;
473
Bram Moolenaar080457c2020-03-03 21:53:32 +0100474 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 if (ga_grow(instr, 1) == FAIL)
476 return NULL;
477 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
478 isn->isn_type = isn_type;
479 isn->isn_lnum = cctx->ctx_lnum + 1;
480 ++instr->ga_len;
481
482 return isn;
483}
484
485/*
486 * Generate an instruction without arguments.
487 * "drop" will be removed from the stack.
488 * Returns a pointer to the new instruction, NULL if failed.
489 */
490 static isn_T *
491generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
492{
493 garray_T *stack = &cctx->ctx_type_stack;
494
Bram Moolenaar080457c2020-03-03 21:53:32 +0100495 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 stack->ga_len -= drop;
497 return generate_instr(cctx, isn_type);
498}
499
500/*
501 * Generate instruction "isn_type" and put "type" on the type stack.
502 */
503 static isn_T *
504generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
505{
506 isn_T *isn;
507 garray_T *stack = &cctx->ctx_type_stack;
508
509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
510 return NULL;
511
512 if (ga_grow(stack, 1) == FAIL)
513 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200514 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 ++stack->ga_len;
516
517 return isn;
518}
519
520/*
521 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200522 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 */
524 static int
525may_generate_2STRING(int offset, cctx_T *cctx)
526{
527 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200528 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100530 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100532 RETURN_OK_IF_SKIP(cctx);
533 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200534 switch ((*type)->tt_type)
535 {
536 // nothing to be done
537 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200539 // conversion possible
540 case VAR_SPECIAL:
541 case VAR_BOOL:
542 case VAR_NUMBER:
543 case VAR_FLOAT:
544 break;
545
546 // conversion possible (with runtime check)
547 case VAR_ANY:
548 case VAR_UNKNOWN:
549 isntype = ISN_2STRING_ANY;
550 break;
551
552 // conversion not possible
553 case VAR_VOID:
554 case VAR_BLOB:
555 case VAR_FUNC:
556 case VAR_PARTIAL:
557 case VAR_LIST:
558 case VAR_DICT:
559 case VAR_JOB:
560 case VAR_CHANNEL:
561 to_string_error((*type)->tt_type);
562 return FAIL;
563 }
564
565 *type = &t_string;
566 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 return FAIL;
568 isn->isn_arg.number = offset;
569
570 return OK;
571}
572
573 static int
574check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
575{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200578 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 {
580 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200581 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200583 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200589 static int
590generate_add_instr(
591 cctx_T *cctx,
592 vartype_T vartype,
593 type_T *type1,
594 type_T *type2)
595{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100596 garray_T *stack = &cctx->ctx_type_stack;
597 isn_T *isn = generate_instr_drop(cctx,
598 vartype == VAR_NUMBER ? ISN_OPNR
599 : vartype == VAR_LIST ? ISN_ADDLIST
600 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200601#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100602 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200603#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100604 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605
606 if (vartype != VAR_LIST && vartype != VAR_BLOB
607 && type1->tt_type != VAR_ANY
608 && type2->tt_type != VAR_ANY
609 && check_number_or_float(
610 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
611 return FAIL;
612
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100615
616 // When concatenating two lists with different member types the member type
617 // becomes "any".
618 if (vartype == VAR_LIST
619 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
620 && type1->tt_member != type2->tt_member)
621 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
622
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200623 return isn == NULL ? FAIL : OK;
624}
625
626/*
627 * Get the type to use for an instruction for an operation on "type1" and
628 * "type2". If they are matching use a type-specific instruction. Otherwise
629 * fall back to runtime type checking.
630 */
631 static vartype_T
632operator_type(type_T *type1, type_T *type2)
633{
634 if (type1->tt_type == type2->tt_type
635 && (type1->tt_type == VAR_NUMBER
636 || type1->tt_type == VAR_LIST
637#ifdef FEAT_FLOAT
638 || type1->tt_type == VAR_FLOAT
639#endif
640 || type1->tt_type == VAR_BLOB))
641 return type1->tt_type;
642 return VAR_ANY;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Generate an instruction with two arguments. The instruction depends on the
647 * type of the arguments.
648 */
649 static int
650generate_two_op(cctx_T *cctx, char_u *op)
651{
652 garray_T *stack = &cctx->ctx_type_stack;
653 type_T *type1;
654 type_T *type2;
655 vartype_T vartype;
656 isn_T *isn;
657
Bram Moolenaar080457c2020-03-03 21:53:32 +0100658 RETURN_OK_IF_SKIP(cctx);
659
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
662 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 switch (*op)
666 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 case '+':
668 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 break;
671
672 case '-':
673 case '*':
674 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
675 op) == FAIL)
676 return FAIL;
677 if (vartype == VAR_NUMBER)
678 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
679#ifdef FEAT_FLOAT
680 else if (vartype == VAR_FLOAT)
681 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
682#endif
683 else
684 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
685 if (isn != NULL)
686 isn->isn_arg.op.op_type = *op == '*'
687 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
688 break;
689
Bram Moolenaar4c683752020-04-05 21:38:23 +0200690 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200692 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 && type2->tt_type != VAR_NUMBER))
694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200695 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 return FAIL;
697 }
698 isn = generate_instr_drop(cctx,
699 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_REM;
702 break;
703 }
704
705 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200706 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 type_T *type = &t_any;
709
710#ifdef FEAT_FLOAT
711 // float+number and number+float results in float
712 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
713 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
714 type = &t_float;
715#endif
716 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
717 }
718
719 return OK;
720}
721
722/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 * Get the instruction to use for comparing "type1" with "type2"
724 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200726 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100727get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728{
729 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730
Bram Moolenaar4c683752020-04-05 21:38:23 +0200731 if (type1 == VAR_UNKNOWN)
732 type1 = VAR_ANY;
733 if (type2 == VAR_UNKNOWN)
734 type2 = VAR_ANY;
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if (type1 == type2)
737 {
738 switch (type1)
739 {
740 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
741 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
742 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
743 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
744 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
745 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
746 case VAR_LIST: isntype = ISN_COMPARELIST; break;
747 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
748 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 default: isntype = ISN_COMPAREANY; break;
750 }
751 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200752 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100754 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 isntype = ISN_COMPAREANY;
756
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 && (isntype == ISN_COMPAREBOOL
759 || isntype == ISN_COMPARESPECIAL
760 || isntype == ISN_COMPARENR
761 || isntype == ISN_COMPAREFLOAT))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200765 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 }
767 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
770 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
772 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BLOB || type2 == VAR_BLOB
774 || type1 == VAR_LIST || type2 == VAR_LIST))))
775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200776 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200778 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 return isntype;
781}
782
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200783 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200785{
786 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
787 return FAIL;
788 return OK;
789}
790
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791/*
792 * Generate an ISN_COMPARE* instruction with a boolean result.
793 */
794 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200796{
797 isntype_T isntype;
798 isn_T *isn;
799 garray_T *stack = &cctx->ctx_type_stack;
800 vartype_T type1;
801 vartype_T type2;
802
803 RETURN_OK_IF_SKIP(cctx);
804
805 // Get the known type of the two items on the stack. If they are matching
806 // use a type-specific instruction. Otherwise fall back to runtime type
807 // checking.
808 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
809 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100810 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 if (isntype == ISN_DROP)
812 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 if ((isn = generate_instr(cctx, isntype)) == NULL)
815 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100816 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 isn->isn_arg.op.op_ic = ic;
818
819 // takes two arguments, puts one bool back
820 if (stack->ga_len >= 2)
821 {
822 --stack->ga_len;
823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
824 }
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_2BOOL instruction.
831 */
832 static int
833generate_2BOOL(cctx_T *cctx, int invert)
834{
835 isn_T *isn;
836 garray_T *stack = &cctx->ctx_type_stack;
837
Bram Moolenaar080457c2020-03-03 21:53:32 +0100838 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
840 return FAIL;
841 isn->isn_arg.number = invert;
842
843 // type becomes bool
844 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
845
846 return OK;
847}
848
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200849/*
850 * Generate an ISN_COND2BOOL instruction.
851 */
852 static int
853generate_COND2BOOL(cctx_T *cctx)
854{
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857
858 RETURN_OK_IF_SKIP(cctx);
859 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
860 return FAIL;
861
862 // type becomes bool
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864
865 return OK;
866}
867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200869generate_TYPECHECK(
870 cctx_T *cctx,
871 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100872 int offset,
873 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
880 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200881 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100882 isn->isn_arg.type.ct_off = (int8_T)offset;
883 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 // type becomes expected
886 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 return OK;
889}
890
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100891 static int
892generate_SETTYPE(
893 cctx_T *cctx,
894 type_T *expected)
895{
896 isn_T *isn;
897
898 RETURN_OK_IF_SKIP(cctx);
899 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
900 return FAIL;
901 isn->isn_arg.type.ct_type = alloc_type(expected);
902 return OK;
903}
904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200906 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
907 * used. Return FALSE if the types will never match.
908 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100909 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910use_typecheck(type_T *actual, type_T *expected)
911{
912 if (actual->tt_type == VAR_ANY
913 || actual->tt_type == VAR_UNKNOWN
914 || (actual->tt_type == VAR_FUNC
915 && (expected->tt_type == VAR_FUNC
916 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100917 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
918 && ((actual->tt_member == &t_void)
919 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200920 return TRUE;
921 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
922 && actual->tt_type == expected->tt_type)
923 // This takes care of a nested list or dict.
924 return use_typecheck(actual->tt_member, expected->tt_member);
925 return FALSE;
926}
927
928/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200929 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200930 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 * - "actual" is a type that can be "expected" type: add a runtime check; or
932 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200933 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
934 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100936 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200937need_type(
938 type_T *actual,
939 type_T *expected,
940 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100941 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200942 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200943 int silent,
944 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200945{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100946 where_T where;
947
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200948 if (expected == &t_bool && actual != &t_bool
949 && (actual->tt_flags & TTFLAG_BOOL_OK))
950 {
951 // Using "0", "1" or the result of an expression with "&&" or "||" as a
952 // boolean is OK but requires a conversion.
953 generate_2BOOL(cctx, FALSE);
954 return OK;
955 }
956
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100957 where.wt_index = arg_idx;
958 where.wt_variable = FALSE;
959 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200960 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200961
962 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 // If it's a constant a runtime check makes no sense.
964 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100966 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200967 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200968 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200969
970 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100971 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200972 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200973}
974
975/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100976 * Check that the top of the type stack has a type that can be used as a
977 * condition. Give an error and return FAIL if not.
978 */
979 static int
980bool_on_stack(cctx_T *cctx)
981{
982 garray_T *stack = &cctx->ctx_type_stack;
983 type_T *type;
984
985 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
986 if (type == &t_bool)
987 return OK;
988
989 if (type == &t_any || type == &t_number)
990 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
991 // This requires a runtime type check.
992 return generate_COND2BOOL(cctx);
993
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Generate an ISN_PUSHNR instruction.
999 */
1000 static int
1001generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001004 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001012 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001013 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHBOOL instruction.
1019 */
1020 static int
1021generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1022{
1023 isn_T *isn;
1024
Bram Moolenaar080457c2020-03-03 21:53:32 +01001025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.number = number;
1029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_PUSHSPEC instruction.
1035 */
1036 static int
1037generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.number = number;
1045
1046 return OK;
1047}
1048
1049#ifdef FEAT_FLOAT
1050/*
1051 * Generate an ISN_PUSHF instruction.
1052 */
1053 static int
1054generate_PUSHF(cctx_T *cctx, float_T fnumber)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.fnumber = fnumber;
1062
1063 return OK;
1064}
1065#endif
1066
1067/*
1068 * Generate an ISN_PUSHS instruction.
1069 * Consumes "str".
1070 */
1071 static int
1072generate_PUSHS(cctx_T *cctx, char_u *str)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar508b5612021-01-02 18:17:26 +01001076 if (cctx->ctx_skip == SKIP_YES)
1077 {
1078 vim_free(str);
1079 return OK;
1080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.string = str;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001089 * Generate an ISN_PUSHCHANNEL instruction.
1090 * Consumes "channel".
1091 */
1092 static int
1093generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1094{
1095 isn_T *isn;
1096
Bram Moolenaar080457c2020-03-03 21:53:32 +01001097 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001098 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1099 return FAIL;
1100 isn->isn_arg.channel = channel;
1101
1102 return OK;
1103}
1104
1105/*
1106 * Generate an ISN_PUSHJOB instruction.
1107 * Consumes "job".
1108 */
1109 static int
1110generate_PUSHJOB(cctx_T *cctx, job_T *job)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 return FAIL;
1117 isn->isn_arg.job = job;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Generate an ISN_PUSHBLOB instruction.
1124 * Consumes "blob".
1125 */
1126 static int
1127generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.blob = blob;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001140 * Generate an ISN_PUSHFUNC instruction with name "name".
1141 * Consumes "name".
1142 */
1143 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001144generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001149 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001150 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001151 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152
1153 return OK;
1154}
1155
1156/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001157 * Generate an ISN_GETITEM instruction with "index".
1158 */
1159 static int
1160generate_GETITEM(cctx_T *cctx, int index)
1161{
1162 isn_T *isn;
1163 garray_T *stack = &cctx->ctx_type_stack;
1164 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1165 type_T *item_type = &t_any;
1166
1167 RETURN_OK_IF_SKIP(cctx);
1168
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001169 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001170 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001171 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001172 emsg(_(e_listreq));
1173 return FAIL;
1174 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.number = index;
1179
1180 // add the item type to the type stack
1181 if (ga_grow(stack, 1) == FAIL)
1182 return FAIL;
1183 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1184 ++stack->ga_len;
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001189 * Generate an ISN_SLICE instruction with "count".
1190 */
1191 static int
1192generate_SLICE(cctx_T *cctx, int count)
1193{
1194 isn_T *isn;
1195
1196 RETURN_OK_IF_SKIP(cctx);
1197 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1198 return FAIL;
1199 isn->isn_arg.number = count;
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_CHECKLEN instruction with "min_len".
1205 */
1206 static int
1207generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1208{
1209 isn_T *isn;
1210
1211 RETURN_OK_IF_SKIP(cctx);
1212
1213 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.checklen.cl_min_len = min_len;
1216 isn->isn_arg.checklen.cl_more_OK = more_OK;
1217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 * Generate an ISN_STORE instruction.
1223 */
1224 static int
1225generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1226{
1227 isn_T *isn;
1228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1231 return FAIL;
1232 if (name != NULL)
1233 isn->isn_arg.string = vim_strsave(name);
1234 else
1235 isn->isn_arg.number = idx;
1236
1237 return OK;
1238}
1239
1240/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001241 * Generate an ISN_STOREOUTER instruction.
1242 */
1243 static int
1244generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1245{
1246 isn_T *isn;
1247
1248 RETURN_OK_IF_SKIP(cctx);
1249 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.outer.outer_idx = idx;
1252 isn->isn_arg.outer.outer_depth = level;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1259 */
1260 static int
1261generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1262{
1263 isn_T *isn;
1264
Bram Moolenaar080457c2020-03-03 21:53:32 +01001265 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1267 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001268 isn->isn_arg.storenr.stnr_idx = idx;
1269 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 return OK;
1272}
1273
1274/*
1275 * Generate an ISN_STOREOPT instruction
1276 */
1277 static int
1278generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1279{
1280 isn_T *isn;
1281
Bram Moolenaar080457c2020-03-03 21:53:32 +01001282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001283 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 return FAIL;
1285 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1286 isn->isn_arg.storeopt.so_flags = opt_flags;
1287
1288 return OK;
1289}
1290
1291/*
1292 * Generate an ISN_LOAD or similar instruction.
1293 */
1294 static int
1295generate_LOAD(
1296 cctx_T *cctx,
1297 isntype_T isn_type,
1298 int idx,
1299 char_u *name,
1300 type_T *type)
1301{
1302 isn_T *isn;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1306 return FAIL;
1307 if (name != NULL)
1308 isn->isn_arg.string = vim_strsave(name);
1309 else
1310 isn->isn_arg.number = idx;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001316 * Generate an ISN_LOADOUTER instruction
1317 */
1318 static int
1319generate_LOADOUTER(
1320 cctx_T *cctx,
1321 int idx,
1322 int nesting,
1323 type_T *type)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.outer.outer_idx = idx;
1331 isn->isn_arg.outer.outer_depth = nesting;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001353 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001379 * Generate an ISN_LOCKCONST instruction.
1380 */
1381 static int
1382generate_LOCKCONST(cctx_T *cctx)
1383{
1384 isn_T *isn;
1385
1386 RETURN_OK_IF_SKIP(cctx);
1387 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1388 return FAIL;
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 * Generate an ISN_LOADS instruction.
1394 */
1395 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400 int sid,
1401 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406 if (isn_type == ISN_LOADS)
1407 isn = generate_instr_type(cctx, isn_type, type);
1408 else
1409 isn = generate_instr_drop(cctx, isn_type, 1);
1410 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1413 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414
1415 return OK;
1416}
1417
1418/*
1419 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1420 */
1421 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 cctx_T *cctx,
1424 isntype_T isn_type,
1425 int sid,
1426 int idx,
1427 type_T *type)
1428{
1429 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001430 scriptref_T *sref;
1431 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if (isn_type == ISN_LOADSCRIPT)
1435 isn = generate_instr_type(cctx, isn_type, type);
1436 else
1437 isn = generate_instr_drop(cctx, isn_type, 1);
1438 if (isn == NULL)
1439 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001440
1441 // This requires three arguments, which doesn't fit in an instruction, thus
1442 // we need to allocate a struct for this.
1443 sref = ALLOC_ONE(scriptref_T);
1444 if (sref == NULL)
1445 return FAIL;
1446 isn->isn_arg.script.scriptref = sref;
1447 sref->sref_sid = sid;
1448 sref->sref_idx = idx;
1449 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001450 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_NEWLIST instruction.
1456 */
1457 static int
1458generate_NEWLIST(cctx_T *cctx, int count)
1459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 type_T *type;
1463 type_T *member;
1464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1467 return FAIL;
1468 isn->isn_arg.number = count;
1469
Bram Moolenaar127542b2020-08-09 17:22:04 +02001470 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001471 if (count == 0)
1472 member = &t_void;
1473 else
1474 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001475 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1476 cctx->ctx_type_list);
1477 type = get_list_type(member, cctx->ctx_type_list);
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // drop the value types
1480 stack->ga_len -= count;
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 // add the list type to the type stack
1483 if (ga_grow(stack, 1) == FAIL)
1484 return FAIL;
1485 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1486 ++stack->ga_len;
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_NEWDICT instruction.
1493 */
1494 static int
1495generate_NEWDICT(cctx_T *cctx, int count)
1496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 type_T *type;
1500 type_T *member;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1504 return FAIL;
1505 isn->isn_arg.number = count;
1506
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001507 if (count == 0)
1508 member = &t_void;
1509 else
1510 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001511 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1512 cctx->ctx_type_list);
1513 type = get_dict_type(member, cctx->ctx_type_list);
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 // drop the key and value types
1516 stack->ga_len -= 2 * count;
1517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // add the dict type to the type stack
1519 if (ga_grow(stack, 1) == FAIL)
1520 return FAIL;
1521 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1522 ++stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Generate an ISN_FUNCREF instruction.
1529 */
1530 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001531generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
Bram Moolenaar080457c2020-03-03 21:53:32 +01001536 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1538 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001539 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001540 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001542 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001543 // the nested context, including this one.
1544 if (ufunc->uf_flags & FC_CLOSURE)
1545 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if (ga_grow(stack, 1) == FAIL)
1548 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001549 ((type_T **)stack->ga_data)[stack->ga_len] =
1550 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 ++stack->ga_len;
1552
1553 return OK;
1554}
1555
1556/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001558 * "lambda_name" and "func_name" must be in allocated memory and will be
1559 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001560 */
1561 static int
1562generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1563{
1564 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001565
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001566 if (cctx->ctx_skip == SKIP_YES)
1567 {
1568 vim_free(lambda_name);
1569 vim_free(func_name);
1570 return OK;
1571 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 {
1574 vim_free(lambda_name);
1575 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 }
1578 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 isn->isn_arg.newfunc.nf_global = func_name;
1580
1581 return OK;
1582}
1583
1584/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001585 * Generate an ISN_DEF instruction: list functions
1586 */
1587 static int
1588generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1589{
1590 isn_T *isn;
1591
1592 RETURN_OK_IF_SKIP(cctx);
1593 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1594 return FAIL;
1595 if (len > 0)
1596 {
1597 isn->isn_arg.string = vim_strnsave(name, len);
1598 if (isn->isn_arg.string == NULL)
1599 return FAIL;
1600 }
1601 return OK;
1602}
1603
1604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 * Generate an ISN_JUMP instruction.
1606 */
1607 static int
1608generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1609{
1610 isn_T *isn;
1611 garray_T *stack = &cctx->ctx_type_stack;
1612
Bram Moolenaar080457c2020-03-03 21:53:32 +01001613 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.jump.jump_when = when;
1617 isn->isn_arg.jump.jump_where = where;
1618
1619 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1620 --stack->ga_len;
1621
1622 return OK;
1623}
1624
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001625/*
1626 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1627 */
1628 static int
1629generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1630{
1631 isn_T *isn;
1632
1633 RETURN_OK_IF_SKIP(cctx);
1634 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1637 // jump_where is set later
1638 return OK;
1639}
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 static int
1642generate_FOR(cctx_T *cctx, int loop_idx)
1643{
1644 isn_T *isn;
1645 garray_T *stack = &cctx->ctx_type_stack;
1646
Bram Moolenaar080457c2020-03-03 21:53:32 +01001647 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.forloop.for_idx = loop_idx;
1651
1652 if (ga_grow(stack, 1) == FAIL)
1653 return FAIL;
1654 // type doesn't matter, will be stored next
1655 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1656 ++stack->ga_len;
1657
1658 return OK;
1659}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001660/*
1661 * Generate an ISN_TRYCONT instruction.
1662 */
1663 static int
1664generate_TRYCONT(cctx_T *cctx, int levels, int where)
1665{
1666 isn_T *isn;
1667
1668 RETURN_OK_IF_SKIP(cctx);
1669 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.trycont.tct_levels = levels;
1672 isn->isn_arg.trycont.tct_where = where;
1673
1674 return OK;
1675}
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
1678/*
1679 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001680 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 * Return FAIL if the number of arguments is wrong.
1682 */
1683 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001684generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685{
1686 isn_T *isn;
1687 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001689 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001690 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar080457c2020-03-03 21:53:32 +01001692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001693 argoff = check_internal_func(func_idx, argcount);
1694 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 return FAIL;
1696
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 if (method_call && argoff > 1)
1698 {
1699 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.shuffle.shfl_item = argcount;
1702 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1703 }
1704
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 if (argcount > 0)
1706 {
1707 // Check the types of the arguments.
1708 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001709 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1710 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001711 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001712 if (internal_func_is_map(func_idx))
1713 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001714 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1717 return FAIL;
1718 isn->isn_arg.bfunc.cbf_idx = func_idx;
1719 isn->isn_arg.bfunc.cbf_argcount = argcount;
1720
Bram Moolenaar94738d82020-10-21 14:25:07 +02001721 // Drop the argument types and push the return type.
1722 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if (ga_grow(stack, 1) == FAIL)
1724 return FAIL;
1725 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001726 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001727 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001729 if (maptype != NULL && maptype->tt_member != NULL
1730 && maptype->tt_member != &t_any)
1731 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001732 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 return OK;
1735}
1736
1737/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001738 * Generate an ISN_LISTAPPEND instruction. Works like add().
1739 * Argument count is already checked.
1740 */
1741 static int
1742generate_LISTAPPEND(cctx_T *cctx)
1743{
1744 garray_T *stack = &cctx->ctx_type_stack;
1745 type_T *list_type;
1746 type_T *item_type;
1747 type_T *expected;
1748
1749 // Caller already checked that list_type is a list.
1750 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1751 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1752 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001753 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001754 return FAIL;
1755
1756 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1757 return FAIL;
1758
1759 --stack->ga_len; // drop the argument
1760 return OK;
1761}
1762
1763/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001764 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1765 * Argument count is already checked.
1766 */
1767 static int
1768generate_BLOBAPPEND(cctx_T *cctx)
1769{
1770 garray_T *stack = &cctx->ctx_type_stack;
1771 type_T *item_type;
1772
1773 // Caller already checked that blob_type is a blob.
1774 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001775 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001776 return FAIL;
1777
1778 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1779 return FAIL;
1780
1781 --stack->ga_len; // drop the argument
1782 return OK;
1783}
1784
1785/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 * Return TRUE if "ufunc" should be compiled, taking into account whether
1787 * "profile" indicates profiling is to be done.
1788 */
1789 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001790func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001791{
1792 switch (ufunc->uf_def_status)
1793 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001794 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001795 case UF_TO_BE_COMPILED: return TRUE;
1796 case UF_COMPILED:
1797 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001798#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001799 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1800 + ufunc->uf_dfunc_idx;
1801
1802 return profile ? dfunc->df_instr_prof == NULL
1803 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001804#else
1805 break;
1806#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001807 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001808 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001809 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001810 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001811}
1812
1813/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 * Generate an ISN_DCALL or ISN_UCALL instruction.
1815 * Return FAIL if the number of arguments is wrong.
1816 */
1817 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001818generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819{
1820 isn_T *isn;
1821 garray_T *stack = &cctx->ctx_type_stack;
1822 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001823 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if (argcount > regular_args && !has_varargs(ufunc))
1827 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001828 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 return FAIL;
1830 }
1831 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1832 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001833 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 return FAIL;
1835 }
1836
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001837 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001838 {
1839 int i;
1840
1841 for (i = 0; i < argcount; ++i)
1842 {
1843 type_T *expected;
1844 type_T *actual;
1845
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001846 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1847 if (actual == &t_special
1848 && i >= regular_args - ufunc->uf_def_args.ga_len)
1849 {
1850 // assume v:none used for default argument value
1851 continue;
1852 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001853 if (i < regular_args)
1854 {
1855 if (ufunc->uf_arg_types == NULL)
1856 continue;
1857 expected = ufunc->uf_arg_types[i];
1858 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001859 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1860 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001861 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001862 else
1863 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001864 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001865 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001866 {
1867 arg_type_mismatch(expected, actual, i + 1);
1868 return FAIL;
1869 }
1870 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001871 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001872 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001873 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001874 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001875 }
1876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001878 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001879 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001881 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 {
1883 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1884 isn->isn_arg.dfunc.cdf_argcount = argcount;
1885 }
1886 else
1887 {
1888 // A user function may be deleted and redefined later, can't use the
1889 // ufunc pointer, need to look it up again at runtime.
1890 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1891 isn->isn_arg.ufunc.cuf_argcount = argcount;
1892 }
1893
1894 stack->ga_len -= argcount; // drop the arguments
1895 if (ga_grow(stack, 1) == FAIL)
1896 return FAIL;
1897 // add return value
1898 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1899 ++stack->ga_len;
1900
1901 return OK;
1902}
1903
1904/*
1905 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1906 */
1907 static int
1908generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1909{
1910 isn_T *isn;
1911 garray_T *stack = &cctx->ctx_type_stack;
1912
Bram Moolenaar080457c2020-03-03 21:53:32 +01001913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1915 return FAIL;
1916 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1917 isn->isn_arg.ufunc.cuf_argcount = argcount;
1918
1919 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001920 if (ga_grow(stack, 1) == FAIL)
1921 return FAIL;
1922 // add return value
1923 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1924 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925
1926 return OK;
1927}
1928
1929/*
1930 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001931 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 */
1933 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001934generate_PCALL(
1935 cctx_T *cctx,
1936 int argcount,
1937 char_u *name,
1938 type_T *type,
1939 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940{
1941 isn_T *isn;
1942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001943 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944
Bram Moolenaar080457c2020-03-03 21:53:32 +01001945 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001946
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001947 if (type->tt_type == VAR_ANY)
1948 ret_type = &t_any;
1949 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001950 {
1951 if (type->tt_argcount != -1)
1952 {
1953 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1954
1955 if (argcount < type->tt_min_argcount - varargs)
1956 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001957 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001958 return FAIL;
1959 }
1960 if (!varargs && argcount > type->tt_argcount)
1961 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001962 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001963 return FAIL;
1964 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001965 if (type->tt_args != NULL)
1966 {
1967 int i;
1968
1969 for (i = 0; i < argcount; ++i)
1970 {
1971 int offset = -argcount + i - 1;
1972 type_T *actual = ((type_T **)stack->ga_data)[
1973 stack->ga_len + offset];
1974 type_T *expected;
1975
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001976 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001977 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001978 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001979 else if (i >= type->tt_min_argcount
1980 && actual == &t_special)
1981 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001982 else
1983 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001984 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001985 cctx, TRUE, FALSE) == FAIL)
1986 {
1987 arg_type_mismatch(expected, actual, i + 1);
1988 return FAIL;
1989 }
1990 }
1991 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001992 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001993 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001994 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001995 else
1996 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001997 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001998 return FAIL;
1999 }
2000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2002 return FAIL;
2003 isn->isn_arg.pfunc.cpf_top = at_top;
2004 isn->isn_arg.pfunc.cpf_argcount = argcount;
2005
2006 stack->ga_len -= argcount; // drop the arguments
2007
2008 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002009 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002011 // If partial is above the arguments it must be cleared and replaced with
2012 // the return value.
2013 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2014 return FAIL;
2015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 return OK;
2017}
2018
2019/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002020 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 */
2022 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002023generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024{
2025 isn_T *isn;
2026 garray_T *stack = &cctx->ctx_type_stack;
2027 type_T *type;
2028
Bram Moolenaar080457c2020-03-03 21:53:32 +01002029 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002032 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002034 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002036 if (type->tt_type != VAR_DICT && type != &t_any)
2037 {
2038 emsg(_(e_dictreq));
2039 return FAIL;
2040 }
2041 // change dict type to dict member type
2042 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002043 {
2044 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2045 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2046 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047
2048 return OK;
2049}
2050
2051/*
2052 * Generate an ISN_ECHO instruction.
2053 */
2054 static int
2055generate_ECHO(cctx_T *cctx, int with_white, int count)
2056{
2057 isn_T *isn;
2058
Bram Moolenaar080457c2020-03-03 21:53:32 +01002059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2061 return FAIL;
2062 isn->isn_arg.echo.echo_with_white = with_white;
2063 isn->isn_arg.echo.echo_count = count;
2064
2065 return OK;
2066}
2067
Bram Moolenaarad39c092020-02-26 18:23:43 +01002068/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002069 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002070 */
2071 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002072generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002073{
2074 isn_T *isn;
2075
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077 return FAIL;
2078 isn->isn_arg.number = count;
2079
2080 return OK;
2081}
2082
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002083/*
2084 * Generate an ISN_PUT instruction.
2085 */
2086 static int
2087generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2088{
2089 isn_T *isn;
2090
2091 RETURN_OK_IF_SKIP(cctx);
2092 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2093 return FAIL;
2094 isn->isn_arg.put.put_regname = regname;
2095 isn->isn_arg.put.put_lnum = lnum;
2096 return OK;
2097}
2098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 static int
2100generate_EXEC(cctx_T *cctx, char_u *line)
2101{
2102 isn_T *isn;
2103
Bram Moolenaar080457c2020-03-03 21:53:32 +01002104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.string = vim_strsave(line);
2108 return OK;
2109}
2110
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002111 static int
2112generate_EXECCONCAT(cctx_T *cctx, int count)
2113{
2114 isn_T *isn;
2115
2116 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2117 return FAIL;
2118 isn->isn_arg.number = count;
2119 return OK;
2120}
2121
Bram Moolenaar08597872020-12-10 19:43:40 +01002122/*
2123 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2124 */
2125 static int
2126generate_RANGE(cctx_T *cctx, char_u *range)
2127{
2128 isn_T *isn;
2129 garray_T *stack = &cctx->ctx_type_stack;
2130
2131 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2132 return FAIL;
2133 isn->isn_arg.string = range;
2134
2135 if (ga_grow(stack, 1) == FAIL)
2136 return FAIL;
2137 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2138 ++stack->ga_len;
2139 return OK;
2140}
2141
Bram Moolenaar792f7862020-11-23 08:31:18 +01002142 static int
2143generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2144{
2145 isn_T *isn;
2146
2147 RETURN_OK_IF_SKIP(cctx);
2148 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2149 return FAIL;
2150 isn->isn_arg.unpack.unp_count = var_count;
2151 isn->isn_arg.unpack.unp_semicolon = semicolon;
2152 return OK;
2153}
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002156 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002157 */
2158 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002159generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002160{
2161 isn_T *isn;
2162
Bram Moolenaarfa984412021-03-25 22:15:28 +01002163 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002165 cctx->ctx_has_cmdmod = TRUE;
2166
2167 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002169 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2170 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2171 return FAIL;
2172 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002173 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002174 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002177 return OK;
2178}
2179
2180 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002181generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002182{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002183 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2184 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002185 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002186 return OK;
2187}
2188
Bram Moolenaarfa984412021-03-25 22:15:28 +01002189 static int
2190misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002191{
2192 garray_T *instr = &cctx->ctx_instr;
2193
Bram Moolenaara91a7132021-03-25 21:12:15 +01002194 if (cctx->ctx_has_cmdmod
2195 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2196 == ISN_CMDMOD)
2197 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002198 emsg(_(e_misplaced_command_modifier));
2199 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002200 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002201 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002202}
2203
2204/*
2205 * Get the index of the current instruction.
2206 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2207 */
2208 static int
2209current_instr_idx(cctx_T *cctx)
2210{
2211 garray_T *instr = &cctx->ctx_instr;
2212 int idx = instr->ga_len;
2213
2214 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2215 .isn_type == ISN_CMDMOD)
2216 --idx;
2217#ifdef FEAT_PROFILE
2218 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2219 .isn_type == ISN_PROF_START)
2220 --idx;
2221#endif
2222 return idx;
2223}
2224
Bram Moolenaarf002a412021-01-24 13:34:18 +01002225#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002226 static void
2227may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2228{
2229 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002230 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002231}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002232#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002233
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002234/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002236 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002238 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002239reserve_local(
2240 cctx_T *cctx,
2241 char_u *name,
2242 size_t len,
2243 int isConst,
2244 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 lvar_T *lvar;
2247
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002248 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002250 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002251 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
2253
2254 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002255 return NULL;
2256 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002257 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002259 // Every local variable uses the next entry on the stack. We could re-use
2260 // the last ones when leaving a scope, but then variables used in a closure
2261 // might get overwritten. To keep things simple do not re-use stack
2262 // entries. This is less efficient, but memory is cheap these days.
2263 lvar->lv_idx = cctx->ctx_locals_count++;
2264
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002265 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 lvar->lv_const = isConst;
2267 lvar->lv_type = type;
2268
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002269 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270}
2271
2272/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002273 * Remove local variables above "new_top".
2274 */
2275 static void
2276unwind_locals(cctx_T *cctx, int new_top)
2277{
2278 if (cctx->ctx_locals.ga_len > new_top)
2279 {
2280 int idx;
2281 lvar_T *lvar;
2282
2283 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2284 {
2285 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2286 vim_free(lvar->lv_name);
2287 }
2288 }
2289 cctx->ctx_locals.ga_len = new_top;
2290}
2291
2292/*
2293 * Free all local variables.
2294 */
2295 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002296free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002297{
2298 unwind_locals(cctx, 0);
2299 ga_clear(&cctx->ctx_locals);
2300}
2301
2302/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002303 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2304 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2305 * error if the variable was defined with :const.
2306 */
2307 static int
2308check_item_writable(svar_T *sv, int check_writable, char_u *name)
2309{
2310 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2311 || (check_writable == ASSIGN_FINAL
2312 && sv->sv_const == ASSIGN_CONST))
2313 {
2314 semsg(_(e_readonlyvar), name);
2315 return FAIL;
2316 }
2317 return OK;
2318}
2319
2320/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002322 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 * Returns the index in "sn_var_vals" if found.
2324 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002325 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 */
2327 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002328get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329{
2330 hashtab_T *ht;
2331 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002332 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002333 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 int idx;
2335
Bram Moolenaare3d46852020-08-29 13:39:17 +02002336 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002338 if (sid == current_sctx.sc_sid)
2339 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002340 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002341
2342 if (sav == NULL)
2343 return -2;
2344 idx = sav->sav_var_vals_idx;
2345 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002346 if (check_item_writable(sv, check_writable, name) == FAIL)
2347 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348 return idx;
2349 }
2350
2351 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 ht = &SCRIPT_VARS(sid);
2353 di = find_var_in_ht(ht, 0, name, TRUE);
2354 if (di == NULL)
2355 return -2;
2356
2357 // Now find the svar_T index in sn_var_vals.
2358 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2359 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002360 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 if (sv->sv_tv == &di->di_tv)
2362 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002363 if (check_item_writable(sv, check_writable, name) == FAIL)
2364 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 return idx;
2366 }
2367 }
2368 return -1;
2369}
2370
2371/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002372 * Find "name" in imported items of the current script or in "cctx" if not
2373 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 */
2375 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002376find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 int idx;
2379
Bram Moolenaare3d46852020-08-29 13:39:17 +02002380 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002381 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 if (cctx != NULL)
2383 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2384 {
2385 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2386 + idx;
2387
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002388 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2389 : STRLEN(import->imp_name) == len
2390 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 return import;
2392 }
2393
Bram Moolenaarefa94442020-08-08 22:16:00 +02002394 return find_imported_in_script(name, len, current_sctx.sc_sid);
2395}
2396
2397 imported_T *
2398find_imported_in_script(char_u *name, size_t len, int sid)
2399{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002400 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002401 int idx;
2402
Bram Moolenaare3d46852020-08-29 13:39:17 +02002403 if (!SCRIPT_ID_VALID(sid))
2404 return NULL;
2405 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2407 {
2408 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2409
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002410 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2411 : STRLEN(import->imp_name) == len
2412 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 return import;
2414 }
2415 return NULL;
2416}
2417
2418/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002419 * Free all imported variables.
2420 */
2421 static void
2422free_imported(cctx_T *cctx)
2423{
2424 int idx;
2425
2426 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2427 {
2428 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2429
2430 vim_free(import->imp_name);
2431 }
2432 ga_clear(&cctx->ctx_imports);
2433}
2434
2435/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002436 * Return a pointer to the next line that isn't empty or only contains a
2437 * comment. Skips over white space.
2438 * Returns NULL if there is none.
2439 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002440 char_u *
2441peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002442{
2443 int lnum = cctx->ctx_lnum;
2444
2445 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2446 {
2447 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002448 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002450 // ignore NULLs inserted for continuation lines
2451 if (line != NULL)
2452 {
2453 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002454 if (vim9_bad_comment(p))
2455 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002456 if (*p != NUL && !vim9_comment_start(p))
2457 return p;
2458 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002459 }
2460 return NULL;
2461}
2462
2463/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002464 * Called when checking for a following operator at "arg". When the rest of
2465 * the line is empty or only a comment, peek the next line. If there is a next
2466 * line return a pointer to it and set "nextp".
2467 * Otherwise skip over white space.
2468 */
2469 static char_u *
2470may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2471{
2472 char_u *p = skipwhite(arg);
2473
2474 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002475 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002476 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002477 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002478 if (*nextp != NULL)
2479 return *nextp;
2480 }
2481 return p;
2482}
2483
2484/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002485 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002486 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002487 * Returns NULL when at the end.
2488 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002489 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002490next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002491{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002492 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002493
2494 do
2495 {
2496 ++cctx->ctx_lnum;
2497 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002498 {
2499 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002500 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002501 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002502 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002503 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002504 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002505 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002506 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002507 return line;
2508}
2509
2510/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002511 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002512 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002513 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002514 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2515 */
2516 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002517may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002518{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002519 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002520 if (vim9_bad_comment(*arg))
2521 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002522 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002523 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002524 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525
2526 if (next == NULL)
2527 return FAIL;
2528 *arg = skipwhite(next);
2529 }
2530 return OK;
2531}
2532
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002533/*
2534 * Idem, and give an error when failed.
2535 */
2536 static int
2537may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2538{
2539 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2540 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002541 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002542 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002543 return FAIL;
2544 }
2545 return OK;
2546}
2547
2548
Bram Moolenaara5565e42020-05-09 15:44:01 +02002549// Structure passed between the compile_expr* functions to keep track of
2550// constants that have been parsed but for which no code was produced yet. If
2551// possible expressions on these constants are applied at compile time. If
2552// that is not possible, the code to push the constants needs to be generated
2553// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002554// Using 50 should be more than enough of 5 levels of ().
2555#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002557 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002558 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002559 int pp_is_const; // all generated code was constants, used for a
2560 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002561} ppconst_T;
2562
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002563static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002564static int compile_expr0(char_u **arg, cctx_T *cctx);
2565static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2566
Bram Moolenaara5565e42020-05-09 15:44:01 +02002567/*
2568 * Generate a PUSH instruction for "tv".
2569 * "tv" will be consumed or cleared.
2570 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2571 */
2572 static int
2573generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2574{
2575 if (tv != NULL)
2576 {
2577 switch (tv->v_type)
2578 {
2579 case VAR_UNKNOWN:
2580 break;
2581 case VAR_BOOL:
2582 generate_PUSHBOOL(cctx, tv->vval.v_number);
2583 break;
2584 case VAR_SPECIAL:
2585 generate_PUSHSPEC(cctx, tv->vval.v_number);
2586 break;
2587 case VAR_NUMBER:
2588 generate_PUSHNR(cctx, tv->vval.v_number);
2589 break;
2590#ifdef FEAT_FLOAT
2591 case VAR_FLOAT:
2592 generate_PUSHF(cctx, tv->vval.v_float);
2593 break;
2594#endif
2595 case VAR_BLOB:
2596 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2597 tv->vval.v_blob = NULL;
2598 break;
2599 case VAR_STRING:
2600 generate_PUSHS(cctx, tv->vval.v_string);
2601 tv->vval.v_string = NULL;
2602 break;
2603 default:
2604 iemsg("constant type not supported");
2605 clear_tv(tv);
2606 return FAIL;
2607 }
2608 tv->v_type = VAR_UNKNOWN;
2609 }
2610 return OK;
2611}
2612
2613/*
2614 * Generate code for any ppconst entries.
2615 */
2616 static int
2617generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2618{
2619 int i;
2620 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002621 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002622
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002623 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002624 for (i = 0; i < ppconst->pp_used; ++i)
2625 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2626 ret = FAIL;
2627 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002628 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002629 return ret;
2630}
2631
2632/*
2633 * Clear ppconst constants. Used when failing.
2634 */
2635 static void
2636clear_ppconst(ppconst_T *ppconst)
2637{
2638 int i;
2639
2640 for (i = 0; i < ppconst->pp_used; ++i)
2641 clear_tv(&ppconst->pp_tv[i]);
2642 ppconst->pp_used = 0;
2643}
2644
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002645/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002646 * Compile getting a member from a list/dict/string/blob. Stack has the
2647 * indexable value and the index.
2648 */
2649 static int
2650compile_member(int is_slice, cctx_T *cctx)
2651{
2652 type_T **typep;
2653 garray_T *stack = &cctx->ctx_type_stack;
2654 vartype_T vtype;
2655 type_T *valtype;
2656
2657 // We can index a list and a dict. If we don't know the type
2658 // we can use the index value type.
2659 // TODO: If we don't know use an instruction to figure it out at
2660 // runtime.
2661 typep = ((type_T **)stack->ga_data) + stack->ga_len
2662 - (is_slice ? 3 : 2);
2663 vtype = (*typep)->tt_type;
2664 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2665 // If the index is a string, the variable must be a Dict.
2666 if (*typep == &t_any && valtype == &t_string)
2667 vtype = VAR_DICT;
2668 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2669 {
2670 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2671 return FAIL;
2672 if (is_slice)
2673 {
2674 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2675 if (need_type(valtype, &t_number, -2, 0, cctx,
2676 FALSE, FALSE) == FAIL)
2677 return FAIL;
2678 }
2679 }
2680
2681 if (vtype == VAR_DICT)
2682 {
2683 if (is_slice)
2684 {
2685 emsg(_(e_cannot_slice_dictionary));
2686 return FAIL;
2687 }
2688 if ((*typep)->tt_type == VAR_DICT)
2689 {
2690 *typep = (*typep)->tt_member;
2691 if (*typep == &t_unknown)
2692 // empty dict was used
2693 *typep = &t_any;
2694 }
2695 else
2696 {
2697 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2698 FALSE, FALSE) == FAIL)
2699 return FAIL;
2700 *typep = &t_any;
2701 }
2702 if (may_generate_2STRING(-1, cctx) == FAIL)
2703 return FAIL;
2704 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2705 return FAIL;
2706 }
2707 else if (vtype == VAR_STRING)
2708 {
2709 *typep = &t_string;
2710 if ((is_slice
2711 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2712 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2713 return FAIL;
2714 }
2715 else if (vtype == VAR_BLOB)
2716 {
2717 emsg("Sorry, blob index and slice not implemented yet");
2718 return FAIL;
2719 }
2720 else if (vtype == VAR_LIST || *typep == &t_any)
2721 {
2722 if (is_slice)
2723 {
2724 if (generate_instr_drop(cctx,
2725 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2726 2) == FAIL)
2727 return FAIL;
2728 }
2729 else
2730 {
2731 if ((*typep)->tt_type == VAR_LIST)
2732 {
2733 *typep = (*typep)->tt_member;
2734 if (*typep == &t_unknown)
2735 // empty list was used
2736 *typep = &t_any;
2737 }
2738 if (generate_instr_drop(cctx,
2739 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2740 return FAIL;
2741 }
2742 }
2743 else
2744 {
2745 emsg(_(e_string_list_dict_or_blob_required));
2746 return FAIL;
2747 }
2748 return OK;
2749}
2750
2751/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002752 * Generate an instruction to load script-local variable "name", without the
2753 * leading "s:".
2754 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 */
2756 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002757compile_load_scriptvar(
2758 cctx_T *cctx,
2759 char_u *name, // variable NUL terminated
2760 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002761 char_u **end, // end of variable
2762 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002764 scriptitem_T *si;
2765 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 imported_T *import;
2767
Bram Moolenaare3d46852020-08-29 13:39:17 +02002768 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2769 return FAIL;
2770 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002771 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002772 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002774 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002775 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2776 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 }
2778 if (idx >= 0)
2779 {
2780 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2781
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002782 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 current_sctx.sc_sid, idx, sv->sv_type);
2784 return OK;
2785 }
2786
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002787 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 if (import != NULL)
2789 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002790 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002791 {
2792 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002793 char_u *exp_name;
2794 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002795 ufunc_T *ufunc;
2796 type_T *type;
2797
2798 // Used "import * as Name", need to lookup the member.
2799 if (*p != '.')
2800 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002801 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 return FAIL;
2803 }
2804 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002805 if (VIM_ISWHITE(*p))
2806 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002807 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002808 return FAIL;
2809 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002810
Bram Moolenaar1c991142020-07-04 13:15:31 +02002811 // isolate one name
2812 exp_name = p;
2813 while (eval_isnamec(*p))
2814 ++p;
2815 cc = *p;
2816 *p = NUL;
2817
Bram Moolenaaredba7072021-03-13 21:14:18 +01002818 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2819 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002820 *p = cc;
2821 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002822 *end = p;
2823
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002824 if (idx < 0)
2825 {
2826 if (*p == '(' && ufunc != NULL)
2827 {
2828 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2829 return OK;
2830 }
2831 return FAIL;
2832 }
2833
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002834 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2835 import->imp_sid,
2836 idx,
2837 type);
2838 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002839 else if (import->imp_funcname != NULL)
2840 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002842 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2843 import->imp_sid,
2844 import->imp_var_vals_idx,
2845 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 return OK;
2847 }
2848
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002849 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002850 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 return FAIL;
2852}
2853
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002854 static int
2855generate_funcref(cctx_T *cctx, char_u *name)
2856{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002857 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002858
2859 if (ufunc == NULL)
2860 return FAIL;
2861
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002862 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002863 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2864 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002865 == FAIL)
2866 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002867 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002868}
2869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870/*
2871 * Compile a variable name into a load instruction.
2872 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002873 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 * When "error" is FALSE do not give an error when not found.
2875 */
2876 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002877compile_load(
2878 char_u **arg,
2879 char_u *end_arg,
2880 cctx_T *cctx,
2881 int is_expr,
2882 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883{
2884 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002885 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002886 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002888 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 if (*(*arg + 1) == ':')
2891 {
2892 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002893 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002894 {
2895 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002897 switch (**arg)
2898 {
2899 case 'g': isn_type = ISN_LOADGDICT; break;
2900 case 'w': isn_type = ISN_LOADWDICT; break;
2901 case 't': isn_type = ISN_LOADTDICT; break;
2902 case 'b': isn_type = ISN_LOADBDICT; break;
2903 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002904 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002905 goto theend;
2906 }
2907 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2908 goto theend;
2909 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 else
2912 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002913 isntype_T isn_type = ISN_DROP;
2914
2915 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2916 if (name == NULL)
2917 return FAIL;
2918
2919 switch (**arg)
2920 {
2921 case 'v': res = generate_LOADV(cctx, name, error);
2922 break;
2923 case 's': res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002924 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002925 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002926 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2927 isn_type = ISN_LOADG;
2928 else
2929 {
2930 isn_type = ISN_LOADAUTO;
2931 vim_free(name);
2932 name = vim_strnsave(*arg, end - *arg);
2933 if (name == NULL)
2934 return FAIL;
2935 }
2936 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 case 'w': isn_type = ISN_LOADW; break;
2938 case 't': isn_type = ISN_LOADT; break;
2939 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002940 default: // cannot happen, just in case
2941 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002942 goto theend;
2943 }
2944 if (isn_type != ISN_DROP)
2945 {
2946 // Global, Buffer-local, Window-local and Tabpage-local
2947 // variables can be defined later, thus we don't check if it
2948 // exists, give error at runtime.
2949 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 }
2952 }
2953 else
2954 {
2955 size_t len = end - *arg;
2956 int idx;
2957 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002958 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959
2960 name = vim_strnsave(*arg, end - *arg);
2961 if (name == NULL)
2962 return FAIL;
2963
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002964 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002966 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002967 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 }
2969 else
2970 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002971 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002972
Bram Moolenaar709664c2020-12-12 14:33:41 +01002973 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002975 type = lvar.lv_type;
2976 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002977 if (lvar.lv_from_outer != 0)
2978 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002979 else
2980 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 }
2982 else
2983 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002984 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002985 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02002986 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002987 || find_imported(name, 0, cctx) != NULL)
2988 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002989
Bram Moolenaar0f769812020-09-12 18:32:34 +02002990 // When evaluating an expression and the name starts with an
2991 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002992 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002993 if (res == FAIL && is_expr
2994 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002995 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 }
2997 }
2998 if (gen_load)
2999 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003000 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003001 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003002 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003003 cctx->ctx_outer_used = TRUE;
3004 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 }
3006
3007 *arg = end;
3008
3009theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003010 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003011 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 vim_free(name);
3013 return res;
3014}
3015
3016/*
3017 * Compile the argument expressions.
3018 * "arg" points to just after the "(" and is advanced to after the ")"
3019 */
3020 static int
3021compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3022{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003023 char_u *p = *arg;
3024 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003025 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
Bram Moolenaare6085c52020-04-12 20:19:16 +02003027 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003029 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3030 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003031 if (*p == ')')
3032 {
3033 *arg = p + 1;
3034 return OK;
3035 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003036 if (must_end)
3037 {
3038 semsg(_(e_missing_comma_before_argument_str), p);
3039 return FAIL;
3040 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003041
Bram Moolenaara5565e42020-05-09 15:44:01 +02003042 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 return FAIL;
3044 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003045
3046 if (*p != ',' && *skipwhite(p) == ',')
3047 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003048 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003049 p = skipwhite(p);
3050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003052 {
3053 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003054 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003055 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003056 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003057 else
3058 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003059 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003060 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003062failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003063 emsg(_(e_missing_close));
3064 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065}
3066
3067/*
3068 * Compile a function call: name(arg1, arg2)
3069 * "arg" points to "name", "arg + varlen" to the "(".
3070 * "argcount_init" is 1 for "value->method()"
3071 * Instructions:
3072 * EVAL arg1
3073 * EVAL arg2
3074 * BCALL / DCALL / UCALL
3075 */
3076 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003077compile_call(
3078 char_u **arg,
3079 size_t varlen,
3080 cctx_T *cctx,
3081 ppconst_T *ppconst,
3082 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083{
3084 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003085 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 int argcount = argcount_init;
3087 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003088 char_u fname_buf[FLEN_FIXED + 1];
3089 char_u *tofree = NULL;
3090 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003091 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003092 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003093 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094
Bram Moolenaara5565e42020-05-09 15:44:01 +02003095 // we can evaluate "has('name')" at compile time
3096 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3097 {
3098 char_u *s = skipwhite(*arg + varlen + 1);
3099 typval_T argvars[2];
3100
3101 argvars[0].v_type = VAR_UNKNOWN;
3102 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003103 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003104 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003105 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003106 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003107 if (*s == ')' && argvars[0].v_type == VAR_STRING
3108 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003109 {
3110 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3111
3112 *arg = s + 1;
3113 argvars[1].v_type = VAR_UNKNOWN;
3114 tv->v_type = VAR_NUMBER;
3115 tv->vval.v_number = 0;
3116 f_has(argvars, tv);
3117 clear_tv(&argvars[0]);
3118 ++ppconst->pp_used;
3119 return OK;
3120 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003121 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003122 }
3123
3124 if (generate_ppconst(cctx, ppconst) == FAIL)
3125 return FAIL;
3126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 if (varlen >= sizeof(namebuf))
3128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003129 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 return FAIL;
3131 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003132 vim_strncpy(namebuf, *arg, varlen);
3133 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
3135 *arg = skipwhite(*arg + varlen + 1);
3136 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003137 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
Bram Moolenaar03290b82020-12-19 16:30:44 +01003139 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003140 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 {
3142 int idx;
3143
3144 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003145 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003147 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003148 if (STRCMP(name, "flatten") == 0)
3149 {
3150 emsg(_(e_cannot_use_flatten_in_vim9_script));
3151 goto theend;
3152 }
3153
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003154 if (STRCMP(name, "add") == 0 && argcount == 2)
3155 {
3156 garray_T *stack = &cctx->ctx_type_stack;
3157 type_T *type = ((type_T **)stack->ga_data)[
3158 stack->ga_len - 2];
3159
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003160 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003161 if (type->tt_type == VAR_LIST)
3162 {
3163 // inline "add(list, item)" so that the type can be checked
3164 res = generate_LISTAPPEND(cctx);
3165 idx = -1;
3166 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003167 else if (type->tt_type == VAR_BLOB)
3168 {
3169 // inline "add(blob, nr)" so that the type can be checked
3170 res = generate_BLOBAPPEND(cctx);
3171 idx = -1;
3172 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003173 }
3174
3175 if (idx >= 0)
3176 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3177 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003178 else
3179 semsg(_(e_unknownfunc), namebuf);
3180 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 }
3182
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003183 // An argument or local variable can be a function reference, this
3184 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003185 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003186 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003187 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003188 // If we can find the function by name generate the right call.
3189 // Skip global functions here, a local funcref takes precedence.
3190 ufunc = find_func(name, FALSE, cctx);
3191 if (ufunc != NULL && !func_is_global(ufunc))
3192 {
3193 res = generate_CALL(cctx, ufunc, argcount);
3194 goto theend;
3195 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197
3198 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003199 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003200 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003202 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003203 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003204 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003205 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003206 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003207
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003208 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003209 goto theend;
3210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaar0f769812020-09-12 18:32:34 +02003212 // If we can find a global function by name generate the right call.
3213 if (ufunc != NULL)
3214 {
3215 res = generate_CALL(cctx, ufunc, argcount);
3216 goto theend;
3217 }
3218
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003219 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003220 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003221 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003222 res = generate_UCALL(cctx, name, argcount);
3223 else
3224 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003225
3226theend:
3227 vim_free(tofree);
3228 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229}
3230
3231// like NAMESPACE_CHAR but with 'a' and 'l'.
3232#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3233
3234/*
3235 * Find the end of a variable or function name. Unlike find_name_end() this
3236 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003237 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 * Return a pointer to just after the name. Equal to "arg" if there is no
3239 * valid name.
3240 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003241 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003242to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243{
3244 char_u *p;
3245
3246 // Quick check for valid starting character.
3247 if (!eval_isnamec1(*arg))
3248 return arg;
3249
3250 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3251 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3252 // and can be used in slice "[n:]".
3253 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003254 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3256 break;
3257 return p;
3258}
3259
3260/*
3261 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003262 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 */
3264 char_u *
3265to_name_const_end(char_u *arg)
3266{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003267 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 typval_T rettv;
3269
3270 if (p == arg && *arg == '[')
3271 {
3272
3273 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 p = arg;
3276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 return p;
3278}
3279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280/*
3281 * parse a list: [expr, expr]
3282 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003283 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 */
3285 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003286compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287{
3288 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003289 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003291 int is_const;
3292 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003294 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003296 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003297 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003298 semsg(_(e_list_end), *arg);
3299 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003300 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003301 if (*p == ',')
3302 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003303 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003304 return FAIL;
3305 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003306 if (*p == ']')
3307 {
3308 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003309 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003310 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003311 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003312 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003313 if (!is_const)
3314 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 ++count;
3316 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003317 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003319 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3320 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003321 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003322 return FAIL;
3323 }
3324 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003325 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 p = skipwhite(p);
3327 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003328 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003330 ppconst->pp_is_const = is_all_const;
3331 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332}
3333
3334/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003335 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003336 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003337 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 */
3339 static int
3340compile_lambda(char_u **arg, cctx_T *cctx)
3341{
Bram Moolenaare462f522020-12-27 14:43:30 +01003342 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 typval_T rettv;
3344 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003345 evalarg_T evalarg;
3346
3347 CLEAR_FIELD(evalarg);
3348 evalarg.eval_flags = EVAL_EVALUATE;
3349 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
3351 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003352 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3353 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003354 {
3355 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003356 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003357 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003358
Bram Moolenaar65c44152020-12-24 15:14:01 +01003359 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003361 ++ufunc->uf_refcount;
3362 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
Bram Moolenaar65c44152020-12-24 15:14:01 +01003364 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003365 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003367 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3368 // points into it. Point to the original line to avoid a dangling pointer.
3369 if (evalarg.eval_tofree_cmdline != NULL)
3370 {
3371 size_t off = *arg - evalarg.eval_tofree_cmdline;
3372
3373 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3374 + off;
3375 }
3376
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003377 clear_evalarg(&evalarg, NULL);
3378
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003379 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003380 {
3381 // The return type will now be known.
3382 set_function_type(ufunc);
3383
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003384 // The function reference count will be 1. When the ISN_FUNCREF
3385 // instruction is deleted the reference count is decremented and the
3386 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003387 return generate_FUNCREF(cctx, ufunc);
3388 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003389
3390 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 return FAIL;
3392}
3393
3394/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003395 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003397 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 */
3399 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003400compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401{
3402 garray_T *instr = &cctx->ctx_instr;
3403 int count = 0;
3404 dict_T *d = dict_alloc();
3405 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003406 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003407 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003408 int is_const;
3409 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410
3411 if (d == NULL)
3412 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003413 if (generate_ppconst(cctx, ppconst) == FAIL)
3414 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003415 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003417 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418
Bram Moolenaar23c55272020-06-21 16:58:13 +02003419 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003420 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003421 *arg = NULL;
3422 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003423 }
3424
3425 if (**arg == '}')
3426 break;
3427
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003428 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003430 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003432 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003433 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003434 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003437 if (isn->isn_type == ISN_PUSHNR)
3438 {
3439 char buf[NUMBUFLEN];
3440
3441 // Convert to string at compile time.
3442 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3443 isn->isn_type = ISN_PUSHS;
3444 isn->isn_arg.string = vim_strsave((char_u *)buf);
3445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 if (isn->isn_type == ISN_PUSHS)
3447 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003448 else if (may_generate_2STRING(-1, cctx) == FAIL)
3449 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003450 *arg = skipwhite(*arg);
3451 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003452 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003453 emsg(_(e_missing_matching_bracket_after_dict_key));
3454 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003455 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003456 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003458 else
3459 {
3460 // {"name": value},
3461 // {'name': value},
3462 // {name: value} use "name" as a literal key
3463 key = get_literal_key(arg);
3464 if (key == NULL)
3465 return FAIL;
3466 if (generate_PUSHS(cctx, key) == FAIL)
3467 return FAIL;
3468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469
3470 // Check for duplicate keys, if using string keys.
3471 if (key != NULL)
3472 {
3473 item = dict_find(d, key, -1);
3474 if (item != NULL)
3475 {
3476 semsg(_(e_duplicate_key), key);
3477 goto failret;
3478 }
3479 item = dictitem_alloc(key);
3480 if (item != NULL)
3481 {
3482 item->di_tv.v_type = VAR_UNKNOWN;
3483 item->di_tv.v_lock = 0;
3484 if (dict_add(d, item) == FAIL)
3485 dictitem_free(item);
3486 }
3487 }
3488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 if (**arg != ':')
3490 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003491 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003492 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003493 else
3494 semsg(_(e_missing_dict_colon), *arg);
3495 return FAIL;
3496 }
3497 whitep = *arg + 1;
3498 if (!IS_WHITE_OR_NUL(*whitep))
3499 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003500 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 return FAIL;
3502 }
3503
Bram Moolenaar23c55272020-06-21 16:58:13 +02003504 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003505 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003506 *arg = NULL;
3507 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003508 }
3509
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003510 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003512 if (!is_const)
3513 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 ++count;
3515
Bram Moolenaar2c330432020-04-13 14:41:35 +02003516 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003517 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003518 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003519 *arg = NULL;
3520 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003521 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 if (**arg == '}')
3523 break;
3524 if (**arg != ',')
3525 {
3526 semsg(_(e_missing_dict_comma), *arg);
3527 goto failret;
3528 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003529 if (IS_WHITE_OR_NUL(*whitep))
3530 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003531 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003532 return FAIL;
3533 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003534 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003535 if (!IS_WHITE_OR_NUL(*whitep))
3536 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003537 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003538 return FAIL;
3539 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003540 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 }
3542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 *arg = *arg + 1;
3544
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003545 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003546 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003547 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003548 *arg += STRLEN(*arg);
3549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003551 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 return generate_NEWDICT(cctx, count);
3553
3554failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003555 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003556 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003557 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003558 *arg = (char_u *)"";
3559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 dict_unref(d);
3561 return FAIL;
3562}
3563
3564/*
3565 * Compile "&option".
3566 */
3567 static int
3568compile_get_option(char_u **arg, cctx_T *cctx)
3569{
3570 typval_T rettv;
3571 char_u *start = *arg;
3572 int ret;
3573
3574 // parse the option and get the current value to get the type.
3575 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003576 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 if (ret == OK)
3578 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003579 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003580 char_u *name = vim_strnsave(start, *arg - start);
3581 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3582 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583
3584 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3585 vim_free(name);
3586 }
3587 clear_tv(&rettv);
3588
3589 return ret;
3590}
3591
3592/*
3593 * Compile "$VAR".
3594 */
3595 static int
3596compile_get_env(char_u **arg, cctx_T *cctx)
3597{
3598 char_u *start = *arg;
3599 int len;
3600 int ret;
3601 char_u *name;
3602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 ++*arg;
3604 len = get_env_len(arg);
3605 if (len == 0)
3606 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003607 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 return FAIL;
3609 }
3610
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003611 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 name = vim_strnsave(start, len + 1);
3613 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3614 vim_free(name);
3615 return ret;
3616}
3617
3618/*
3619 * Compile "@r".
3620 */
3621 static int
3622compile_get_register(char_u **arg, cctx_T *cctx)
3623{
3624 int ret;
3625
3626 ++*arg;
3627 if (**arg == NUL)
3628 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003629 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 return FAIL;
3631 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003632 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 {
3634 emsg_invreg(**arg);
3635 return FAIL;
3636 }
3637 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3638 ++*arg;
3639 return ret;
3640}
3641
3642/*
3643 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003644 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 */
3646 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003647apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003649 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650
3651 // this works from end to start
3652 while (p > start)
3653 {
3654 --p;
3655 if (*p == '-' || *p == '+')
3656 {
3657 // only '-' has an effect, for '+' we only check the type
3658#ifdef FEAT_FLOAT
3659 if (rettv->v_type == VAR_FLOAT)
3660 {
3661 if (*p == '-')
3662 rettv->vval.v_float = -rettv->vval.v_float;
3663 }
3664 else
3665#endif
3666 {
3667 varnumber_T val;
3668 int error = FALSE;
3669
3670 // tv_get_number_chk() accepts a string, but we don't want that
3671 // here
3672 if (check_not_string(rettv) == FAIL)
3673 return FAIL;
3674 val = tv_get_number_chk(rettv, &error);
3675 clear_tv(rettv);
3676 if (error)
3677 return FAIL;
3678 if (*p == '-')
3679 val = -val;
3680 rettv->v_type = VAR_NUMBER;
3681 rettv->vval.v_number = val;
3682 }
3683 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003684 else if (numeric_only)
3685 {
3686 ++p;
3687 break;
3688 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003689 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 {
3691 int v = tv2bool(rettv);
3692
3693 // '!' is permissive in the type.
3694 clear_tv(rettv);
3695 rettv->v_type = VAR_BOOL;
3696 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3697 }
3698 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003699 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 return OK;
3701}
3702
3703/*
3704 * Recognize v: variables that are constants and set "rettv".
3705 */
3706 static void
3707get_vim_constant(char_u **arg, typval_T *rettv)
3708{
3709 if (STRNCMP(*arg, "v:true", 6) == 0)
3710 {
3711 rettv->v_type = VAR_BOOL;
3712 rettv->vval.v_number = VVAL_TRUE;
3713 *arg += 6;
3714 }
3715 else if (STRNCMP(*arg, "v:false", 7) == 0)
3716 {
3717 rettv->v_type = VAR_BOOL;
3718 rettv->vval.v_number = VVAL_FALSE;
3719 *arg += 7;
3720 }
3721 else if (STRNCMP(*arg, "v:null", 6) == 0)
3722 {
3723 rettv->v_type = VAR_SPECIAL;
3724 rettv->vval.v_number = VVAL_NULL;
3725 *arg += 6;
3726 }
3727 else if (STRNCMP(*arg, "v:none", 6) == 0)
3728 {
3729 rettv->v_type = VAR_SPECIAL;
3730 rettv->vval.v_number = VVAL_NONE;
3731 *arg += 6;
3732 }
3733}
3734
Bram Moolenaar657137c2021-01-09 15:45:23 +01003735 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003736get_compare_type(char_u *p, int *len, int *type_is)
3737{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003738 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003739 int i;
3740
3741 switch (p[0])
3742 {
3743 case '=': if (p[1] == '=')
3744 type = EXPR_EQUAL;
3745 else if (p[1] == '~')
3746 type = EXPR_MATCH;
3747 break;
3748 case '!': if (p[1] == '=')
3749 type = EXPR_NEQUAL;
3750 else if (p[1] == '~')
3751 type = EXPR_NOMATCH;
3752 break;
3753 case '>': if (p[1] != '=')
3754 {
3755 type = EXPR_GREATER;
3756 *len = 1;
3757 }
3758 else
3759 type = EXPR_GEQUAL;
3760 break;
3761 case '<': if (p[1] != '=')
3762 {
3763 type = EXPR_SMALLER;
3764 *len = 1;
3765 }
3766 else
3767 type = EXPR_SEQUAL;
3768 break;
3769 case 'i': if (p[1] == 's')
3770 {
3771 // "is" and "isnot"; but not a prefix of a name
3772 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3773 *len = 5;
3774 i = p[*len];
3775 if (!isalnum(i) && i != '_')
3776 {
3777 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3778 *type_is = TRUE;
3779 }
3780 }
3781 break;
3782 }
3783 return type;
3784}
3785
3786/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003787 * Skip over an expression, ignoring most errors.
3788 */
3789 static void
3790skip_expr_cctx(char_u **arg, cctx_T *cctx)
3791{
3792 evalarg_T evalarg;
3793
3794 CLEAR_FIELD(evalarg);
3795 evalarg.eval_cctx = cctx;
3796 skip_expr(arg, &evalarg);
3797}
3798
3799/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003801 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 */
3803 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003804compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003806 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
3808 // this works from end to start
3809 while (p > start)
3810 {
3811 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003812 while (VIM_ISWHITE(*p))
3813 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (*p == '-' || *p == '+')
3815 {
3816 int negate = *p == '-';
3817 isn_T *isn;
3818
3819 // TODO: check type
3820 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3821 {
3822 --p;
3823 if (*p == '-')
3824 negate = !negate;
3825 }
3826 // only '-' has an effect, for '+' we only check the type
3827 if (negate)
3828 isn = generate_instr(cctx, ISN_NEGATENR);
3829 else
3830 isn = generate_instr(cctx, ISN_CHECKNR);
3831 if (isn == NULL)
3832 return FAIL;
3833 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003834 else if (numeric_only)
3835 {
3836 ++p;
3837 break;
3838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 else
3840 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003841 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003843 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003845 if (p[-1] == '!')
3846 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
3849 if (generate_2BOOL(cctx, invert) == FAIL)
3850 return FAIL;
3851 }
3852 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003853 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 return OK;
3855}
3856
3857/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003858 * Compile "(expression)": recursive!
3859 * Return FAIL/OK.
3860 */
3861 static int
3862compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3863{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003864 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003865 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003866
Bram Moolenaar24156692021-01-14 20:35:49 +01003867 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3868 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003869 if (ppconst->pp_used <= PPSIZE - 10)
3870 {
3871 ret = compile_expr1(arg, cctx, ppconst);
3872 }
3873 else
3874 {
3875 // Not enough space in ppconst, flush constants.
3876 if (generate_ppconst(cctx, ppconst) == FAIL)
3877 return FAIL;
3878 ret = compile_expr0(arg, cctx);
3879 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003880 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3881 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003882 if (**arg == ')')
3883 ++*arg;
3884 else if (ret == OK)
3885 {
3886 emsg(_(e_missing_close));
3887 ret = FAIL;
3888 }
3889 return ret;
3890}
3891
3892/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003894 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 */
3896 static int
3897compile_subscript(
3898 char_u **arg,
3899 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003900 char_u *start_leader,
3901 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003902 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003904 char_u *name_start = *end_leader;
3905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 for (;;)
3907 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003908 char_u *p = skipwhite(*arg);
3909
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003910 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003911 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003912 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003913
3914 // If a following line starts with "->{" or "->X" advance to that
3915 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003916 // Also if a following line starts with ".x".
3917 if (next != NULL &&
3918 ((next[0] == '-' && next[1] == '>'
3919 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003920 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003921 {
3922 next = next_line_from_context(cctx, TRUE);
3923 if (next == NULL)
3924 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003925 *arg = next;
3926 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003927 }
3928 }
3929
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003930 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003931 // not a function call.
3932 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003934 garray_T *stack = &cctx->ctx_type_stack;
3935 type_T *type;
3936 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003938 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003939 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003940 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003943 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3944
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003945 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3947 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003948 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 return FAIL;
3950 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003951 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003953 char_u *pstart = p;
3954
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003955 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003956 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003957 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 // something->method()
3960 // Apply the '!', '-' and '+' first:
3961 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003962 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003965 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003966 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003967 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003968 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003969 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003970 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003971 garray_T *stack = &cctx->ctx_type_stack;
3972 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003973 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003974 int expr_isn_start = cctx->ctx_instr.ga_len;
3975 int expr_isn_end;
3976 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003977
3978 // Funcref call: list->(Refs[2])(arg)
3979 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003980 //
3981 // Fist compile the function expression.
3982 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01003983 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003984
3985 // Remember the next instruction index, where the instructions
3986 // for arguments are being written.
3987 expr_isn_end = cctx->ctx_instr.ga_len;
3988
3989 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01003990 if (**arg != '(')
3991 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003992 if (*skipwhite(*arg) == '(')
3993 emsg(_(e_nowhitespace));
3994 else
3995 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003996 return FAIL;
3997 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01003998 *arg = skipwhite(*arg + 1);
3999 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4000 return FAIL;
4001
Bram Moolenaar2927c072021-04-05 19:41:21 +02004002 // Move the instructions for the arguments to before the
4003 // instructions of the expression and move the type of the
4004 // expression after the argument types. This is what ISN_PCALL
4005 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004006 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004007 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4008 if (arg_isn_count > 0)
4009 {
4010 int expr_isn_count = expr_isn_end - expr_isn_start;
4011 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4012
4013 if (isn == NULL)
4014 return FAIL;
4015 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4016 + expr_isn_start,
4017 sizeof(isn_T) * expr_isn_count);
4018 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4019 + expr_isn_start,
4020 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4021 sizeof(isn_T) * arg_isn_count);
4022 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4023 + expr_isn_start + arg_isn_count,
4024 isn, sizeof(isn_T) * expr_isn_count);
4025 vim_free(isn);
4026
4027 type = ((type_T **)stack->ga_data)[type_idx_start];
4028 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4029 ((type_T **)stack->ga_data) + type_idx_start + 1,
4030 sizeof(type_T *)
4031 * (stack->ga_len - type_idx_start - 1));
4032 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4033 }
4034
Bram Moolenaar7e368202020-12-25 21:56:57 +01004035 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4036 if (generate_PCALL(cctx, argcount,
4037 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 return FAIL;
4039 }
4040 else
4041 {
4042 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004043 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004044 if (!eval_isnamec1(*p))
4045 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004046 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004047 return FAIL;
4048 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004049 if (ASCII_ISALPHA(*p) && p[1] == ':')
4050 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004051 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 ;
4053 if (*p != '(')
4054 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004055 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 return FAIL;
4057 }
4058 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004059 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 return FAIL;
4061 }
4062 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004063 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004065 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004066
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004067 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004068 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004069 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004070 // TODO: blob index
4071 // TODO: more arguments
4072 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004074 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004075 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004076
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004077 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004078 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004079 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004080 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004081 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 // missing first index is equal to zero
4083 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004084 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004085 else
4086 {
4087 if (compile_expr0(arg, cctx) == FAIL)
4088 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004089 if (**arg == ':')
4090 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004091 semsg(_(e_white_space_required_before_and_after_str_at_str),
4092 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004093 return FAIL;
4094 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004095 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004096 return FAIL;
4097 *arg = skipwhite(*arg);
4098 }
4099 if (**arg == ':')
4100 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004101 is_slice = TRUE;
4102 ++*arg;
4103 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4104 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004105 semsg(_(e_white_space_required_before_and_after_str_at_str),
4106 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004107 return FAIL;
4108 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004109 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004110 return FAIL;
4111 if (**arg == ']')
4112 // missing second index is equal to end of string
4113 generate_PUSHNR(cctx, -1);
4114 else
4115 {
4116 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004117 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004118 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004119 return FAIL;
4120 *arg = skipwhite(*arg);
4121 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123
4124 if (**arg != ']')
4125 {
4126 emsg(_(e_missbrac));
4127 return FAIL;
4128 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004129 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130
Bram Moolenaare42939a2021-04-05 17:11:17 +02004131 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004132 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004134 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004136 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004137 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004138 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004139 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004141 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004142 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004143 {
4144 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004145 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004146 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004147 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004148 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 while (eval_isnamec(*p))
4150 MB_PTR_ADV(p);
4151 if (p == *arg)
4152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004153 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 return FAIL;
4155 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004156 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 return FAIL;
4158 *arg = p;
4159 }
4160 else
4161 break;
4162 }
4163
4164 // TODO - see handle_subscript():
4165 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4166 // Don't do this when "Func" is already a partial that was bound
4167 // explicitly (pt_auto is FALSE).
4168
4169 return OK;
4170}
4171
4172/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004173 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4174 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004176 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004177 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004178 *
4179 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 */
4181
4182/*
4183 * number number constant
4184 * 0zFFFFFFFF Blob constant
4185 * "string" string constant
4186 * 'string' literal string constant
4187 * &option-name option value
4188 * @r register contents
4189 * identifier variable value
4190 * function() function call
4191 * $VAR environment variable
4192 * (expression) nested expression
4193 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004194 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 *
4196 * Also handle:
4197 * ! in front logical NOT
4198 * - in front unary minus
4199 * + in front unary plus (ignored)
4200 * trailing (arg) funcref/partial call
4201 * trailing [] subscript in String or List
4202 * trailing .name entry in Dictionary
4203 * trailing ->name() method call
4204 */
4205 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004206compile_expr7(
4207 char_u **arg,
4208 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004209 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 char_u *start_leader, *end_leader;
4212 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004213 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004214 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004216 ppconst->pp_is_const = FALSE;
4217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 /*
4219 * Skip '!', '-' and '+' characters. They are handled later.
4220 */
4221 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004222 if (eval_leader(arg, TRUE) == FAIL)
4223 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 end_leader = *arg;
4225
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004226 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 switch (**arg)
4228 {
4229 /*
4230 * Number constant.
4231 */
4232 case '0': // also for blob starting with 0z
4233 case '1':
4234 case '2':
4235 case '3':
4236 case '4':
4237 case '5':
4238 case '6':
4239 case '7':
4240 case '8':
4241 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004242 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004244 // Apply "-" and "+" just before the number now, right to
4245 // left. Matters especially when "->" follows. Stops at
4246 // '!'.
4247 if (apply_leader(rettv, TRUE,
4248 start_leader, &end_leader) == FAIL)
4249 {
4250 clear_tv(rettv);
4251 return FAIL;
4252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 break;
4254
4255 /*
4256 * String constant: "string".
4257 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004258 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 return FAIL;
4260 break;
4261
4262 /*
4263 * Literal string constant: 'str''ing'.
4264 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004265 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 return FAIL;
4267 break;
4268
4269 /*
4270 * Constant Vim variable.
4271 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004272 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 ret = NOTDONE;
4274 break;
4275
4276 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 * "true" constant
4278 */
4279 case 't': if (STRNCMP(*arg, "true", 4) == 0
4280 && !eval_isnamec((*arg)[4]))
4281 {
4282 *arg += 4;
4283 rettv->v_type = VAR_BOOL;
4284 rettv->vval.v_number = VVAL_TRUE;
4285 }
4286 else
4287 ret = NOTDONE;
4288 break;
4289
4290 /*
4291 * "false" constant
4292 */
4293 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4294 && !eval_isnamec((*arg)[5]))
4295 {
4296 *arg += 5;
4297 rettv->v_type = VAR_BOOL;
4298 rettv->vval.v_number = VVAL_FALSE;
4299 }
4300 else
4301 ret = NOTDONE;
4302 break;
4303
4304 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004305 * "null" constant
4306 */
4307 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004308 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004309 {
4310 *arg += 4;
4311 rettv->v_type = VAR_SPECIAL;
4312 rettv->vval.v_number = VVAL_NULL;
4313 }
4314 else
4315 ret = NOTDONE;
4316 break;
4317
4318 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 * List: [expr, expr]
4320 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004321 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4322 return FAIL;
4323 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 break;
4325
4326 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 * Dictionary: {'key': val, 'key': val}
4328 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004329 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4330 return FAIL;
4331 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 break;
4333
4334 /*
4335 * Option value: &name
4336 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004337 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4338 return FAIL;
4339 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 break;
4341
4342 /*
4343 * Environment variable: $VAR.
4344 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004345 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4346 return FAIL;
4347 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 break;
4349
4350 /*
4351 * Register contents: @r.
4352 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004353 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4354 return FAIL;
4355 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356 break;
4357 /*
4358 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004359 * lambda: (arg, arg) => expr
4360 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004362 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4363 ret = compile_lambda(arg, cctx);
4364 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004365 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 break;
4367
4368 default: ret = NOTDONE;
4369 break;
4370 }
4371 if (ret == FAIL)
4372 return FAIL;
4373
Bram Moolenaar1c747212020-05-09 18:28:34 +02004374 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004376 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004377 clear_tv(rettv);
4378 else
4379 // A constant expression can possibly be handled compile time,
4380 // return the value instead of generating code.
4381 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 else if (ret == NOTDONE)
4384 {
4385 char_u *p;
4386 int r;
4387
4388 if (!eval_isnamec1(**arg))
4389 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004390 if (!vim9_bad_comment(*arg))
4391 {
4392 if (ends_excmd(*skipwhite(*arg)))
4393 semsg(_(e_empty_expression_str), *arg);
4394 else
4395 semsg(_(e_name_expected_str), *arg);
4396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 return FAIL;
4398 }
4399
4400 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004401 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 {
4404 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004407 {
4408 if (generate_ppconst(cctx, ppconst) == FAIL)
4409 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004410 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 if (r == FAIL)
4413 return FAIL;
4414 }
4415
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004416 // Handle following "[]", ".member", etc.
4417 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004418 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 ppconst) == FAIL)
4420 return FAIL;
4421 if (ppconst->pp_used > 0)
4422 {
4423 // apply the '!', '-' and '+' before the constant
4424 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004425 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 return FAIL;
4427 return OK;
4428 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004429 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004431 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432}
4433
4434/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004435 * Give the "white on both sides" error, taking the operator from "p[len]".
4436 */
4437 void
4438error_white_both(char_u *op, int len)
4439{
4440 char_u buf[10];
4441
4442 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004443 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004444}
4445
4446/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004447 * <type>expr7: runtime type check / conversion
4448 */
4449 static int
4450compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4451{
4452 type_T *want_type = NULL;
4453
4454 // Recognize <type>
4455 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4456 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004457 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004458 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4459 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004460 return FAIL;
4461
4462 if (**arg != '>')
4463 {
4464 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004465 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004466 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004467 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004468 return FAIL;
4469 }
4470 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004471 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004472 return FAIL;
4473 }
4474
4475 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4476 return FAIL;
4477
4478 if (want_type != NULL)
4479 {
4480 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004481 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004482 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004483
Bram Moolenaard1103582020-08-14 22:44:25 +02004484 generate_ppconst(cctx, ppconst);
4485 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004486 where.wt_index = 0;
4487 where.wt_variable = FALSE;
4488 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004489 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004490 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004491 return FAIL;
4492 }
4493 }
4494
4495 return OK;
4496}
4497
4498/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 * * number multiplication
4500 * / number division
4501 * % number modulo
4502 */
4503 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505{
4506 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004507 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004508 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004510 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004511 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 return FAIL;
4513
4514 /*
4515 * Repeat computing, until no "*", "/" or "%" is following.
4516 */
4517 for (;;)
4518 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004519 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 if (*op != '*' && *op != '/' && *op != '%')
4521 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004522 if (next != NULL)
4523 {
4524 *arg = next_line_from_context(cctx, TRUE);
4525 op = skipwhite(*arg);
4526 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004527
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004528 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004530 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004531 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004533 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004534 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004536 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004537 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004539
4540 if (ppconst->pp_used == ppconst_used + 2
4541 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4542 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004543 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004544 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4545 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4546 varnumber_T res = 0;
4547 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004549 // both are numbers: compute the result
4550 switch (*op)
4551 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004552 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004553 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004554 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004555 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004556 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004557 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004558 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004559 break;
4560 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004561 if (failed)
4562 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004563 tv1->vval.v_number = res;
4564 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004565 }
4566 else
4567 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004568 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004569 generate_two_op(cctx, op);
4570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
4572
4573 return OK;
4574}
4575
4576/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004577 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 * - number subtraction
4579 * .. string concatenation
4580 */
4581 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583{
4584 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004585 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004587 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588
4589 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 return FAIL;
4592
4593 /*
4594 * Repeat computing, until no "+", "-" or ".." is following.
4595 */
4596 for (;;)
4597 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004598 op = may_peek_next_line(cctx, *arg, &next);
4599 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004602 if (next != NULL)
4603 {
4604 *arg = next_line_from_context(cctx, TRUE);
4605 op = skipwhite(*arg);
4606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004608 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004610 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004611 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 }
4613
Bram Moolenaare0de1712020-12-02 17:36:54 +01004614 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004615 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004617 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 return FAIL;
4620
Bram Moolenaara5565e42020-05-09 15:44:01 +02004621 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004622 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004623 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4624 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4625 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4626 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4629 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004630
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004631 // concat/subtract/add constant numbers
4632 if (*op == '+')
4633 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4634 else if (*op == '-')
4635 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4636 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004637 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004638 // concatenate constant strings
4639 char_u *s1 = tv1->vval.v_string;
4640 char_u *s2 = tv2->vval.v_string;
4641 size_t len1 = STRLEN(s1);
4642
4643 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4644 if (tv1->vval.v_string == NULL)
4645 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004647 return FAIL;
4648 }
4649 mch_memmove(tv1->vval.v_string, s1, len1);
4650 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004651 vim_free(s1);
4652 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004653 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004654 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 }
4656 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004657 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004658 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004659 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004660 if (*op == '.')
4661 {
4662 if (may_generate_2STRING(-2, cctx) == FAIL
4663 || may_generate_2STRING(-1, cctx) == FAIL)
4664 return FAIL;
4665 generate_instr_drop(cctx, ISN_CONCAT, 1);
4666 }
4667 else
4668 generate_two_op(cctx, op);
4669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * expr5a == expr5b
4677 * expr5a =~ expr5b
4678 * expr5a != expr5b
4679 * expr5a !~ expr5b
4680 * expr5a > expr5b
4681 * expr5a >= expr5b
4682 * expr5a < expr5b
4683 * expr5a <= expr5b
4684 * expr5a is expr5b
4685 * expr5a isnot expr5b
4686 *
4687 * Produces instructions:
4688 * EVAL expr5a Push result of "expr5a"
4689 * EVAL expr5b Push result of "expr5b"
4690 * COMPARE one of the compare instructions
4691 */
4692 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004695 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004697 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
4702 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004703 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 return FAIL;
4705
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004706 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004707 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
4709 /*
4710 * If there is a comparative operator, use it.
4711 */
4712 if (type != EXPR_UNKNOWN)
4713 {
4714 int ic = FALSE; // Default: do not ignore case
4715
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004716 if (next != NULL)
4717 {
4718 *arg = next_line_from_context(cctx, TRUE);
4719 p = skipwhite(*arg);
4720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 if (type_is && (p[len] == '?' || p[len] == '#'))
4722 {
4723 semsg(_(e_invexpr2), *arg);
4724 return FAIL;
4725 }
4726 // extra question mark appended: ignore case
4727 if (p[len] == '?')
4728 {
4729 ic = TRUE;
4730 ++len;
4731 }
4732 // extra '#' appended: match case (ignored)
4733 else if (p[len] == '#')
4734 ++len;
4735 // nothing appended: match case
4736
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004737 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004739 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004740 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 }
4742
4743 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004744 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004745 return FAIL;
4746
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 return FAIL;
4749
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750 if (ppconst->pp_used == ppconst_used + 2)
4751 {
4752 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4753 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4754 int ret;
4755
4756 // Both sides are a constant, compute the result now.
4757 // First check for a valid combination of types, this is more
4758 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004759 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004760 ret = FAIL;
4761 else
4762 {
4763 ret = typval_compare(tv1, tv2, type, ic);
4764 tv1->v_type = VAR_BOOL;
4765 tv1->vval.v_number = tv1->vval.v_number
4766 ? VVAL_TRUE : VVAL_FALSE;
4767 clear_tv(tv2);
4768 --ppconst->pp_used;
4769 }
4770 return ret;
4771 }
4772
4773 generate_ppconst(cctx, ppconst);
4774 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004775 }
4776
4777 return OK;
4778}
4779
Bram Moolenaar7f141552020-05-09 17:35:53 +02004780static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004782/*
4783 * Compile || or &&.
4784 */
4785 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004786compile_and_or(
4787 char_u **arg,
4788 cctx_T *cctx,
4789 char *op,
4790 ppconst_T *ppconst,
4791 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793 char_u *next;
4794 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 int opchar = *op;
4796
4797 if (p[0] == opchar && p[1] == opchar)
4798 {
4799 garray_T *instr = &cctx->ctx_instr;
4800 garray_T end_ga;
4801
4802 /*
4803 * Repeat until there is no following "||" or "&&"
4804 */
4805 ga_init2(&end_ga, sizeof(int), 10);
4806 while (p[0] == opchar && p[1] == opchar)
4807 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004808 long start_lnum = SOURCING_LNUM;
4809 int start_ctx_lnum = cctx->ctx_lnum;
4810 int save_lnum;
4811
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004812 if (next != NULL)
4813 {
4814 *arg = next_line_from_context(cctx, TRUE);
4815 p = skipwhite(*arg);
4816 }
4817
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004818 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4819 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004820 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004821 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004822 return FAIL;
4823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004825 // TODO: use ppconst if the value is a constant and check
4826 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004827 generate_ppconst(cctx, ppconst);
4828
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004829 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004830 SOURCING_LNUM = start_lnum;
4831 save_lnum = cctx->ctx_lnum;
4832 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004833 if (bool_on_stack(cctx) == FAIL)
4834 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004835 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004836 ga_clear(&end_ga);
4837 return FAIL;
4838 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004839 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 if (ga_grow(&end_ga, 1) == FAIL)
4842 {
4843 ga_clear(&end_ga);
4844 return FAIL;
4845 }
4846 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4847 ++end_ga.ga_len;
4848 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004849 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850
4851 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004852 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004853 {
4854 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004855 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004856 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004857
Bram Moolenaara5565e42020-05-09 15:44:01 +02004858 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4859 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 {
4861 ga_clear(&end_ga);
4862 return FAIL;
4863 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004864
4865 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004867 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004869 // Every part must evaluate to a bool.
4870 if (bool_on_stack(cctx) == FAIL)
4871 {
4872 ga_clear(&end_ga);
4873 return FAIL;
4874 }
4875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 // Fill in the end label in all jumps.
4877 while (end_ga.ga_len > 0)
4878 {
4879 isn_T *isn;
4880
4881 --end_ga.ga_len;
4882 isn = ((isn_T *)instr->ga_data)
4883 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4884 isn->isn_arg.jump.jump_where = instr->ga_len;
4885 }
4886 ga_clear(&end_ga);
4887 }
4888
4889 return OK;
4890}
4891
4892/*
4893 * expr4a && expr4a && expr4a logical AND
4894 *
4895 * Produces instructions:
4896 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004897 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004898 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004900 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 * EVAL expr4c Push result of "expr4c"
4902 * end:
4903 */
4904 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004905compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004907 int ppconst_used = ppconst->pp_used;
4908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004910 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 return FAIL;
4912
4913 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004914 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915}
4916
4917/*
4918 * expr3a || expr3b || expr3c logical OR
4919 *
4920 * Produces instructions:
4921 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004922 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004923 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004925 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 * EVAL expr3c Push result of "expr3c"
4927 * end:
4928 */
4929 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004932 int ppconst_used = ppconst->pp_used;
4933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004935 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 return FAIL;
4937
4938 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004939 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940}
4941
4942/*
4943 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004945 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 * JUMP_IF_FALSE alt jump if false
4947 * EVAL expr1a
4948 * JUMP_ALWAYS end
4949 * alt: EVAL expr1b
4950 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004951 *
4952 * Toplevel expression: expr2 ?? expr1
4953 * Produces instructions:
4954 * EVAL expr2 Push result of "expr2"
4955 * JUMP_AND_KEEP_IF_TRUE end jump if true
4956 * EVAL expr1
4957 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 */
4959 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004960compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961{
4962 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004963 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004964 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965
Bram Moolenaar3988f642020-08-27 22:43:03 +02004966 // Ignore all kinds of errors when not producing code.
4967 if (cctx->ctx_skip == SKIP_YES)
4968 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004969 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004970 return OK;
4971 }
4972
Bram Moolenaar61a89812020-05-07 16:58:17 +02004973 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004974 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 return FAIL;
4976
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004977 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 if (*p == '?')
4979 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004980 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 garray_T *instr = &cctx->ctx_instr;
4982 garray_T *stack = &cctx->ctx_type_stack;
4983 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004984 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004986 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004987 int has_const_expr = FALSE;
4988 int const_value = FALSE;
4989 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004991 if (next != NULL)
4992 {
4993 *arg = next_line_from_context(cctx, TRUE);
4994 p = skipwhite(*arg);
4995 }
4996
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004997 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004998 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004999 semsg(_(e_white_space_required_before_and_after_str_at_str),
5000 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005001 return FAIL;
5002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003
Bram Moolenaara5565e42020-05-09 15:44:01 +02005004 if (ppconst->pp_used == ppconst_used + 1)
5005 {
5006 // the condition is a constant, we know whether the ? or the :
5007 // expression is to be evaluated.
5008 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005009 if (op_falsy)
5010 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5011 else
5012 {
5013 int error = FALSE;
5014
5015 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5016 &error);
5017 if (error)
5018 return FAIL;
5019 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005020 cctx->ctx_skip = save_skip == SKIP_YES ||
5021 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5022
5023 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5024 // "left ?? right" and "left" is truthy: produce "left"
5025 generate_ppconst(cctx, ppconst);
5026 else
5027 {
5028 clear_tv(&ppconst->pp_tv[ppconst_used]);
5029 --ppconst->pp_used;
5030 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 }
5032 else
5033 {
5034 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005035 if (op_falsy)
5036 end_idx = instr->ga_len;
5037 generate_JUMP(cctx, op_falsy
5038 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5039 if (op_falsy)
5040 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042
5043 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005044 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005045 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005046 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005047 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048
Bram Moolenaara5565e42020-05-09 15:44:01 +02005049 if (!has_const_expr)
5050 {
5051 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005053 if (!op_falsy)
5054 {
5055 // remember the type and drop it
5056 --stack->ga_len;
5057 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005059 end_idx = instr->ga_len;
5060 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005061
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005062 // jump here from JUMP_IF_FALSE
5063 isn = ((isn_T *)instr->ga_data) + alt_idx;
5064 isn->isn_arg.jump.jump_where = instr->ga_len;
5065 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005068 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005070 // Check for the ":".
5071 p = may_peek_next_line(cctx, *arg, &next);
5072 if (*p != ':')
5073 {
5074 emsg(_(e_missing_colon));
5075 return FAIL;
5076 }
5077 if (next != NULL)
5078 {
5079 *arg = next_line_from_context(cctx, TRUE);
5080 p = skipwhite(*arg);
5081 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005082
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005083 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5084 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005085 semsg(_(e_white_space_required_before_and_after_str_at_str),
5086 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005087 return FAIL;
5088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005090 // evaluate the third expression
5091 if (has_const_expr)
5092 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005093 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005094 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005095 return FAIL;
5096 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5097 return FAIL;
5098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099
Bram Moolenaara5565e42020-05-09 15:44:01 +02005100 if (!has_const_expr)
5101 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005102 type_T **typep;
5103
Bram Moolenaara5565e42020-05-09 15:44:01 +02005104 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105
Bram Moolenaara5565e42020-05-09 15:44:01 +02005106 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005107 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5108 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005109
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005110 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005111 isn = ((isn_T *)instr->ga_data) + end_idx;
5112 isn->isn_arg.jump.jump_where = instr->ga_len;
5113 }
5114
5115 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 }
5117 return OK;
5118}
5119
5120/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005121 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005122 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5123 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005124 */
5125 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005126compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127{
5128 ppconst_T ppconst;
5129
5130 CLEAR_FIELD(ppconst);
5131 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5132 {
5133 clear_ppconst(&ppconst);
5134 return FAIL;
5135 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005136 if (is_const != NULL)
5137 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005138 if (generate_ppconst(cctx, &ppconst) == FAIL)
5139 return FAIL;
5140 return OK;
5141}
5142
5143/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005144 * Toplevel expression.
5145 */
5146 static int
5147compile_expr0(char_u **arg, cctx_T *cctx)
5148{
5149 return compile_expr0_ext(arg, cctx, NULL);
5150}
5151
5152/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 * compile "return [expr]"
5154 */
5155 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005156compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157{
5158 char_u *p = arg;
5159 garray_T *stack = &cctx->ctx_type_stack;
5160 type_T *stack_type;
5161
5162 if (*p != NUL && *p != '|' && *p != '\n')
5163 {
5164 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005165 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 return NULL;
5167
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005168 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005169 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005170 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005171 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005172 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5173 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005174 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005175 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005176 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005177 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005178 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005179 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5180 && stack_type->tt_type != VAR_VOID
5181 && stack_type->tt_type != VAR_UNKNOWN)
5182 {
5183 emsg(_(e_returning_value_in_function_without_return_type));
5184 return NULL;
5185 }
5186 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005187 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005188 return NULL;
5189 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 }
5192 else
5193 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005194 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005195 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005196 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5197 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005199 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 return NULL;
5201 }
5202
5203 // No argument, return zero.
5204 generate_PUSHNR(cctx, 0);
5205 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005206
5207 // Undo any command modifiers.
5208 generate_undo_cmdmods(cctx);
5209
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005210 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 return NULL;
5212
5213 // "return val | endif" is possible
5214 return skipwhite(p);
5215}
5216
5217/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005218 * Get a line from the compilation context, compatible with exarg_T getline().
5219 * Return a pointer to the line in allocated memory.
5220 * Return NULL for end-of-file or some error.
5221 */
5222 static char_u *
5223exarg_getline(
5224 int c UNUSED,
5225 void *cookie,
5226 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005227 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005228{
5229 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005230 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005231
Bram Moolenaar66250c92020-08-20 15:02:42 +02005232 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005233 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005234 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005235 return NULL;
5236 ++cctx->ctx_lnum;
5237 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5238 // Comment lines result in NULL pointers, skip them.
5239 if (p != NULL)
5240 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005241 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005242}
5243
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005244 void
5245fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5246{
5247 eap->getline = exarg_getline;
5248 eap->cookie = cctx;
5249}
5250
Bram Moolenaar04b12692020-05-04 23:24:44 +02005251/*
5252 * Compile a nested :def command.
5253 */
5254 static char_u *
5255compile_nested_function(exarg_T *eap, cctx_T *cctx)
5256{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005257 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005258 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005259 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005260 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005261 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005262 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005263
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005264 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005265 {
5266 emsg(_(e_cannot_use_bang_with_nested_def));
5267 return NULL;
5268 }
5269
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005270 if (*name_start == '/')
5271 {
5272 name_end = skip_regexp(name_start + 1, '/', TRUE);
5273 if (*name_end == '/')
5274 ++name_end;
5275 eap->nextcmd = check_nextcmd(name_end);
5276 }
5277 if (name_end == name_start || *skipwhite(name_end) != '(')
5278 {
5279 if (!ends_excmd2(name_start, name_end))
5280 {
5281 semsg(_(e_invalid_command_str), eap->cmd);
5282 return NULL;
5283 }
5284
5285 // "def" or "def Name": list functions
5286 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5287 return NULL;
5288 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5289 }
5290
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005291 // Only g:Func() can use a namespace.
5292 if (name_start[1] == ':' && !is_global)
5293 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005294 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005295 return NULL;
5296 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005297 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005298 return NULL;
5299
Bram Moolenaar04b12692020-05-04 23:24:44 +02005300 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005301 fill_exarg_from_cctx(eap, cctx);
5302
Bram Moolenaar04b12692020-05-04 23:24:44 +02005303 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005304 lambda_name = vim_strsave(get_lambda_name());
5305 if (lambda_name == NULL)
5306 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005307 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005308
Bram Moolenaar822ba242020-05-24 23:00:18 +02005309 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005310 {
5311 r = eap->skip ? OK : FAIL;
5312 goto theend;
5313 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005314
5315 // copy over the block scope IDs before compiling
5316 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5317 {
5318 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5319
5320 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5321 if (ufunc->uf_block_ids != NULL)
5322 {
5323 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5324 sizeof(int) * block_depth);
5325 ufunc->uf_block_depth = block_depth;
5326 }
5327 }
5328
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005329 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5330 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005331 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005332 {
5333 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005334 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005335 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005336
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005337 if (is_global)
5338 {
5339 char_u *func_name = vim_strnsave(name_start + 2,
5340 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005341
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005342 if (func_name == NULL)
5343 r = FAIL;
5344 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005345 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005346 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005347 lambda_name = NULL;
5348 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005349 }
5350 else
5351 {
5352 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005353 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005354 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005355
Bram Moolenaareef21022020-08-01 22:16:43 +02005356 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005357 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005358 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005359 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005360 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5361 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005362 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005363
5364theend:
5365 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005366 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005367}
5368
5369/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 * Return the length of an assignment operator, or zero if there isn't one.
5371 */
5372 int
5373assignment_len(char_u *p, int *heredoc)
5374{
5375 if (*p == '=')
5376 {
5377 if (p[1] == '<' && p[2] == '<')
5378 {
5379 *heredoc = TRUE;
5380 return 3;
5381 }
5382 return 1;
5383 }
5384 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5385 return 2;
5386 if (STRNCMP(p, "..=", 3) == 0)
5387 return 3;
5388 return 0;
5389}
5390
5391// words that cannot be used as a variable
5392static char *reserved[] = {
5393 "true",
5394 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005395 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 NULL
5397};
5398
Bram Moolenaar752fc692021-01-04 21:57:11 +01005399// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005400typedef enum {
5401 dest_local,
5402 dest_option,
5403 dest_env,
5404 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005405 dest_buffer,
5406 dest_window,
5407 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005408 dest_vimvar,
5409 dest_script,
5410 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005411 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005412} assign_dest_T;
5413
Bram Moolenaar752fc692021-01-04 21:57:11 +01005414// Used by compile_lhs() to store information about the LHS of an assignment
5415// and one argument of ":unlet" with an index.
5416typedef struct {
5417 assign_dest_T lhs_dest; // type of destination
5418
5419 char_u *lhs_name; // allocated name including
5420 // "[expr]" or ".name".
5421 size_t lhs_varlen; // length of the variable without
5422 // "[expr]" or ".name"
5423 char_u *lhs_dest_end; // end of the destination, including
5424 // "[expr]" or ".name".
5425
5426 int lhs_has_index; // has "[expr]" or ".name"
5427
5428 int lhs_new_local; // create new local variable
5429 int lhs_opt_flags; // for when destination is an option
5430 int lhs_vimvaridx; // for when destination is a v:var
5431
5432 lvar_T lhs_local_lvar; // used for existing local destination
5433 lvar_T lhs_arg_lvar; // used for argument destination
5434 lvar_T *lhs_lvar; // points to destination lvar
5435 int lhs_scriptvar_sid;
5436 int lhs_scriptvar_idx;
5437
5438 int lhs_has_type; // type was specified
5439 type_T *lhs_type;
5440 type_T *lhs_member_type;
5441} lhs_T;
5442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005444 * Generate the load instruction for "name".
5445 */
5446 static void
5447generate_loadvar(
5448 cctx_T *cctx,
5449 assign_dest_T dest,
5450 char_u *name,
5451 lvar_T *lvar,
5452 type_T *type)
5453{
5454 switch (dest)
5455 {
5456 case dest_option:
5457 // TODO: check the option exists
5458 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5459 break;
5460 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005461 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5462 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5463 else
5464 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005465 break;
5466 case dest_buffer:
5467 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5468 break;
5469 case dest_window:
5470 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5471 break;
5472 case dest_tab:
5473 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5474 break;
5475 case dest_script:
5476 compile_load_scriptvar(cctx,
5477 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5478 break;
5479 case dest_env:
5480 // Include $ in the name here
5481 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5482 break;
5483 case dest_reg:
5484 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5485 break;
5486 case dest_vimvar:
5487 generate_LOADV(cctx, name + 2, TRUE);
5488 break;
5489 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005490 if (lvar->lv_from_outer > 0)
5491 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5492 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005493 else
5494 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5495 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005496 case dest_expr:
5497 // list or dict value should already be on the stack.
5498 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005499 }
5500}
5501
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005502/*
5503 * Skip over "[expr]" or ".member".
5504 * Does not check for any errors.
5505 */
5506 static char_u *
5507skip_index(char_u *start)
5508{
5509 char_u *p = start;
5510
5511 if (*p == '[')
5512 {
5513 p = skipwhite(p + 1);
5514 (void)skip_expr(&p, NULL);
5515 p = skipwhite(p);
5516 if (*p == ']')
5517 return p + 1;
5518 return p;
5519 }
5520 // if (*p == '.')
5521 return to_name_end(p + 1, TRUE);
5522}
5523
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005524 void
5525vim9_declare_error(char_u *name)
5526{
5527 char *scope = "";
5528
5529 switch (*name)
5530 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005531 case 'g': scope = _("global"); break;
5532 case 'b': scope = _("buffer"); break;
5533 case 'w': scope = _("window"); break;
5534 case 't': scope = _("tab"); break;
5535 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005536 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005537 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005538 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005539 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005540 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005541 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005542 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005543 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005544 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005545}
5546
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005547/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005548 * For one assignment figure out the type of destination. Return it in "dest".
5549 * When not recognized "dest" is not set.
5550 * For an option "opt_flags" is set.
5551 * For a v:var "vimvaridx" is set.
5552 * "type" is set to the destination type if known, unchanted otherwise.
5553 * Return FAIL if an error message was given.
5554 */
5555 static int
5556get_var_dest(
5557 char_u *name,
5558 assign_dest_T *dest,
5559 int cmdidx,
5560 int *opt_flags,
5561 int *vimvaridx,
5562 type_T **type,
5563 cctx_T *cctx)
5564{
5565 char_u *p;
5566
5567 if (*name == '&')
5568 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005569 int cc;
5570 long numval;
5571 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005572
5573 *dest = dest_option;
5574 if (cmdidx == CMD_final || cmdidx == CMD_const)
5575 {
5576 emsg(_(e_const_option));
5577 return FAIL;
5578 }
5579 p = name;
5580 p = find_option_end(&p, opt_flags);
5581 if (p == NULL)
5582 {
5583 // cannot happen?
5584 emsg(_(e_letunexp));
5585 return FAIL;
5586 }
5587 cc = *p;
5588 *p = NUL;
5589 opt_type = get_option_value(skip_option_env_lead(name),
5590 &numval, NULL, *opt_flags);
5591 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005592 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005593 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005594 case gov_unknown:
5595 semsg(_(e_unknown_option), name);
5596 return FAIL;
5597 case gov_string:
5598 case gov_hidden_string:
5599 *type = &t_string;
5600 break;
5601 case gov_bool:
5602 case gov_hidden_bool:
5603 *type = &t_bool;
5604 break;
5605 case gov_number:
5606 case gov_hidden_number:
5607 *type = &t_number;
5608 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005609 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005610 }
5611 else if (*name == '$')
5612 {
5613 *dest = dest_env;
5614 *type = &t_string;
5615 }
5616 else if (*name == '@')
5617 {
5618 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5619 {
5620 emsg_invreg(name[1]);
5621 return FAIL;
5622 }
5623 *dest = dest_reg;
5624 *type = &t_string;
5625 }
5626 else if (STRNCMP(name, "g:", 2) == 0)
5627 {
5628 *dest = dest_global;
5629 }
5630 else if (STRNCMP(name, "b:", 2) == 0)
5631 {
5632 *dest = dest_buffer;
5633 }
5634 else if (STRNCMP(name, "w:", 2) == 0)
5635 {
5636 *dest = dest_window;
5637 }
5638 else if (STRNCMP(name, "t:", 2) == 0)
5639 {
5640 *dest = dest_tab;
5641 }
5642 else if (STRNCMP(name, "v:", 2) == 0)
5643 {
5644 typval_T *vtv;
5645 int di_flags;
5646
5647 *vimvaridx = find_vim_var(name + 2, &di_flags);
5648 if (*vimvaridx < 0)
5649 {
5650 semsg(_(e_variable_not_found_str), name);
5651 return FAIL;
5652 }
5653 // We use the current value of "sandbox" here, is that OK?
5654 if (var_check_ro(di_flags, name, FALSE))
5655 return FAIL;
5656 *dest = dest_vimvar;
5657 vtv = get_vim_var_tv(*vimvaridx);
5658 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5659 }
5660 return OK;
5661}
5662
5663/*
5664 * Generate a STORE instruction for "dest", not being "dest_local".
5665 * Return FAIL when out of memory.
5666 */
5667 static int
5668generate_store_var(
5669 cctx_T *cctx,
5670 assign_dest_T dest,
5671 int opt_flags,
5672 int vimvaridx,
5673 int scriptvar_idx,
5674 int scriptvar_sid,
5675 type_T *type,
5676 char_u *name)
5677{
5678 switch (dest)
5679 {
5680 case dest_option:
5681 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5682 opt_flags);
5683 case dest_global:
5684 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005685 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5686 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005687 case dest_buffer:
5688 // include b: with the name, easier to execute that way
5689 return generate_STORE(cctx, ISN_STOREB, 0, name);
5690 case dest_window:
5691 // include w: with the name, easier to execute that way
5692 return generate_STORE(cctx, ISN_STOREW, 0, name);
5693 case dest_tab:
5694 // include t: with the name, easier to execute that way
5695 return generate_STORE(cctx, ISN_STORET, 0, name);
5696 case dest_env:
5697 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5698 case dest_reg:
5699 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5700 case dest_vimvar:
5701 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5702 case dest_script:
5703 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005704 // "s:" may be included in the name.
5705 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005706 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005707 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5708 scriptvar_sid, scriptvar_idx, type);
5709 case dest_local:
5710 case dest_expr:
5711 // cannot happen
5712 break;
5713 }
5714 return FAIL;
5715}
5716
Bram Moolenaar752fc692021-01-04 21:57:11 +01005717 static int
5718is_decl_command(int cmdidx)
5719{
5720 return cmdidx == CMD_let || cmdidx == CMD_var
5721 || cmdidx == CMD_final || cmdidx == CMD_const;
5722}
5723
5724/*
5725 * Figure out the LHS type and other properties for an assignment or one item
5726 * of ":unlet" with an index.
5727 * Returns OK or FAIL.
5728 */
5729 static int
5730compile_lhs(
5731 char_u *var_start,
5732 lhs_T *lhs,
5733 int cmdidx,
5734 int heredoc,
5735 int oplen,
5736 cctx_T *cctx)
5737{
5738 char_u *var_end;
5739 int is_decl = is_decl_command(cmdidx);
5740
5741 CLEAR_POINTER(lhs);
5742 lhs->lhs_dest = dest_local;
5743 lhs->lhs_vimvaridx = -1;
5744 lhs->lhs_scriptvar_idx = -1;
5745
5746 // "dest_end" is the end of the destination, including "[expr]" or
5747 // ".name".
5748 // "var_end" is the end of the variable/option/etc. name.
5749 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5750 if (*var_start == '@')
5751 var_end = var_start + 2;
5752 else
5753 {
5754 // skip over the leading "&", "&l:", "&g:" and "$"
5755 var_end = skip_option_env_lead(var_start);
5756 var_end = to_name_end(var_end, TRUE);
5757 }
5758
5759 // "a: type" is declaring variable "a" with a type, not dict "a:".
5760 if (is_decl && lhs->lhs_dest_end == var_start + 2
5761 && lhs->lhs_dest_end[-1] == ':')
5762 --lhs->lhs_dest_end;
5763 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5764 --var_end;
5765
5766 // compute the length of the destination without "[expr]" or ".name"
5767 lhs->lhs_varlen = var_end - var_start;
5768 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5769 if (lhs->lhs_name == NULL)
5770 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005771
5772 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5773 // Something follows after the variable: "var[idx]" or "var.key".
5774 lhs->lhs_has_index = TRUE;
5775
Bram Moolenaar752fc692021-01-04 21:57:11 +01005776 if (heredoc)
5777 lhs->lhs_type = &t_list_string;
5778 else
5779 lhs->lhs_type = &t_any;
5780
5781 if (cctx->ctx_skip != SKIP_YES)
5782 {
5783 int declare_error = FALSE;
5784
5785 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5786 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5787 &lhs->lhs_type, cctx) == FAIL)
5788 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005789 if (lhs->lhs_dest != dest_local
5790 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005791 {
5792 // Specific kind of variable recognized.
5793 declare_error = is_decl;
5794 }
5795 else
5796 {
5797 int idx;
5798
5799 // No specific kind of variable recognized, just a name.
5800 for (idx = 0; reserved[idx] != NULL; ++idx)
5801 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5802 {
5803 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5804 return FAIL;
5805 }
5806
5807
5808 if (lookup_local(var_start, lhs->lhs_varlen,
5809 &lhs->lhs_local_lvar, cctx) == OK)
5810 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5811 else
5812 {
5813 CLEAR_FIELD(lhs->lhs_arg_lvar);
5814 if (arg_exists(var_start, lhs->lhs_varlen,
5815 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5816 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5817 {
5818 if (is_decl)
5819 {
5820 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5821 return FAIL;
5822 }
5823 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5824 }
5825 }
5826 if (lhs->lhs_lvar != NULL)
5827 {
5828 if (is_decl)
5829 {
5830 semsg(_(e_variable_already_declared), lhs->lhs_name);
5831 return FAIL;
5832 }
5833 }
5834 else
5835 {
5836 int script_namespace = lhs->lhs_varlen > 1
5837 && STRNCMP(var_start, "s:", 2) == 0;
5838 int script_var = (script_namespace
5839 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005840 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005841 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005842 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005843 imported_T *import =
5844 find_imported(var_start, lhs->lhs_varlen, cctx);
5845
5846 if (script_namespace || script_var || import != NULL)
5847 {
5848 char_u *rawname = lhs->lhs_name
5849 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5850
5851 if (is_decl)
5852 {
5853 if (script_namespace)
5854 semsg(_(e_cannot_declare_script_variable_in_function),
5855 lhs->lhs_name);
5856 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005857 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005858 lhs->lhs_name);
5859 return FAIL;
5860 }
5861 else if (cctx->ctx_ufunc->uf_script_ctx_version
5862 == SCRIPT_VERSION_VIM9
5863 && script_namespace
5864 && !script_var && import == NULL)
5865 {
5866 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5867 return FAIL;
5868 }
5869
5870 lhs->lhs_dest = dest_script;
5871
5872 // existing script-local variables should have a type
5873 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5874 if (import != NULL)
5875 lhs->lhs_scriptvar_sid = import->imp_sid;
5876 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5877 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005878 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005879 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005880 lhs->lhs_scriptvar_sid, rawname,
5881 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5882 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005883 if (lhs->lhs_scriptvar_idx >= 0)
5884 {
5885 scriptitem_T *si = SCRIPT_ITEM(
5886 lhs->lhs_scriptvar_sid);
5887 svar_T *sv =
5888 ((svar_T *)si->sn_var_vals.ga_data)
5889 + lhs->lhs_scriptvar_idx;
5890 lhs->lhs_type = sv->sv_type;
5891 }
5892 }
5893 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005894 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005895 == FAIL)
5896 return FAIL;
5897 }
5898 }
5899
5900 if (declare_error)
5901 {
5902 vim9_declare_error(lhs->lhs_name);
5903 return FAIL;
5904 }
5905 }
5906
5907 // handle "a:name" as a name, not index "name" on "a"
5908 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5909 var_end = lhs->lhs_dest_end;
5910
5911 if (lhs->lhs_dest != dest_option)
5912 {
5913 if (is_decl && *var_end == ':')
5914 {
5915 char_u *p;
5916
5917 // parse optional type: "let var: type = expr"
5918 if (!VIM_ISWHITE(var_end[1]))
5919 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005920 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005921 return FAIL;
5922 }
5923 p = skipwhite(var_end + 1);
5924 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5925 if (lhs->lhs_type == NULL)
5926 return FAIL;
5927 lhs->lhs_has_type = TRUE;
5928 }
5929 else if (lhs->lhs_lvar != NULL)
5930 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5931 }
5932
Bram Moolenaare42939a2021-04-05 17:11:17 +02005933 if (oplen == 3 && !heredoc
5934 && lhs->lhs_dest != dest_global
5935 && !lhs->lhs_has_index
5936 && lhs->lhs_type->tt_type != VAR_STRING
5937 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005938 {
5939 emsg(_(e_can_only_concatenate_to_string));
5940 return FAIL;
5941 }
5942
5943 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5944 && cctx->ctx_skip != SKIP_YES)
5945 {
5946 if (oplen > 1 && !heredoc)
5947 {
5948 // +=, /=, etc. require an existing variable
5949 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5950 return FAIL;
5951 }
5952 if (!is_decl)
5953 {
5954 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5955 return FAIL;
5956 }
5957
Bram Moolenaar3f327882021-03-17 20:56:38 +01005958 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005959 if ((lhs->lhs_type->tt_type == VAR_FUNC
5960 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005961 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005962 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005963
5964 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005965 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5966 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5967 if (lhs->lhs_lvar == NULL)
5968 return FAIL;
5969 lhs->lhs_new_local = TRUE;
5970 }
5971
5972 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005973 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005974 {
5975 // Something follows after the variable: "var[idx]" or "var.key".
5976 // TODO: should we also handle "->func()" here?
5977 if (is_decl)
5978 {
5979 emsg(_(e_cannot_use_index_when_declaring_variable));
5980 return FAIL;
5981 }
5982
5983 if (var_start[lhs->lhs_varlen] == '['
5984 || var_start[lhs->lhs_varlen] == '.')
5985 {
5986 char_u *after = var_start + lhs->lhs_varlen;
5987 char_u *p;
5988
5989 // Only the last index is used below, if there are others
5990 // before it generate code for the expression. Thus for
5991 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5992 for (;;)
5993 {
5994 p = skip_index(after);
5995 if (*p != '[' && *p != '.')
5996 break;
5997 after = p;
5998 }
5999 if (after > var_start + lhs->lhs_varlen)
6000 {
6001 lhs->lhs_varlen = after - var_start;
6002 lhs->lhs_dest = dest_expr;
6003 // We don't know the type before evaluating the expression,
6004 // use "any" until then.
6005 lhs->lhs_type = &t_any;
6006 }
6007
Bram Moolenaar752fc692021-01-04 21:57:11 +01006008 if (lhs->lhs_type->tt_member == NULL)
6009 lhs->lhs_member_type = &t_any;
6010 else
6011 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6012 }
6013 else
6014 {
6015 semsg("Not supported yet: %s", var_start);
6016 return FAIL;
6017 }
6018 }
6019 return OK;
6020}
6021
6022/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006023 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6024 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006025 */
6026 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006027compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006028 char_u *var_start,
6029 lhs_T *lhs,
6030 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006031 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006032 cctx_T *cctx)
6033{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006034 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006035 char_u *p;
6036 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006037
Bram Moolenaar752fc692021-01-04 21:57:11 +01006038 p = var_start + varlen;
6039 if (*p == '[')
6040 {
6041 p = skipwhite(p + 1);
6042 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006043
6044 if (r == OK && *skipwhite(p) == ':')
6045 {
6046 // unlet var[idx : idx]
6047 if (is_assign)
6048 {
6049 semsg(_(e_cannot_use_range_with_assignment_str), p);
6050 return FAIL;
6051 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006052 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006053 p = skipwhite(p);
6054 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6055 {
6056 semsg(_(e_white_space_required_before_and_after_str_at_str),
6057 ":", p);
6058 return FAIL;
6059 }
6060 p = skipwhite(p + 1);
6061 r = compile_expr0(&p, cctx);
6062 }
6063
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064 if (r == OK && *skipwhite(p) != ']')
6065 {
6066 // this should not happen
6067 emsg(_(e_missbrac));
6068 r = FAIL;
6069 }
6070 }
6071 else // if (*p == '.')
6072 {
6073 char_u *key_end = to_name_end(p + 1, TRUE);
6074 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6075
6076 r = generate_PUSHS(cctx, key);
6077 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006078 return r;
6079}
6080
6081/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006082 * For a LHS with an index, load the variable to be indexed.
6083 */
6084 static int
6085compile_load_lhs(
6086 lhs_T *lhs,
6087 char_u *var_start,
6088 type_T *rhs_type,
6089 cctx_T *cctx)
6090{
6091 if (lhs->lhs_dest == dest_expr)
6092 {
6093 size_t varlen = lhs->lhs_varlen;
6094 int c = var_start[varlen];
6095 char_u *p = var_start;
6096 garray_T *stack = &cctx->ctx_type_stack;
6097
6098 // Evaluate "ll[expr]" of "ll[expr][idx]"
6099 var_start[varlen] = NUL;
6100 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6101 {
6102 // this should not happen
6103 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006104 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006105 return FAIL;
6106 }
6107 var_start[varlen] = c;
6108
6109 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6110 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6111 // now we can properly check the type
6112 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6113 && rhs_type != &t_void
6114 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6115 FALSE, FALSE) == FAIL)
6116 return FAIL;
6117 }
6118 else
6119 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6120 lhs->lhs_lvar, lhs->lhs_type);
6121 return OK;
6122}
6123
6124/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006125 * Assignment to a list or dict member, or ":unlet" for the item, using the
6126 * information in "lhs".
6127 * Returns OK or FAIL.
6128 */
6129 static int
6130compile_assign_unlet(
6131 char_u *var_start,
6132 lhs_T *lhs,
6133 int is_assign,
6134 type_T *rhs_type,
6135 cctx_T *cctx)
6136{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006137 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006138 garray_T *stack = &cctx->ctx_type_stack;
6139 int range = FALSE;
6140
6141 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006142 return FAIL;
6143
6144 if (lhs->lhs_type == &t_any)
6145 {
6146 // Index on variable of unknown type: check at runtime.
6147 dest_type = VAR_ANY;
6148 }
6149 else
6150 {
6151 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006152 if (dest_type == VAR_DICT && range)
6153 {
6154 emsg(e_cannot_use_range_with_dictionary);
6155 return FAIL;
6156 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6158 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006159 if (dest_type == VAR_LIST)
6160 {
6161 if (range
6162 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006163 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006164 return FAIL;
6165 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6166 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6167 return FAIL;
6168 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 }
6170
6171 // Load the dict or list. On the stack we then have:
6172 // - value (for assignment, not for :unlet)
6173 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006174 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006176 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6177 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006178
6179 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6180 {
6181 if (is_assign)
6182 {
6183 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6184
6185 if (isn == NULL)
6186 return FAIL;
6187 isn->isn_arg.vartype = dest_type;
6188 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006189 else if (range)
6190 {
6191 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6192 return FAIL;
6193 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006194 else
6195 {
6196 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6197 return FAIL;
6198 }
6199 }
6200 else
6201 {
6202 emsg(_(e_indexable_type_required));
6203 return FAIL;
6204 }
6205
6206 return OK;
6207}
6208
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006209/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006210 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006211 * "let name"
6212 * "var name = expr"
6213 * "final name = expr"
6214 * "const name = expr"
6215 * "name = expr"
6216 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006217 * Return NULL for an error.
6218 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 */
6220 static char_u *
6221compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6222{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006223 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006225 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 char_u *ret = NULL;
6227 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006228 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006231 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 int oplen = 0;
6234 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006235 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006237 int is_decl = is_decl_command(cmdidx);
6238 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006239 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006240
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006241 // Skip over the "var" or "[var, var]" to get to any "=".
6242 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6243 if (p == NULL)
6244 return *arg == '[' ? arg : NULL;
6245
6246 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006248 // TODO: should we allow this, and figure out type inference from list
6249 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006250 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 return NULL;
6252 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006253 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 sp = p;
6256 p = skipwhite(p);
6257 op = p;
6258 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006259
6260 if (var_count > 0 && oplen == 0)
6261 // can be something like "[1, 2]->func()"
6262 return arg;
6263
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006264 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006266 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006267 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006268 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 if (heredoc)
6271 {
6272 list_T *l;
6273 listitem_T *li;
6274
6275 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006276 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006278 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006279 if (l == NULL)
6280 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281
Bram Moolenaar078269b2020-09-21 20:35:55 +02006282 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006284 // Push each line and the create the list.
6285 FOR_ALL_LIST_ITEMS(l, li)
6286 {
6287 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6288 li->li_tv.vval.v_string = NULL;
6289 }
6290 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 list_free(l);
6293 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006294 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006296 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006298 char_u *wp;
6299
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300 // for "[var, var] = expr" evaluate the expression here, loop over the
6301 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006302 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006303
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006304 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006305 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006306 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006307 if (compile_expr0(&p, cctx) == FAIL)
6308 return NULL;
6309 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006311 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006313 type_T *stacktype;
6314
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006315 stacktype = stack->ga_len == 0 ? &t_void
6316 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006319 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006320 goto theend;
6321 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006322 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006323 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006324 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006325 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006326 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6327 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006328 if (stacktype->tt_member != NULL)
6329 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330 }
6331 }
6332
6333 /*
6334 * Loop over variables in "[var, var] = expr".
6335 * For "var = expr" and "let var: type" this is done only once.
6336 */
6337 if (var_count > 0)
6338 var_start = skipwhite(arg + 1); // skip over the "["
6339 else
6340 var_start = arg;
6341 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6342 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006343 int instr_count = -1;
6344
Bram Moolenaar752fc692021-01-04 21:57:11 +01006345 vim_free(lhs.lhs_name);
6346
6347 /*
6348 * Figure out the LHS type and other properties.
6349 */
6350 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6351 goto theend;
6352
6353 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006354 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006355 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006356 goto theend;
6357 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006358 if (!is_decl && lhs.lhs_lvar != NULL
6359 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006360 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006362 goto theend;
6363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006364
6365 if (!heredoc)
6366 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006367 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006368 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006369 if (oplen > 0 && var_count == 0)
6370 {
6371 // skip over the "=" and the expression
6372 p = skipwhite(op + oplen);
6373 compile_expr0(&p, cctx);
6374 }
6375 }
6376 else if (oplen > 0)
6377 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006378 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006379 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006380
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006381 // For "var = expr" evaluate the expression.
6382 if (var_count == 0)
6383 {
6384 int r;
6385
6386 // for "+=", "*=", "..=" etc. first load the current value
6387 if (*op != '=')
6388 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006389 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006390
Bram Moolenaar752fc692021-01-04 21:57:11 +01006391 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006393 int range = FALSE;
6394
6395 // Get member from list or dict. First compile the
6396 // index value.
6397 if (compile_assign_index(var_start, &lhs,
6398 TRUE, &range, cctx) == FAIL)
6399 goto theend;
6400
6401 // Get the member.
6402 if (compile_member(FALSE, cctx) == FAIL)
6403 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006404 }
6405 }
6406
6407 // Compile the expression. Temporarily hide the new local
6408 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006409 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006410 --cctx->ctx_locals.ga_len;
6411 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006412 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006413 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006414 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006415 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006416 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006417 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006418 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006419 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006420 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006421 ++cctx->ctx_locals.ga_len;
6422 if (r == FAIL)
6423 goto theend;
6424 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006425 else if (semicolon && var_idx == var_count - 1)
6426 {
6427 // For "[var; var] = expr" get the rest of the list
6428 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6429 goto theend;
6430 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006431 else
6432 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006433 // For "[var, var] = expr" get the "var_idx" item from the
6434 // list.
6435 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006436 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006437 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006438
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006439 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006440 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006441 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006442 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006443 if ((rhs_type->tt_type == VAR_FUNC
6444 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006445 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006446 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006447 goto theend;
6448
Bram Moolenaar752fc692021-01-04 21:57:11 +01006449 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006450 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006451 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006452 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006453 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006454 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006455 }
6456 else
6457 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006458 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006459 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006460 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006461 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006462 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006463 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006464 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006465 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006466 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006468 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006469 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006470 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006471 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006473
Bram Moolenaar77709b12021-04-03 21:01:01 +02006474 // Without operator check type here, otherwise below.
6475 // Use the line number of the assignment.
6476 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 if (lhs.lhs_has_index)
6478 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006479 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006480 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006481 goto theend;
6482 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006483 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006484 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006485 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006486 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006487 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006488 else if (cmdidx == CMD_final)
6489 {
6490 emsg(_(e_final_requires_a_value));
6491 goto theend;
6492 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006493 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006495 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006496 goto theend;
6497 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006498 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006499 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006500 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006501 goto theend;
6502 }
6503 else
6504 {
6505 // variables are always initialized
6506 if (ga_grow(instr, 1) == FAIL)
6507 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006508 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006509 {
6510 case VAR_BOOL:
6511 generate_PUSHBOOL(cctx, VVAL_FALSE);
6512 break;
6513 case VAR_FLOAT:
6514#ifdef FEAT_FLOAT
6515 generate_PUSHF(cctx, 0.0);
6516#endif
6517 break;
6518 case VAR_STRING:
6519 generate_PUSHS(cctx, NULL);
6520 break;
6521 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006522 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006523 break;
6524 case VAR_FUNC:
6525 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6526 break;
6527 case VAR_LIST:
6528 generate_NEWLIST(cctx, 0);
6529 break;
6530 case VAR_DICT:
6531 generate_NEWDICT(cctx, 0);
6532 break;
6533 case VAR_JOB:
6534 generate_PUSHJOB(cctx, NULL);
6535 break;
6536 case VAR_CHANNEL:
6537 generate_PUSHCHANNEL(cctx, NULL);
6538 break;
6539 case VAR_NUMBER:
6540 case VAR_UNKNOWN:
6541 case VAR_ANY:
6542 case VAR_PARTIAL:
6543 case VAR_VOID:
6544 case VAR_SPECIAL: // cannot happen
6545 generate_PUSHNR(cctx, 0);
6546 break;
6547 }
6548 }
6549 if (var_count == 0)
6550 end = p;
6551 }
6552
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006553 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006554 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006555 break;
6556
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006557 if (oplen > 0 && *op != '=')
6558 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006559 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006560 type_T *stacktype;
6561
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006562 if (*op == '.')
6563 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006564 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006565 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006566 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006567 if (
6568#ifdef FEAT_FLOAT
6569 // If variable is float operation with number is OK.
6570 !(expected == &t_float && stacktype == &t_number) &&
6571#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006572 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006573 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006574 goto theend;
6575
6576 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006577 {
6578 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6579 goto theend;
6580 }
6581 else if (*op == '+')
6582 {
6583 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006584 operator_type(lhs.lhs_member_type, stacktype),
6585 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006586 goto theend;
6587 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006588 else if (generate_two_op(cctx, op) == FAIL)
6589 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591
Bram Moolenaar752fc692021-01-04 21:57:11 +01006592 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006593 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006594 // Use the info in "lhs" to store the value at the index in the
6595 // list or dict.
6596 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6597 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006598 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006599 }
6600 else
6601 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006602 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006603 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006604 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006605 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006606 generate_LOCKCONST(cctx);
6607
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006608 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006609 && (lhs.lhs_type->tt_type == VAR_DICT
6610 || lhs.lhs_type->tt_type == VAR_LIST)
6611 && lhs.lhs_type->tt_member != NULL
6612 && lhs.lhs_type->tt_member != &t_any
6613 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006614 // Set the type in the list or dict, so that it can be checked,
6615 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006616 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006617
Bram Moolenaar752fc692021-01-04 21:57:11 +01006618 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006619 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 if (generate_store_var(cctx, lhs.lhs_dest,
6621 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6622 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6623 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006624 goto theend;
6625 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006626 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006627 {
6628 isn_T *isn = ((isn_T *)instr->ga_data)
6629 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006630
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006631 // optimization: turn "var = 123" from ISN_PUSHNR +
6632 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006633 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006634 && instr->ga_len == instr_count + 1
6635 && isn->isn_type == ISN_PUSHNR)
6636 {
6637 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006638
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006639 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006640 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006641 isn->isn_arg.storenr.stnr_val = val;
6642 if (stack->ga_len > 0)
6643 --stack->ga_len;
6644 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006645 else if (lhs.lhs_lvar->lv_from_outer > 0)
6646 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6647 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006648 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006649 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006650 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006651 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006652
6653 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006655 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006656
6657 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006658 if (var_count > 0 && !semicolon)
6659 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006660 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006661 goto theend;
6662 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006663
Bram Moolenaarb2097502020-07-19 17:17:02 +02006664 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665
6666theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006667 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006668 return ret;
6669}
6670
6671/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006672 * Check for an assignment at "eap->cmd", compile it if found.
6673 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6674 */
6675 static int
6676may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6677{
6678 char_u *pskip;
6679 char_u *p;
6680
6681 // Assuming the command starts with a variable or function name,
6682 // find what follows.
6683 // Skip over "var.member", "var[idx]" and the like.
6684 // Also "&opt = val", "$ENV = val" and "@r = val".
6685 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6686 ? eap->cmd + 1 : eap->cmd;
6687 p = to_name_end(pskip, TRUE);
6688 if (p > eap->cmd && *p != NUL)
6689 {
6690 char_u *var_end;
6691 int oplen;
6692 int heredoc;
6693
6694 if (eap->cmd[0] == '@')
6695 var_end = eap->cmd + 2;
6696 else
6697 var_end = find_name_end(pskip, NULL, NULL,
6698 FNE_CHECK_START | FNE_INCL_BR);
6699 oplen = assignment_len(skipwhite(var_end), &heredoc);
6700 if (oplen > 0)
6701 {
6702 size_t len = p - eap->cmd;
6703
6704 // Recognize an assignment if we recognize the variable
6705 // name:
6706 // "g:var = expr"
6707 // "local = expr" where "local" is a local var.
6708 // "script = expr" where "script" is a script-local var.
6709 // "import = expr" where "import" is an imported var
6710 // "&opt = expr"
6711 // "$ENV = expr"
6712 // "@r = expr"
6713 if (*eap->cmd == '&'
6714 || *eap->cmd == '$'
6715 || *eap->cmd == '@'
6716 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006717 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006718 {
6719 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6720 if (*line == NULL || *line == eap->cmd)
6721 return FAIL;
6722 return OK;
6723 }
6724 }
6725 }
6726
6727 if (*eap->cmd == '[')
6728 {
6729 // [var, var] = expr
6730 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6731 if (*line == NULL)
6732 return FAIL;
6733 if (*line != eap->cmd)
6734 return OK;
6735 }
6736 return NOTDONE;
6737}
6738
6739/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006740 * Check if "name" can be "unlet".
6741 */
6742 int
6743check_vim9_unlet(char_u *name)
6744{
6745 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6746 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006747 // "unlet s:var" is allowed in legacy script.
6748 if (*name == 's' && !script_is_vim9())
6749 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006750 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006751 return FAIL;
6752 }
6753 return OK;
6754}
6755
6756/*
6757 * Callback passed to ex_unletlock().
6758 */
6759 static int
6760compile_unlet(
6761 lval_T *lvp,
6762 char_u *name_end,
6763 exarg_T *eap,
6764 int deep UNUSED,
6765 void *coookie)
6766{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006767 cctx_T *cctx = coookie;
6768 char_u *p = lvp->ll_name;
6769 int cc = *name_end;
6770 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006771
Bram Moolenaar752fc692021-01-04 21:57:11 +01006772 if (cctx->ctx_skip == SKIP_YES)
6773 return OK;
6774
6775 *name_end = NUL;
6776 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006777 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006778 // :unlet $ENV_VAR
6779 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6780 }
6781 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6782 {
6783 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006784
Bram Moolenaar752fc692021-01-04 21:57:11 +01006785 // This is similar to assigning: lookup the list/dict, compile the
6786 // idx/key. Then instead of storing the value unlet the item.
6787 // unlet {list}[idx]
6788 // unlet {dict}[key] dict.key
6789 //
6790 // Figure out the LHS type and other properties.
6791 //
6792 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6793
6794 // : unlet an indexed item
6795 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006796 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006797 iemsg("called compile_lhs() without an index");
6798 ret = FAIL;
6799 }
6800 else
6801 {
6802 // Use the info in "lhs" to unlet the item at the index in the
6803 // list or dict.
6804 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006805 }
6806
Bram Moolenaar752fc692021-01-04 21:57:11 +01006807 vim_free(lhs.lhs_name);
6808 }
6809 else if (check_vim9_unlet(p) == FAIL)
6810 {
6811 ret = FAIL;
6812 }
6813 else
6814 {
6815 // Normal name. Only supports g:, w:, t: and b: namespaces.
6816 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006817 }
6818
Bram Moolenaar752fc692021-01-04 21:57:11 +01006819 *name_end = cc;
6820 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006821}
6822
6823/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006824 * Callback passed to ex_unletlock().
6825 */
6826 static int
6827compile_lock_unlock(
6828 lval_T *lvp,
6829 char_u *name_end,
6830 exarg_T *eap,
6831 int deep UNUSED,
6832 void *coookie)
6833{
6834 cctx_T *cctx = coookie;
6835 int cc = *name_end;
6836 char_u *p = lvp->ll_name;
6837 int ret = OK;
6838 size_t len;
6839 char_u *buf;
6840
6841 if (cctx->ctx_skip == SKIP_YES)
6842 return OK;
6843
6844 // Cannot use :lockvar and :unlockvar on local variables.
6845 if (p[1] != ':')
6846 {
6847 char_u *end = skip_var_one(p, FALSE);
6848
6849 if (lookup_local(p, end - p, NULL, cctx) == OK)
6850 {
6851 emsg(_(e_cannot_lock_unlock_local_variable));
6852 return FAIL;
6853 }
6854 }
6855
6856 // Checking is done at runtime.
6857 *name_end = NUL;
6858 len = name_end - p + 20;
6859 buf = alloc(len);
6860 if (buf == NULL)
6861 ret = FAIL;
6862 else
6863 {
6864 vim_snprintf((char *)buf, len, "%s %s",
6865 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6866 p);
6867 ret = generate_EXEC(cctx, buf);
6868
6869 vim_free(buf);
6870 *name_end = cc;
6871 }
6872 return ret;
6873}
6874
6875/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006876 * compile "unlet var", "lock var" and "unlock var"
6877 * "arg" points to "var".
6878 */
6879 static char_u *
6880compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6881{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006882 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6883 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6884 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006885 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6886}
6887
6888/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006889 * Compile an :import command.
6890 */
6891 static char_u *
6892compile_import(char_u *arg, cctx_T *cctx)
6893{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006894 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895}
6896
6897/*
6898 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6899 */
6900 static int
6901compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6902{
6903 garray_T *instr = &cctx->ctx_instr;
6904 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6905
6906 if (endlabel == NULL)
6907 return FAIL;
6908 endlabel->el_next = *el;
6909 *el = endlabel;
6910 endlabel->el_end_label = instr->ga_len;
6911
6912 generate_JUMP(cctx, when, 0);
6913 return OK;
6914}
6915
6916 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006917compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918{
6919 garray_T *instr = &cctx->ctx_instr;
6920
6921 while (*el != NULL)
6922 {
6923 endlabel_T *cur = (*el);
6924 isn_T *isn;
6925
6926 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006927 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006928 *el = cur->el_next;
6929 vim_free(cur);
6930 }
6931}
6932
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006933 static void
6934compile_free_jump_to_end(endlabel_T **el)
6935{
6936 while (*el != NULL)
6937 {
6938 endlabel_T *cur = (*el);
6939
6940 *el = cur->el_next;
6941 vim_free(cur);
6942 }
6943}
6944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945/*
6946 * Create a new scope and set up the generic items.
6947 */
6948 static scope_T *
6949new_scope(cctx_T *cctx, scopetype_T type)
6950{
6951 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6952
6953 if (scope == NULL)
6954 return NULL;
6955 scope->se_outer = cctx->ctx_scope;
6956 cctx->ctx_scope = scope;
6957 scope->se_type = type;
6958 scope->se_local_count = cctx->ctx_locals.ga_len;
6959 return scope;
6960}
6961
6962/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006963 * Free the current scope and go back to the outer scope.
6964 */
6965 static void
6966drop_scope(cctx_T *cctx)
6967{
6968 scope_T *scope = cctx->ctx_scope;
6969
6970 if (scope == NULL)
6971 {
6972 iemsg("calling drop_scope() without a scope");
6973 return;
6974 }
6975 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006976 switch (scope->se_type)
6977 {
6978 case IF_SCOPE:
6979 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6980 case FOR_SCOPE:
6981 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6982 case WHILE_SCOPE:
6983 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6984 case TRY_SCOPE:
6985 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6986 case NO_SCOPE:
6987 case BLOCK_SCOPE:
6988 break;
6989 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006990 vim_free(scope);
6991}
6992
6993/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 * compile "if expr"
6995 *
6996 * "if expr" Produces instructions:
6997 * EVAL expr Push result of "expr"
6998 * JUMP_IF_FALSE end
6999 * ... body ...
7000 * end:
7001 *
7002 * "if expr | else" Produces instructions:
7003 * EVAL expr Push result of "expr"
7004 * JUMP_IF_FALSE else
7005 * ... body ...
7006 * JUMP_ALWAYS end
7007 * else:
7008 * ... body ...
7009 * end:
7010 *
7011 * "if expr1 | elseif expr2 | else" Produces instructions:
7012 * EVAL expr Push result of "expr"
7013 * JUMP_IF_FALSE elseif
7014 * ... body ...
7015 * JUMP_ALWAYS end
7016 * elseif:
7017 * EVAL expr Push result of "expr"
7018 * JUMP_IF_FALSE else
7019 * ... body ...
7020 * JUMP_ALWAYS end
7021 * else:
7022 * ... body ...
7023 * end:
7024 */
7025 static char_u *
7026compile_if(char_u *arg, cctx_T *cctx)
7027{
7028 char_u *p = arg;
7029 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007030 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007032 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007033 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034
Bram Moolenaara5565e42020-05-09 15:44:01 +02007035 CLEAR_FIELD(ppconst);
7036 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007037 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007038 clear_ppconst(&ppconst);
7039 return NULL;
7040 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007041 if (!ends_excmd2(arg, skipwhite(p)))
7042 {
7043 semsg(_(e_trailing_arg), p);
7044 return NULL;
7045 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007046 if (cctx->ctx_skip == SKIP_YES)
7047 clear_ppconst(&ppconst);
7048 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007049 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007050 int error = FALSE;
7051 int v;
7052
Bram Moolenaara5565e42020-05-09 15:44:01 +02007053 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007054 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007055 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007056 if (error)
7057 return NULL;
7058 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007059 }
7060 else
7061 {
7062 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007063 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007064 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007065 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007066 if (bool_on_stack(cctx) == FAIL)
7067 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069
Bram Moolenaara91a7132021-03-25 21:12:15 +01007070 // CMDMOD_REV must come before the jump
7071 generate_undo_cmdmods(cctx);
7072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 scope = new_scope(cctx, IF_SCOPE);
7074 if (scope == NULL)
7075 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007076 scope->se_skip_save = skip_save;
7077 // "is_had_return" will be reset if any block does not end in :return
7078 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007080 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007081 {
7082 // "where" is set when ":elseif", "else" or ":endif" is found
7083 scope->se_u.se_if.is_if_label = instr->ga_len;
7084 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7085 }
7086 else
7087 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088
Bram Moolenaarced68a02021-01-24 17:53:47 +01007089#ifdef FEAT_PROFILE
7090 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7091 && skip_save != SKIP_YES)
7092 {
7093 // generated a profile start, need to generate a profile end, since it
7094 // won't be done after returning
7095 cctx->ctx_skip = SKIP_NOT;
7096 generate_instr(cctx, ISN_PROF_END);
7097 cctx->ctx_skip = SKIP_YES;
7098 }
7099#endif
7100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 return p;
7102}
7103
7104 static char_u *
7105compile_elseif(char_u *arg, cctx_T *cctx)
7106{
7107 char_u *p = arg;
7108 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007109 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110 isn_T *isn;
7111 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007112 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007113 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
7115 if (scope == NULL || scope->se_type != IF_SCOPE)
7116 {
7117 emsg(_(e_elseif_without_if));
7118 return NULL;
7119 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007120 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007121 if (!cctx->ctx_had_return)
7122 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123
Bram Moolenaarced68a02021-01-24 17:53:47 +01007124 if (cctx->ctx_skip == SKIP_NOT)
7125 {
7126 // previous block was executed, this one and following will not
7127 cctx->ctx_skip = SKIP_YES;
7128 scope->se_u.se_if.is_seen_skip_not = TRUE;
7129 }
7130 if (scope->se_u.se_if.is_seen_skip_not)
7131 {
7132 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007133 // Do not count the "elseif" for profiling and cmdmod
7134 instr->ga_len = current_instr_idx(cctx);
7135
Bram Moolenaarced68a02021-01-24 17:53:47 +01007136 skip_expr_cctx(&p, cctx);
7137 return p;
7138 }
7139
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007140 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007141 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007142 int moved_cmdmod = FALSE;
7143
7144 // Move any CMDMOD instruction to after the jump
7145 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7146 {
7147 if (ga_grow(instr, 1) == FAIL)
7148 return NULL;
7149 ((isn_T *)instr->ga_data)[instr->ga_len] =
7150 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7151 --instr->ga_len;
7152 moved_cmdmod = TRUE;
7153 }
7154
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007155 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007156 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007157 return NULL;
7158 // previous "if" or "elseif" jumps here
7159 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7160 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007161 if (moved_cmdmod)
7162 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007165 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007166 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007167 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007168 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007169 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007170#ifdef FEAT_PROFILE
7171 if (cctx->ctx_profiling)
7172 {
7173 // the previous block was skipped, need to profile this line
7174 generate_instr(cctx, ISN_PROF_START);
7175 instr_count = instr->ga_len;
7176 }
7177#endif
7178 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007179 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007180 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007181 clear_ppconst(&ppconst);
7182 return NULL;
7183 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007184 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007185 if (!ends_excmd2(arg, skipwhite(p)))
7186 {
7187 semsg(_(e_trailing_arg), p);
7188 return NULL;
7189 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007190 if (scope->se_skip_save == SKIP_YES)
7191 clear_ppconst(&ppconst);
7192 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007193 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007194 int error = FALSE;
7195 int v;
7196
Bram Moolenaar7f141552020-05-09 17:35:53 +02007197 // The expression results in a constant.
7198 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007199 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7200 if (error)
7201 return NULL;
7202 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007203 clear_ppconst(&ppconst);
7204 scope->se_u.se_if.is_if_label = -1;
7205 }
7206 else
7207 {
7208 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007209 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007210 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007211 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007212 if (bool_on_stack(cctx) == FAIL)
7213 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
Bram Moolenaara91a7132021-03-25 21:12:15 +01007215 // CMDMOD_REV must come before the jump
7216 generate_undo_cmdmods(cctx);
7217
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007218 // "where" is set when ":elseif", "else" or ":endif" is found
7219 scope->se_u.se_if.is_if_label = instr->ga_len;
7220 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222
7223 return p;
7224}
7225
7226 static char_u *
7227compile_else(char_u *arg, cctx_T *cctx)
7228{
7229 char_u *p = arg;
7230 garray_T *instr = &cctx->ctx_instr;
7231 isn_T *isn;
7232 scope_T *scope = cctx->ctx_scope;
7233
7234 if (scope == NULL || scope->se_type != IF_SCOPE)
7235 {
7236 emsg(_(e_else_without_if));
7237 return NULL;
7238 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007239 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007240 if (!cctx->ctx_had_return)
7241 scope->se_u.se_if.is_had_return = FALSE;
7242 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007243
Bram Moolenaarced68a02021-01-24 17:53:47 +01007244#ifdef FEAT_PROFILE
7245 if (cctx->ctx_profiling)
7246 {
7247 if (cctx->ctx_skip == SKIP_NOT
7248 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7249 .isn_type == ISN_PROF_START)
7250 // the previous block was executed, do not count "else" for profiling
7251 --instr->ga_len;
7252 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7253 {
7254 // the previous block was not executed, this one will, do count the
7255 // "else" for profiling
7256 cctx->ctx_skip = SKIP_NOT;
7257 generate_instr(cctx, ISN_PROF_END);
7258 generate_instr(cctx, ISN_PROF_START);
7259 cctx->ctx_skip = SKIP_YES;
7260 }
7261 }
7262#endif
7263
7264 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007265 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007266 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007267 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007268 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007269 if (!cctx->ctx_had_return
7270 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7271 JUMP_ALWAYS, cctx) == FAIL)
7272 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007273 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007274
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007275 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007276 {
7277 if (scope->se_u.se_if.is_if_label >= 0)
7278 {
7279 // previous "if" or "elseif" jumps here
7280 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7281 isn->isn_arg.jump.jump_where = instr->ga_len;
7282 scope->se_u.se_if.is_if_label = -1;
7283 }
7284 }
7285
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007286 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007287 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007289
7290 return p;
7291}
7292
7293 static char_u *
7294compile_endif(char_u *arg, cctx_T *cctx)
7295{
7296 scope_T *scope = cctx->ctx_scope;
7297 ifscope_T *ifscope;
7298 garray_T *instr = &cctx->ctx_instr;
7299 isn_T *isn;
7300
Bram Moolenaarfa984412021-03-25 22:15:28 +01007301 if (misplaced_cmdmod(cctx))
7302 return NULL;
7303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304 if (scope == NULL || scope->se_type != IF_SCOPE)
7305 {
7306 emsg(_(e_endif_without_if));
7307 return NULL;
7308 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007309 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007310 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007311 if (!cctx->ctx_had_return)
7312 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007313
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007314 if (scope->se_u.se_if.is_if_label >= 0)
7315 {
7316 // previous "if" or "elseif" jumps here
7317 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7318 isn->isn_arg.jump.jump_where = instr->ga_len;
7319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007320 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007321 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007322
7323#ifdef FEAT_PROFILE
7324 // even when skipping we count the endif as executed, unless the block it's
7325 // in is skipped
7326 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7327 && scope->se_skip_save != SKIP_YES)
7328 {
7329 cctx->ctx_skip = SKIP_NOT;
7330 generate_instr(cctx, ISN_PROF_START);
7331 }
7332#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007333 cctx->ctx_skip = scope->se_skip_save;
7334
7335 // If all the blocks end in :return and there is an :else then the
7336 // had_return flag is set.
7337 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007339 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 return arg;
7341}
7342
7343/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007344 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 *
7346 * Produces instructions:
7347 * PUSHNR -1
7348 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007349 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007350 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7351 * - if beyond end, jump to "end"
7352 * - otherwise get item from list and push it
7353 * STORE var Store item in "var"
7354 * ... body ...
7355 * JUMP top Jump back to repeat
7356 * end: DROP Drop the result of "expr"
7357 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007358 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7359 * UNPACK 2 Split item in 2
7360 * STORE var1 Store item in "var1"
7361 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 */
7363 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007364compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007366 char_u *arg;
7367 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007368 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007369 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007370 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007371 int var_count = 0;
7372 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374 garray_T *stack = &cctx->ctx_type_stack;
7375 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007376 lvar_T *loop_lvar; // loop iteration variable
7377 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007379 type_T *item_type = &t_any;
7380 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381
Bram Moolenaar792f7862020-11-23 08:31:18 +01007382 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007383 if (p == NULL)
7384 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007385 if (var_count == 0)
7386 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387
7388 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007389 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007390 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7391 return NULL;
7392 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 {
7394 emsg(_(e_missing_in));
7395 return NULL;
7396 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007397 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007398 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7399 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 scope = new_scope(cctx, FOR_SCOPE);
7402 if (scope == NULL)
7403 return NULL;
7404
Bram Moolenaar792f7862020-11-23 08:31:18 +01007405 // Reserve a variable to store the loop iteration counter and initialize it
7406 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007407 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7408 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007409 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007410 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007411 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007412 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007413 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007414 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415
7416 // compile "expr", it remains on the stack until "endfor"
7417 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007418 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007419 {
7420 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007422 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007423 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007425 // If we know the type of "var" and it is a not a list or string we can
7426 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007428 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7429 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007431 // TODO: support Blob
7432 semsg(_(e_for_loop_on_str_not_supported),
7433 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007434 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 return NULL;
7436 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007437
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007438 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007439 {
7440 if (var_count == 1)
7441 item_type = vartype->tt_member;
7442 else if (vartype->tt_member->tt_type == VAR_LIST
7443 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7444 item_type = vartype->tt_member->tt_member;
7445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446
Bram Moolenaara91a7132021-03-25 21:12:15 +01007447 // CMDMOD_REV must come before the FOR instruction
7448 generate_undo_cmdmods(cctx);
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007451 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007452 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453
Bram Moolenaar792f7862020-11-23 08:31:18 +01007454 arg = arg_start;
7455 if (var_count > 1)
7456 {
7457 generate_UNPACK(cctx, var_count, semicolon);
7458 arg = skipwhite(arg + 1); // skip white after '['
7459
7460 // the list item is replaced by a number of items
7461 if (ga_grow(stack, var_count - 1) == FAIL)
7462 {
7463 drop_scope(cctx);
7464 return NULL;
7465 }
7466 --stack->ga_len;
7467 for (idx = 0; idx < var_count; ++idx)
7468 {
7469 ((type_T **)stack->ga_data)[stack->ga_len] =
7470 (semicolon && idx == 0) ? vartype : item_type;
7471 ++stack->ga_len;
7472 }
7473 }
7474
7475 for (idx = 0; idx < var_count; ++idx)
7476 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007477 assign_dest_T dest = dest_local;
7478 int opt_flags = 0;
7479 int vimvaridx = -1;
7480 type_T *type = &t_any;
7481
7482 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007483 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007484 name = vim_strnsave(arg, varlen);
7485 if (name == NULL)
7486 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007487
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007488 // TODO: script var not supported?
7489 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7490 &vimvaridx, &type, cctx) == FAIL)
7491 goto failed;
7492 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007493 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007494 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7495 0, 0, type, name) == FAIL)
7496 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007497 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007498 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007499 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007500 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007501 {
7502 semsg(_(e_variable_already_declared), arg);
7503 goto failed;
7504 }
7505
Bram Moolenaarea870692020-12-02 14:24:30 +01007506 if (STRNCMP(name, "s:", 2) == 0)
7507 {
7508 semsg(_(e_cannot_declare_script_variable_in_function), name);
7509 goto failed;
7510 }
7511
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007512 // Reserve a variable to store "var".
7513 // TODO: check for type
7514 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7515 if (var_lvar == NULL)
7516 // out of memory or used as an argument
7517 goto failed;
7518
7519 if (semicolon && idx == var_count - 1)
7520 var_lvar->lv_type = vartype;
7521 else
7522 var_lvar->lv_type = item_type;
7523 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7524 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007525
Bram Moolenaar036d0712021-01-17 20:23:38 +01007526 if (*p == ':')
7527 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007528 if (*p == ',' || *p == ';')
7529 ++p;
7530 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007531 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007532 }
7533
7534 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007535
7536failed:
7537 vim_free(name);
7538 drop_scope(cctx);
7539 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007540}
7541
7542/*
7543 * compile "endfor"
7544 */
7545 static char_u *
7546compile_endfor(char_u *arg, cctx_T *cctx)
7547{
7548 garray_T *instr = &cctx->ctx_instr;
7549 scope_T *scope = cctx->ctx_scope;
7550 forscope_T *forscope;
7551 isn_T *isn;
7552
Bram Moolenaarfa984412021-03-25 22:15:28 +01007553 if (misplaced_cmdmod(cctx))
7554 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 if (scope == NULL || scope->se_type != FOR_SCOPE)
7557 {
7558 emsg(_(e_for));
7559 return NULL;
7560 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007561 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007563 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564
7565 // At end of ":for" scope jump back to the FOR instruction.
7566 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7567
7568 // Fill in the "end" label in the FOR statement so it can jump here
7569 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7570 isn->isn_arg.forloop.for_end = instr->ga_len;
7571
7572 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007573 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574
7575 // Below the ":for" scope drop the "expr" list from the stack.
7576 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7577 return NULL;
7578
7579 vim_free(scope);
7580
7581 return arg;
7582}
7583
7584/*
7585 * compile "while expr"
7586 *
7587 * Produces instructions:
7588 * top: EVAL expr Push result of "expr"
7589 * JUMP_IF_FALSE end jump if false
7590 * ... body ...
7591 * JUMP top Jump back to repeat
7592 * end:
7593 *
7594 */
7595 static char_u *
7596compile_while(char_u *arg, cctx_T *cctx)
7597{
7598 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007599 scope_T *scope;
7600
7601 scope = new_scope(cctx, WHILE_SCOPE);
7602 if (scope == NULL)
7603 return NULL;
7604
Bram Moolenaara91a7132021-03-25 21:12:15 +01007605 // "endwhile" jumps back here, one before when profiling or using cmdmods
7606 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007607
7608 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007609 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007610 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007611 if (!ends_excmd2(arg, skipwhite(p)))
7612 {
7613 semsg(_(e_trailing_arg), p);
7614 return NULL;
7615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616
Bram Moolenaar13106602020-10-04 16:06:05 +02007617 if (bool_on_stack(cctx) == FAIL)
7618 return FAIL;
7619
Bram Moolenaara91a7132021-03-25 21:12:15 +01007620 // CMDMOD_REV must come before the jump
7621 generate_undo_cmdmods(cctx);
7622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007623 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007624 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007625 JUMP_IF_FALSE, cctx) == FAIL)
7626 return FAIL;
7627
7628 return p;
7629}
7630
7631/*
7632 * compile "endwhile"
7633 */
7634 static char_u *
7635compile_endwhile(char_u *arg, cctx_T *cctx)
7636{
7637 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007638 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007639
Bram Moolenaarfa984412021-03-25 22:15:28 +01007640 if (misplaced_cmdmod(cctx))
7641 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7643 {
7644 emsg(_(e_while));
7645 return NULL;
7646 }
7647 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007648 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649
Bram Moolenaarf002a412021-01-24 13:34:18 +01007650#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007651 // count the endwhile before jumping
7652 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007653#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007656 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657
7658 // Fill in the "end" label in the WHILE statement so it can jump here.
7659 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007660 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7661 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662
7663 vim_free(scope);
7664
7665 return arg;
7666}
7667
7668/*
7669 * compile "continue"
7670 */
7671 static char_u *
7672compile_continue(char_u *arg, cctx_T *cctx)
7673{
7674 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007675 int try_scopes = 0;
7676 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677
7678 for (;;)
7679 {
7680 if (scope == NULL)
7681 {
7682 emsg(_(e_continue));
7683 return NULL;
7684 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007685 if (scope->se_type == FOR_SCOPE)
7686 {
7687 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007689 }
7690 if (scope->se_type == WHILE_SCOPE)
7691 {
7692 loop_label = scope->se_u.se_while.ws_top_label;
7693 break;
7694 }
7695 if (scope->se_type == TRY_SCOPE)
7696 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007697 scope = scope->se_outer;
7698 }
7699
Bram Moolenaarc150c092021-02-13 15:02:46 +01007700 if (try_scopes > 0)
7701 // Inside one or more try/catch blocks we first need to jump to the
7702 // "finally" or "endtry" to cleanup.
7703 generate_TRYCONT(cctx, try_scopes, loop_label);
7704 else
7705 // Jump back to the FOR or WHILE instruction.
7706 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 return arg;
7709}
7710
7711/*
7712 * compile "break"
7713 */
7714 static char_u *
7715compile_break(char_u *arg, cctx_T *cctx)
7716{
7717 scope_T *scope = cctx->ctx_scope;
7718 endlabel_T **el;
7719
7720 for (;;)
7721 {
7722 if (scope == NULL)
7723 {
7724 emsg(_(e_break));
7725 return NULL;
7726 }
7727 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7728 break;
7729 scope = scope->se_outer;
7730 }
7731
7732 // Jump to the end of the FOR or WHILE loop.
7733 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007734 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007735 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007736 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7738 return FAIL;
7739
7740 return arg;
7741}
7742
7743/*
7744 * compile "{" start of block
7745 */
7746 static char_u *
7747compile_block(char_u *arg, cctx_T *cctx)
7748{
7749 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7750 return NULL;
7751 return skipwhite(arg + 1);
7752}
7753
7754/*
7755 * compile end of block: drop one scope
7756 */
7757 static void
7758compile_endblock(cctx_T *cctx)
7759{
7760 scope_T *scope = cctx->ctx_scope;
7761
7762 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007763 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007764 vim_free(scope);
7765}
7766
7767/*
7768 * compile "try"
7769 * Creates a new scope for the try-endtry, pointing to the first catch and
7770 * finally.
7771 * Creates another scope for the "try" block itself.
7772 * TRY instruction sets up exception handling at runtime.
7773 *
7774 * "try"
7775 * TRY -> catch1, -> finally push trystack entry
7776 * ... try block
7777 * "throw {exception}"
7778 * EVAL {exception}
7779 * THROW create exception
7780 * ... try block
7781 * " catch {expr}"
7782 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007783 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007784 * EVAL {expr}
7785 * MATCH
7786 * JUMP nomatch -> catch2
7787 * CATCH remove exception
7788 * ... catch block
7789 * " catch"
7790 * JUMP -> finally
7791 * catch2: CATCH remove exception
7792 * ... catch block
7793 * " finally"
7794 * finally:
7795 * ... finally block
7796 * " endtry"
7797 * ENDTRY pop trystack entry, may rethrow
7798 */
7799 static char_u *
7800compile_try(char_u *arg, cctx_T *cctx)
7801{
7802 garray_T *instr = &cctx->ctx_instr;
7803 scope_T *try_scope;
7804 scope_T *scope;
7805
Bram Moolenaarfa984412021-03-25 22:15:28 +01007806 if (misplaced_cmdmod(cctx))
7807 return NULL;
7808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007809 // scope that holds the jumps that go to catch/finally/endtry
7810 try_scope = new_scope(cctx, TRY_SCOPE);
7811 if (try_scope == NULL)
7812 return NULL;
7813
Bram Moolenaar69f70502021-01-01 16:10:46 +01007814 if (cctx->ctx_skip != SKIP_YES)
7815 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007816 isn_T *isn;
7817
7818 // "try_catch" is set when the first ":catch" is found or when no catch
7819 // is found and ":finally" is found.
7820 // "try_finally" is set when ":finally" is found
7821 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007822 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007823 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7824 return NULL;
7825 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7826 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007827 return NULL;
7828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829
7830 // scope for the try block itself
7831 scope = new_scope(cctx, BLOCK_SCOPE);
7832 if (scope == NULL)
7833 return NULL;
7834
7835 return arg;
7836}
7837
7838/*
7839 * compile "catch {expr}"
7840 */
7841 static char_u *
7842compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7843{
7844 scope_T *scope = cctx->ctx_scope;
7845 garray_T *instr = &cctx->ctx_instr;
7846 char_u *p;
7847 isn_T *isn;
7848
Bram Moolenaarfa984412021-03-25 22:15:28 +01007849 if (misplaced_cmdmod(cctx))
7850 return NULL;
7851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007852 // end block scope from :try or :catch
7853 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7854 compile_endblock(cctx);
7855 scope = cctx->ctx_scope;
7856
7857 // Error if not in a :try scope
7858 if (scope == NULL || scope->se_type != TRY_SCOPE)
7859 {
7860 emsg(_(e_catch));
7861 return NULL;
7862 }
7863
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007864 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007865 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007866 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867 return NULL;
7868 }
7869
Bram Moolenaar69f70502021-01-01 16:10:46 +01007870 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007872#ifdef FEAT_PROFILE
7873 // the profile-start should be after the jump
7874 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7875 .isn_type == ISN_PROF_START)
7876 --instr->ga_len;
7877#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007878 // Jump from end of previous block to :finally or :endtry
7879 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7880 JUMP_ALWAYS, cctx) == FAIL)
7881 return NULL;
7882
7883 // End :try or :catch scope: set value in ISN_TRY instruction
7884 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007885 if (isn->isn_arg.try.try_ref->try_catch == 0)
7886 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007887 if (scope->se_u.se_try.ts_catch_label != 0)
7888 {
7889 // Previous catch without match jumps here
7890 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7891 isn->isn_arg.jump.jump_where = instr->ga_len;
7892 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007893#ifdef FEAT_PROFILE
7894 if (cctx->ctx_profiling)
7895 {
7896 // a "throw" that jumps here needs to be counted
7897 generate_instr(cctx, ISN_PROF_END);
7898 // the "catch" is also counted
7899 generate_instr(cctx, ISN_PROF_START);
7900 }
7901#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007902 }
7903
7904 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007905 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007906 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007907 scope->se_u.se_try.ts_caught_all = TRUE;
7908 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007909 }
7910 else
7911 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007912 char_u *end;
7913 char_u *pat;
7914 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007915 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007916 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918 // Push v:exception, push {expr} and MATCH
7919 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7920
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007921 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007922 if (*end != *p)
7923 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007924 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007925 vim_free(tofree);
7926 return FAIL;
7927 }
7928 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007929 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007930 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007931 len = (int)(end - tofree);
7932 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007933 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007934 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007935 if (pat == NULL)
7936 return FAIL;
7937 if (generate_PUSHS(cctx, pat) == FAIL)
7938 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7941 return NULL;
7942
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007943 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007944 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7945 return NULL;
7946 }
7947
Bram Moolenaar69f70502021-01-01 16:10:46 +01007948 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007949 return NULL;
7950
7951 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7952 return NULL;
7953 return p;
7954}
7955
7956 static char_u *
7957compile_finally(char_u *arg, cctx_T *cctx)
7958{
7959 scope_T *scope = cctx->ctx_scope;
7960 garray_T *instr = &cctx->ctx_instr;
7961 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007962 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963
Bram Moolenaarfa984412021-03-25 22:15:28 +01007964 if (misplaced_cmdmod(cctx))
7965 return NULL;
7966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 // end block scope from :try or :catch
7968 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7969 compile_endblock(cctx);
7970 scope = cctx->ctx_scope;
7971
7972 // Error if not in a :try scope
7973 if (scope == NULL || scope->se_type != TRY_SCOPE)
7974 {
7975 emsg(_(e_finally));
7976 return NULL;
7977 }
7978
7979 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007980 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007981 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 {
7983 emsg(_(e_finally_dup));
7984 return NULL;
7985 }
7986
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007987 this_instr = instr->ga_len;
7988#ifdef FEAT_PROFILE
7989 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7990 .isn_type == ISN_PROF_START)
7991 // jump to the profile start of the "finally"
7992 --this_instr;
7993#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007994
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007995 // Fill in the "end" label in jumps at the end of the blocks.
7996 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7997 this_instr, cctx);
7998
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007999 // If there is no :catch then an exception jumps to :finally.
8000 if (isn->isn_arg.try.try_ref->try_catch == 0)
8001 isn->isn_arg.try.try_ref->try_catch = this_instr;
8002 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008003 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 {
8005 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008006 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008007 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008008 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008010 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8011 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008013 // TODO: set index in ts_finally_label jumps
8014
8015 return arg;
8016}
8017
8018 static char_u *
8019compile_endtry(char_u *arg, cctx_T *cctx)
8020{
8021 scope_T *scope = cctx->ctx_scope;
8022 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008023 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024
Bram Moolenaarfa984412021-03-25 22:15:28 +01008025 if (misplaced_cmdmod(cctx))
8026 return NULL;
8027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008028 // end block scope from :catch or :finally
8029 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8030 compile_endblock(cctx);
8031 scope = cctx->ctx_scope;
8032
8033 // Error if not in a :try scope
8034 if (scope == NULL || scope->se_type != TRY_SCOPE)
8035 {
8036 if (scope == NULL)
8037 emsg(_(e_no_endtry));
8038 else if (scope->se_type == WHILE_SCOPE)
8039 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008040 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 emsg(_(e_endfor));
8042 else
8043 emsg(_(e_endif));
8044 return NULL;
8045 }
8046
Bram Moolenaarc150c092021-02-13 15:02:46 +01008047 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008048 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008049 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008050 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8051 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008052 {
8053 emsg(_(e_missing_catch_or_finally));
8054 return NULL;
8055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008057#ifdef FEAT_PROFILE
8058 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8059 .isn_type == ISN_PROF_START)
8060 // move the profile start after "endtry" so that it's not counted when
8061 // the exception is rethrown.
8062 --instr->ga_len;
8063#endif
8064
Bram Moolenaar69f70502021-01-01 16:10:46 +01008065 // Fill in the "end" label in jumps at the end of the blocks, if not
8066 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008067 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8068 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069
Bram Moolenaar69f70502021-01-01 16:10:46 +01008070 if (scope->se_u.se_try.ts_catch_label != 0)
8071 {
8072 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008073 isn_T *isn = ((isn_T *)instr->ga_data)
8074 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008075 isn->isn_arg.jump.jump_where = instr->ga_len;
8076 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008077 }
8078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 compile_endblock(cctx);
8080
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008081 if (cctx->ctx_skip != SKIP_YES)
8082 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008083 // End :catch or :finally scope: set instruction index in ISN_TRY
8084 // instruction
8085 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008086 if (cctx->ctx_skip != SKIP_YES
8087 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8088 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008089#ifdef FEAT_PROFILE
8090 if (cctx->ctx_profiling)
8091 generate_instr(cctx, ISN_PROF_START);
8092#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008094 return arg;
8095}
8096
8097/*
8098 * compile "throw {expr}"
8099 */
8100 static char_u *
8101compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8102{
8103 char_u *p = skipwhite(arg);
8104
Bram Moolenaara5565e42020-05-09 15:44:01 +02008105 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008107 if (cctx->ctx_skip == SKIP_YES)
8108 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 if (may_generate_2STRING(-1, cctx) == FAIL)
8110 return NULL;
8111 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8112 return NULL;
8113
8114 return p;
8115}
8116
8117/*
8118 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008119 * compile "echomsg expr"
8120 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008121 * compile "execute expr"
8122 */
8123 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008124compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008125{
8126 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008127 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008128 int count = 0;
8129
8130 for (;;)
8131 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008132 if (ends_excmd2(prev, p))
8133 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008134 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008135 return NULL;
8136 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008137 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008138 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008139 }
8140
Bram Moolenaare4984292020-12-13 14:19:25 +01008141 if (count > 0)
8142 {
8143 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8144 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8145 else if (cmdidx == CMD_execute)
8146 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8147 else if (cmdidx == CMD_echomsg)
8148 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8149 else
8150 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 return p;
8153}
8154
8155/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008156 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008157 * instruction to compute it and return OK.
8158 * Otherwise return FAIL, the caller must deal with any range.
8159 */
8160 static int
8161compile_variable_range(exarg_T *eap, cctx_T *cctx)
8162{
8163 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8164 char_u *p = skipdigits(eap->cmd);
8165
8166 if (p == range_end)
8167 return FAIL;
8168 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8169}
8170
8171/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008172 * :put r
8173 * :put ={expr}
8174 */
8175 static char_u *
8176compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8177{
8178 char_u *line = arg;
8179 linenr_T lnum;
8180 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008181 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008182
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008183 eap->regname = *line;
8184
8185 if (eap->regname == '=')
8186 {
8187 char_u *p = line + 1;
8188
8189 if (compile_expr0(&p, cctx) == FAIL)
8190 return NULL;
8191 line = p;
8192 }
8193 else if (eap->regname != NUL)
8194 ++line;
8195
Bram Moolenaar08597872020-12-10 19:43:40 +01008196 if (compile_variable_range(eap, cctx) == OK)
8197 {
8198 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8199 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008200 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008201 {
8202 // Either no range or a number.
8203 // "errormsg" will not be set because the range is ADDR_LINES.
8204 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008205 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008206 return NULL;
8207 if (eap->addr_count == 0)
8208 lnum = -1;
8209 else
8210 lnum = eap->line2;
8211 if (above)
8212 --lnum;
8213 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008214
8215 generate_PUT(cctx, eap->regname, lnum);
8216 return line;
8217}
8218
8219/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008220 * A command that is not compiled, execute with legacy code.
8221 */
8222 static char_u *
8223compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8224{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008225 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008226 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008227 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008228
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008229 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008230 goto theend;
8231
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008232 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008233 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008234 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008235 int usefilter = FALSE;
8236
8237 has_expr = argt & (EX_XFILE | EX_EXPAND);
8238
8239 // If the command can be followed by a bar, find the bar and truncate
8240 // it, so that the following command can be compiled.
8241 // The '|' is overwritten with a NUL, it is put back below.
8242 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8243 && *eap->arg == '!')
8244 // :w !filter or :r !filter or :r! filter
8245 usefilter = TRUE;
8246 if ((argt & EX_TRLBAR) && !usefilter)
8247 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008248 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008249 separate_nextcmd(eap);
8250 if (eap->nextcmd != NULL)
8251 nextcmd = eap->nextcmd;
8252 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008253 else if (eap->cmdidx == CMD_wincmd)
8254 {
8255 p = eap->arg;
8256 if (*p != NUL)
8257 ++p;
8258 if (*p == 'g' || *p == Ctrl_G)
8259 ++p;
8260 p = skipwhite(p);
8261 if (*p == '|')
8262 {
8263 *p = NUL;
8264 nextcmd = p + 1;
8265 }
8266 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008267 }
8268
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008269 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8270 {
8271 // expand filename in "syntax include [@group] filename"
8272 has_expr = TRUE;
8273 eap->arg = skipwhite(eap->arg + 7);
8274 if (*eap->arg == '@')
8275 eap->arg = skiptowhite(eap->arg);
8276 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008277
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008278 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8279 && STRLEN(eap->arg) > 4)
8280 {
8281 int delim = *eap->arg;
8282
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008283 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008284 if (*p == delim)
8285 {
8286 eap->arg = p + 1;
8287 has_expr = TRUE;
8288 }
8289 }
8290
Bram Moolenaarecac5912021-01-05 19:23:28 +01008291 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8292 {
8293 // TODO: should only expand when appropriate for the command
8294 eap->arg = skiptowhite(eap->arg);
8295 has_expr = TRUE;
8296 }
8297
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008298 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008299 {
8300 int count = 0;
8301 char_u *start = skipwhite(line);
8302
8303 // :cmd xxx`=expr1`yyy`=expr2`zzz
8304 // PUSHS ":cmd xxx"
8305 // eval expr1
8306 // PUSHS "yyy"
8307 // eval expr2
8308 // PUSHS "zzz"
8309 // EXECCONCAT 5
8310 for (;;)
8311 {
8312 if (p > start)
8313 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008314 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008315 ++count;
8316 }
8317 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008318 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008319 return NULL;
8320 may_generate_2STRING(-1, cctx);
8321 ++count;
8322 p = skipwhite(p);
8323 if (*p != '`')
8324 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008325 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008326 return NULL;
8327 }
8328 start = p + 1;
8329
8330 p = (char_u *)strstr((char *)start, "`=");
8331 if (p == NULL)
8332 {
8333 if (*skipwhite(start) != NUL)
8334 {
8335 generate_PUSHS(cctx, vim_strsave(start));
8336 ++count;
8337 }
8338 break;
8339 }
8340 }
8341 generate_EXECCONCAT(cctx, count);
8342 }
8343 else
8344 generate_EXEC(cctx, line);
8345
8346theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008347 if (*nextcmd != NUL)
8348 {
8349 // the parser expects a pointer to the bar, put it back
8350 --nextcmd;
8351 *nextcmd = '|';
8352 }
8353
8354 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008355}
8356
8357/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008358 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008359 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008360 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008361 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008362add_def_function(ufunc_T *ufunc)
8363{
8364 dfunc_T *dfunc;
8365
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008366 if (def_functions.ga_len == 0)
8367 {
8368 // The first position is not used, so that a zero uf_dfunc_idx means it
8369 // wasn't set.
8370 if (ga_grow(&def_functions, 1) == FAIL)
8371 return FAIL;
8372 ++def_functions.ga_len;
8373 }
8374
Bram Moolenaar09689a02020-05-09 22:50:08 +02008375 // Add the function to "def_functions".
8376 if (ga_grow(&def_functions, 1) == FAIL)
8377 return FAIL;
8378 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8379 CLEAR_POINTER(dfunc);
8380 dfunc->df_idx = def_functions.ga_len;
8381 ufunc->uf_dfunc_idx = dfunc->df_idx;
8382 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008383 dfunc->df_name = vim_strsave(ufunc->uf_name);
8384 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008385 ++def_functions.ga_len;
8386 return OK;
8387}
8388
8389/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008390 * After ex_function() has collected all the function lines: parse and compile
8391 * the lines into instructions.
8392 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008393 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8394 * the return statement (used for lambda). When uf_ret_type is already set
8395 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008396 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008397 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008398 * This can be used recursively through compile_lambda(), which may reallocate
8399 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008400 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008402 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008403compile_def_function(
8404 ufunc_T *ufunc,
8405 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008406 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008407 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008408{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008409 char_u *line = NULL;
8410 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008412 cctx_T cctx;
8413 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008414 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 int ret = FAIL;
8416 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008417 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008418 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008419 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008420#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008421 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008422#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008424 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008425 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008426 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008428 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8429 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008430 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008432 else
8433 {
8434 if (add_def_function(ufunc) == FAIL)
8435 return FAIL;
8436 new_def_function = TRUE;
8437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008438
Bram Moolenaar985116a2020-07-12 17:31:09 +02008439 ufunc->uf_def_status = UF_COMPILING;
8440
Bram Moolenaara80faa82020-04-12 19:37:17 +02008441 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008442
Bram Moolenaarf002a412021-01-24 13:34:18 +01008443#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008444 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008445#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008446 cctx.ctx_ufunc = ufunc;
8447 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008448 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8450 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8451 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8452 cctx.ctx_type_list = &ufunc->uf_type_list;
8453 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8454 instr = &cctx.ctx_instr;
8455
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008456 // Set the context to the function, it may be compiled when called from
8457 // another script. Set the script version to the most modern one.
8458 // The line number will be set in next_line_from_context().
8459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8461
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008462 // Make sure error messages are OK.
8463 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8464 if (do_estack_push)
8465 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008466 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008467
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008468 if (ufunc->uf_def_args.ga_len > 0)
8469 {
8470 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008471 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008472 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008473 int i;
8474 char_u *arg;
8475 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008476 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008477
8478 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008479 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008480 for (i = 0; i < count; ++i)
8481 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008482 garray_T *stack = &cctx.ctx_type_stack;
8483 type_T *val_type;
8484 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008485 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008486 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008487 int jump_instr_idx = instr->ga_len;
8488 isn_T *isn;
8489
8490 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8491 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8492 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008493
8494 // Make sure later arguments are not found.
8495 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008496
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008497 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008498 r = compile_expr0(&arg, &cctx);
8499
8500 ufunc->uf_args.ga_len = uf_args_len;
8501 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008502 goto erret;
8503
8504 // If no type specified use the type of the default value.
8505 // Otherwise check that the default value type matches the
8506 // specified type.
8507 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008508 where.wt_index = arg_idx + 1;
8509 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008510 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008511 {
8512 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008513 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008514 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008515 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008516 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008517 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008518
8519 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008520 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008521
8522 // set instruction index in JUMP_IF_ARG_SET to here
8523 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8524 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008525 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008526
8527 if (did_set_arg_type)
8528 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008529 }
8530
8531 /*
8532 * Loop over all the lines of the function and generate instructions.
8533 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008534 for (;;)
8535 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008536 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008537 int starts_with_colon = FALSE;
8538 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008539 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008540
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008541 // Bail out on the first error to avoid a flood of errors and report
8542 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008543 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008544 goto erret;
8545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546 if (line != NULL && *line == '|')
8547 // the line continues after a '|'
8548 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008549 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008550 && !(*line == '#' && (line == cctx.ctx_line_start
8551 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008553 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554 goto erret;
8555 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008556 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8557 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008558 else
8559 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008560 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008561 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008562 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008563 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008564#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008565 if (cctx.ctx_skip != SKIP_YES)
8566 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008567#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008568 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008570 }
8571
Bram Moolenaara80faa82020-04-12 19:37:17 +02008572 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 ea.cmdlinep = &line;
8574 ea.cmd = skipwhite(line);
8575
Bram Moolenaarb2049902021-01-24 12:53:53 +01008576 if (*ea.cmd == '#')
8577 {
8578 // "#" starts a comment
8579 line = (char_u *)"";
8580 continue;
8581 }
8582
Bram Moolenaarf002a412021-01-24 13:34:18 +01008583#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008584 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8585 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008586 {
8587 may_generate_prof_end(&cctx, prof_lnum);
8588
8589 prof_lnum = cctx.ctx_lnum;
8590 generate_instr(&cctx, ISN_PROF_START);
8591 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008592#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008593
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008594 // Some things can be recognized by the first character.
8595 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008597 case '}':
8598 {
8599 // "}" ends a block scope
8600 scopetype_T stype = cctx.ctx_scope == NULL
8601 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008603 if (stype == BLOCK_SCOPE)
8604 {
8605 compile_endblock(&cctx);
8606 line = ea.cmd;
8607 }
8608 else
8609 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008610 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008611 goto erret;
8612 }
8613 if (line != NULL)
8614 line = skipwhite(ea.cmd + 1);
8615 continue;
8616 }
8617
8618 case '{':
8619 // "{" starts a block scope
8620 // "{'a': 1}->func() is something else
8621 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8622 {
8623 line = compile_block(ea.cmd, &cctx);
8624 continue;
8625 }
8626 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008627 }
8628
8629 /*
8630 * COMMAND MODIFIERS
8631 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008632 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008633 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8634 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635 {
8636 if (errormsg != NULL)
8637 goto erret;
8638 // empty line or comment
8639 line = (char_u *)"";
8640 continue;
8641 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008642 generate_cmdmods(&cctx, &local_cmdmod);
8643 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008644
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008645 // Check if there was a colon after the last command modifier or before
8646 // the current position.
8647 for (p = ea.cmd; p >= line; --p)
8648 {
8649 if (*p == ':')
8650 starts_with_colon = TRUE;
8651 if (p < ea.cmd && !VIM_ISWHITE(*p))
8652 break;
8653 }
8654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008656 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008658 {
8659 if (*ea.cmd == '(')
8660 // not for "call()"
8661 ea.cmd = p;
8662 else
8663 ea.cmd = skipwhite(ea.cmd);
8664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008665
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008666 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008667 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008668 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008669
Bram Moolenaar17126b12021-01-07 22:03:02 +01008670 // Check for assignment after command modifiers.
8671 assign = may_compile_assignment(&ea, &line, &cctx);
8672 if (assign == OK)
8673 goto nextline;
8674 if (assign == FAIL)
8675 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676 }
8677
8678 /*
8679 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008680 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008681 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008682 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008683 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008684 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008685 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008686 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008687 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008688 if (!starts_with_colon)
8689 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008690 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008691 goto erret;
8692 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008693 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008694 if (ends_excmd2(line, ea.cmd))
8695 {
8696 // A range without a command: jump to the line.
8697 // TODO: compile to a more efficient command, possibly
8698 // calling parse_cmd_address().
8699 ea.cmdidx = CMD_SIZE;
8700 line = compile_exec(line, &ea, &cctx);
8701 goto nextline;
8702 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008703 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008704 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008705 p = find_ex_command(&ea, NULL, starts_with_colon
8706 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008707
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008708 if (p == NULL)
8709 {
8710 if (cctx.ctx_skip != SKIP_YES)
8711 emsg(_(e_ambiguous_use_of_user_defined_command));
8712 goto erret;
8713 }
8714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8716 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008717 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008718 {
8719 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008720 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008721 }
8722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008724 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008725 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008726 // CMD_var cannot happen, compile_assignment() above would be
8727 // used. Most likely an assignment to a non-existing variable.
8728 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008729 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 }
8732
Bram Moolenaar3988f642020-08-27 22:43:03 +02008733 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008734 && ea.cmdidx != CMD_elseif
8735 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008736 && ea.cmdidx != CMD_endif
8737 && ea.cmdidx != CMD_endfor
8738 && ea.cmdidx != CMD_endwhile
8739 && ea.cmdidx != CMD_catch
8740 && ea.cmdidx != CMD_finally
8741 && ea.cmdidx != CMD_endtry)
8742 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008743 emsg(_(e_unreachable_code_after_return));
8744 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008745 }
8746
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008747 p = skipwhite(p);
8748 if (ea.cmdidx != CMD_SIZE
8749 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8750 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008751 if (ea.cmdidx >= 0)
8752 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008753 if ((ea.argt & EX_BANG) && *p == '!')
8754 {
8755 ea.forceit = TRUE;
8756 p = skipwhite(p + 1);
8757 }
8758 }
8759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008760 switch (ea.cmdidx)
8761 {
8762 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008763 ea.arg = p;
8764 line = compile_nested_function(&ea, &cctx);
8765 break;
8766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008767 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008768 // TODO: should we allow this, e.g. to declare a global
8769 // function?
8770 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008771 goto erret;
8772
8773 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008774 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008775 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008776 break;
8777
8778 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008779 emsg(_(e_cannot_use_let_in_vim9_script));
8780 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008781 case CMD_var:
8782 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008783 case CMD_const:
8784 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008785 if (line == p)
8786 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 break;
8788
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008789 case CMD_unlet:
8790 case CMD_unlockvar:
8791 case CMD_lockvar:
8792 line = compile_unletlock(p, &ea, &cctx);
8793 break;
8794
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008795 case CMD_import:
8796 line = compile_import(p, &cctx);
8797 break;
8798
8799 case CMD_if:
8800 line = compile_if(p, &cctx);
8801 break;
8802 case CMD_elseif:
8803 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008804 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 break;
8806 case CMD_else:
8807 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008808 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 break;
8810 case CMD_endif:
8811 line = compile_endif(p, &cctx);
8812 break;
8813
8814 case CMD_while:
8815 line = compile_while(p, &cctx);
8816 break;
8817 case CMD_endwhile:
8818 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008819 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 break;
8821
8822 case CMD_for:
8823 line = compile_for(p, &cctx);
8824 break;
8825 case CMD_endfor:
8826 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008827 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828 break;
8829 case CMD_continue:
8830 line = compile_continue(p, &cctx);
8831 break;
8832 case CMD_break:
8833 line = compile_break(p, &cctx);
8834 break;
8835
8836 case CMD_try:
8837 line = compile_try(p, &cctx);
8838 break;
8839 case CMD_catch:
8840 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008841 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008842 break;
8843 case CMD_finally:
8844 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008845 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008846 break;
8847 case CMD_endtry:
8848 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008849 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008850 break;
8851 case CMD_throw:
8852 line = compile_throw(p, &cctx);
8853 break;
8854
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008855 case CMD_eval:
8856 if (compile_expr0(&p, &cctx) == FAIL)
8857 goto erret;
8858
Bram Moolenaar3988f642020-08-27 22:43:03 +02008859 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008860 generate_instr_drop(&cctx, ISN_DROP, 1);
8861
8862 line = skipwhite(p);
8863 break;
8864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008865 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008866 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008867 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008868 case CMD_echomsg:
8869 case CMD_echoerr:
8870 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008871 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008873 case CMD_put:
8874 ea.cmd = cmd;
8875 line = compile_put(p, &ea, &cctx);
8876 break;
8877
Bram Moolenaar3988f642020-08-27 22:43:03 +02008878 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008879
Bram Moolenaarae616492020-07-28 20:07:27 +02008880 case CMD_append:
8881 case CMD_change:
8882 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008883 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008884 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008885 case CMD_xit:
8886 not_in_vim9(&ea);
8887 goto erret;
8888
Bram Moolenaar002262f2020-07-08 17:47:57 +02008889 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008890 if (cctx.ctx_skip != SKIP_YES)
8891 {
8892 semsg(_(e_invalid_command_str), ea.cmd);
8893 goto erret;
8894 }
8895 // We don't check for a next command here.
8896 line = (char_u *)"";
8897 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008900 if (cctx.ctx_skip == SKIP_YES)
8901 {
8902 // We don't check for a next command here.
8903 line = (char_u *)"";
8904 }
8905 else
8906 {
8907 // Not recognized, execute with do_cmdline_cmd().
8908 ea.arg = p;
8909 line = compile_exec(line, &ea, &cctx);
8910 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911 break;
8912 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008913nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008914 if (line == NULL)
8915 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008916 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008917
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008918 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008919 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008921 if (cctx.ctx_type_stack.ga_len < 0)
8922 {
8923 iemsg("Type stack underflow");
8924 goto erret;
8925 }
8926 }
8927
8928 if (cctx.ctx_scope != NULL)
8929 {
8930 if (cctx.ctx_scope->se_type == IF_SCOPE)
8931 emsg(_(e_endif));
8932 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8933 emsg(_(e_endwhile));
8934 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8935 emsg(_(e_endfor));
8936 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008937 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008938 goto erret;
8939 }
8940
Bram Moolenaarefd88552020-06-18 20:50:10 +02008941 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008942 {
8943 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8944 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008945 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008946 goto erret;
8947 }
8948
8949 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008950 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008951 }
8952
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008953 {
8954 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8955 + ufunc->uf_dfunc_idx;
8956 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008957 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008958#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008959 if (cctx.ctx_profiling)
8960 {
8961 dfunc->df_instr_prof = instr->ga_data;
8962 dfunc->df_instr_prof_count = instr->ga_len;
8963 }
8964 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008965#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008966 {
8967 dfunc->df_instr = instr->ga_data;
8968 dfunc->df_instr_count = instr->ga_len;
8969 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008970 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008971 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008972 if (cctx.ctx_outer_used)
8973 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008974 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008976
8977 ret = OK;
8978
8979erret:
8980 if (ret == FAIL)
8981 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008982 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008983 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8984 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008985
8986 for (idx = 0; idx < instr->ga_len; ++idx)
8987 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008988 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008989 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008990
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008991 // If using the last entry in the table and it was added above, we
8992 // might as well remove it.
8993 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008994 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008995 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008996 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008997 ufunc->uf_dfunc_idx = 0;
8998 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008999 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009000
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009001 while (cctx.ctx_scope != NULL)
9002 drop_scope(&cctx);
9003
Bram Moolenaar20431c92020-03-20 18:39:46 +01009004 // Don't execute this function body.
9005 ga_clear_strings(&ufunc->uf_lines);
9006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009007 if (errormsg != NULL)
9008 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009009 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009010 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009011 }
9012
9013 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009014 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009015 if (do_estack_push)
9016 estack_pop();
9017
Bram Moolenaar20431c92020-03-20 18:39:46 +01009018 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009019 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009020 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009021 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009022}
9023
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009024 void
9025set_function_type(ufunc_T *ufunc)
9026{
9027 int varargs = ufunc->uf_va_name != NULL;
9028 int argcount = ufunc->uf_args.ga_len;
9029
9030 // Create a type for the function, with the return type and any
9031 // argument types.
9032 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9033 // The type is included in "tt_args".
9034 if (argcount > 0 || varargs)
9035 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009036 if (ufunc->uf_type_list.ga_itemsize == 0)
9037 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009038 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9039 argcount, &ufunc->uf_type_list);
9040 // Add argument types to the function type.
9041 if (func_type_add_arg_types(ufunc->uf_func_type,
9042 argcount + varargs,
9043 &ufunc->uf_type_list) == FAIL)
9044 return;
9045 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9046 ufunc->uf_func_type->tt_min_argcount =
9047 argcount - ufunc->uf_def_args.ga_len;
9048 if (ufunc->uf_arg_types == NULL)
9049 {
9050 int i;
9051
9052 // lambda does not have argument types.
9053 for (i = 0; i < argcount; ++i)
9054 ufunc->uf_func_type->tt_args[i] = &t_any;
9055 }
9056 else
9057 mch_memmove(ufunc->uf_func_type->tt_args,
9058 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9059 if (varargs)
9060 {
9061 ufunc->uf_func_type->tt_args[argcount] =
9062 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
9063 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9064 }
9065 }
9066 else
9067 // No arguments, can use a predefined type.
9068 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9069 argcount, &ufunc->uf_type_list);
9070}
9071
9072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009073/*
9074 * Delete an instruction, free what it contains.
9075 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009076 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009077delete_instr(isn_T *isn)
9078{
9079 switch (isn->isn_type)
9080 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009081 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009082 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009083 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009084 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009085 case ISN_LOADENV:
9086 case ISN_LOADG:
9087 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009088 case ISN_LOADT:
9089 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009090 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009091 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009092 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009093 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009094 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009095 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009096 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009097 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009098 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009099 case ISN_STOREW:
9100 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009101 vim_free(isn->isn_arg.string);
9102 break;
9103
9104 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009105 case ISN_STORES:
9106 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009107 break;
9108
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009109 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009110 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009111 vim_free(isn->isn_arg.unlet.ul_name);
9112 break;
9113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009114 case ISN_STOREOPT:
9115 vim_free(isn->isn_arg.storeopt.so_name);
9116 break;
9117
9118 case ISN_PUSHBLOB: // push blob isn_arg.blob
9119 blob_unref(isn->isn_arg.blob);
9120 break;
9121
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009122 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009123#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009124 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009125#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009126 break;
9127
9128 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009130 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009131#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009132 break;
9133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009134 case ISN_UCALL:
9135 vim_free(isn->isn_arg.ufunc.cuf_name);
9136 break;
9137
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009138 case ISN_FUNCREF:
9139 {
9140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9141 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009142 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009143
Bram Moolenaar077a4232020-12-22 18:33:27 +01009144 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9145 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009146 }
9147 break;
9148
9149 case ISN_DCALL:
9150 {
9151 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9152 + isn->isn_arg.dfunc.cdf_idx;
9153
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009154 if (dfunc->df_ufunc != NULL
9155 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009156 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009157 }
9158 break;
9159
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009160 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009161 {
9162 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9163 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9164
9165 if (ufunc != NULL)
9166 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009167 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009168 func_ptr_unref(ufunc);
9169 }
9170
9171 vim_free(lambda);
9172 vim_free(isn->isn_arg.newfunc.nf_global);
9173 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009174 break;
9175
Bram Moolenaar5e654232020-09-16 15:22:00 +02009176 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009177 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009178 free_type(isn->isn_arg.type.ct_type);
9179 break;
9180
Bram Moolenaar02194d22020-10-24 23:08:38 +02009181 case ISN_CMDMOD:
9182 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9183 ->cmod_filter_regmatch.regprog);
9184 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9185 break;
9186
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009187 case ISN_LOADSCRIPT:
9188 case ISN_STORESCRIPT:
9189 vim_free(isn->isn_arg.script.scriptref);
9190 break;
9191
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009192 case ISN_TRY:
9193 vim_free(isn->isn_arg.try.try_ref);
9194 break;
9195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009196 case ISN_2BOOL:
9197 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009198 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009199 case ISN_ADDBLOB:
9200 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009201 case ISN_ANYINDEX:
9202 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009203 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009204 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009205 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009206 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009208 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 case ISN_COMPAREANY:
9210 case ISN_COMPAREBLOB:
9211 case ISN_COMPAREBOOL:
9212 case ISN_COMPAREDICT:
9213 case ISN_COMPAREFLOAT:
9214 case ISN_COMPAREFUNC:
9215 case ISN_COMPARELIST:
9216 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009217 case ISN_COMPARESPECIAL:
9218 case ISN_COMPARESTRING:
9219 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009220 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009221 case ISN_DROP:
9222 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009223 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009224 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009226 case ISN_EXECCONCAT:
9227 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009228 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009229 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009230 case ISN_GETITEM:
9231 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009232 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009233 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009234 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009235 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009237 case ISN_LOADBDICT:
9238 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009239 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009240 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009241 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009242 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009243 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009244 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009245 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 case ISN_NEGATENR:
9247 case ISN_NEWDICT:
9248 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009249 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009250 case ISN_OPFLOAT:
9251 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009252 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009253 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009254 case ISN_PROF_END:
9255 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009256 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009257 case ISN_PUSHF:
9258 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009259 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009260 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009261 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009262 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009263 case ISN_SHUFFLE:
9264 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009266 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009267 case ISN_STORENR:
9268 case ISN_STOREOUTER:
9269 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009270 case ISN_STOREV:
9271 case ISN_STRINDEX:
9272 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009273 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009274 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009275 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009276 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009277 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009278 // nothing allocated
9279 break;
9280 }
9281}
9282
9283/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009284 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009285 */
9286 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009287delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009288{
9289 int idx;
9290
9291 ga_clear(&dfunc->df_def_args_isn);
9292
9293 if (dfunc->df_instr != NULL)
9294 {
9295 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9296 delete_instr(dfunc->df_instr + idx);
9297 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009298 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009299 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009300#ifdef FEAT_PROFILE
9301 if (dfunc->df_instr_prof != NULL)
9302 {
9303 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9304 delete_instr(dfunc->df_instr_prof + idx);
9305 VIM_CLEAR(dfunc->df_instr_prof);
9306 dfunc->df_instr_prof = NULL;
9307 }
9308#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009309
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009310 if (mark_deleted)
9311 dfunc->df_deleted = TRUE;
9312 if (dfunc->df_ufunc != NULL)
9313 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009314}
9315
9316/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009317 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009318 * function, unless another user function still uses it.
9319 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009320 */
9321 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009322unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009323{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009324 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009325 {
9326 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9327 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009328
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009329 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009330 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009331 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009332 ufunc->uf_dfunc_idx = 0;
9333 if (dfunc->df_ufunc == ufunc)
9334 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009335 }
9336}
9337
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009338/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009339 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009340 */
9341 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009342link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009343{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009344 if (ufunc->uf_dfunc_idx > 0)
9345 {
9346 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9347 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009348
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009349 ++dfunc->df_refcount;
9350 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009351}
9352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009353#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009354/*
9355 * Free all functions defined with ":def".
9356 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009357 void
9358free_def_functions(void)
9359{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009360 int idx;
9361
9362 for (idx = 0; idx < def_functions.ga_len; ++idx)
9363 {
9364 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9365
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009366 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009367 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009368 }
9369
9370 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009371}
9372#endif
9373
9374
9375#endif // FEAT_EVAL