blob: b005ff23657c3093e20e398adddca23b087e4516 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100277 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200338 static int
339script_var_exists(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200343 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200344 {
345 // Check script variables that were visible where the function was
346 // defined.
347 if (find_script_var(name, len, cctx) != NULL)
348 return OK;
349 }
350 else
351 {
352 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
353 dictitem_T *di;
354 int cc;
355
356 // Check script variables that are currently visible
357 cc = name[len];
358 name[len] = NUL;
359 di = find_var_in_ht(ht, 0, name, TRUE);
360 name[len] = cc;
361 if (di != NULL)
362 return OK;
363 }
364
365 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366}
367
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100368/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100369 * Return TRUE if "name" is a local variable, argument, script variable or
370 * imported.
371 */
372 static int
373variable_exists(char_u *name, size_t len, cctx_T *cctx)
374{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100375 return (cctx != NULL
376 && (lookup_local(name, len, NULL, cctx) == OK
377 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200378 || script_var_exists(name, len, cctx) == OK
Bram Moolenaare0890d62021-02-17 14:52:14 +0100379 || find_imported(name, len, cctx) != NULL;
380}
381
382/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100383 * Return TRUE if "name" is a local variable, argument, script variable,
384 * imported or function.
385 */
386 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100387item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100388{
389 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100390 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100391
392 if (variable_exists(name, len, cctx))
393 return TRUE;
394
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100395 // This is similar to what is in lookup_scriptitem():
396 // Find a function, so that a following "->" works.
397 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
398 // a function call.
399 p = skipwhite(name + len);
400
401 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
402 {
403 // Do not check for an internal function, since it might also be a
404 // valid command, such as ":split" versuse "split()".
405 // Skip "g:" before a function name.
406 is_global = (name[0] == 'g' && name[1] == ':');
407 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
408 }
409 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100410}
411
412/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100413 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200414 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200415 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100416 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100417 * Return FAIL and give an error if it defined.
418 */
419 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100420check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100421{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 int c = p[len];
423 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200424
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200425 if (script_var_exists(p, len, cctx) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100426 {
427 if (is_arg)
428 semsg(_(e_argument_already_declared_in_script_str), p);
429 else
430 semsg(_(e_variable_already_declared_in_script_str), p);
431 return FAIL;
432 }
433
Bram Moolenaarad486a02020-08-01 23:22:18 +0200434 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100435 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100436 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200437 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200438 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200439 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100440 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200441 // A local or script-local function can shadow a global function.
442 if (ufunc == NULL || !func_is_global(ufunc)
443 || (p[0] == 'g' && p[1] == ':'))
444 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 if (is_arg)
446 semsg(_(e_argument_name_shadows_existing_variable_str), p);
447 else
448 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200449 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200450 return FAIL;
451 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100452 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200453 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100454 return OK;
455}
456
Bram Moolenaar65b95452020-07-19 14:03:09 +0200457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458/////////////////////////////////////////////////////////////////////
459// Following generate_ functions expect the caller to call ga_grow().
460
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200461#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
462#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464/*
465 * Generate an instruction without arguments.
466 * Returns a pointer to the new instruction, NULL if failed.
467 */
468 static isn_T *
469generate_instr(cctx_T *cctx, isntype_T isn_type)
470{
471 garray_T *instr = &cctx->ctx_instr;
472 isn_T *isn;
473
Bram Moolenaar080457c2020-03-03 21:53:32 +0100474 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 if (ga_grow(instr, 1) == FAIL)
476 return NULL;
477 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
478 isn->isn_type = isn_type;
479 isn->isn_lnum = cctx->ctx_lnum + 1;
480 ++instr->ga_len;
481
482 return isn;
483}
484
485/*
486 * Generate an instruction without arguments.
487 * "drop" will be removed from the stack.
488 * Returns a pointer to the new instruction, NULL if failed.
489 */
490 static isn_T *
491generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
492{
493 garray_T *stack = &cctx->ctx_type_stack;
494
Bram Moolenaar080457c2020-03-03 21:53:32 +0100495 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 stack->ga_len -= drop;
497 return generate_instr(cctx, isn_type);
498}
499
500/*
501 * Generate instruction "isn_type" and put "type" on the type stack.
502 */
503 static isn_T *
504generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
505{
506 isn_T *isn;
507 garray_T *stack = &cctx->ctx_type_stack;
508
509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
510 return NULL;
511
512 if (ga_grow(stack, 1) == FAIL)
513 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200514 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 ++stack->ga_len;
516
517 return isn;
518}
519
520/*
521 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200522 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 */
524 static int
525may_generate_2STRING(int offset, cctx_T *cctx)
526{
527 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200528 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100530 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100532 RETURN_OK_IF_SKIP(cctx);
533 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200534 switch ((*type)->tt_type)
535 {
536 // nothing to be done
537 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200539 // conversion possible
540 case VAR_SPECIAL:
541 case VAR_BOOL:
542 case VAR_NUMBER:
543 case VAR_FLOAT:
544 break;
545
546 // conversion possible (with runtime check)
547 case VAR_ANY:
548 case VAR_UNKNOWN:
549 isntype = ISN_2STRING_ANY;
550 break;
551
552 // conversion not possible
553 case VAR_VOID:
554 case VAR_BLOB:
555 case VAR_FUNC:
556 case VAR_PARTIAL:
557 case VAR_LIST:
558 case VAR_DICT:
559 case VAR_JOB:
560 case VAR_CHANNEL:
561 to_string_error((*type)->tt_type);
562 return FAIL;
563 }
564
565 *type = &t_string;
566 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 return FAIL;
568 isn->isn_arg.number = offset;
569
570 return OK;
571}
572
573 static int
574check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
575{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200576 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200578 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 {
580 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200581 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200583 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200589 static int
590generate_add_instr(
591 cctx_T *cctx,
592 vartype_T vartype,
593 type_T *type1,
594 type_T *type2)
595{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100596 garray_T *stack = &cctx->ctx_type_stack;
597 isn_T *isn = generate_instr_drop(cctx,
598 vartype == VAR_NUMBER ? ISN_OPNR
599 : vartype == VAR_LIST ? ISN_ADDLIST
600 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200601#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100602 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200603#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100604 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605
606 if (vartype != VAR_LIST && vartype != VAR_BLOB
607 && type1->tt_type != VAR_ANY
608 && type2->tt_type != VAR_ANY
609 && check_number_or_float(
610 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
611 return FAIL;
612
613 if (isn != NULL)
614 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100615
616 // When concatenating two lists with different member types the member type
617 // becomes "any".
618 if (vartype == VAR_LIST
619 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
620 && type1->tt_member != type2->tt_member)
621 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
622
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200623 return isn == NULL ? FAIL : OK;
624}
625
626/*
627 * Get the type to use for an instruction for an operation on "type1" and
628 * "type2". If they are matching use a type-specific instruction. Otherwise
629 * fall back to runtime type checking.
630 */
631 static vartype_T
632operator_type(type_T *type1, type_T *type2)
633{
634 if (type1->tt_type == type2->tt_type
635 && (type1->tt_type == VAR_NUMBER
636 || type1->tt_type == VAR_LIST
637#ifdef FEAT_FLOAT
638 || type1->tt_type == VAR_FLOAT
639#endif
640 || type1->tt_type == VAR_BLOB))
641 return type1->tt_type;
642 return VAR_ANY;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
646 * Generate an instruction with two arguments. The instruction depends on the
647 * type of the arguments.
648 */
649 static int
650generate_two_op(cctx_T *cctx, char_u *op)
651{
652 garray_T *stack = &cctx->ctx_type_stack;
653 type_T *type1;
654 type_T *type2;
655 vartype_T vartype;
656 isn_T *isn;
657
Bram Moolenaar080457c2020-03-03 21:53:32 +0100658 RETURN_OK_IF_SKIP(cctx);
659
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200660 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
662 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200663 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
665 switch (*op)
666 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 case '+':
668 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 break;
671
672 case '-':
673 case '*':
674 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
675 op) == FAIL)
676 return FAIL;
677 if (vartype == VAR_NUMBER)
678 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
679#ifdef FEAT_FLOAT
680 else if (vartype == VAR_FLOAT)
681 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
682#endif
683 else
684 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
685 if (isn != NULL)
686 isn->isn_arg.op.op_type = *op == '*'
687 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
688 break;
689
Bram Moolenaar4c683752020-04-05 21:38:23 +0200690 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200692 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 && type2->tt_type != VAR_NUMBER))
694 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200695 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 return FAIL;
697 }
698 isn = generate_instr_drop(cctx,
699 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
700 if (isn != NULL)
701 isn->isn_arg.op.op_type = EXPR_REM;
702 break;
703 }
704
705 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200706 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 type_T *type = &t_any;
709
710#ifdef FEAT_FLOAT
711 // float+number and number+float results in float
712 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
713 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
714 type = &t_float;
715#endif
716 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
717 }
718
719 return OK;
720}
721
722/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 * Get the instruction to use for comparing "type1" with "type2"
724 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200726 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100727get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728{
729 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730
Bram Moolenaar4c683752020-04-05 21:38:23 +0200731 if (type1 == VAR_UNKNOWN)
732 type1 = VAR_ANY;
733 if (type2 == VAR_UNKNOWN)
734 type2 = VAR_ANY;
735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 if (type1 == type2)
737 {
738 switch (type1)
739 {
740 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
741 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
742 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
743 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
744 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
745 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
746 case VAR_LIST: isntype = ISN_COMPARELIST; break;
747 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
748 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 default: isntype = ISN_COMPAREANY; break;
750 }
751 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200752 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100754 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 isntype = ISN_COMPAREANY;
756
Bram Moolenaar657137c2021-01-09 15:45:23 +0100757 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 && (isntype == ISN_COMPAREBOOL
759 || isntype == ISN_COMPARESPECIAL
760 || isntype == ISN_COMPARENR
761 || isntype == ISN_COMPAREFLOAT))
762 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200763 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200765 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 }
767 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100768 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
770 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
772 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 && (type1 == VAR_BLOB || type2 == VAR_BLOB
774 || type1 == VAR_LIST || type2 == VAR_LIST))))
775 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200776 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200778 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200780 return isntype;
781}
782
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200783 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100784check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200785{
786 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
787 return FAIL;
788 return OK;
789}
790
Bram Moolenaara5565e42020-05-09 15:44:01 +0200791/*
792 * Generate an ISN_COMPARE* instruction with a boolean result.
793 */
794 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100795generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200796{
797 isntype_T isntype;
798 isn_T *isn;
799 garray_T *stack = &cctx->ctx_type_stack;
800 vartype_T type1;
801 vartype_T type2;
802
803 RETURN_OK_IF_SKIP(cctx);
804
805 // Get the known type of the two items on the stack. If they are matching
806 // use a type-specific instruction. Otherwise fall back to runtime type
807 // checking.
808 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
809 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100810 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200811 if (isntype == ISN_DROP)
812 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813
814 if ((isn = generate_instr(cctx, isntype)) == NULL)
815 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100816 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 isn->isn_arg.op.op_ic = ic;
818
819 // takes two arguments, puts one bool back
820 if (stack->ga_len >= 2)
821 {
822 --stack->ga_len;
823 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
824 }
825
826 return OK;
827}
828
829/*
830 * Generate an ISN_2BOOL instruction.
831 */
832 static int
833generate_2BOOL(cctx_T *cctx, int invert)
834{
835 isn_T *isn;
836 garray_T *stack = &cctx->ctx_type_stack;
837
Bram Moolenaar080457c2020-03-03 21:53:32 +0100838 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
840 return FAIL;
841 isn->isn_arg.number = invert;
842
843 // type becomes bool
844 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
845
846 return OK;
847}
848
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200849/*
850 * Generate an ISN_COND2BOOL instruction.
851 */
852 static int
853generate_COND2BOOL(cctx_T *cctx)
854{
855 isn_T *isn;
856 garray_T *stack = &cctx->ctx_type_stack;
857
858 RETURN_OK_IF_SKIP(cctx);
859 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
860 return FAIL;
861
862 // type becomes bool
863 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
864
865 return OK;
866}
867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200869generate_TYPECHECK(
870 cctx_T *cctx,
871 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100872 int offset,
873 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874{
875 isn_T *isn;
876 garray_T *stack = &cctx->ctx_type_stack;
877
Bram Moolenaar080457c2020-03-03 21:53:32 +0100878 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
880 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200881 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100882 isn->isn_arg.type.ct_off = (int8_T)offset;
883 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
Bram Moolenaar5e654232020-09-16 15:22:00 +0200885 // type becomes expected
886 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887
888 return OK;
889}
890
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100891 static int
892generate_SETTYPE(
893 cctx_T *cctx,
894 type_T *expected)
895{
896 isn_T *isn;
897
898 RETURN_OK_IF_SKIP(cctx);
899 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
900 return FAIL;
901 isn->isn_arg.type.ct_type = alloc_type(expected);
902 return OK;
903}
904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200906 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
907 * used. Return FALSE if the types will never match.
908 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100909 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200910use_typecheck(type_T *actual, type_T *expected)
911{
912 if (actual->tt_type == VAR_ANY
913 || actual->tt_type == VAR_UNKNOWN
914 || (actual->tt_type == VAR_FUNC
915 && (expected->tt_type == VAR_FUNC
916 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100917 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
918 && ((actual->tt_member == &t_void)
919 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200920 return TRUE;
921 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
922 && actual->tt_type == expected->tt_type)
923 // This takes care of a nested list or dict.
924 return use_typecheck(actual->tt_member, expected->tt_member);
925 return FALSE;
926}
927
928/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200929 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200930 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200931 * - "actual" is a type that can be "expected" type: add a runtime check; or
932 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200933 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
934 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200935 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100936 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200937need_type(
938 type_T *actual,
939 type_T *expected,
940 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100941 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200942 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200943 int silent,
944 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200945{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100946 where_T where;
947
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200948 if (expected == &t_bool && actual != &t_bool
949 && (actual->tt_flags & TTFLAG_BOOL_OK))
950 {
951 // Using "0", "1" or the result of an expression with "&&" or "||" as a
952 // boolean is OK but requires a conversion.
953 generate_2BOOL(cctx, FALSE);
954 return OK;
955 }
956
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100957 where.wt_index = arg_idx;
958 where.wt_variable = FALSE;
959 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200960 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200961
962 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200963 // If it's a constant a runtime check makes no sense.
964 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200965 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100966 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200967 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200968 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200969
970 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100971 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200972 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200973}
974
975/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100976 * Check that the top of the type stack has a type that can be used as a
977 * condition. Give an error and return FAIL if not.
978 */
979 static int
980bool_on_stack(cctx_T *cctx)
981{
982 garray_T *stack = &cctx->ctx_type_stack;
983 type_T *type;
984
985 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
986 if (type == &t_bool)
987 return OK;
988
989 if (type == &t_any || type == &t_number)
990 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
991 // This requires a runtime type check.
992 return generate_COND2BOOL(cctx);
993
Bram Moolenaar351ead02021-01-16 16:07:01 +0100994 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100995}
996
997/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 * Generate an ISN_PUSHNR instruction.
999 */
1000 static int
1001generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001004 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005
Bram Moolenaar080457c2020-03-03 21:53:32 +01001006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1008 return FAIL;
1009 isn->isn_arg.number = number;
1010
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001012 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001013 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 return OK;
1015}
1016
1017/*
1018 * Generate an ISN_PUSHBOOL instruction.
1019 */
1020 static int
1021generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1022{
1023 isn_T *isn;
1024
Bram Moolenaar080457c2020-03-03 21:53:32 +01001025 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.number = number;
1029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_PUSHSPEC instruction.
1035 */
1036 static int
1037generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.number = number;
1045
1046 return OK;
1047}
1048
1049#ifdef FEAT_FLOAT
1050/*
1051 * Generate an ISN_PUSHF instruction.
1052 */
1053 static int
1054generate_PUSHF(cctx_T *cctx, float_T fnumber)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.fnumber = fnumber;
1062
1063 return OK;
1064}
1065#endif
1066
1067/*
1068 * Generate an ISN_PUSHS instruction.
1069 * Consumes "str".
1070 */
1071 static int
1072generate_PUSHS(cctx_T *cctx, char_u *str)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar508b5612021-01-02 18:17:26 +01001076 if (cctx->ctx_skip == SKIP_YES)
1077 {
1078 vim_free(str);
1079 return OK;
1080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1082 return FAIL;
1083 isn->isn_arg.string = str;
1084
1085 return OK;
1086}
1087
1088/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001089 * Generate an ISN_PUSHCHANNEL instruction.
1090 * Consumes "channel".
1091 */
1092 static int
1093generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1094{
1095 isn_T *isn;
1096
Bram Moolenaar080457c2020-03-03 21:53:32 +01001097 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001098 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1099 return FAIL;
1100 isn->isn_arg.channel = channel;
1101
1102 return OK;
1103}
1104
1105/*
1106 * Generate an ISN_PUSHJOB instruction.
1107 * Consumes "job".
1108 */
1109 static int
1110generate_PUSHJOB(cctx_T *cctx, job_T *job)
1111{
1112 isn_T *isn;
1113
Bram Moolenaar080457c2020-03-03 21:53:32 +01001114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001115 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116 return FAIL;
1117 isn->isn_arg.job = job;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Generate an ISN_PUSHBLOB instruction.
1124 * Consumes "blob".
1125 */
1126 static int
1127generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1128{
1129 isn_T *isn;
1130
Bram Moolenaar080457c2020-03-03 21:53:32 +01001131 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1133 return FAIL;
1134 isn->isn_arg.blob = blob;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001140 * Generate an ISN_PUSHFUNC instruction with name "name".
1141 * Consumes "name".
1142 */
1143 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001144generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001145{
1146 isn_T *isn;
1147
Bram Moolenaar080457c2020-03-03 21:53:32 +01001148 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001149 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001150 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001151 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152
1153 return OK;
1154}
1155
1156/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001157 * Generate an ISN_GETITEM instruction with "index".
1158 */
1159 static int
1160generate_GETITEM(cctx_T *cctx, int index)
1161{
1162 isn_T *isn;
1163 garray_T *stack = &cctx->ctx_type_stack;
1164 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1165 type_T *item_type = &t_any;
1166
1167 RETURN_OK_IF_SKIP(cctx);
1168
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001169 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001170 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001171 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001172 emsg(_(e_listreq));
1173 return FAIL;
1174 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001175 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001176 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.number = index;
1179
1180 // add the item type to the type stack
1181 if (ga_grow(stack, 1) == FAIL)
1182 return FAIL;
1183 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1184 ++stack->ga_len;
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001189 * Generate an ISN_SLICE instruction with "count".
1190 */
1191 static int
1192generate_SLICE(cctx_T *cctx, int count)
1193{
1194 isn_T *isn;
1195
1196 RETURN_OK_IF_SKIP(cctx);
1197 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1198 return FAIL;
1199 isn->isn_arg.number = count;
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_CHECKLEN instruction with "min_len".
1205 */
1206 static int
1207generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1208{
1209 isn_T *isn;
1210
1211 RETURN_OK_IF_SKIP(cctx);
1212
1213 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.checklen.cl_min_len = min_len;
1216 isn->isn_arg.checklen.cl_more_OK = more_OK;
1217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 * Generate an ISN_STORE instruction.
1223 */
1224 static int
1225generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1226{
1227 isn_T *isn;
1228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1231 return FAIL;
1232 if (name != NULL)
1233 isn->isn_arg.string = vim_strsave(name);
1234 else
1235 isn->isn_arg.number = idx;
1236
1237 return OK;
1238}
1239
1240/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001241 * Generate an ISN_STOREOUTER instruction.
1242 */
1243 static int
1244generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1245{
1246 isn_T *isn;
1247
1248 RETURN_OK_IF_SKIP(cctx);
1249 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1250 return FAIL;
1251 isn->isn_arg.outer.outer_idx = idx;
1252 isn->isn_arg.outer.outer_depth = level;
1253
1254 return OK;
1255}
1256
1257/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1259 */
1260 static int
1261generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1262{
1263 isn_T *isn;
1264
Bram Moolenaar080457c2020-03-03 21:53:32 +01001265 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1267 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001268 isn->isn_arg.storenr.stnr_idx = idx;
1269 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 return OK;
1272}
1273
1274/*
1275 * Generate an ISN_STOREOPT instruction
1276 */
1277 static int
1278generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1279{
1280 isn_T *isn;
1281
Bram Moolenaar080457c2020-03-03 21:53:32 +01001282 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001283 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 return FAIL;
1285 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1286 isn->isn_arg.storeopt.so_flags = opt_flags;
1287
1288 return OK;
1289}
1290
1291/*
1292 * Generate an ISN_LOAD or similar instruction.
1293 */
1294 static int
1295generate_LOAD(
1296 cctx_T *cctx,
1297 isntype_T isn_type,
1298 int idx,
1299 char_u *name,
1300 type_T *type)
1301{
1302 isn_T *isn;
1303
Bram Moolenaar080457c2020-03-03 21:53:32 +01001304 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1306 return FAIL;
1307 if (name != NULL)
1308 isn->isn_arg.string = vim_strsave(name);
1309 else
1310 isn->isn_arg.number = idx;
1311
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001316 * Generate an ISN_LOADOUTER instruction
1317 */
1318 static int
1319generate_LOADOUTER(
1320 cctx_T *cctx,
1321 int idx,
1322 int nesting,
1323 type_T *type)
1324{
1325 isn_T *isn;
1326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1329 return FAIL;
1330 isn->isn_arg.outer.outer_idx = idx;
1331 isn->isn_arg.outer.outer_depth = nesting;
1332
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001337 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 */
1339 static int
1340generate_LOADV(
1341 cctx_T *cctx,
1342 char_u *name,
1343 int error)
1344{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001345 int di_flags;
1346 int vidx = find_vim_var(name, &di_flags);
1347 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348
Bram Moolenaar080457c2020-03-03 21:53:32 +01001349 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 if (vidx < 0)
1351 {
1352 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001353 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 return FAIL;
1355 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001358 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001359}
1360
1361/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001362 * Generate an ISN_UNLET instruction.
1363 */
1364 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001365generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001366{
1367 isn_T *isn;
1368
1369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001370 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001371 return FAIL;
1372 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1373 isn->isn_arg.unlet.ul_forceit = forceit;
1374
1375 return OK;
1376}
1377
1378/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001379 * Generate an ISN_LOCKCONST instruction.
1380 */
1381 static int
1382generate_LOCKCONST(cctx_T *cctx)
1383{
1384 isn_T *isn;
1385
1386 RETURN_OK_IF_SKIP(cctx);
1387 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1388 return FAIL;
1389 return OK;
1390}
1391
1392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 * Generate an ISN_LOADS instruction.
1394 */
1395 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001396generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001398 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001400 int sid,
1401 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402{
1403 isn_T *isn;
1404
Bram Moolenaar080457c2020-03-03 21:53:32 +01001405 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001406 if (isn_type == ISN_LOADS)
1407 isn = generate_instr_type(cctx, isn_type, type);
1408 else
1409 isn = generate_instr_drop(cctx, isn_type, 1);
1410 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001412 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1413 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414
1415 return OK;
1416}
1417
1418/*
1419 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1420 */
1421 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 cctx_T *cctx,
1424 isntype_T isn_type,
1425 int sid,
1426 int idx,
1427 type_T *type)
1428{
1429 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001430 scriptref_T *sref;
1431 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432
Bram Moolenaar080457c2020-03-03 21:53:32 +01001433 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 if (isn_type == ISN_LOADSCRIPT)
1435 isn = generate_instr_type(cctx, isn_type, type);
1436 else
1437 isn = generate_instr_drop(cctx, isn_type, 1);
1438 if (isn == NULL)
1439 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001440
1441 // This requires three arguments, which doesn't fit in an instruction, thus
1442 // we need to allocate a struct for this.
1443 sref = ALLOC_ONE(scriptref_T);
1444 if (sref == NULL)
1445 return FAIL;
1446 isn->isn_arg.script.scriptref = sref;
1447 sref->sref_sid = sid;
1448 sref->sref_idx = idx;
1449 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001450 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 return OK;
1452}
1453
1454/*
1455 * Generate an ISN_NEWLIST instruction.
1456 */
1457 static int
1458generate_NEWLIST(cctx_T *cctx, int count)
1459{
1460 isn_T *isn;
1461 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 type_T *type;
1463 type_T *member;
1464
Bram Moolenaar080457c2020-03-03 21:53:32 +01001465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1467 return FAIL;
1468 isn->isn_arg.number = count;
1469
Bram Moolenaar127542b2020-08-09 17:22:04 +02001470 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001471 if (count == 0)
1472 member = &t_void;
1473 else
1474 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001475 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1476 cctx->ctx_type_list);
1477 type = get_list_type(member, cctx->ctx_type_list);
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // drop the value types
1480 stack->ga_len -= count;
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 // add the list type to the type stack
1483 if (ga_grow(stack, 1) == FAIL)
1484 return FAIL;
1485 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1486 ++stack->ga_len;
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_NEWDICT instruction.
1493 */
1494 static int
1495generate_NEWDICT(cctx_T *cctx, int count)
1496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 type_T *type;
1500 type_T *member;
1501
Bram Moolenaar080457c2020-03-03 21:53:32 +01001502 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1504 return FAIL;
1505 isn->isn_arg.number = count;
1506
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001507 if (count == 0)
1508 member = &t_void;
1509 else
1510 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001511 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1512 cctx->ctx_type_list);
1513 type = get_dict_type(member, cctx->ctx_type_list);
1514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 // drop the key and value types
1516 stack->ga_len -= 2 * count;
1517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // add the dict type to the type stack
1519 if (ga_grow(stack, 1) == FAIL)
1520 return FAIL;
1521 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1522 ++stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
1528 * Generate an ISN_FUNCREF instruction.
1529 */
1530 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001531generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
Bram Moolenaar080457c2020-03-03 21:53:32 +01001536 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1538 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001539 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001540 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001542 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001543 // the nested context, including this one.
1544 if (ufunc->uf_flags & FC_CLOSURE)
1545 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 if (ga_grow(stack, 1) == FAIL)
1548 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001549 ((type_T **)stack->ga_data)[stack->ga_len] =
1550 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 ++stack->ga_len;
1552
1553 return OK;
1554}
1555
1556/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001558 * "lambda_name" and "func_name" must be in allocated memory and will be
1559 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001560 */
1561 static int
1562generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1563{
1564 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001565
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001566 if (cctx->ctx_skip == SKIP_YES)
1567 {
1568 vim_free(lambda_name);
1569 vim_free(func_name);
1570 return OK;
1571 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 {
1574 vim_free(lambda_name);
1575 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001576 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001577 }
1578 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 isn->isn_arg.newfunc.nf_global = func_name;
1580
1581 return OK;
1582}
1583
1584/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001585 * Generate an ISN_DEF instruction: list functions
1586 */
1587 static int
1588generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1589{
1590 isn_T *isn;
1591
1592 RETURN_OK_IF_SKIP(cctx);
1593 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1594 return FAIL;
1595 if (len > 0)
1596 {
1597 isn->isn_arg.string = vim_strnsave(name, len);
1598 if (isn->isn_arg.string == NULL)
1599 return FAIL;
1600 }
1601 return OK;
1602}
1603
1604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 * Generate an ISN_JUMP instruction.
1606 */
1607 static int
1608generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1609{
1610 isn_T *isn;
1611 garray_T *stack = &cctx->ctx_type_stack;
1612
Bram Moolenaar080457c2020-03-03 21:53:32 +01001613 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1615 return FAIL;
1616 isn->isn_arg.jump.jump_when = when;
1617 isn->isn_arg.jump.jump_where = where;
1618
1619 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1620 --stack->ga_len;
1621
1622 return OK;
1623}
1624
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001625/*
1626 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1627 */
1628 static int
1629generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1630{
1631 isn_T *isn;
1632
1633 RETURN_OK_IF_SKIP(cctx);
1634 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1637 // jump_where is set later
1638 return OK;
1639}
1640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 static int
1642generate_FOR(cctx_T *cctx, int loop_idx)
1643{
1644 isn_T *isn;
1645 garray_T *stack = &cctx->ctx_type_stack;
1646
Bram Moolenaar080457c2020-03-03 21:53:32 +01001647 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.forloop.for_idx = loop_idx;
1651
1652 if (ga_grow(stack, 1) == FAIL)
1653 return FAIL;
1654 // type doesn't matter, will be stored next
1655 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1656 ++stack->ga_len;
1657
1658 return OK;
1659}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001660/*
1661 * Generate an ISN_TRYCONT instruction.
1662 */
1663 static int
1664generate_TRYCONT(cctx_T *cctx, int levels, int where)
1665{
1666 isn_T *isn;
1667
1668 RETURN_OK_IF_SKIP(cctx);
1669 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1670 return FAIL;
1671 isn->isn_arg.trycont.tct_levels = levels;
1672 isn->isn_arg.trycont.tct_where = where;
1673
1674 return OK;
1675}
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677
1678/*
1679 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001680 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 * Return FAIL if the number of arguments is wrong.
1682 */
1683 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001684generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685{
1686 isn_T *isn;
1687 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001689 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001690 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar080457c2020-03-03 21:53:32 +01001692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001693 argoff = check_internal_func(func_idx, argcount);
1694 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 return FAIL;
1696
Bram Moolenaar389df252020-07-09 21:20:47 +02001697 if (method_call && argoff > 1)
1698 {
1699 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1700 return FAIL;
1701 isn->isn_arg.shuffle.shfl_item = argcount;
1702 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1703 }
1704
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 if (argcount > 0)
1706 {
1707 // Check the types of the arguments.
1708 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001709 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1710 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001711 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001712 if (internal_func_is_map(func_idx))
1713 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001714 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1717 return FAIL;
1718 isn->isn_arg.bfunc.cbf_idx = func_idx;
1719 isn->isn_arg.bfunc.cbf_argcount = argcount;
1720
Bram Moolenaar94738d82020-10-21 14:25:07 +02001721 // Drop the argument types and push the return type.
1722 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 if (ga_grow(stack, 1) == FAIL)
1724 return FAIL;
1725 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001726 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001727 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001729 if (maptype != NULL && maptype->tt_member != NULL
1730 && maptype->tt_member != &t_any)
1731 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001732 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 return OK;
1735}
1736
1737/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001738 * Generate an ISN_LISTAPPEND instruction. Works like add().
1739 * Argument count is already checked.
1740 */
1741 static int
1742generate_LISTAPPEND(cctx_T *cctx)
1743{
1744 garray_T *stack = &cctx->ctx_type_stack;
1745 type_T *list_type;
1746 type_T *item_type;
1747 type_T *expected;
1748
1749 // Caller already checked that list_type is a list.
1750 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1751 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1752 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001753 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001754 return FAIL;
1755
1756 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1757 return FAIL;
1758
1759 --stack->ga_len; // drop the argument
1760 return OK;
1761}
1762
1763/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001764 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1765 * Argument count is already checked.
1766 */
1767 static int
1768generate_BLOBAPPEND(cctx_T *cctx)
1769{
1770 garray_T *stack = &cctx->ctx_type_stack;
1771 type_T *item_type;
1772
1773 // Caller already checked that blob_type is a blob.
1774 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001775 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001776 return FAIL;
1777
1778 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1779 return FAIL;
1780
1781 --stack->ga_len; // drop the argument
1782 return OK;
1783}
1784
1785/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 * Return TRUE if "ufunc" should be compiled, taking into account whether
1787 * "profile" indicates profiling is to be done.
1788 */
1789 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001790func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001791{
1792 switch (ufunc->uf_def_status)
1793 {
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001794 case UF_TO_BE_COMPILED:
1795 return TRUE;
1796
Bram Moolenaarb2049902021-01-24 12:53:53 +01001797 case UF_COMPILED:
1798 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001799#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001800 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1801 + ufunc->uf_dfunc_idx;
1802
1803 return profile ? dfunc->df_instr_prof == NULL
1804 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001805#else
1806 break;
1807#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001808 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001809
1810 case UF_NOT_COMPILED:
1811 case UF_COMPILE_ERROR:
1812 case UF_COMPILING:
1813 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001814 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001815 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001816}
1817
1818/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 * Generate an ISN_DCALL or ISN_UCALL instruction.
1820 * Return FAIL if the number of arguments is wrong.
1821 */
1822 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001823generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824{
1825 isn_T *isn;
1826 garray_T *stack = &cctx->ctx_type_stack;
1827 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001828 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaar080457c2020-03-03 21:53:32 +01001830 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 if (argcount > regular_args && !has_varargs(ufunc))
1832 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001833 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 return FAIL;
1835 }
1836 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1837 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001838 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 return FAIL;
1840 }
1841
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02001842 if (ufunc->uf_def_status != UF_NOT_COMPILED
1843 && ufunc->uf_def_status != UF_COMPILE_ERROR)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001844 {
1845 int i;
1846
1847 for (i = 0; i < argcount; ++i)
1848 {
1849 type_T *expected;
1850 type_T *actual;
1851
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001852 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
1853 if (actual == &t_special
1854 && i >= regular_args - ufunc->uf_def_args.ga_len)
1855 {
1856 // assume v:none used for default argument value
1857 continue;
1858 }
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001859 if (i < regular_args)
1860 {
1861 if (ufunc->uf_arg_types == NULL)
1862 continue;
1863 expected = ufunc->uf_arg_types[i];
1864 }
Bram Moolenaar2a389082021-04-09 20:24:31 +02001865 else if (ufunc->uf_va_type == NULL
1866 || ufunc->uf_va_type == &t_list_any)
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001867 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001868 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001869 else
1870 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaare32e5162021-01-21 20:21:29 +01001871 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001872 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001873 {
1874 arg_type_mismatch(expected, actual, i + 1);
1875 return FAIL;
1876 }
1877 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001878 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001879 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001880 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001881 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001882 }
1883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001885 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001886 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001888 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 {
1890 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1891 isn->isn_arg.dfunc.cdf_argcount = argcount;
1892 }
1893 else
1894 {
1895 // A user function may be deleted and redefined later, can't use the
1896 // ufunc pointer, need to look it up again at runtime.
1897 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1898 isn->isn_arg.ufunc.cuf_argcount = argcount;
1899 }
1900
1901 stack->ga_len -= argcount; // drop the arguments
1902 if (ga_grow(stack, 1) == FAIL)
1903 return FAIL;
1904 // add return value
1905 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1906 ++stack->ga_len;
1907
1908 return OK;
1909}
1910
1911/*
1912 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1913 */
1914 static int
1915generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1916{
1917 isn_T *isn;
1918 garray_T *stack = &cctx->ctx_type_stack;
1919
Bram Moolenaar080457c2020-03-03 21:53:32 +01001920 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1922 return FAIL;
1923 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1924 isn->isn_arg.ufunc.cuf_argcount = argcount;
1925
1926 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001927 if (ga_grow(stack, 1) == FAIL)
1928 return FAIL;
1929 // add return value
1930 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1931 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932
1933 return OK;
1934}
1935
1936/*
1937 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001938 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 */
1940 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001941generate_PCALL(
1942 cctx_T *cctx,
1943 int argcount,
1944 char_u *name,
1945 type_T *type,
1946 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947{
1948 isn_T *isn;
1949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001950 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951
Bram Moolenaar080457c2020-03-03 21:53:32 +01001952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001953
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001954 if (type->tt_type == VAR_ANY)
1955 ret_type = &t_any;
1956 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001957 {
1958 if (type->tt_argcount != -1)
1959 {
1960 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1961
1962 if (argcount < type->tt_min_argcount - varargs)
1963 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001964 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001965 return FAIL;
1966 }
1967 if (!varargs && argcount > type->tt_argcount)
1968 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001969 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001970 return FAIL;
1971 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001972 if (type->tt_args != NULL)
1973 {
1974 int i;
1975
1976 for (i = 0; i < argcount; ++i)
1977 {
Bram Moolenaar1088b692021-04-09 22:12:44 +02001978 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001979 type_T *actual = ((type_T **)stack->ga_data)[
1980 stack->ga_len + offset];
1981 type_T *expected;
1982
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001983 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001984 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001985 type->tt_argcount - 1]->tt_member;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001986 else if (i >= type->tt_min_argcount
1987 && actual == &t_special)
1988 expected = &t_any;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001989 else
1990 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001991 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001992 cctx, TRUE, FALSE) == FAIL)
1993 {
1994 arg_type_mismatch(expected, actual, i + 1);
1995 return FAIL;
1996 }
1997 }
1998 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001999 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002000 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02002001 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002002 else
2003 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002004 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002005 return FAIL;
2006 }
2007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2009 return FAIL;
2010 isn->isn_arg.pfunc.cpf_top = at_top;
2011 isn->isn_arg.pfunc.cpf_argcount = argcount;
2012
2013 stack->ga_len -= argcount; // drop the arguments
2014
2015 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002016 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002018 // If partial is above the arguments it must be cleared and replaced with
2019 // the return value.
2020 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2021 return FAIL;
2022
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 return OK;
2024}
2025
2026/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002027 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 */
2029 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031{
2032 isn_T *isn;
2033 garray_T *stack = &cctx->ctx_type_stack;
2034 type_T *type;
2035
Bram Moolenaar080457c2020-03-03 21:53:32 +01002036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002037 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002039 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002041 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002043 if (type->tt_type != VAR_DICT && type != &t_any)
2044 {
2045 emsg(_(e_dictreq));
2046 return FAIL;
2047 }
2048 // change dict type to dict member type
2049 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002050 {
2051 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2052 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
2055 return OK;
2056}
2057
2058/*
2059 * Generate an ISN_ECHO instruction.
2060 */
2061 static int
2062generate_ECHO(cctx_T *cctx, int with_white, int count)
2063{
2064 isn_T *isn;
2065
Bram Moolenaar080457c2020-03-03 21:53:32 +01002066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.echo.echo_with_white = with_white;
2070 isn->isn_arg.echo.echo_count = count;
2071
2072 return OK;
2073}
2074
Bram Moolenaarad39c092020-02-26 18:23:43 +01002075/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002076 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077 */
2078 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002079generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080{
2081 isn_T *isn;
2082
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002083 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002084 return FAIL;
2085 isn->isn_arg.number = count;
2086
2087 return OK;
2088}
2089
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002090/*
2091 * Generate an ISN_PUT instruction.
2092 */
2093 static int
2094generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2095{
2096 isn_T *isn;
2097
2098 RETURN_OK_IF_SKIP(cctx);
2099 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2100 return FAIL;
2101 isn->isn_arg.put.put_regname = regname;
2102 isn->isn_arg.put.put_lnum = lnum;
2103 return OK;
2104}
2105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 static int
2107generate_EXEC(cctx_T *cctx, char_u *line)
2108{
2109 isn_T *isn;
2110
Bram Moolenaar080457c2020-03-03 21:53:32 +01002111 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2113 return FAIL;
2114 isn->isn_arg.string = vim_strsave(line);
2115 return OK;
2116}
2117
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002118 static int
2119generate_EXECCONCAT(cctx_T *cctx, int count)
2120{
2121 isn_T *isn;
2122
2123 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2124 return FAIL;
2125 isn->isn_arg.number = count;
2126 return OK;
2127}
2128
Bram Moolenaar08597872020-12-10 19:43:40 +01002129/*
2130 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2131 */
2132 static int
2133generate_RANGE(cctx_T *cctx, char_u *range)
2134{
2135 isn_T *isn;
2136 garray_T *stack = &cctx->ctx_type_stack;
2137
2138 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2139 return FAIL;
2140 isn->isn_arg.string = range;
2141
2142 if (ga_grow(stack, 1) == FAIL)
2143 return FAIL;
2144 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2145 ++stack->ga_len;
2146 return OK;
2147}
2148
Bram Moolenaar792f7862020-11-23 08:31:18 +01002149 static int
2150generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2151{
2152 isn_T *isn;
2153
2154 RETURN_OK_IF_SKIP(cctx);
2155 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2156 return FAIL;
2157 isn->isn_arg.unpack.unp_count = var_count;
2158 isn->isn_arg.unpack.unp_semicolon = semicolon;
2159 return OK;
2160}
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002163 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164 */
2165 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002166generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002167{
2168 isn_T *isn;
2169
Bram Moolenaarfa984412021-03-25 22:15:28 +01002170 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002171 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002172 cctx->ctx_has_cmdmod = TRUE;
2173
2174 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002175 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002176 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2177 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2178 return FAIL;
2179 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002180 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002181 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002182 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002183
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002184 return OK;
2185}
2186
2187 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002188generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002189{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002190 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2191 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002192 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002193 return OK;
2194}
2195
Bram Moolenaarfa984412021-03-25 22:15:28 +01002196 static int
2197misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002198{
2199 garray_T *instr = &cctx->ctx_instr;
2200
Bram Moolenaara91a7132021-03-25 21:12:15 +01002201 if (cctx->ctx_has_cmdmod
2202 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2203 == ISN_CMDMOD)
2204 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002205 emsg(_(e_misplaced_command_modifier));
2206 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002207 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002208 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002209}
2210
2211/*
2212 * Get the index of the current instruction.
2213 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2214 */
2215 static int
2216current_instr_idx(cctx_T *cctx)
2217{
2218 garray_T *instr = &cctx->ctx_instr;
2219 int idx = instr->ga_len;
2220
2221 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2222 .isn_type == ISN_CMDMOD)
2223 --idx;
2224#ifdef FEAT_PROFILE
2225 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2226 .isn_type == ISN_PROF_START)
2227 --idx;
2228#endif
2229 return idx;
2230}
2231
Bram Moolenaarf002a412021-01-24 13:34:18 +01002232#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002233 static void
2234may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2235{
2236 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002237 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002238}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002239#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002240
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002241/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002243 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002245 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002246reserve_local(
2247 cctx_T *cctx,
2248 char_u *name,
2249 size_t len,
2250 int isConst,
2251 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 lvar_T *lvar;
2254
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002255 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002257 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002258 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260
2261 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002262 return NULL;
2263 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002264 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002266 // Every local variable uses the next entry on the stack. We could re-use
2267 // the last ones when leaving a scope, but then variables used in a closure
2268 // might get overwritten. To keep things simple do not re-use stack
2269 // entries. This is less efficient, but memory is cheap these days.
2270 lvar->lv_idx = cctx->ctx_locals_count++;
2271
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002272 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 lvar->lv_const = isConst;
2274 lvar->lv_type = type;
2275
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002276 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277}
2278
2279/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002280 * Remove local variables above "new_top".
2281 */
2282 static void
2283unwind_locals(cctx_T *cctx, int new_top)
2284{
2285 if (cctx->ctx_locals.ga_len > new_top)
2286 {
2287 int idx;
2288 lvar_T *lvar;
2289
2290 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2291 {
2292 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2293 vim_free(lvar->lv_name);
2294 }
2295 }
2296 cctx->ctx_locals.ga_len = new_top;
2297}
2298
2299/*
2300 * Free all local variables.
2301 */
2302 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002303free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002304{
2305 unwind_locals(cctx, 0);
2306 ga_clear(&cctx->ctx_locals);
2307}
2308
2309/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002310 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2311 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2312 * error if the variable was defined with :const.
2313 */
2314 static int
2315check_item_writable(svar_T *sv, int check_writable, char_u *name)
2316{
2317 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2318 || (check_writable == ASSIGN_FINAL
2319 && sv->sv_const == ASSIGN_CONST))
2320 {
2321 semsg(_(e_readonlyvar), name);
2322 return FAIL;
2323 }
2324 return OK;
2325}
2326
2327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002329 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 * Returns the index in "sn_var_vals" if found.
2331 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002332 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 */
2334 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002335get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336{
2337 hashtab_T *ht;
2338 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002339 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002340 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 int idx;
2342
Bram Moolenaare3d46852020-08-29 13:39:17 +02002343 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002345 if (sid == current_sctx.sc_sid)
2346 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002347 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002348
2349 if (sav == NULL)
2350 return -2;
2351 idx = sav->sav_var_vals_idx;
2352 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002353 if (check_item_writable(sv, check_writable, name) == FAIL)
2354 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002355 return idx;
2356 }
2357
2358 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 ht = &SCRIPT_VARS(sid);
2360 di = find_var_in_ht(ht, 0, name, TRUE);
2361 if (di == NULL)
2362 return -2;
2363
2364 // Now find the svar_T index in sn_var_vals.
2365 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2366 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002367 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 if (sv->sv_tv == &di->di_tv)
2369 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002370 if (check_item_writable(sv, check_writable, name) == FAIL)
2371 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 return idx;
2373 }
2374 }
2375 return -1;
2376}
2377
2378/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002379 * Find "name" in imported items of the current script or in "cctx" if not
2380 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 */
2382 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002383find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 int idx;
2386
Bram Moolenaare3d46852020-08-29 13:39:17 +02002387 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002388 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 if (cctx != NULL)
2390 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2391 {
2392 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2393 + idx;
2394
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002395 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2396 : STRLEN(import->imp_name) == len
2397 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 return import;
2399 }
2400
Bram Moolenaarefa94442020-08-08 22:16:00 +02002401 return find_imported_in_script(name, len, current_sctx.sc_sid);
2402}
2403
2404 imported_T *
2405find_imported_in_script(char_u *name, size_t len, int sid)
2406{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002407 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002408 int idx;
2409
Bram Moolenaare3d46852020-08-29 13:39:17 +02002410 if (!SCRIPT_ID_VALID(sid))
2411 return NULL;
2412 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2414 {
2415 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2416
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002417 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2418 : STRLEN(import->imp_name) == len
2419 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 return import;
2421 }
2422 return NULL;
2423}
2424
2425/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002426 * Free all imported variables.
2427 */
2428 static void
2429free_imported(cctx_T *cctx)
2430{
2431 int idx;
2432
2433 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2434 {
2435 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2436
2437 vim_free(import->imp_name);
2438 }
2439 ga_clear(&cctx->ctx_imports);
2440}
2441
2442/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002443 * Return a pointer to the next line that isn't empty or only contains a
2444 * comment. Skips over white space.
2445 * Returns NULL if there is none.
2446 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002447 char_u *
2448peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002449{
2450 int lnum = cctx->ctx_lnum;
2451
2452 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2453 {
2454 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002455 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002456
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002457 // ignore NULLs inserted for continuation lines
2458 if (line != NULL)
2459 {
2460 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002461 if (vim9_bad_comment(p))
2462 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002463 if (*p != NUL && !vim9_comment_start(p))
2464 return p;
2465 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002466 }
2467 return NULL;
2468}
2469
2470/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002471 * Called when checking for a following operator at "arg". When the rest of
2472 * the line is empty or only a comment, peek the next line. If there is a next
2473 * line return a pointer to it and set "nextp".
2474 * Otherwise skip over white space.
2475 */
2476 static char_u *
2477may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2478{
2479 char_u *p = skipwhite(arg);
2480
2481 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002482 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002483 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002484 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002485 if (*nextp != NULL)
2486 return *nextp;
2487 }
2488 return p;
2489}
2490
2491/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002492 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002493 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002494 * Returns NULL when at the end.
2495 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002496 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002497next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002498{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002499 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002500
2501 do
2502 {
2503 ++cctx->ctx_lnum;
2504 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002505 {
2506 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002507 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002508 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002509 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002510 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002511 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002512 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002513 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002514 return line;
2515}
2516
2517/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002518 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002519 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002520 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002521 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2522 */
2523 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002524may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002525{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002526 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002527 if (vim9_bad_comment(*arg))
2528 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002529 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002530 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002531 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002532
2533 if (next == NULL)
2534 return FAIL;
2535 *arg = skipwhite(next);
2536 }
2537 return OK;
2538}
2539
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002540/*
2541 * Idem, and give an error when failed.
2542 */
2543 static int
2544may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2545{
2546 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2547 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002548 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002549 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002550 return FAIL;
2551 }
2552 return OK;
2553}
2554
2555
Bram Moolenaara5565e42020-05-09 15:44:01 +02002556// Structure passed between the compile_expr* functions to keep track of
2557// constants that have been parsed but for which no code was produced yet. If
2558// possible expressions on these constants are applied at compile time. If
2559// that is not possible, the code to push the constants needs to be generated
2560// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002561// Using 50 should be more than enough of 5 levels of ().
2562#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002563typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002564 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002565 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002566 int pp_is_const; // all generated code was constants, used for a
2567 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002568} ppconst_T;
2569
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002570static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002571static int compile_expr0(char_u **arg, cctx_T *cctx);
2572static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2573
Bram Moolenaara5565e42020-05-09 15:44:01 +02002574/*
2575 * Generate a PUSH instruction for "tv".
2576 * "tv" will be consumed or cleared.
2577 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2578 */
2579 static int
2580generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2581{
2582 if (tv != NULL)
2583 {
2584 switch (tv->v_type)
2585 {
2586 case VAR_UNKNOWN:
2587 break;
2588 case VAR_BOOL:
2589 generate_PUSHBOOL(cctx, tv->vval.v_number);
2590 break;
2591 case VAR_SPECIAL:
2592 generate_PUSHSPEC(cctx, tv->vval.v_number);
2593 break;
2594 case VAR_NUMBER:
2595 generate_PUSHNR(cctx, tv->vval.v_number);
2596 break;
2597#ifdef FEAT_FLOAT
2598 case VAR_FLOAT:
2599 generate_PUSHF(cctx, tv->vval.v_float);
2600 break;
2601#endif
2602 case VAR_BLOB:
2603 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2604 tv->vval.v_blob = NULL;
2605 break;
2606 case VAR_STRING:
2607 generate_PUSHS(cctx, tv->vval.v_string);
2608 tv->vval.v_string = NULL;
2609 break;
2610 default:
2611 iemsg("constant type not supported");
2612 clear_tv(tv);
2613 return FAIL;
2614 }
2615 tv->v_type = VAR_UNKNOWN;
2616 }
2617 return OK;
2618}
2619
2620/*
2621 * Generate code for any ppconst entries.
2622 */
2623 static int
2624generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2625{
2626 int i;
2627 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002628 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002629
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002630 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002631 for (i = 0; i < ppconst->pp_used; ++i)
2632 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2633 ret = FAIL;
2634 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002635 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002636 return ret;
2637}
2638
2639/*
2640 * Clear ppconst constants. Used when failing.
2641 */
2642 static void
2643clear_ppconst(ppconst_T *ppconst)
2644{
2645 int i;
2646
2647 for (i = 0; i < ppconst->pp_used; ++i)
2648 clear_tv(&ppconst->pp_tv[i]);
2649 ppconst->pp_used = 0;
2650}
2651
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002652/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002653 * Compile getting a member from a list/dict/string/blob. Stack has the
2654 * indexable value and the index.
2655 */
2656 static int
2657compile_member(int is_slice, cctx_T *cctx)
2658{
2659 type_T **typep;
2660 garray_T *stack = &cctx->ctx_type_stack;
2661 vartype_T vtype;
2662 type_T *valtype;
2663
2664 // We can index a list and a dict. If we don't know the type
2665 // we can use the index value type.
2666 // TODO: If we don't know use an instruction to figure it out at
2667 // runtime.
2668 typep = ((type_T **)stack->ga_data) + stack->ga_len
2669 - (is_slice ? 3 : 2);
2670 vtype = (*typep)->tt_type;
2671 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2672 // If the index is a string, the variable must be a Dict.
2673 if (*typep == &t_any && valtype == &t_string)
2674 vtype = VAR_DICT;
2675 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
2676 {
2677 if (need_type(valtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
2678 return FAIL;
2679 if (is_slice)
2680 {
2681 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
2682 if (need_type(valtype, &t_number, -2, 0, cctx,
2683 FALSE, FALSE) == FAIL)
2684 return FAIL;
2685 }
2686 }
2687
2688 if (vtype == VAR_DICT)
2689 {
2690 if (is_slice)
2691 {
2692 emsg(_(e_cannot_slice_dictionary));
2693 return FAIL;
2694 }
2695 if ((*typep)->tt_type == VAR_DICT)
2696 {
2697 *typep = (*typep)->tt_member;
2698 if (*typep == &t_unknown)
2699 // empty dict was used
2700 *typep = &t_any;
2701 }
2702 else
2703 {
2704 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
2705 FALSE, FALSE) == FAIL)
2706 return FAIL;
2707 *typep = &t_any;
2708 }
2709 if (may_generate_2STRING(-1, cctx) == FAIL)
2710 return FAIL;
2711 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
2712 return FAIL;
2713 }
2714 else if (vtype == VAR_STRING)
2715 {
2716 *typep = &t_string;
2717 if ((is_slice
2718 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
2719 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
2720 return FAIL;
2721 }
2722 else if (vtype == VAR_BLOB)
2723 {
2724 emsg("Sorry, blob index and slice not implemented yet");
2725 return FAIL;
2726 }
2727 else if (vtype == VAR_LIST || *typep == &t_any)
2728 {
2729 if (is_slice)
2730 {
2731 if (generate_instr_drop(cctx,
2732 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2733 2) == FAIL)
2734 return FAIL;
2735 }
2736 else
2737 {
2738 if ((*typep)->tt_type == VAR_LIST)
2739 {
2740 *typep = (*typep)->tt_member;
2741 if (*typep == &t_unknown)
2742 // empty list was used
2743 *typep = &t_any;
2744 }
2745 if (generate_instr_drop(cctx,
2746 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1) == FAIL)
2747 return FAIL;
2748 }
2749 }
2750 else
2751 {
2752 emsg(_(e_string_list_dict_or_blob_required));
2753 return FAIL;
2754 }
2755 return OK;
2756}
2757
2758/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002759 * Generate an instruction to load script-local variable "name", without the
2760 * leading "s:".
2761 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 */
2763 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002764compile_load_scriptvar(
2765 cctx_T *cctx,
2766 char_u *name, // variable NUL terminated
2767 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002768 char_u **end, // end of variable
2769 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002771 scriptitem_T *si;
2772 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 imported_T *import;
2774
Bram Moolenaare3d46852020-08-29 13:39:17 +02002775 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2776 return FAIL;
2777 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002778 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002779 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002781 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002782 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2783 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 }
2785 if (idx >= 0)
2786 {
2787 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2788
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002789 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 current_sctx.sc_sid, idx, sv->sv_type);
2791 return OK;
2792 }
2793
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002794 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 if (import != NULL)
2796 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002797 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002798 {
2799 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002800 char_u *exp_name;
2801 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002802 ufunc_T *ufunc;
2803 type_T *type;
2804
2805 // Used "import * as Name", need to lookup the member.
2806 if (*p != '.')
2807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002808 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002809 return FAIL;
2810 }
2811 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002812 if (VIM_ISWHITE(*p))
2813 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002814 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002815 return FAIL;
2816 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002817
Bram Moolenaar1c991142020-07-04 13:15:31 +02002818 // isolate one name
2819 exp_name = p;
2820 while (eval_isnamec(*p))
2821 ++p;
2822 cc = *p;
2823 *p = NUL;
2824
Bram Moolenaaredba7072021-03-13 21:14:18 +01002825 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2826 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002827 *p = cc;
2828 p = skipwhite(p);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002829 *end = p;
2830
Bram Moolenaar529fb5a2021-04-01 12:57:57 +02002831 if (idx < 0)
2832 {
2833 if (*p == '(' && ufunc != NULL)
2834 {
2835 generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
2836 return OK;
2837 }
2838 return FAIL;
2839 }
2840
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002841 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2842 import->imp_sid,
2843 idx,
2844 type);
2845 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002846 else if (import->imp_funcname != NULL)
2847 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002848 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002849 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2850 import->imp_sid,
2851 import->imp_var_vals_idx,
2852 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 return OK;
2854 }
2855
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002856 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002857 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 return FAIL;
2859}
2860
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002861 static int
2862generate_funcref(cctx_T *cctx, char_u *name)
2863{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002864 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002865
2866 if (ufunc == NULL)
2867 return FAIL;
2868
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002869 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002870 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2871 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002872 == FAIL)
2873 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002874 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002875}
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877/*
2878 * Compile a variable name into a load instruction.
2879 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002880 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 * When "error" is FALSE do not give an error when not found.
2882 */
2883 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002884compile_load(
2885 char_u **arg,
2886 char_u *end_arg,
2887 cctx_T *cctx,
2888 int is_expr,
2889 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890{
2891 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002892 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002893 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002895 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
2897 if (*(*arg + 1) == ':')
2898 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002899 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002900 {
2901 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
Bram Moolenaarfa596382021-04-07 21:58:16 +02002903 // load dictionary of namespace
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002904 switch (**arg)
2905 {
2906 case 'g': isn_type = ISN_LOADGDICT; break;
2907 case 'w': isn_type = ISN_LOADWDICT; break;
2908 case 't': isn_type = ISN_LOADTDICT; break;
2909 case 'b': isn_type = ISN_LOADBDICT; break;
2910 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002911 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002912 goto theend;
2913 }
2914 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2915 goto theend;
2916 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 else
2919 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002920 isntype_T isn_type = ISN_DROP;
2921
Bram Moolenaarfa596382021-04-07 21:58:16 +02002922 // load namespaced variable
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002923 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2924 if (name == NULL)
2925 return FAIL;
2926
2927 switch (**arg)
2928 {
2929 case 'v': res = generate_LOADV(cctx, name, error);
2930 break;
Bram Moolenaarfa596382021-04-07 21:58:16 +02002931 case 's': if (is_expr && ASCII_ISUPPER(*name)
2932 && find_func(name, FALSE, cctx) != NULL)
2933 res = generate_funcref(cctx, name);
2934 else
2935 res = compile_load_scriptvar(cctx, name,
Bram Moolenaarca51cc02021-04-01 21:38:53 +02002936 NULL, &end, error);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002937 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002938 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaarfa596382021-04-07 21:58:16 +02002939 {
2940 if (is_expr && ASCII_ISUPPER(*name)
2941 && find_func(name, FALSE, cctx) != NULL)
2942 res = generate_funcref(cctx, name);
2943 else
2944 isn_type = ISN_LOADG;
2945 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01002946 else
2947 {
2948 isn_type = ISN_LOADAUTO;
2949 vim_free(name);
2950 name = vim_strnsave(*arg, end - *arg);
2951 if (name == NULL)
2952 return FAIL;
2953 }
2954 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002955 case 'w': isn_type = ISN_LOADW; break;
2956 case 't': isn_type = ISN_LOADT; break;
2957 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002958 default: // cannot happen, just in case
2959 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 goto theend;
2961 }
2962 if (isn_type != ISN_DROP)
2963 {
2964 // Global, Buffer-local, Window-local and Tabpage-local
2965 // variables can be defined later, thus we don't check if it
Bram Moolenaarfa596382021-04-07 21:58:16 +02002966 // exists, give an error at runtime.
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002967 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 }
2970 }
2971 else
2972 {
2973 size_t len = end - *arg;
2974 int idx;
2975 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002976 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977
2978 name = vim_strnsave(*arg, end - *arg);
2979 if (name == NULL)
2980 return FAIL;
2981
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002982 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002984 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002985 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 }
2987 else
2988 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002989 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002990
Bram Moolenaar709664c2020-12-12 14:33:41 +01002991 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002993 type = lvar.lv_type;
2994 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002995 if (lvar.lv_from_outer != 0)
2996 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002997 else
2998 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 }
3000 else
3001 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02003002 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02003003 // already exists in a Vim9 script or when it's imported.
Bram Moolenaar15e5e532021-04-07 21:21:13 +02003004 if (script_var_exists(*arg, len, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02003005 || find_imported(name, 0, cctx) != NULL)
3006 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003007
Bram Moolenaar0f769812020-09-12 18:32:34 +02003008 // When evaluating an expression and the name starts with an
Bram Moolenaarfa596382021-04-07 21:58:16 +02003009 // uppercase letter it can be a user defined function.
3010 // generate_funcref() will fail if the function can't be found.
3011 if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003012 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 }
3014 }
3015 if (gen_load)
3016 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01003017 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02003018 {
Bram Moolenaarab360522021-01-10 14:02:28 +01003019 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02003020 cctx->ctx_outer_used = TRUE;
3021 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 }
3023
3024 *arg = end;
3025
3026theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01003027 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003028 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 vim_free(name);
3030 return res;
3031}
3032
3033/*
3034 * Compile the argument expressions.
3035 * "arg" points to just after the "(" and is advanced to after the ")"
3036 */
3037 static int
3038compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
3039{
Bram Moolenaar2c330432020-04-13 14:41:35 +02003040 char_u *p = *arg;
3041 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003042 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
Bram Moolenaare6085c52020-04-12 20:19:16 +02003044 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003046 if (may_get_next_line(whitep, &p, cctx) == FAIL)
3047 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003048 if (*p == ')')
3049 {
3050 *arg = p + 1;
3051 return OK;
3052 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003053 if (must_end)
3054 {
3055 semsg(_(e_missing_comma_before_argument_str), p);
3056 return FAIL;
3057 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02003058
Bram Moolenaara5565e42020-05-09 15:44:01 +02003059 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003060 return FAIL;
3061 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003062
3063 if (*p != ',' && *skipwhite(p) == ',')
3064 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003065 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003066 p = skipwhite(p);
3067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003069 {
3070 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02003071 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003072 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003073 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02003074 else
3075 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003076 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01003077 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003079failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02003080 emsg(_(e_missing_close));
3081 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082}
3083
3084/*
3085 * Compile a function call: name(arg1, arg2)
3086 * "arg" points to "name", "arg + varlen" to the "(".
3087 * "argcount_init" is 1 for "value->method()"
3088 * Instructions:
3089 * EVAL arg1
3090 * EVAL arg2
3091 * BCALL / DCALL / UCALL
3092 */
3093 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02003094compile_call(
3095 char_u **arg,
3096 size_t varlen,
3097 cctx_T *cctx,
3098 ppconst_T *ppconst,
3099 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100{
3101 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01003102 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 int argcount = argcount_init;
3104 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003105 char_u fname_buf[FLEN_FIXED + 1];
3106 char_u *tofree = NULL;
3107 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01003108 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003109 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003110 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111
Bram Moolenaara5565e42020-05-09 15:44:01 +02003112 // we can evaluate "has('name')" at compile time
3113 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
3114 {
3115 char_u *s = skipwhite(*arg + varlen + 1);
3116 typval_T argvars[2];
3117
3118 argvars[0].v_type = VAR_UNKNOWN;
3119 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003120 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003121 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003122 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003123 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01003124 if (*s == ')' && argvars[0].v_type == VAR_STRING
3125 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02003126 {
3127 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
3128
3129 *arg = s + 1;
3130 argvars[1].v_type = VAR_UNKNOWN;
3131 tv->v_type = VAR_NUMBER;
3132 tv->vval.v_number = 0;
3133 f_has(argvars, tv);
3134 clear_tv(&argvars[0]);
3135 ++ppconst->pp_used;
3136 return OK;
3137 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02003138 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003139 }
3140
3141 if (generate_ppconst(cctx, ppconst) == FAIL)
3142 return FAIL;
3143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (varlen >= sizeof(namebuf))
3145 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003146 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return FAIL;
3148 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003149 vim_strncpy(namebuf, *arg, varlen);
3150 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151
3152 *arg = skipwhite(*arg + varlen + 1);
3153 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003154 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
Bram Moolenaar03290b82020-12-19 16:30:44 +01003156 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003157 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 {
3159 int idx;
3160
3161 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003162 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003164 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003165 if (STRCMP(name, "flatten") == 0)
3166 {
3167 emsg(_(e_cannot_use_flatten_in_vim9_script));
3168 goto theend;
3169 }
3170
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003171 if (STRCMP(name, "add") == 0 && argcount == 2)
3172 {
3173 garray_T *stack = &cctx->ctx_type_stack;
3174 type_T *type = ((type_T **)stack->ga_data)[
3175 stack->ga_len - 2];
3176
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003177 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003178 if (type->tt_type == VAR_LIST)
3179 {
3180 // inline "add(list, item)" so that the type can be checked
3181 res = generate_LISTAPPEND(cctx);
3182 idx = -1;
3183 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003184 else if (type->tt_type == VAR_BLOB)
3185 {
3186 // inline "add(blob, nr)" so that the type can be checked
3187 res = generate_BLOBAPPEND(cctx);
3188 idx = -1;
3189 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003190 }
3191
3192 if (idx >= 0)
3193 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3194 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003195 else
3196 semsg(_(e_unknownfunc), namebuf);
3197 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 }
3199
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003200 // An argument or local variable can be a function reference, this
3201 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003202 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003203 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003204 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003205 // If we can find the function by name generate the right call.
3206 // Skip global functions here, a local funcref takes precedence.
3207 ufunc = find_func(name, FALSE, cctx);
3208 if (ufunc != NULL && !func_is_global(ufunc))
3209 {
3210 res = generate_CALL(cctx, ufunc, argcount);
3211 goto theend;
3212 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
3215 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003216 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003217 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003219 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003220 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003221 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003222 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003223 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003224
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003225 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003226 goto theend;
3227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228
Bram Moolenaar0f769812020-09-12 18:32:34 +02003229 // If we can find a global function by name generate the right call.
3230 if (ufunc != NULL)
3231 {
3232 res = generate_CALL(cctx, ufunc, argcount);
3233 goto theend;
3234 }
3235
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003236 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003237 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003238 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003239 res = generate_UCALL(cctx, name, argcount);
3240 else
3241 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003242
3243theend:
3244 vim_free(tofree);
3245 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246}
3247
3248// like NAMESPACE_CHAR but with 'a' and 'l'.
3249#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3250
3251/*
3252 * Find the end of a variable or function name. Unlike find_name_end() this
3253 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003254 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 * Return a pointer to just after the name. Equal to "arg" if there is no
3256 * valid name.
3257 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003258 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003259to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260{
3261 char_u *p;
3262
3263 // Quick check for valid starting character.
3264 if (!eval_isnamec1(*arg))
3265 return arg;
3266
3267 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3268 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3269 // and can be used in slice "[n:]".
3270 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003271 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3273 break;
3274 return p;
3275}
3276
3277/*
3278 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003279 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 */
3281 char_u *
3282to_name_const_end(char_u *arg)
3283{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003284 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 typval_T rettv;
3286
3287 if (p == arg && *arg == '[')
3288 {
3289
3290 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003291 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 p = arg;
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 return p;
3295}
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297/*
3298 * parse a list: [expr, expr]
3299 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003300 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 */
3302 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003303compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304{
3305 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003306 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003308 int is_const;
3309 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003311 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003313 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003314 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003315 semsg(_(e_list_end), *arg);
3316 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003317 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003318 if (*p == ',')
3319 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003320 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003321 return FAIL;
3322 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003323 if (*p == ']')
3324 {
3325 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003326 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003327 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003328 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003329 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003330 if (!is_const)
3331 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 ++count;
3333 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003334 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003336 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3337 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003338 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003339 return FAIL;
3340 }
3341 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003342 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 p = skipwhite(p);
3344 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003345 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003347 ppconst->pp_is_const = is_all_const;
3348 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349}
3350
3351/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003352 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003353 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003354 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 */
3356 static int
3357compile_lambda(char_u **arg, cctx_T *cctx)
3358{
Bram Moolenaare462f522020-12-27 14:43:30 +01003359 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 typval_T rettv;
3361 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003362 evalarg_T evalarg;
3363
3364 CLEAR_FIELD(evalarg);
3365 evalarg.eval_flags = EVAL_EVALUATE;
3366 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367
3368 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003369 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3370 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003371 {
3372 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003373 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003374 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003375
Bram Moolenaar65c44152020-12-24 15:14:01 +01003376 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003378 ++ufunc->uf_refcount;
3379 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
Bram Moolenaar65c44152020-12-24 15:14:01 +01003381 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003382 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003384 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3385 // points into it. Point to the original line to avoid a dangling pointer.
3386 if (evalarg.eval_tofree_cmdline != NULL)
3387 {
3388 size_t off = *arg - evalarg.eval_tofree_cmdline;
3389
3390 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3391 + off;
3392 }
3393
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003394 clear_evalarg(&evalarg, NULL);
3395
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003396 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003397 {
3398 // The return type will now be known.
3399 set_function_type(ufunc);
3400
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003401 // The function reference count will be 1. When the ISN_FUNCREF
3402 // instruction is deleted the reference count is decremented and the
3403 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003404 return generate_FUNCREF(cctx, ufunc);
3405 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003406
3407 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 return FAIL;
3409}
3410
3411/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003412 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003414 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 */
3416 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003417compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418{
3419 garray_T *instr = &cctx->ctx_instr;
3420 int count = 0;
3421 dict_T *d = dict_alloc();
3422 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003423 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003424 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003425 int is_const;
3426 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427
3428 if (d == NULL)
3429 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003430 if (generate_ppconst(cctx, ppconst) == FAIL)
3431 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003432 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003434 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435
Bram Moolenaar23c55272020-06-21 16:58:13 +02003436 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003437 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003438 *arg = NULL;
3439 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003440 }
3441
3442 if (**arg == '}')
3443 break;
3444
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003445 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003447 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003449 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003450 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003451 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003454 if (isn->isn_type == ISN_PUSHNR)
3455 {
3456 char buf[NUMBUFLEN];
3457
3458 // Convert to string at compile time.
3459 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3460 isn->isn_type = ISN_PUSHS;
3461 isn->isn_arg.string = vim_strsave((char_u *)buf);
3462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 if (isn->isn_type == ISN_PUSHS)
3464 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003465 else if (may_generate_2STRING(-1, cctx) == FAIL)
3466 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003467 *arg = skipwhite(*arg);
3468 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003469 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003470 emsg(_(e_missing_matching_bracket_after_dict_key));
3471 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003472 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003473 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003475 else
3476 {
3477 // {"name": value},
3478 // {'name': value},
3479 // {name: value} use "name" as a literal key
3480 key = get_literal_key(arg);
3481 if (key == NULL)
3482 return FAIL;
3483 if (generate_PUSHS(cctx, key) == FAIL)
3484 return FAIL;
3485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486
3487 // Check for duplicate keys, if using string keys.
3488 if (key != NULL)
3489 {
3490 item = dict_find(d, key, -1);
3491 if (item != NULL)
3492 {
3493 semsg(_(e_duplicate_key), key);
3494 goto failret;
3495 }
3496 item = dictitem_alloc(key);
3497 if (item != NULL)
3498 {
3499 item->di_tv.v_type = VAR_UNKNOWN;
3500 item->di_tv.v_lock = 0;
3501 if (dict_add(d, item) == FAIL)
3502 dictitem_free(item);
3503 }
3504 }
3505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 if (**arg != ':')
3507 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003508 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003509 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003510 else
3511 semsg(_(e_missing_dict_colon), *arg);
3512 return FAIL;
3513 }
3514 whitep = *arg + 1;
3515 if (!IS_WHITE_OR_NUL(*whitep))
3516 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003517 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 return FAIL;
3519 }
3520
Bram Moolenaar23c55272020-06-21 16:58:13 +02003521 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003522 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003523 *arg = NULL;
3524 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003525 }
3526
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003527 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003529 if (!is_const)
3530 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 ++count;
3532
Bram Moolenaar2c330432020-04-13 14:41:35 +02003533 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003534 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003535 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003536 *arg = NULL;
3537 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003538 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 if (**arg == '}')
3540 break;
3541 if (**arg != ',')
3542 {
3543 semsg(_(e_missing_dict_comma), *arg);
3544 goto failret;
3545 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003546 if (IS_WHITE_OR_NUL(*whitep))
3547 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003548 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003549 return FAIL;
3550 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003551 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003552 if (!IS_WHITE_OR_NUL(*whitep))
3553 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003554 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003555 return FAIL;
3556 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003557 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 }
3559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 *arg = *arg + 1;
3561
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003562 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003563 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003564 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003565 *arg += STRLEN(*arg);
3566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003568 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 return generate_NEWDICT(cctx, count);
3570
3571failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003572 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003573 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003574 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003575 *arg = (char_u *)"";
3576 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 dict_unref(d);
3578 return FAIL;
3579}
3580
3581/*
3582 * Compile "&option".
3583 */
3584 static int
3585compile_get_option(char_u **arg, cctx_T *cctx)
3586{
3587 typval_T rettv;
3588 char_u *start = *arg;
3589 int ret;
3590
3591 // parse the option and get the current value to get the type.
3592 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003593 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 if (ret == OK)
3595 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003596 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003597 char_u *name = vim_strnsave(start, *arg - start);
3598 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3599 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600
3601 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3602 vim_free(name);
3603 }
3604 clear_tv(&rettv);
3605
3606 return ret;
3607}
3608
3609/*
3610 * Compile "$VAR".
3611 */
3612 static int
3613compile_get_env(char_u **arg, cctx_T *cctx)
3614{
3615 char_u *start = *arg;
3616 int len;
3617 int ret;
3618 char_u *name;
3619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 ++*arg;
3621 len = get_env_len(arg);
3622 if (len == 0)
3623 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003624 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 return FAIL;
3626 }
3627
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003628 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 name = vim_strnsave(start, len + 1);
3630 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3631 vim_free(name);
3632 return ret;
3633}
3634
3635/*
3636 * Compile "@r".
3637 */
3638 static int
3639compile_get_register(char_u **arg, cctx_T *cctx)
3640{
3641 int ret;
3642
3643 ++*arg;
3644 if (**arg == NUL)
3645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003646 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 return FAIL;
3648 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003649 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
3651 emsg_invreg(**arg);
3652 return FAIL;
3653 }
3654 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3655 ++*arg;
3656 return ret;
3657}
3658
3659/*
3660 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003661 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 */
3663 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003664apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003666 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667
3668 // this works from end to start
3669 while (p > start)
3670 {
3671 --p;
3672 if (*p == '-' || *p == '+')
3673 {
3674 // only '-' has an effect, for '+' we only check the type
3675#ifdef FEAT_FLOAT
3676 if (rettv->v_type == VAR_FLOAT)
3677 {
3678 if (*p == '-')
3679 rettv->vval.v_float = -rettv->vval.v_float;
3680 }
3681 else
3682#endif
3683 {
3684 varnumber_T val;
3685 int error = FALSE;
3686
3687 // tv_get_number_chk() accepts a string, but we don't want that
3688 // here
3689 if (check_not_string(rettv) == FAIL)
3690 return FAIL;
3691 val = tv_get_number_chk(rettv, &error);
3692 clear_tv(rettv);
3693 if (error)
3694 return FAIL;
3695 if (*p == '-')
3696 val = -val;
3697 rettv->v_type = VAR_NUMBER;
3698 rettv->vval.v_number = val;
3699 }
3700 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003701 else if (numeric_only)
3702 {
3703 ++p;
3704 break;
3705 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003706 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 {
3708 int v = tv2bool(rettv);
3709
3710 // '!' is permissive in the type.
3711 clear_tv(rettv);
3712 rettv->v_type = VAR_BOOL;
3713 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3714 }
3715 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003716 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 return OK;
3718}
3719
3720/*
3721 * Recognize v: variables that are constants and set "rettv".
3722 */
3723 static void
3724get_vim_constant(char_u **arg, typval_T *rettv)
3725{
3726 if (STRNCMP(*arg, "v:true", 6) == 0)
3727 {
3728 rettv->v_type = VAR_BOOL;
3729 rettv->vval.v_number = VVAL_TRUE;
3730 *arg += 6;
3731 }
3732 else if (STRNCMP(*arg, "v:false", 7) == 0)
3733 {
3734 rettv->v_type = VAR_BOOL;
3735 rettv->vval.v_number = VVAL_FALSE;
3736 *arg += 7;
3737 }
3738 else if (STRNCMP(*arg, "v:null", 6) == 0)
3739 {
3740 rettv->v_type = VAR_SPECIAL;
3741 rettv->vval.v_number = VVAL_NULL;
3742 *arg += 6;
3743 }
3744 else if (STRNCMP(*arg, "v:none", 6) == 0)
3745 {
3746 rettv->v_type = VAR_SPECIAL;
3747 rettv->vval.v_number = VVAL_NONE;
3748 *arg += 6;
3749 }
3750}
3751
Bram Moolenaar657137c2021-01-09 15:45:23 +01003752 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003753get_compare_type(char_u *p, int *len, int *type_is)
3754{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003755 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003756 int i;
3757
3758 switch (p[0])
3759 {
3760 case '=': if (p[1] == '=')
3761 type = EXPR_EQUAL;
3762 else if (p[1] == '~')
3763 type = EXPR_MATCH;
3764 break;
3765 case '!': if (p[1] == '=')
3766 type = EXPR_NEQUAL;
3767 else if (p[1] == '~')
3768 type = EXPR_NOMATCH;
3769 break;
3770 case '>': if (p[1] != '=')
3771 {
3772 type = EXPR_GREATER;
3773 *len = 1;
3774 }
3775 else
3776 type = EXPR_GEQUAL;
3777 break;
3778 case '<': if (p[1] != '=')
3779 {
3780 type = EXPR_SMALLER;
3781 *len = 1;
3782 }
3783 else
3784 type = EXPR_SEQUAL;
3785 break;
3786 case 'i': if (p[1] == 's')
3787 {
3788 // "is" and "isnot"; but not a prefix of a name
3789 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3790 *len = 5;
3791 i = p[*len];
3792 if (!isalnum(i) && i != '_')
3793 {
3794 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3795 *type_is = TRUE;
3796 }
3797 }
3798 break;
3799 }
3800 return type;
3801}
3802
3803/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003804 * Skip over an expression, ignoring most errors.
3805 */
3806 static void
3807skip_expr_cctx(char_u **arg, cctx_T *cctx)
3808{
3809 evalarg_T evalarg;
3810
3811 CLEAR_FIELD(evalarg);
3812 evalarg.eval_cctx = cctx;
3813 skip_expr(arg, &evalarg);
3814}
3815
3816/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003818 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 */
3820 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003821compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003823 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
3825 // this works from end to start
3826 while (p > start)
3827 {
3828 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003829 while (VIM_ISWHITE(*p))
3830 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 if (*p == '-' || *p == '+')
3832 {
3833 int negate = *p == '-';
3834 isn_T *isn;
3835
3836 // TODO: check type
3837 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3838 {
3839 --p;
3840 if (*p == '-')
3841 negate = !negate;
3842 }
3843 // only '-' has an effect, for '+' we only check the type
3844 if (negate)
3845 isn = generate_instr(cctx, ISN_NEGATENR);
3846 else
3847 isn = generate_instr(cctx, ISN_CHECKNR);
3848 if (isn == NULL)
3849 return FAIL;
3850 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003851 else if (numeric_only)
3852 {
3853 ++p;
3854 break;
3855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 else
3857 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003858 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003860 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003862 if (p[-1] == '!')
3863 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 }
3866 if (generate_2BOOL(cctx, invert) == FAIL)
3867 return FAIL;
3868 }
3869 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003870 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 return OK;
3872}
3873
3874/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003875 * Compile "(expression)": recursive!
3876 * Return FAIL/OK.
3877 */
3878 static int
3879compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3880{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003881 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003882 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003883
Bram Moolenaar24156692021-01-14 20:35:49 +01003884 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3885 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003886 if (ppconst->pp_used <= PPSIZE - 10)
3887 {
3888 ret = compile_expr1(arg, cctx, ppconst);
3889 }
3890 else
3891 {
3892 // Not enough space in ppconst, flush constants.
3893 if (generate_ppconst(cctx, ppconst) == FAIL)
3894 return FAIL;
3895 ret = compile_expr0(arg, cctx);
3896 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003897 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3898 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003899 if (**arg == ')')
3900 ++*arg;
3901 else if (ret == OK)
3902 {
3903 emsg(_(e_missing_close));
3904 ret = FAIL;
3905 }
3906 return ret;
3907}
3908
3909/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003911 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 */
3913 static int
3914compile_subscript(
3915 char_u **arg,
3916 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003917 char_u *start_leader,
3918 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003919 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003921 char_u *name_start = *end_leader;
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 for (;;)
3924 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003925 char_u *p = skipwhite(*arg);
3926
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003927 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003928 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003929 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003930
3931 // If a following line starts with "->{" or "->X" advance to that
3932 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003933 // Also if a following line starts with ".x".
3934 if (next != NULL &&
3935 ((next[0] == '-' && next[1] == '>'
3936 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003937 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003938 {
3939 next = next_line_from_context(cctx, TRUE);
3940 if (next == NULL)
3941 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003942 *arg = next;
3943 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003944 }
3945 }
3946
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003947 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003948 // not a function call.
3949 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003951 garray_T *stack = &cctx->ctx_type_stack;
3952 type_T *type;
3953 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003955 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003956 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003957 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003960 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3961
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003962 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3964 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003965 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 return FAIL;
3967 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003968 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003970 char_u *pstart = p;
3971
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003972 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003974 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 // something->method()
3977 // Apply the '!', '-' and '+' first:
3978 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003979 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003982 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003983 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003984 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003985 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003986 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003987 int argcount = 1;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003988 garray_T *stack = &cctx->ctx_type_stack;
3989 int type_idx_start = stack->ga_len;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003990 type_T *type;
Bram Moolenaar2927c072021-04-05 19:41:21 +02003991 int expr_isn_start = cctx->ctx_instr.ga_len;
3992 int expr_isn_end;
3993 int arg_isn_count;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003994
3995 // Funcref call: list->(Refs[2])(arg)
3996 // or lambda: list->((arg) => expr)(arg)
Bram Moolenaar2927c072021-04-05 19:41:21 +02003997 //
3998 // Fist compile the function expression.
3999 if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004000 return FAIL;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004001
4002 // Remember the next instruction index, where the instructions
4003 // for arguments are being written.
4004 expr_isn_end = cctx->ctx_instr.ga_len;
4005
4006 // Compile the arguments.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004007 if (**arg != '(')
4008 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004009 if (*skipwhite(*arg) == '(')
4010 emsg(_(e_nowhitespace));
4011 else
4012 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01004013 return FAIL;
4014 }
Bram Moolenaar7e368202020-12-25 21:56:57 +01004015 *arg = skipwhite(*arg + 1);
4016 if (compile_arguments(arg, cctx, &argcount) == FAIL)
4017 return FAIL;
4018
Bram Moolenaar2927c072021-04-05 19:41:21 +02004019 // Move the instructions for the arguments to before the
4020 // instructions of the expression and move the type of the
4021 // expression after the argument types. This is what ISN_PCALL
4022 // expects.
Bram Moolenaar7e368202020-12-25 21:56:57 +01004023 stack = &cctx->ctx_type_stack;
Bram Moolenaar2927c072021-04-05 19:41:21 +02004024 arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
4025 if (arg_isn_count > 0)
4026 {
4027 int expr_isn_count = expr_isn_end - expr_isn_start;
4028 isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
4029
4030 if (isn == NULL)
4031 return FAIL;
4032 mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
4033 + expr_isn_start,
4034 sizeof(isn_T) * expr_isn_count);
4035 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4036 + expr_isn_start,
4037 ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
4038 sizeof(isn_T) * arg_isn_count);
4039 mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
4040 + expr_isn_start + arg_isn_count,
4041 isn, sizeof(isn_T) * expr_isn_count);
4042 vim_free(isn);
4043
4044 type = ((type_T **)stack->ga_data)[type_idx_start];
4045 mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
4046 ((type_T **)stack->ga_data) + type_idx_start + 1,
4047 sizeof(type_T *)
4048 * (stack->ga_len - type_idx_start - 1));
4049 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
4050 }
4051
Bram Moolenaar7e368202020-12-25 21:56:57 +01004052 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4053 if (generate_PCALL(cctx, argcount,
4054 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 return FAIL;
4056 }
4057 else
4058 {
4059 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004060 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004061 if (!eval_isnamec1(*p))
4062 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02004063 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02004064 return FAIL;
4065 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004066 if (ASCII_ISALPHA(*p) && p[1] == ':')
4067 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02004068 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 ;
4070 if (*p != '(')
4071 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02004072 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 return FAIL;
4074 }
4075 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02004076 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 }
4079 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02004080 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004082 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01004083
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004084 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02004085 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004086 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004087 // TODO: blob index
4088 // TODO: more arguments
4089 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004090 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004091 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004092 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004093
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004094 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02004095 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004096 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004097 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004098 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004099 // missing first index is equal to zero
4100 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004101 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004102 else
4103 {
4104 if (compile_expr0(arg, cctx) == FAIL)
4105 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004106 if (**arg == ':')
4107 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004108 semsg(_(e_white_space_required_before_and_after_str_at_str),
4109 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004110 return FAIL;
4111 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004112 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004113 return FAIL;
4114 *arg = skipwhite(*arg);
4115 }
4116 if (**arg == ':')
4117 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004118 is_slice = TRUE;
4119 ++*arg;
4120 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
4121 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004122 semsg(_(e_white_space_required_before_and_after_str_at_str),
4123 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004124 return FAIL;
4125 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004126 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004127 return FAIL;
4128 if (**arg == ']')
4129 // missing second index is equal to end of string
4130 generate_PUSHNR(cctx, -1);
4131 else
4132 {
4133 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01004134 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004135 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004136 return FAIL;
4137 *arg = skipwhite(*arg);
4138 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
4141 if (**arg != ']')
4142 {
4143 emsg(_(e_missbrac));
4144 return FAIL;
4145 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01004146 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147
Bram Moolenaare42939a2021-04-05 17:11:17 +02004148 if (compile_member(is_slice, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004149 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004151 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004153 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004154 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004155 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004156 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004157
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004158 *arg = p + 1;
Bram Moolenaar90193e62021-04-04 20:49:50 +02004159 if (IS_WHITE_OR_NUL(**arg))
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004160 {
4161 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004162 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004163 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004164 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004165 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 while (eval_isnamec(*p))
4167 MB_PTR_ADV(p);
4168 if (p == *arg)
4169 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004170 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 return FAIL;
4172 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004173 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 return FAIL;
4175 *arg = p;
4176 }
4177 else
4178 break;
4179 }
4180
4181 // TODO - see handle_subscript():
4182 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4183 // Don't do this when "Func" is already a partial that was bound
4184 // explicitly (pt_auto is FALSE).
4185
4186 return OK;
4187}
4188
4189/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004190 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4191 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004193 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004194 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004195 *
4196 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 */
4198
4199/*
4200 * number number constant
4201 * 0zFFFFFFFF Blob constant
4202 * "string" string constant
4203 * 'string' literal string constant
4204 * &option-name option value
4205 * @r register contents
4206 * identifier variable value
4207 * function() function call
4208 * $VAR environment variable
4209 * (expression) nested expression
4210 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004211 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 *
4213 * Also handle:
4214 * ! in front logical NOT
4215 * - in front unary minus
4216 * + in front unary plus (ignored)
4217 * trailing (arg) funcref/partial call
4218 * trailing [] subscript in String or List
4219 * trailing .name entry in Dictionary
4220 * trailing ->name() method call
4221 */
4222 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004223compile_expr7(
4224 char_u **arg,
4225 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004226 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 char_u *start_leader, *end_leader;
4229 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004230 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004231 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004233 ppconst->pp_is_const = FALSE;
4234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 /*
4236 * Skip '!', '-' and '+' characters. They are handled later.
4237 */
4238 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004239 if (eval_leader(arg, TRUE) == FAIL)
4240 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 end_leader = *arg;
4242
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004243 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 switch (**arg)
4245 {
4246 /*
4247 * Number constant.
4248 */
4249 case '0': // also for blob starting with 0z
4250 case '1':
4251 case '2':
4252 case '3':
4253 case '4':
4254 case '5':
4255 case '6':
4256 case '7':
4257 case '8':
4258 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004259 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004261 // Apply "-" and "+" just before the number now, right to
4262 // left. Matters especially when "->" follows. Stops at
4263 // '!'.
4264 if (apply_leader(rettv, TRUE,
4265 start_leader, &end_leader) == FAIL)
4266 {
4267 clear_tv(rettv);
4268 return FAIL;
4269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270 break;
4271
4272 /*
4273 * String constant: "string".
4274 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004275 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 return FAIL;
4277 break;
4278
4279 /*
4280 * Literal string constant: 'str''ing'.
4281 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004282 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 return FAIL;
4284 break;
4285
4286 /*
4287 * Constant Vim variable.
4288 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004289 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 ret = NOTDONE;
4291 break;
4292
4293 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004294 * "true" constant
4295 */
4296 case 't': if (STRNCMP(*arg, "true", 4) == 0
4297 && !eval_isnamec((*arg)[4]))
4298 {
4299 *arg += 4;
4300 rettv->v_type = VAR_BOOL;
4301 rettv->vval.v_number = VVAL_TRUE;
4302 }
4303 else
4304 ret = NOTDONE;
4305 break;
4306
4307 /*
4308 * "false" constant
4309 */
4310 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4311 && !eval_isnamec((*arg)[5]))
4312 {
4313 *arg += 5;
4314 rettv->v_type = VAR_BOOL;
4315 rettv->vval.v_number = VVAL_FALSE;
4316 }
4317 else
4318 ret = NOTDONE;
4319 break;
4320
4321 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004322 * "null" constant
4323 */
4324 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004325 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004326 {
4327 *arg += 4;
4328 rettv->v_type = VAR_SPECIAL;
4329 rettv->vval.v_number = VVAL_NULL;
4330 }
4331 else
4332 ret = NOTDONE;
4333 break;
4334
4335 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 * List: [expr, expr]
4337 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004338 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4339 return FAIL;
4340 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 break;
4342
4343 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 * Dictionary: {'key': val, 'key': val}
4345 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004346 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4347 return FAIL;
4348 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 break;
4350
4351 /*
4352 * Option value: &name
4353 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004354 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4355 return FAIL;
4356 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 break;
4358
4359 /*
4360 * Environment variable: $VAR.
4361 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004362 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4363 return FAIL;
4364 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365 break;
4366
4367 /*
4368 * Register contents: @r.
4369 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004370 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4371 return FAIL;
4372 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 break;
4374 /*
4375 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004376 * lambda: (arg, arg) => expr
4377 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004379 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4380 ret = compile_lambda(arg, cctx);
4381 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004382 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 break;
4384
4385 default: ret = NOTDONE;
4386 break;
4387 }
4388 if (ret == FAIL)
4389 return FAIL;
4390
Bram Moolenaar1c747212020-05-09 18:28:34 +02004391 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004393 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394 clear_tv(rettv);
4395 else
4396 // A constant expression can possibly be handled compile time,
4397 // return the value instead of generating code.
4398 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 else if (ret == NOTDONE)
4401 {
4402 char_u *p;
4403 int r;
4404
4405 if (!eval_isnamec1(**arg))
4406 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004407 if (!vim9_bad_comment(*arg))
4408 {
4409 if (ends_excmd(*skipwhite(*arg)))
4410 semsg(_(e_empty_expression_str), *arg);
4411 else
4412 semsg(_(e_name_expected_str), *arg);
4413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 return FAIL;
4415 }
4416
4417 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004418 p = to_name_end(*arg, TRUE);
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004419 if (p - *arg == (size_t)1 && **arg == '_')
4420 {
4421 emsg(_(e_cannot_use_underscore_here));
4422 return FAIL;
4423 }
4424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 {
4427 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 {
4431 if (generate_ppconst(cctx, ppconst) == FAIL)
4432 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004433 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 if (r == FAIL)
4436 return FAIL;
4437 }
4438
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004439 // Handle following "[]", ".member", etc.
4440 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004441 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004442 ppconst) == FAIL)
4443 return FAIL;
4444 if (ppconst->pp_used > 0)
4445 {
4446 // apply the '!', '-' and '+' before the constant
4447 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004448 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 return FAIL;
4450 return OK;
4451 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004452 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004454 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455}
4456
4457/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004458 * Give the "white on both sides" error, taking the operator from "p[len]".
4459 */
4460 void
4461error_white_both(char_u *op, int len)
4462{
4463 char_u buf[10];
4464
4465 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004466 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004467}
4468
4469/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004470 * <type>expr7: runtime type check / conversion
4471 */
4472 static int
4473compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4474{
4475 type_T *want_type = NULL;
4476
4477 // Recognize <type>
4478 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4479 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004480 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004481 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4482 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004483 return FAIL;
4484
4485 if (**arg != '>')
4486 {
4487 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004488 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004489 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004490 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004491 return FAIL;
4492 }
4493 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004494 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004495 return FAIL;
4496 }
4497
4498 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4499 return FAIL;
4500
4501 if (want_type != NULL)
4502 {
4503 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004504 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004505 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004506
Bram Moolenaard1103582020-08-14 22:44:25 +02004507 generate_ppconst(cctx, ppconst);
4508 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004509 where.wt_index = 0;
4510 where.wt_variable = FALSE;
4511 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004512 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004513 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004514 return FAIL;
4515 }
4516 }
4517
4518 return OK;
4519}
4520
4521/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 * * number multiplication
4523 * / number division
4524 * % number modulo
4525 */
4526 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004527compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528{
4529 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004530 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004531 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004533 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004534 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 return FAIL;
4536
4537 /*
4538 * Repeat computing, until no "*", "/" or "%" is following.
4539 */
4540 for (;;)
4541 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004542 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (*op != '*' && *op != '/' && *op != '%')
4544 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004545 if (next != NULL)
4546 {
4547 *arg = next_line_from_context(cctx, TRUE);
4548 op = skipwhite(*arg);
4549 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004550
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004553 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004556 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004557 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004559 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004560 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004561 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004562
4563 if (ppconst->pp_used == ppconst_used + 2
4564 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4565 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004566 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004567 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4568 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4569 varnumber_T res = 0;
4570 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004572 // both are numbers: compute the result
4573 switch (*op)
4574 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004575 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004576 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004577 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004578 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004579 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004580 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004581 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004582 break;
4583 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004584 if (failed)
4585 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004586 tv1->vval.v_number = res;
4587 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004588 }
4589 else
4590 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004591 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004592 generate_two_op(cctx, op);
4593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 }
4595
4596 return OK;
4597}
4598
4599/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004600 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 * - number subtraction
4602 * .. string concatenation
4603 */
4604 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004605compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606{
4607 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004608 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004610 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
4612 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004613 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 return FAIL;
4615
4616 /*
4617 * Repeat computing, until no "+", "-" or ".." is following.
4618 */
4619 for (;;)
4620 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004621 op = may_peek_next_line(cctx, *arg, &next);
4622 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 break;
4624 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004625 if (next != NULL)
4626 {
4627 *arg = next_line_from_context(cctx, TRUE);
4628 op = skipwhite(*arg);
4629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004631 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004633 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004634 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 }
4636
Bram Moolenaare0de1712020-12-02 17:36:54 +01004637 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004638 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004640 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004641 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 return FAIL;
4643
Bram Moolenaara5565e42020-05-09 15:44:01 +02004644 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004645 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4647 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4648 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4649 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004651 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4652 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004653
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004654 // concat/subtract/add constant numbers
4655 if (*op == '+')
4656 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4657 else if (*op == '-')
4658 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4659 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004660 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004661 // concatenate constant strings
4662 char_u *s1 = tv1->vval.v_string;
4663 char_u *s2 = tv2->vval.v_string;
4664 size_t len1 = STRLEN(s1);
4665
4666 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4667 if (tv1->vval.v_string == NULL)
4668 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004670 return FAIL;
4671 }
4672 mch_memmove(tv1->vval.v_string, s1, len1);
4673 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004674 vim_free(s1);
4675 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004676 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004677 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 }
4679 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004680 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004681 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004682 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004683 if (*op == '.')
4684 {
4685 if (may_generate_2STRING(-2, cctx) == FAIL
4686 || may_generate_2STRING(-1, cctx) == FAIL)
4687 return FAIL;
4688 generate_instr_drop(cctx, ISN_CONCAT, 1);
4689 }
4690 else
4691 generate_two_op(cctx, op);
4692 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 }
4694
4695 return OK;
4696}
4697
4698/*
4699 * expr5a == expr5b
4700 * expr5a =~ expr5b
4701 * expr5a != expr5b
4702 * expr5a !~ expr5b
4703 * expr5a > expr5b
4704 * expr5a >= expr5b
4705 * expr5a < expr5b
4706 * expr5a <= expr5b
4707 * expr5a is expr5b
4708 * expr5a isnot expr5b
4709 *
4710 * Produces instructions:
4711 * EVAL expr5a Push result of "expr5a"
4712 * EVAL expr5b Push result of "expr5b"
4713 * COMPARE one of the compare instructions
4714 */
4715 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004716compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004718 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004720 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004723 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724
4725 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004726 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 return FAIL;
4728
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004729 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004730 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731
4732 /*
4733 * If there is a comparative operator, use it.
4734 */
4735 if (type != EXPR_UNKNOWN)
4736 {
4737 int ic = FALSE; // Default: do not ignore case
4738
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004739 if (next != NULL)
4740 {
4741 *arg = next_line_from_context(cctx, TRUE);
4742 p = skipwhite(*arg);
4743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 if (type_is && (p[len] == '?' || p[len] == '#'))
4745 {
4746 semsg(_(e_invexpr2), *arg);
4747 return FAIL;
4748 }
4749 // extra question mark appended: ignore case
4750 if (p[len] == '?')
4751 {
4752 ic = TRUE;
4753 ++len;
4754 }
4755 // extra '#' appended: match case (ignored)
4756 else if (p[len] == '#')
4757 ++len;
4758 // nothing appended: match case
4759
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004760 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004762 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004763 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 }
4765
4766 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004767 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004768 return FAIL;
4769
Bram Moolenaara5565e42020-05-09 15:44:01 +02004770 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 return FAIL;
4772
Bram Moolenaara5565e42020-05-09 15:44:01 +02004773 if (ppconst->pp_used == ppconst_used + 2)
4774 {
4775 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4776 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4777 int ret;
4778
4779 // Both sides are a constant, compute the result now.
4780 // First check for a valid combination of types, this is more
4781 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004782 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783 ret = FAIL;
4784 else
4785 {
4786 ret = typval_compare(tv1, tv2, type, ic);
4787 tv1->v_type = VAR_BOOL;
4788 tv1->vval.v_number = tv1->vval.v_number
4789 ? VVAL_TRUE : VVAL_FALSE;
4790 clear_tv(tv2);
4791 --ppconst->pp_used;
4792 }
4793 return ret;
4794 }
4795
4796 generate_ppconst(cctx, ppconst);
4797 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 }
4799
4800 return OK;
4801}
4802
Bram Moolenaar7f141552020-05-09 17:35:53 +02004803static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805/*
4806 * Compile || or &&.
4807 */
4808 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004809compile_and_or(
4810 char_u **arg,
4811 cctx_T *cctx,
4812 char *op,
4813 ppconst_T *ppconst,
4814 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004816 char_u *next;
4817 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 int opchar = *op;
4819
4820 if (p[0] == opchar && p[1] == opchar)
4821 {
4822 garray_T *instr = &cctx->ctx_instr;
4823 garray_T end_ga;
4824
4825 /*
4826 * Repeat until there is no following "||" or "&&"
4827 */
4828 ga_init2(&end_ga, sizeof(int), 10);
4829 while (p[0] == opchar && p[1] == opchar)
4830 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004831 long start_lnum = SOURCING_LNUM;
4832 int start_ctx_lnum = cctx->ctx_lnum;
4833 int save_lnum;
4834
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004835 if (next != NULL)
4836 {
4837 *arg = next_line_from_context(cctx, TRUE);
4838 p = skipwhite(*arg);
4839 }
4840
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004841 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4842 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004843 semsg(_(e_white_space_required_before_and_after_str_at_str),
Bram Moolenaar90193e62021-04-04 20:49:50 +02004844 op, p);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004845 return FAIL;
4846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004848 // TODO: use ppconst if the value is a constant and check
4849 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004850 generate_ppconst(cctx, ppconst);
4851
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004852 // Every part must evaluate to a bool.
Bram Moolenaara7511c02021-04-03 21:47:07 +02004853 SOURCING_LNUM = start_lnum;
4854 save_lnum = cctx->ctx_lnum;
4855 cctx->ctx_lnum = start_ctx_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004856 if (bool_on_stack(cctx) == FAIL)
4857 {
Bram Moolenaara7511c02021-04-03 21:47:07 +02004858 cctx->ctx_lnum = save_lnum;
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004859 ga_clear(&end_ga);
4860 return FAIL;
4861 }
Bram Moolenaara7511c02021-04-03 21:47:07 +02004862 cctx->ctx_lnum = save_lnum;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004863
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 if (ga_grow(&end_ga, 1) == FAIL)
4865 {
4866 ga_clear(&end_ga);
4867 return FAIL;
4868 }
4869 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4870 ++end_ga.ga_len;
4871 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004872 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873
4874 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004875 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004876 {
4877 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004878 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004879 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004880
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4882 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 {
4884 ga_clear(&end_ga);
4885 return FAIL;
4886 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004887
4888 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004892 // Every part must evaluate to a bool.
4893 if (bool_on_stack(cctx) == FAIL)
4894 {
4895 ga_clear(&end_ga);
4896 return FAIL;
4897 }
4898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 // Fill in the end label in all jumps.
4900 while (end_ga.ga_len > 0)
4901 {
4902 isn_T *isn;
4903
4904 --end_ga.ga_len;
4905 isn = ((isn_T *)instr->ga_data)
4906 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4907 isn->isn_arg.jump.jump_where = instr->ga_len;
4908 }
4909 ga_clear(&end_ga);
4910 }
4911
4912 return OK;
4913}
4914
4915/*
4916 * expr4a && expr4a && expr4a logical AND
4917 *
4918 * Produces instructions:
4919 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004920 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004921 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004923 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 * EVAL expr4c Push result of "expr4c"
4925 * end:
4926 */
4927 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004928compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930 int ppconst_used = ppconst->pp_used;
4931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004933 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 return FAIL;
4935
4936 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938}
4939
4940/*
4941 * expr3a || expr3b || expr3c logical OR
4942 *
4943 * Produces instructions:
4944 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004945 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004946 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004948 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 * EVAL expr3c Push result of "expr3c"
4950 * end:
4951 */
4952 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004953compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004955 int ppconst_used = ppconst->pp_used;
4956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004958 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 return FAIL;
4960
4961 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004962 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963}
4964
4965/*
4966 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004968 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 * JUMP_IF_FALSE alt jump if false
4970 * EVAL expr1a
4971 * JUMP_ALWAYS end
4972 * alt: EVAL expr1b
4973 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004974 *
4975 * Toplevel expression: expr2 ?? expr1
4976 * Produces instructions:
4977 * EVAL expr2 Push result of "expr2"
4978 * JUMP_AND_KEEP_IF_TRUE end jump if true
4979 * EVAL expr1
4980 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 */
4982 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004983compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984{
4985 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004986 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004987 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988
Bram Moolenaar3988f642020-08-27 22:43:03 +02004989 // Ignore all kinds of errors when not producing code.
4990 if (cctx->ctx_skip == SKIP_YES)
4991 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004992 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004993 return OK;
4994 }
4995
Bram Moolenaar61a89812020-05-07 16:58:17 +02004996 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004997 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 return FAIL;
4999
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005000 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 if (*p == '?')
5002 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005003 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 garray_T *instr = &cctx->ctx_instr;
5005 garray_T *stack = &cctx->ctx_type_stack;
5006 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005007 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02005009 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005010 int has_const_expr = FALSE;
5011 int const_value = FALSE;
5012 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005014 if (next != NULL)
5015 {
5016 *arg = next_line_from_context(cctx, TRUE);
5017 p = skipwhite(*arg);
5018 }
5019
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005020 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005021 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005022 semsg(_(e_white_space_required_before_and_after_str_at_str),
5023 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005024 return FAIL;
5025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 if (ppconst->pp_used == ppconst_used + 1)
5028 {
5029 // the condition is a constant, we know whether the ? or the :
5030 // expression is to be evaluated.
5031 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02005032 if (op_falsy)
5033 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
5034 else
5035 {
5036 int error = FALSE;
5037
5038 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
5039 &error);
5040 if (error)
5041 return FAIL;
5042 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005043 cctx->ctx_skip = save_skip == SKIP_YES ||
5044 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
5045
5046 if (op_falsy && cctx->ctx_skip == SKIP_YES)
5047 // "left ?? right" and "left" is truthy: produce "left"
5048 generate_ppconst(cctx, ppconst);
5049 else
5050 {
5051 clear_tv(&ppconst->pp_tv[ppconst_used]);
5052 --ppconst->pp_used;
5053 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005054 }
5055 else
5056 {
5057 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005058 if (op_falsy)
5059 end_idx = instr->ga_len;
5060 generate_JUMP(cctx, op_falsy
5061 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
5062 if (op_falsy)
5063 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02005064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065
5066 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01005067 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02005068 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005069 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01005070 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071
Bram Moolenaara5565e42020-05-09 15:44:01 +02005072 if (!has_const_expr)
5073 {
5074 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005076 if (!op_falsy)
5077 {
5078 // remember the type and drop it
5079 --stack->ga_len;
5080 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005082 end_idx = instr->ga_len;
5083 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005084
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005085 // jump here from JUMP_IF_FALSE
5086 isn = ((isn_T *)instr->ga_data) + alt_idx;
5087 isn->isn_arg.jump.jump_where = instr->ga_len;
5088 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02005089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005091 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005093 // Check for the ":".
5094 p = may_peek_next_line(cctx, *arg, &next);
5095 if (*p != ':')
5096 {
5097 emsg(_(e_missing_colon));
5098 return FAIL;
5099 }
5100 if (next != NULL)
5101 {
5102 *arg = next_line_from_context(cctx, TRUE);
5103 p = skipwhite(*arg);
5104 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005105
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005106 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5107 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005108 semsg(_(e_white_space_required_before_and_after_str_at_str),
5109 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005110 return FAIL;
5111 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005113 // evaluate the third expression
5114 if (has_const_expr)
5115 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005116 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005117 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005118 return FAIL;
5119 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5120 return FAIL;
5121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122
Bram Moolenaara5565e42020-05-09 15:44:01 +02005123 if (!has_const_expr)
5124 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005125 type_T **typep;
5126
Bram Moolenaara5565e42020-05-09 15:44:01 +02005127 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128
Bram Moolenaara5565e42020-05-09 15:44:01 +02005129 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005130 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5131 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005132
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005133 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005134 isn = ((isn_T *)instr->ga_data) + end_idx;
5135 isn->isn_arg.jump.jump_where = instr->ga_len;
5136 }
5137
5138 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 }
5140 return OK;
5141}
5142
5143/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005144 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005145 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5146 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005147 */
5148 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005149compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005150{
5151 ppconst_T ppconst;
5152
5153 CLEAR_FIELD(ppconst);
5154 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5155 {
5156 clear_ppconst(&ppconst);
5157 return FAIL;
5158 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005159 if (is_const != NULL)
5160 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005161 if (generate_ppconst(cctx, &ppconst) == FAIL)
5162 return FAIL;
5163 return OK;
5164}
5165
5166/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005167 * Toplevel expression.
5168 */
5169 static int
5170compile_expr0(char_u **arg, cctx_T *cctx)
5171{
5172 return compile_expr0_ext(arg, cctx, NULL);
5173}
5174
5175/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 * compile "return [expr]"
5177 */
5178 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005179compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180{
5181 char_u *p = arg;
5182 garray_T *stack = &cctx->ctx_type_stack;
5183 type_T *stack_type;
5184
5185 if (*p != NUL && *p != '|' && *p != '\n')
5186 {
5187 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005188 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 return NULL;
5190
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005191 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005192 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005193 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005194 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005195 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5196 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005197 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005198 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005199 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005200 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005201 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005202 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5203 && stack_type->tt_type != VAR_VOID
5204 && stack_type->tt_type != VAR_UNKNOWN)
5205 {
5206 emsg(_(e_returning_value_in_function_without_return_type));
5207 return NULL;
5208 }
5209 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005210 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005211 return NULL;
5212 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 }
5215 else
5216 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005217 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005218 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005219 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5220 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005222 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 return NULL;
5224 }
5225
5226 // No argument, return zero.
5227 generate_PUSHNR(cctx, 0);
5228 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005229
5230 // Undo any command modifiers.
5231 generate_undo_cmdmods(cctx);
5232
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005233 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 return NULL;
5235
5236 // "return val | endif" is possible
5237 return skipwhite(p);
5238}
5239
5240/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005241 * Get a line from the compilation context, compatible with exarg_T getline().
5242 * Return a pointer to the line in allocated memory.
5243 * Return NULL for end-of-file or some error.
5244 */
5245 static char_u *
5246exarg_getline(
5247 int c UNUSED,
5248 void *cookie,
5249 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005250 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005251{
5252 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005253 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005254
Bram Moolenaar66250c92020-08-20 15:02:42 +02005255 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005256 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005257 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005258 return NULL;
5259 ++cctx->ctx_lnum;
5260 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5261 // Comment lines result in NULL pointers, skip them.
5262 if (p != NULL)
5263 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005264 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005265}
5266
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005267 void
5268fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5269{
5270 eap->getline = exarg_getline;
5271 eap->cookie = cctx;
5272}
5273
Bram Moolenaar04b12692020-05-04 23:24:44 +02005274/*
5275 * Compile a nested :def command.
5276 */
5277 static char_u *
5278compile_nested_function(exarg_T *eap, cctx_T *cctx)
5279{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005280 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005281 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005282 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005283 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005284 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005285 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005286
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005287 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005288 {
5289 emsg(_(e_cannot_use_bang_with_nested_def));
5290 return NULL;
5291 }
5292
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005293 if (*name_start == '/')
5294 {
5295 name_end = skip_regexp(name_start + 1, '/', TRUE);
5296 if (*name_end == '/')
5297 ++name_end;
5298 eap->nextcmd = check_nextcmd(name_end);
5299 }
5300 if (name_end == name_start || *skipwhite(name_end) != '(')
5301 {
5302 if (!ends_excmd2(name_start, name_end))
5303 {
5304 semsg(_(e_invalid_command_str), eap->cmd);
5305 return NULL;
5306 }
5307
5308 // "def" or "def Name": list functions
5309 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5310 return NULL;
5311 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5312 }
5313
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005314 // Only g:Func() can use a namespace.
5315 if (name_start[1] == ':' && !is_global)
5316 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005317 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005318 return NULL;
5319 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005320 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005321 return NULL;
5322
Bram Moolenaar04b12692020-05-04 23:24:44 +02005323 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005324 fill_exarg_from_cctx(eap, cctx);
5325
Bram Moolenaar04b12692020-05-04 23:24:44 +02005326 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005327 lambda_name = vim_strsave(get_lambda_name());
5328 if (lambda_name == NULL)
5329 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005330 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005331
Bram Moolenaar822ba242020-05-24 23:00:18 +02005332 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005333 {
5334 r = eap->skip ? OK : FAIL;
5335 goto theend;
5336 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005337
5338 // copy over the block scope IDs before compiling
5339 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5340 {
5341 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5342
5343 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5344 if (ufunc->uf_block_ids != NULL)
5345 {
5346 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5347 sizeof(int) * block_depth);
5348 ufunc->uf_block_depth = block_depth;
5349 }
5350 }
5351
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005352 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5353 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005354 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005355 {
5356 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005357 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005358 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005359
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005360 if (is_global)
5361 {
5362 char_u *func_name = vim_strnsave(name_start + 2,
5363 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005364
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005365 if (func_name == NULL)
5366 r = FAIL;
5367 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005368 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005369 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005370 lambda_name = NULL;
5371 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005372 }
5373 else
5374 {
5375 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005376 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005377 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005378
Bram Moolenaareef21022020-08-01 22:16:43 +02005379 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005380 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005381 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005382 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005383 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5384 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005385 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005386
5387theend:
5388 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005389 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005390}
5391
5392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 * Return the length of an assignment operator, or zero if there isn't one.
5394 */
5395 int
5396assignment_len(char_u *p, int *heredoc)
5397{
5398 if (*p == '=')
5399 {
5400 if (p[1] == '<' && p[2] == '<')
5401 {
5402 *heredoc = TRUE;
5403 return 3;
5404 }
5405 return 1;
5406 }
5407 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5408 return 2;
5409 if (STRNCMP(p, "..=", 3) == 0)
5410 return 3;
5411 return 0;
5412}
5413
5414// words that cannot be used as a variable
5415static char *reserved[] = {
5416 "true",
5417 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005418 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005419 NULL
5420};
5421
Bram Moolenaar752fc692021-01-04 21:57:11 +01005422// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005423typedef enum {
5424 dest_local,
5425 dest_option,
5426 dest_env,
5427 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005428 dest_buffer,
5429 dest_window,
5430 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005431 dest_vimvar,
5432 dest_script,
5433 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005434 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005435} assign_dest_T;
5436
Bram Moolenaar752fc692021-01-04 21:57:11 +01005437// Used by compile_lhs() to store information about the LHS of an assignment
5438// and one argument of ":unlet" with an index.
5439typedef struct {
5440 assign_dest_T lhs_dest; // type of destination
5441
5442 char_u *lhs_name; // allocated name including
5443 // "[expr]" or ".name".
5444 size_t lhs_varlen; // length of the variable without
5445 // "[expr]" or ".name"
5446 char_u *lhs_dest_end; // end of the destination, including
5447 // "[expr]" or ".name".
5448
5449 int lhs_has_index; // has "[expr]" or ".name"
5450
5451 int lhs_new_local; // create new local variable
5452 int lhs_opt_flags; // for when destination is an option
5453 int lhs_vimvaridx; // for when destination is a v:var
5454
5455 lvar_T lhs_local_lvar; // used for existing local destination
5456 lvar_T lhs_arg_lvar; // used for argument destination
5457 lvar_T *lhs_lvar; // points to destination lvar
5458 int lhs_scriptvar_sid;
5459 int lhs_scriptvar_idx;
5460
5461 int lhs_has_type; // type was specified
5462 type_T *lhs_type;
5463 type_T *lhs_member_type;
5464} lhs_T;
5465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005467 * Generate the load instruction for "name".
5468 */
5469 static void
5470generate_loadvar(
5471 cctx_T *cctx,
5472 assign_dest_T dest,
5473 char_u *name,
5474 lvar_T *lvar,
5475 type_T *type)
5476{
5477 switch (dest)
5478 {
5479 case dest_option:
5480 // TODO: check the option exists
5481 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5482 break;
5483 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005484 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5485 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5486 else
5487 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005488 break;
5489 case dest_buffer:
5490 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5491 break;
5492 case dest_window:
5493 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5494 break;
5495 case dest_tab:
5496 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5497 break;
5498 case dest_script:
5499 compile_load_scriptvar(cctx,
5500 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5501 break;
5502 case dest_env:
5503 // Include $ in the name here
5504 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5505 break;
5506 case dest_reg:
5507 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5508 break;
5509 case dest_vimvar:
5510 generate_LOADV(cctx, name + 2, TRUE);
5511 break;
5512 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005513 if (lvar->lv_from_outer > 0)
5514 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5515 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005516 else
5517 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5518 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005519 case dest_expr:
5520 // list or dict value should already be on the stack.
5521 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005522 }
5523}
5524
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005525/*
5526 * Skip over "[expr]" or ".member".
5527 * Does not check for any errors.
5528 */
5529 static char_u *
5530skip_index(char_u *start)
5531{
5532 char_u *p = start;
5533
5534 if (*p == '[')
5535 {
5536 p = skipwhite(p + 1);
5537 (void)skip_expr(&p, NULL);
5538 p = skipwhite(p);
5539 if (*p == ']')
5540 return p + 1;
5541 return p;
5542 }
5543 // if (*p == '.')
5544 return to_name_end(p + 1, TRUE);
5545}
5546
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005547 void
5548vim9_declare_error(char_u *name)
5549{
5550 char *scope = "";
5551
5552 switch (*name)
5553 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005554 case 'g': scope = _("global"); break;
5555 case 'b': scope = _("buffer"); break;
5556 case 'w': scope = _("window"); break;
5557 case 't': scope = _("tab"); break;
5558 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005559 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005560 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005561 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005562 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005563 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005564 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005565 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005566 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005567 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005568}
5569
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005570/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005571 * For one assignment figure out the type of destination. Return it in "dest".
5572 * When not recognized "dest" is not set.
5573 * For an option "opt_flags" is set.
5574 * For a v:var "vimvaridx" is set.
5575 * "type" is set to the destination type if known, unchanted otherwise.
5576 * Return FAIL if an error message was given.
5577 */
5578 static int
5579get_var_dest(
5580 char_u *name,
5581 assign_dest_T *dest,
5582 int cmdidx,
5583 int *opt_flags,
5584 int *vimvaridx,
5585 type_T **type,
5586 cctx_T *cctx)
5587{
5588 char_u *p;
5589
5590 if (*name == '&')
5591 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005592 int cc;
5593 long numval;
5594 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005595
5596 *dest = dest_option;
5597 if (cmdidx == CMD_final || cmdidx == CMD_const)
5598 {
5599 emsg(_(e_const_option));
5600 return FAIL;
5601 }
5602 p = name;
5603 p = find_option_end(&p, opt_flags);
5604 if (p == NULL)
5605 {
5606 // cannot happen?
5607 emsg(_(e_letunexp));
5608 return FAIL;
5609 }
5610 cc = *p;
5611 *p = NUL;
5612 opt_type = get_option_value(skip_option_env_lead(name),
5613 &numval, NULL, *opt_flags);
5614 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005615 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005616 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005617 case gov_unknown:
5618 semsg(_(e_unknown_option), name);
5619 return FAIL;
5620 case gov_string:
5621 case gov_hidden_string:
5622 *type = &t_string;
5623 break;
5624 case gov_bool:
5625 case gov_hidden_bool:
5626 *type = &t_bool;
5627 break;
5628 case gov_number:
5629 case gov_hidden_number:
5630 *type = &t_number;
5631 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005632 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005633 }
5634 else if (*name == '$')
5635 {
5636 *dest = dest_env;
5637 *type = &t_string;
5638 }
5639 else if (*name == '@')
5640 {
5641 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5642 {
5643 emsg_invreg(name[1]);
5644 return FAIL;
5645 }
5646 *dest = dest_reg;
5647 *type = &t_string;
5648 }
5649 else if (STRNCMP(name, "g:", 2) == 0)
5650 {
5651 *dest = dest_global;
5652 }
5653 else if (STRNCMP(name, "b:", 2) == 0)
5654 {
5655 *dest = dest_buffer;
5656 }
5657 else if (STRNCMP(name, "w:", 2) == 0)
5658 {
5659 *dest = dest_window;
5660 }
5661 else if (STRNCMP(name, "t:", 2) == 0)
5662 {
5663 *dest = dest_tab;
5664 }
5665 else if (STRNCMP(name, "v:", 2) == 0)
5666 {
5667 typval_T *vtv;
5668 int di_flags;
5669
5670 *vimvaridx = find_vim_var(name + 2, &di_flags);
5671 if (*vimvaridx < 0)
5672 {
5673 semsg(_(e_variable_not_found_str), name);
5674 return FAIL;
5675 }
5676 // We use the current value of "sandbox" here, is that OK?
5677 if (var_check_ro(di_flags, name, FALSE))
5678 return FAIL;
5679 *dest = dest_vimvar;
5680 vtv = get_vim_var_tv(*vimvaridx);
5681 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5682 }
5683 return OK;
5684}
5685
5686/*
5687 * Generate a STORE instruction for "dest", not being "dest_local".
5688 * Return FAIL when out of memory.
5689 */
5690 static int
5691generate_store_var(
5692 cctx_T *cctx,
5693 assign_dest_T dest,
5694 int opt_flags,
5695 int vimvaridx,
5696 int scriptvar_idx,
5697 int scriptvar_sid,
5698 type_T *type,
5699 char_u *name)
5700{
5701 switch (dest)
5702 {
5703 case dest_option:
5704 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5705 opt_flags);
5706 case dest_global:
5707 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005708 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5709 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005710 case dest_buffer:
5711 // include b: with the name, easier to execute that way
5712 return generate_STORE(cctx, ISN_STOREB, 0, name);
5713 case dest_window:
5714 // include w: with the name, easier to execute that way
5715 return generate_STORE(cctx, ISN_STOREW, 0, name);
5716 case dest_tab:
5717 // include t: with the name, easier to execute that way
5718 return generate_STORE(cctx, ISN_STORET, 0, name);
5719 case dest_env:
5720 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5721 case dest_reg:
5722 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5723 case dest_vimvar:
5724 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5725 case dest_script:
5726 if (scriptvar_idx < 0)
Bram Moolenaar643ce6c2021-04-06 21:17:27 +02005727 // "s:" may be included in the name.
5728 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005729 scriptvar_sid, type);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005730 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5731 scriptvar_sid, scriptvar_idx, type);
5732 case dest_local:
5733 case dest_expr:
5734 // cannot happen
5735 break;
5736 }
5737 return FAIL;
5738}
5739
Bram Moolenaar752fc692021-01-04 21:57:11 +01005740 static int
5741is_decl_command(int cmdidx)
5742{
5743 return cmdidx == CMD_let || cmdidx == CMD_var
5744 || cmdidx == CMD_final || cmdidx == CMD_const;
5745}
5746
5747/*
5748 * Figure out the LHS type and other properties for an assignment or one item
5749 * of ":unlet" with an index.
5750 * Returns OK or FAIL.
5751 */
5752 static int
5753compile_lhs(
5754 char_u *var_start,
5755 lhs_T *lhs,
5756 int cmdidx,
5757 int heredoc,
5758 int oplen,
5759 cctx_T *cctx)
5760{
5761 char_u *var_end;
5762 int is_decl = is_decl_command(cmdidx);
5763
5764 CLEAR_POINTER(lhs);
5765 lhs->lhs_dest = dest_local;
5766 lhs->lhs_vimvaridx = -1;
5767 lhs->lhs_scriptvar_idx = -1;
5768
5769 // "dest_end" is the end of the destination, including "[expr]" or
5770 // ".name".
5771 // "var_end" is the end of the variable/option/etc. name.
5772 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5773 if (*var_start == '@')
5774 var_end = var_start + 2;
5775 else
5776 {
5777 // skip over the leading "&", "&l:", "&g:" and "$"
5778 var_end = skip_option_env_lead(var_start);
5779 var_end = to_name_end(var_end, TRUE);
5780 }
5781
5782 // "a: type" is declaring variable "a" with a type, not dict "a:".
5783 if (is_decl && lhs->lhs_dest_end == var_start + 2
5784 && lhs->lhs_dest_end[-1] == ':')
5785 --lhs->lhs_dest_end;
5786 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5787 --var_end;
5788
5789 // compute the length of the destination without "[expr]" or ".name"
5790 lhs->lhs_varlen = var_end - var_start;
5791 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5792 if (lhs->lhs_name == NULL)
5793 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005794
5795 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5796 // Something follows after the variable: "var[idx]" or "var.key".
5797 lhs->lhs_has_index = TRUE;
5798
Bram Moolenaar752fc692021-01-04 21:57:11 +01005799 if (heredoc)
5800 lhs->lhs_type = &t_list_string;
5801 else
5802 lhs->lhs_type = &t_any;
5803
5804 if (cctx->ctx_skip != SKIP_YES)
5805 {
5806 int declare_error = FALSE;
5807
5808 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5809 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5810 &lhs->lhs_type, cctx) == FAIL)
5811 return FAIL;
Bram Moolenaard877a572021-04-01 19:42:48 +02005812 if (lhs->lhs_dest != dest_local
5813 && cmdidx != CMD_const && cmdidx != CMD_final)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005814 {
5815 // Specific kind of variable recognized.
5816 declare_error = is_decl;
5817 }
5818 else
5819 {
5820 int idx;
5821
5822 // No specific kind of variable recognized, just a name.
5823 for (idx = 0; reserved[idx] != NULL; ++idx)
5824 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5825 {
5826 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5827 return FAIL;
5828 }
5829
5830
5831 if (lookup_local(var_start, lhs->lhs_varlen,
5832 &lhs->lhs_local_lvar, cctx) == OK)
5833 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5834 else
5835 {
5836 CLEAR_FIELD(lhs->lhs_arg_lvar);
5837 if (arg_exists(var_start, lhs->lhs_varlen,
5838 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5839 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5840 {
5841 if (is_decl)
5842 {
5843 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5844 return FAIL;
5845 }
5846 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5847 }
5848 }
5849 if (lhs->lhs_lvar != NULL)
5850 {
5851 if (is_decl)
5852 {
5853 semsg(_(e_variable_already_declared), lhs->lhs_name);
5854 return FAIL;
5855 }
5856 }
5857 else
5858 {
5859 int script_namespace = lhs->lhs_varlen > 1
5860 && STRNCMP(var_start, "s:", 2) == 0;
5861 int script_var = (script_namespace
5862 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005863 cctx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005864 : script_var_exists(var_start, lhs->lhs_varlen,
Bram Moolenaar15e5e532021-04-07 21:21:13 +02005865 cctx)) == OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005866 imported_T *import =
5867 find_imported(var_start, lhs->lhs_varlen, cctx);
5868
5869 if (script_namespace || script_var || import != NULL)
5870 {
5871 char_u *rawname = lhs->lhs_name
5872 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5873
5874 if (is_decl)
5875 {
5876 if (script_namespace)
5877 semsg(_(e_cannot_declare_script_variable_in_function),
5878 lhs->lhs_name);
5879 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005880 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005881 lhs->lhs_name);
5882 return FAIL;
5883 }
5884 else if (cctx->ctx_ufunc->uf_script_ctx_version
5885 == SCRIPT_VERSION_VIM9
5886 && script_namespace
5887 && !script_var && import == NULL)
5888 {
5889 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5890 return FAIL;
5891 }
5892
5893 lhs->lhs_dest = dest_script;
5894
5895 // existing script-local variables should have a type
5896 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5897 if (import != NULL)
5898 lhs->lhs_scriptvar_sid = import->imp_sid;
5899 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5900 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005901 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005902 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005903 lhs->lhs_scriptvar_sid, rawname,
5904 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5905 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 if (lhs->lhs_scriptvar_idx >= 0)
5907 {
5908 scriptitem_T *si = SCRIPT_ITEM(
5909 lhs->lhs_scriptvar_sid);
5910 svar_T *sv =
5911 ((svar_T *)si->sn_var_vals.ga_data)
5912 + lhs->lhs_scriptvar_idx;
5913 lhs->lhs_type = sv->sv_type;
5914 }
5915 }
5916 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005917 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005918 == FAIL)
5919 return FAIL;
5920 }
5921 }
5922
5923 if (declare_error)
5924 {
5925 vim9_declare_error(lhs->lhs_name);
5926 return FAIL;
5927 }
5928 }
5929
5930 // handle "a:name" as a name, not index "name" on "a"
5931 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5932 var_end = lhs->lhs_dest_end;
5933
5934 if (lhs->lhs_dest != dest_option)
5935 {
5936 if (is_decl && *var_end == ':')
5937 {
5938 char_u *p;
5939
5940 // parse optional type: "let var: type = expr"
5941 if (!VIM_ISWHITE(var_end[1]))
5942 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005943 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005944 return FAIL;
5945 }
5946 p = skipwhite(var_end + 1);
5947 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5948 if (lhs->lhs_type == NULL)
5949 return FAIL;
5950 lhs->lhs_has_type = TRUE;
5951 }
5952 else if (lhs->lhs_lvar != NULL)
5953 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5954 }
5955
Bram Moolenaare42939a2021-04-05 17:11:17 +02005956 if (oplen == 3 && !heredoc
5957 && lhs->lhs_dest != dest_global
5958 && !lhs->lhs_has_index
5959 && lhs->lhs_type->tt_type != VAR_STRING
5960 && lhs->lhs_type->tt_type != VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005961 {
5962 emsg(_(e_can_only_concatenate_to_string));
5963 return FAIL;
5964 }
5965
5966 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5967 && cctx->ctx_skip != SKIP_YES)
5968 {
5969 if (oplen > 1 && !heredoc)
5970 {
5971 // +=, /=, etc. require an existing variable
5972 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5973 return FAIL;
5974 }
5975 if (!is_decl)
5976 {
5977 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5978 return FAIL;
5979 }
5980
Bram Moolenaar3f327882021-03-17 20:56:38 +01005981 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005982 if ((lhs->lhs_type->tt_type == VAR_FUNC
5983 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005984 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005985 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005986
5987 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005988 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5989 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5990 if (lhs->lhs_lvar == NULL)
5991 return FAIL;
5992 lhs->lhs_new_local = TRUE;
5993 }
5994
5995 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005996 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005997 {
5998 // Something follows after the variable: "var[idx]" or "var.key".
5999 // TODO: should we also handle "->func()" here?
6000 if (is_decl)
6001 {
6002 emsg(_(e_cannot_use_index_when_declaring_variable));
6003 return FAIL;
6004 }
6005
6006 if (var_start[lhs->lhs_varlen] == '['
6007 || var_start[lhs->lhs_varlen] == '.')
6008 {
6009 char_u *after = var_start + lhs->lhs_varlen;
6010 char_u *p;
6011
6012 // Only the last index is used below, if there are others
6013 // before it generate code for the expression. Thus for
6014 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
6015 for (;;)
6016 {
6017 p = skip_index(after);
6018 if (*p != '[' && *p != '.')
6019 break;
6020 after = p;
6021 }
6022 if (after > var_start + lhs->lhs_varlen)
6023 {
6024 lhs->lhs_varlen = after - var_start;
6025 lhs->lhs_dest = dest_expr;
6026 // We don't know the type before evaluating the expression,
6027 // use "any" until then.
6028 lhs->lhs_type = &t_any;
6029 }
6030
Bram Moolenaar752fc692021-01-04 21:57:11 +01006031 if (lhs->lhs_type->tt_member == NULL)
6032 lhs->lhs_member_type = &t_any;
6033 else
6034 lhs->lhs_member_type = lhs->lhs_type->tt_member;
6035 }
6036 else
6037 {
6038 semsg("Not supported yet: %s", var_start);
6039 return FAIL;
6040 }
6041 }
6042 return OK;
6043}
6044
6045/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006046 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
6047 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 */
6049 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02006050compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01006051 char_u *var_start,
6052 lhs_T *lhs,
6053 int is_assign,
Bram Moolenaare42939a2021-04-05 17:11:17 +02006054 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 cctx_T *cctx)
6056{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006057 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006058 char_u *p;
6059 int r = OK;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006060
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 p = var_start + varlen;
6062 if (*p == '[')
6063 {
6064 p = skipwhite(p + 1);
6065 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006066
6067 if (r == OK && *skipwhite(p) == ':')
6068 {
6069 // unlet var[idx : idx]
6070 if (is_assign)
6071 {
6072 semsg(_(e_cannot_use_range_with_assignment_str), p);
6073 return FAIL;
6074 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006075 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006076 p = skipwhite(p);
6077 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
6078 {
6079 semsg(_(e_white_space_required_before_and_after_str_at_str),
6080 ":", p);
6081 return FAIL;
6082 }
6083 p = skipwhite(p + 1);
6084 r = compile_expr0(&p, cctx);
6085 }
6086
Bram Moolenaar752fc692021-01-04 21:57:11 +01006087 if (r == OK && *skipwhite(p) != ']')
6088 {
6089 // this should not happen
6090 emsg(_(e_missbrac));
6091 r = FAIL;
6092 }
6093 }
6094 else // if (*p == '.')
6095 {
6096 char_u *key_end = to_name_end(p + 1, TRUE);
6097 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6098
6099 r = generate_PUSHS(cctx, key);
6100 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02006101 return r;
6102}
6103
6104/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006105 * For a LHS with an index, load the variable to be indexed.
6106 */
6107 static int
6108compile_load_lhs(
6109 lhs_T *lhs,
6110 char_u *var_start,
6111 type_T *rhs_type,
6112 cctx_T *cctx)
6113{
6114 if (lhs->lhs_dest == dest_expr)
6115 {
6116 size_t varlen = lhs->lhs_varlen;
6117 int c = var_start[varlen];
6118 char_u *p = var_start;
6119 garray_T *stack = &cctx->ctx_type_stack;
6120
6121 // Evaluate "ll[expr]" of "ll[expr][idx]"
6122 var_start[varlen] = NUL;
6123 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6124 {
6125 // this should not happen
6126 emsg(_(e_missbrac));
Bram Moolenaarc9605f02021-04-06 21:29:32 +02006127 var_start[varlen] = c;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006128 return FAIL;
6129 }
6130 var_start[varlen] = c;
6131
6132 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6133 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6134 // now we can properly check the type
6135 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
6136 && rhs_type != &t_void
6137 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
6138 FALSE, FALSE) == FAIL)
6139 return FAIL;
6140 }
6141 else
6142 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6143 lhs->lhs_lvar, lhs->lhs_type);
6144 return OK;
6145}
6146
6147/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02006148 * Assignment to a list or dict member, or ":unlet" for the item, using the
6149 * information in "lhs".
6150 * Returns OK or FAIL.
6151 */
6152 static int
6153compile_assign_unlet(
6154 char_u *var_start,
6155 lhs_T *lhs,
6156 int is_assign,
6157 type_T *rhs_type,
6158 cctx_T *cctx)
6159{
Bram Moolenaare42939a2021-04-05 17:11:17 +02006160 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02006161 garray_T *stack = &cctx->ctx_type_stack;
6162 int range = FALSE;
6163
6164 if (compile_assign_index(var_start, lhs, is_assign, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006165 return FAIL;
6166
6167 if (lhs->lhs_type == &t_any)
6168 {
6169 // Index on variable of unknown type: check at runtime.
6170 dest_type = VAR_ANY;
6171 }
6172 else
6173 {
6174 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006175 if (dest_type == VAR_DICT && range)
6176 {
6177 emsg(e_cannot_use_range_with_dictionary);
6178 return FAIL;
6179 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6181 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006182 if (dest_type == VAR_LIST)
6183 {
6184 if (range
6185 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006186 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006187 return FAIL;
6188 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6189 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6190 return FAIL;
6191 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 }
6193
6194 // Load the dict or list. On the stack we then have:
6195 // - value (for assignment, not for :unlet)
6196 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006197 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006199 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
6200 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006201
6202 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6203 {
6204 if (is_assign)
6205 {
6206 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6207
6208 if (isn == NULL)
6209 return FAIL;
6210 isn->isn_arg.vartype = dest_type;
6211 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006212 else if (range)
6213 {
6214 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6215 return FAIL;
6216 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006217 else
6218 {
6219 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6220 return FAIL;
6221 }
6222 }
6223 else
6224 {
6225 emsg(_(e_indexable_type_required));
6226 return FAIL;
6227 }
6228
6229 return OK;
6230}
6231
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006232/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006233 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006234 * "let name"
6235 * "var name = expr"
6236 * "final name = expr"
6237 * "const name = expr"
6238 * "name = expr"
6239 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 * Return NULL for an error.
6241 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 */
6243 static char_u *
6244compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6245{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006246 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006248 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249 char_u *ret = NULL;
6250 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006251 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006254 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 int oplen = 0;
6257 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006258 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006260 int is_decl = is_decl_command(cmdidx);
6261 lhs_T lhs;
Bram Moolenaar77709b12021-04-03 21:01:01 +02006262 long start_lnum = SOURCING_LNUM;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006264 // Skip over the "var" or "[var, var]" to get to any "=".
6265 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6266 if (p == NULL)
6267 return *arg == '[' ? arg : NULL;
6268
6269 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006271 // TODO: should we allow this, and figure out type inference from list
6272 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006273 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 return NULL;
6275 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006276 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 sp = p;
6279 p = skipwhite(p);
6280 op = p;
6281 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006282
6283 if (var_count > 0 && oplen == 0)
6284 // can be something like "[1, 2]->func()"
6285 return arg;
6286
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006287 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006289 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006290 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006291 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 if (heredoc)
6294 {
6295 list_T *l;
6296 listitem_T *li;
6297
6298 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006299 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006301 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006302 if (l == NULL)
6303 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304
Bram Moolenaar078269b2020-09-21 20:35:55 +02006305 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006306 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006307 // Push each line and the create the list.
6308 FOR_ALL_LIST_ITEMS(l, li)
6309 {
6310 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6311 li->li_tv.vval.v_string = NULL;
6312 }
6313 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315 list_free(l);
6316 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006317 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006318 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006319 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006321 char_u *wp;
6322
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006323 // for "[var, var] = expr" evaluate the expression here, loop over the
6324 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006325 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006326
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006327 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006328 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006329 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330 if (compile_expr0(&p, cctx) == FAIL)
6331 return NULL;
6332 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006334 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006336 type_T *stacktype;
6337
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006338 stacktype = stack->ga_len == 0 ? &t_void
6339 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006340 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006342 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006343 goto theend;
6344 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006345 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006346 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006347 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006348 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006349 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6350 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006351 if (stacktype->tt_member != NULL)
6352 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006353 }
6354 }
6355
6356 /*
6357 * Loop over variables in "[var, var] = expr".
6358 * For "var = expr" and "let var: type" this is done only once.
6359 */
6360 if (var_count > 0)
6361 var_start = skipwhite(arg + 1); // skip over the "["
6362 else
6363 var_start = arg;
6364 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6365 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006366 int instr_count = -1;
6367
Bram Moolenaar752fc692021-01-04 21:57:11 +01006368 vim_free(lhs.lhs_name);
6369
6370 /*
6371 * Figure out the LHS type and other properties.
6372 */
6373 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6374 goto theend;
6375
6376 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006377 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006378 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006379 goto theend;
6380 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006381 if (!is_decl && lhs.lhs_lvar != NULL
6382 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006383 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006384 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006385 goto theend;
6386 }
Bram Moolenaar962c43b2021-04-10 17:18:09 +02006387 if (is_decl && lhs.lhs_name[0] == '_' && lhs.lhs_name[1] == NUL)
6388 {
6389 emsg(_(e_cannot_use_underscore_here));
6390 goto theend;
6391 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006392
6393 if (!heredoc)
6394 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006395 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006396 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006397 if (oplen > 0 && var_count == 0)
6398 {
6399 // skip over the "=" and the expression
6400 p = skipwhite(op + oplen);
6401 compile_expr0(&p, cctx);
6402 }
6403 }
6404 else if (oplen > 0)
6405 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006406 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006407 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006408
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006409 // For "var = expr" evaluate the expression.
6410 if (var_count == 0)
6411 {
6412 int r;
6413
6414 // for "+=", "*=", "..=" etc. first load the current value
6415 if (*op != '=')
6416 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02006417 compile_load_lhs(&lhs, var_start, NULL, cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006418
Bram Moolenaar752fc692021-01-04 21:57:11 +01006419 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006420 {
Bram Moolenaare42939a2021-04-05 17:11:17 +02006421 int range = FALSE;
6422
6423 // Get member from list or dict. First compile the
6424 // index value.
6425 if (compile_assign_index(var_start, &lhs,
6426 TRUE, &range, cctx) == FAIL)
6427 goto theend;
6428
6429 // Get the member.
6430 if (compile_member(FALSE, cctx) == FAIL)
6431 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006432 }
6433 }
6434
6435 // Compile the expression. Temporarily hide the new local
6436 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006437 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006438 --cctx->ctx_locals.ga_len;
6439 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006440 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006441 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006442 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006443 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006444 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006445 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006446 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006447 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006448 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006449 ++cctx->ctx_locals.ga_len;
6450 if (r == FAIL)
6451 goto theend;
6452 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006453 else if (semicolon && var_idx == var_count - 1)
6454 {
6455 // For "[var; var] = expr" get the rest of the list
6456 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6457 goto theend;
6458 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006459 else
6460 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006461 // For "[var, var] = expr" get the "var_idx" item from the
6462 // list.
6463 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006464 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006465 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006466
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006467 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006468 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006470 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006471 if ((rhs_type->tt_type == VAR_FUNC
6472 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006473 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006475 goto theend;
6476
Bram Moolenaar752fc692021-01-04 21:57:11 +01006477 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006478 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006479 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006480 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006481 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006482 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006483 }
6484 else
6485 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006486 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006487 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006488 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006489 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006490 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006491 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006492 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006494 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006495 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006496 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006497 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006498 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006499 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006501
Bram Moolenaar77709b12021-04-03 21:01:01 +02006502 // Without operator check type here, otherwise below.
6503 // Use the line number of the assignment.
6504 SOURCING_LNUM = start_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 if (lhs.lhs_has_index)
6506 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006507 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006508 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006509 goto theend;
6510 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006511 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006512 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006513 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006514 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006515 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006516 else if (cmdidx == CMD_final)
6517 {
6518 emsg(_(e_final_requires_a_value));
6519 goto theend;
6520 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006521 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006522 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006523 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006524 goto theend;
6525 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006526 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006527 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006528 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006529 goto theend;
6530 }
6531 else
6532 {
6533 // variables are always initialized
6534 if (ga_grow(instr, 1) == FAIL)
6535 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006536 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006537 {
6538 case VAR_BOOL:
6539 generate_PUSHBOOL(cctx, VVAL_FALSE);
6540 break;
6541 case VAR_FLOAT:
6542#ifdef FEAT_FLOAT
6543 generate_PUSHF(cctx, 0.0);
6544#endif
6545 break;
6546 case VAR_STRING:
6547 generate_PUSHS(cctx, NULL);
6548 break;
6549 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006550 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006551 break;
6552 case VAR_FUNC:
6553 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6554 break;
6555 case VAR_LIST:
6556 generate_NEWLIST(cctx, 0);
6557 break;
6558 case VAR_DICT:
6559 generate_NEWDICT(cctx, 0);
6560 break;
6561 case VAR_JOB:
6562 generate_PUSHJOB(cctx, NULL);
6563 break;
6564 case VAR_CHANNEL:
6565 generate_PUSHCHANNEL(cctx, NULL);
6566 break;
6567 case VAR_NUMBER:
6568 case VAR_UNKNOWN:
6569 case VAR_ANY:
6570 case VAR_PARTIAL:
6571 case VAR_VOID:
6572 case VAR_SPECIAL: // cannot happen
6573 generate_PUSHNR(cctx, 0);
6574 break;
6575 }
6576 }
6577 if (var_count == 0)
6578 end = p;
6579 }
6580
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006581 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006582 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006583 break;
6584
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006585 if (oplen > 0 && *op != '=')
6586 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006587 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006588 type_T *stacktype;
6589
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006590 if (*op == '.')
6591 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006592 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006593 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006594 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006595 if (
6596#ifdef FEAT_FLOAT
6597 // If variable is float operation with number is OK.
6598 !(expected == &t_float && stacktype == &t_number) &&
6599#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006600 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006601 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006602 goto theend;
6603
6604 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006605 {
6606 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6607 goto theend;
6608 }
6609 else if (*op == '+')
6610 {
6611 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006612 operator_type(lhs.lhs_member_type, stacktype),
6613 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006614 goto theend;
6615 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006616 else if (generate_two_op(cctx, op) == FAIL)
6617 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619
Bram Moolenaar752fc692021-01-04 21:57:11 +01006620 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006621 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006622 // Use the info in "lhs" to store the value at the index in the
6623 // list or dict.
6624 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6625 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006626 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006627 }
6628 else
6629 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006630 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
Bram Moolenaard877a572021-04-01 19:42:48 +02006631 || lhs.lhs_dest == dest_global
Bram Moolenaar752fc692021-01-04 21:57:11 +01006632 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006633 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006634 generate_LOCKCONST(cctx);
6635
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006636 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006637 && (lhs.lhs_type->tt_type == VAR_DICT
6638 || lhs.lhs_type->tt_type == VAR_LIST)
6639 && lhs.lhs_type->tt_member != NULL
6640 && lhs.lhs_type->tt_member != &t_any
6641 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006642 // Set the type in the list or dict, so that it can be checked,
6643 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006644 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006645
Bram Moolenaar752fc692021-01-04 21:57:11 +01006646 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006647 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006648 if (generate_store_var(cctx, lhs.lhs_dest,
6649 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6650 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6651 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006652 goto theend;
6653 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006655 {
6656 isn_T *isn = ((isn_T *)instr->ga_data)
6657 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006658
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006659 // optimization: turn "var = 123" from ISN_PUSHNR +
6660 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006661 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006662 && instr->ga_len == instr_count + 1
6663 && isn->isn_type == ISN_PUSHNR)
6664 {
6665 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006666
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006667 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006668 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006669 isn->isn_arg.storenr.stnr_val = val;
6670 if (stack->ga_len > 0)
6671 --stack->ga_len;
6672 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006673 else if (lhs.lhs_lvar->lv_from_outer > 0)
6674 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6675 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006676 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006677 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006678 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006679 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006680
6681 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006682 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006683 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006684
6685 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006686 if (var_count > 0 && !semicolon)
6687 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006688 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006689 goto theend;
6690 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006691
Bram Moolenaarb2097502020-07-19 17:17:02 +02006692 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006693
6694theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006695 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006696 return ret;
6697}
6698
6699/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006700 * Check for an assignment at "eap->cmd", compile it if found.
6701 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6702 */
6703 static int
6704may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6705{
6706 char_u *pskip;
6707 char_u *p;
6708
6709 // Assuming the command starts with a variable or function name,
6710 // find what follows.
6711 // Skip over "var.member", "var[idx]" and the like.
6712 // Also "&opt = val", "$ENV = val" and "@r = val".
6713 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6714 ? eap->cmd + 1 : eap->cmd;
6715 p = to_name_end(pskip, TRUE);
6716 if (p > eap->cmd && *p != NUL)
6717 {
6718 char_u *var_end;
6719 int oplen;
6720 int heredoc;
6721
6722 if (eap->cmd[0] == '@')
6723 var_end = eap->cmd + 2;
6724 else
6725 var_end = find_name_end(pskip, NULL, NULL,
6726 FNE_CHECK_START | FNE_INCL_BR);
6727 oplen = assignment_len(skipwhite(var_end), &heredoc);
6728 if (oplen > 0)
6729 {
6730 size_t len = p - eap->cmd;
6731
6732 // Recognize an assignment if we recognize the variable
6733 // name:
6734 // "g:var = expr"
6735 // "local = expr" where "local" is a local var.
6736 // "script = expr" where "script" is a script-local var.
6737 // "import = expr" where "import" is an imported var
6738 // "&opt = expr"
6739 // "$ENV = expr"
6740 // "@r = expr"
6741 if (*eap->cmd == '&'
6742 || *eap->cmd == '$'
6743 || *eap->cmd == '@'
6744 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006745 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006746 {
6747 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6748 if (*line == NULL || *line == eap->cmd)
6749 return FAIL;
6750 return OK;
6751 }
6752 }
6753 }
6754
6755 if (*eap->cmd == '[')
6756 {
6757 // [var, var] = expr
6758 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6759 if (*line == NULL)
6760 return FAIL;
6761 if (*line != eap->cmd)
6762 return OK;
6763 }
6764 return NOTDONE;
6765}
6766
6767/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006768 * Check if "name" can be "unlet".
6769 */
6770 int
6771check_vim9_unlet(char_u *name)
6772{
6773 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6774 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006775 // "unlet s:var" is allowed in legacy script.
6776 if (*name == 's' && !script_is_vim9())
6777 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006778 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006779 return FAIL;
6780 }
6781 return OK;
6782}
6783
6784/*
6785 * Callback passed to ex_unletlock().
6786 */
6787 static int
6788compile_unlet(
6789 lval_T *lvp,
6790 char_u *name_end,
6791 exarg_T *eap,
6792 int deep UNUSED,
6793 void *coookie)
6794{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006795 cctx_T *cctx = coookie;
6796 char_u *p = lvp->ll_name;
6797 int cc = *name_end;
6798 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006799
Bram Moolenaar752fc692021-01-04 21:57:11 +01006800 if (cctx->ctx_skip == SKIP_YES)
6801 return OK;
6802
6803 *name_end = NUL;
6804 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006805 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006806 // :unlet $ENV_VAR
6807 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6808 }
6809 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6810 {
6811 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006812
Bram Moolenaar752fc692021-01-04 21:57:11 +01006813 // This is similar to assigning: lookup the list/dict, compile the
6814 // idx/key. Then instead of storing the value unlet the item.
6815 // unlet {list}[idx]
6816 // unlet {dict}[key] dict.key
6817 //
6818 // Figure out the LHS type and other properties.
6819 //
6820 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6821
6822 // : unlet an indexed item
6823 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006824 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006825 iemsg("called compile_lhs() without an index");
6826 ret = FAIL;
6827 }
6828 else
6829 {
6830 // Use the info in "lhs" to unlet the item at the index in the
6831 // list or dict.
6832 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006833 }
6834
Bram Moolenaar752fc692021-01-04 21:57:11 +01006835 vim_free(lhs.lhs_name);
6836 }
6837 else if (check_vim9_unlet(p) == FAIL)
6838 {
6839 ret = FAIL;
6840 }
6841 else
6842 {
6843 // Normal name. Only supports g:, w:, t: and b: namespaces.
6844 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006845 }
6846
Bram Moolenaar752fc692021-01-04 21:57:11 +01006847 *name_end = cc;
6848 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006849}
6850
6851/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006852 * Callback passed to ex_unletlock().
6853 */
6854 static int
6855compile_lock_unlock(
6856 lval_T *lvp,
6857 char_u *name_end,
6858 exarg_T *eap,
6859 int deep UNUSED,
6860 void *coookie)
6861{
6862 cctx_T *cctx = coookie;
6863 int cc = *name_end;
6864 char_u *p = lvp->ll_name;
6865 int ret = OK;
6866 size_t len;
6867 char_u *buf;
6868
6869 if (cctx->ctx_skip == SKIP_YES)
6870 return OK;
6871
6872 // Cannot use :lockvar and :unlockvar on local variables.
6873 if (p[1] != ':')
6874 {
6875 char_u *end = skip_var_one(p, FALSE);
6876
6877 if (lookup_local(p, end - p, NULL, cctx) == OK)
6878 {
6879 emsg(_(e_cannot_lock_unlock_local_variable));
6880 return FAIL;
6881 }
6882 }
6883
6884 // Checking is done at runtime.
6885 *name_end = NUL;
6886 len = name_end - p + 20;
6887 buf = alloc(len);
6888 if (buf == NULL)
6889 ret = FAIL;
6890 else
6891 {
6892 vim_snprintf((char *)buf, len, "%s %s",
6893 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6894 p);
6895 ret = generate_EXEC(cctx, buf);
6896
6897 vim_free(buf);
6898 *name_end = cc;
6899 }
6900 return ret;
6901}
6902
6903/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006904 * compile "unlet var", "lock var" and "unlock var"
6905 * "arg" points to "var".
6906 */
6907 static char_u *
6908compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6909{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006910 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6911 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6912 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006913 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6914}
6915
6916/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 * Compile an :import command.
6918 */
6919 static char_u *
6920compile_import(char_u *arg, cctx_T *cctx)
6921{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006922 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923}
6924
6925/*
6926 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6927 */
6928 static int
6929compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6930{
6931 garray_T *instr = &cctx->ctx_instr;
6932 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6933
6934 if (endlabel == NULL)
6935 return FAIL;
6936 endlabel->el_next = *el;
6937 *el = endlabel;
6938 endlabel->el_end_label = instr->ga_len;
6939
6940 generate_JUMP(cctx, when, 0);
6941 return OK;
6942}
6943
6944 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006945compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006946{
6947 garray_T *instr = &cctx->ctx_instr;
6948
6949 while (*el != NULL)
6950 {
6951 endlabel_T *cur = (*el);
6952 isn_T *isn;
6953
6954 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006955 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 *el = cur->el_next;
6957 vim_free(cur);
6958 }
6959}
6960
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006961 static void
6962compile_free_jump_to_end(endlabel_T **el)
6963{
6964 while (*el != NULL)
6965 {
6966 endlabel_T *cur = (*el);
6967
6968 *el = cur->el_next;
6969 vim_free(cur);
6970 }
6971}
6972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006973/*
6974 * Create a new scope and set up the generic items.
6975 */
6976 static scope_T *
6977new_scope(cctx_T *cctx, scopetype_T type)
6978{
6979 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6980
6981 if (scope == NULL)
6982 return NULL;
6983 scope->se_outer = cctx->ctx_scope;
6984 cctx->ctx_scope = scope;
6985 scope->se_type = type;
6986 scope->se_local_count = cctx->ctx_locals.ga_len;
6987 return scope;
6988}
6989
6990/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006991 * Free the current scope and go back to the outer scope.
6992 */
6993 static void
6994drop_scope(cctx_T *cctx)
6995{
6996 scope_T *scope = cctx->ctx_scope;
6997
6998 if (scope == NULL)
6999 {
7000 iemsg("calling drop_scope() without a scope");
7001 return;
7002 }
7003 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007004 switch (scope->se_type)
7005 {
7006 case IF_SCOPE:
7007 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
7008 case FOR_SCOPE:
7009 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
7010 case WHILE_SCOPE:
7011 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
7012 case TRY_SCOPE:
7013 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
7014 case NO_SCOPE:
7015 case BLOCK_SCOPE:
7016 break;
7017 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007018 vim_free(scope);
7019}
7020
7021/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 * compile "if expr"
7023 *
7024 * "if expr" Produces instructions:
7025 * EVAL expr Push result of "expr"
7026 * JUMP_IF_FALSE end
7027 * ... body ...
7028 * end:
7029 *
7030 * "if expr | else" Produces instructions:
7031 * EVAL expr Push result of "expr"
7032 * JUMP_IF_FALSE else
7033 * ... body ...
7034 * JUMP_ALWAYS end
7035 * else:
7036 * ... body ...
7037 * end:
7038 *
7039 * "if expr1 | elseif expr2 | else" Produces instructions:
7040 * EVAL expr Push result of "expr"
7041 * JUMP_IF_FALSE elseif
7042 * ... body ...
7043 * JUMP_ALWAYS end
7044 * elseif:
7045 * EVAL expr Push result of "expr"
7046 * JUMP_IF_FALSE else
7047 * ... body ...
7048 * JUMP_ALWAYS end
7049 * else:
7050 * ... body ...
7051 * end:
7052 */
7053 static char_u *
7054compile_if(char_u *arg, cctx_T *cctx)
7055{
7056 char_u *p = arg;
7057 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007058 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007060 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007061 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062
Bram Moolenaara5565e42020-05-09 15:44:01 +02007063 CLEAR_FIELD(ppconst);
7064 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007065 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02007066 clear_ppconst(&ppconst);
7067 return NULL;
7068 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007069 if (!ends_excmd2(arg, skipwhite(p)))
7070 {
7071 semsg(_(e_trailing_arg), p);
7072 return NULL;
7073 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007074 if (cctx->ctx_skip == SKIP_YES)
7075 clear_ppconst(&ppconst);
7076 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02007077 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007078 int error = FALSE;
7079 int v;
7080
Bram Moolenaara5565e42020-05-09 15:44:01 +02007081 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02007082 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02007083 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02007084 if (error)
7085 return NULL;
7086 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007087 }
7088 else
7089 {
7090 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007091 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007092 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007093 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007094 if (bool_on_stack(cctx) == FAIL)
7095 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
Bram Moolenaara91a7132021-03-25 21:12:15 +01007098 // CMDMOD_REV must come before the jump
7099 generate_undo_cmdmods(cctx);
7100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101 scope = new_scope(cctx, IF_SCOPE);
7102 if (scope == NULL)
7103 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007104 scope->se_skip_save = skip_save;
7105 // "is_had_return" will be reset if any block does not end in :return
7106 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007108 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007109 {
7110 // "where" is set when ":elseif", "else" or ":endif" is found
7111 scope->se_u.se_if.is_if_label = instr->ga_len;
7112 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7113 }
7114 else
7115 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116
Bram Moolenaarced68a02021-01-24 17:53:47 +01007117#ifdef FEAT_PROFILE
7118 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7119 && skip_save != SKIP_YES)
7120 {
7121 // generated a profile start, need to generate a profile end, since it
7122 // won't be done after returning
7123 cctx->ctx_skip = SKIP_NOT;
7124 generate_instr(cctx, ISN_PROF_END);
7125 cctx->ctx_skip = SKIP_YES;
7126 }
7127#endif
7128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 return p;
7130}
7131
7132 static char_u *
7133compile_elseif(char_u *arg, cctx_T *cctx)
7134{
7135 char_u *p = arg;
7136 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007137 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007138 isn_T *isn;
7139 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007140 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007141 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142
7143 if (scope == NULL || scope->se_type != IF_SCOPE)
7144 {
7145 emsg(_(e_elseif_without_if));
7146 return NULL;
7147 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007148 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007149 if (!cctx->ctx_had_return)
7150 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007151
Bram Moolenaarced68a02021-01-24 17:53:47 +01007152 if (cctx->ctx_skip == SKIP_NOT)
7153 {
7154 // previous block was executed, this one and following will not
7155 cctx->ctx_skip = SKIP_YES;
7156 scope->se_u.se_if.is_seen_skip_not = TRUE;
7157 }
7158 if (scope->se_u.se_if.is_seen_skip_not)
7159 {
7160 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007161 // Do not count the "elseif" for profiling and cmdmod
7162 instr->ga_len = current_instr_idx(cctx);
7163
Bram Moolenaarced68a02021-01-24 17:53:47 +01007164 skip_expr_cctx(&p, cctx);
7165 return p;
7166 }
7167
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007168 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007169 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007170 int moved_cmdmod = FALSE;
7171
7172 // Move any CMDMOD instruction to after the jump
7173 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7174 {
7175 if (ga_grow(instr, 1) == FAIL)
7176 return NULL;
7177 ((isn_T *)instr->ga_data)[instr->ga_len] =
7178 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7179 --instr->ga_len;
7180 moved_cmdmod = TRUE;
7181 }
7182
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007183 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007185 return NULL;
7186 // previous "if" or "elseif" jumps here
7187 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7188 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007189 if (moved_cmdmod)
7190 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007192
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007193 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007194 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007195 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007196 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007197 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007198#ifdef FEAT_PROFILE
7199 if (cctx->ctx_profiling)
7200 {
7201 // the previous block was skipped, need to profile this line
7202 generate_instr(cctx, ISN_PROF_START);
7203 instr_count = instr->ga_len;
7204 }
7205#endif
7206 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007207 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007208 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007209 clear_ppconst(&ppconst);
7210 return NULL;
7211 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007212 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007213 if (!ends_excmd2(arg, skipwhite(p)))
7214 {
7215 semsg(_(e_trailing_arg), p);
7216 return NULL;
7217 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007218 if (scope->se_skip_save == SKIP_YES)
7219 clear_ppconst(&ppconst);
7220 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007221 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007222 int error = FALSE;
7223 int v;
7224
Bram Moolenaar7f141552020-05-09 17:35:53 +02007225 // The expression results in a constant.
7226 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007227 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7228 if (error)
7229 return NULL;
7230 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007231 clear_ppconst(&ppconst);
7232 scope->se_u.se_if.is_if_label = -1;
7233 }
7234 else
7235 {
7236 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007237 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007238 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007239 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007240 if (bool_on_stack(cctx) == FAIL)
7241 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242
Bram Moolenaara91a7132021-03-25 21:12:15 +01007243 // CMDMOD_REV must come before the jump
7244 generate_undo_cmdmods(cctx);
7245
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007246 // "where" is set when ":elseif", "else" or ":endif" is found
7247 scope->se_u.se_if.is_if_label = instr->ga_len;
7248 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007250
7251 return p;
7252}
7253
7254 static char_u *
7255compile_else(char_u *arg, cctx_T *cctx)
7256{
7257 char_u *p = arg;
7258 garray_T *instr = &cctx->ctx_instr;
7259 isn_T *isn;
7260 scope_T *scope = cctx->ctx_scope;
7261
7262 if (scope == NULL || scope->se_type != IF_SCOPE)
7263 {
7264 emsg(_(e_else_without_if));
7265 return NULL;
7266 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007267 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007268 if (!cctx->ctx_had_return)
7269 scope->se_u.se_if.is_had_return = FALSE;
7270 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271
Bram Moolenaarced68a02021-01-24 17:53:47 +01007272#ifdef FEAT_PROFILE
7273 if (cctx->ctx_profiling)
7274 {
7275 if (cctx->ctx_skip == SKIP_NOT
7276 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7277 .isn_type == ISN_PROF_START)
7278 // the previous block was executed, do not count "else" for profiling
7279 --instr->ga_len;
7280 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7281 {
7282 // the previous block was not executed, this one will, do count the
7283 // "else" for profiling
7284 cctx->ctx_skip = SKIP_NOT;
7285 generate_instr(cctx, ISN_PROF_END);
7286 generate_instr(cctx, ISN_PROF_START);
7287 cctx->ctx_skip = SKIP_YES;
7288 }
7289 }
7290#endif
7291
7292 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007293 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007294 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007295 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007296 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007297 if (!cctx->ctx_had_return
7298 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7299 JUMP_ALWAYS, cctx) == FAIL)
7300 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007301 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007302
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007303 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007304 {
7305 if (scope->se_u.se_if.is_if_label >= 0)
7306 {
7307 // previous "if" or "elseif" jumps here
7308 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7309 isn->isn_arg.jump.jump_where = instr->ga_len;
7310 scope->se_u.se_if.is_if_label = -1;
7311 }
7312 }
7313
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007314 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007315 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317
7318 return p;
7319}
7320
7321 static char_u *
7322compile_endif(char_u *arg, cctx_T *cctx)
7323{
7324 scope_T *scope = cctx->ctx_scope;
7325 ifscope_T *ifscope;
7326 garray_T *instr = &cctx->ctx_instr;
7327 isn_T *isn;
7328
Bram Moolenaarfa984412021-03-25 22:15:28 +01007329 if (misplaced_cmdmod(cctx))
7330 return NULL;
7331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007332 if (scope == NULL || scope->se_type != IF_SCOPE)
7333 {
7334 emsg(_(e_endif_without_if));
7335 return NULL;
7336 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007337 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007338 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007339 if (!cctx->ctx_had_return)
7340 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007341
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007342 if (scope->se_u.se_if.is_if_label >= 0)
7343 {
7344 // previous "if" or "elseif" jumps here
7345 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7346 isn->isn_arg.jump.jump_where = instr->ga_len;
7347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007349 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007350
7351#ifdef FEAT_PROFILE
7352 // even when skipping we count the endif as executed, unless the block it's
7353 // in is skipped
7354 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7355 && scope->se_skip_save != SKIP_YES)
7356 {
7357 cctx->ctx_skip = SKIP_NOT;
7358 generate_instr(cctx, ISN_PROF_START);
7359 }
7360#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007361 cctx->ctx_skip = scope->se_skip_save;
7362
7363 // If all the blocks end in :return and there is an :else then the
7364 // had_return flag is set.
7365 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007367 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368 return arg;
7369}
7370
7371/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007372 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007373 *
7374 * Produces instructions:
7375 * PUSHNR -1
7376 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007377 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7379 * - if beyond end, jump to "end"
7380 * - otherwise get item from list and push it
7381 * STORE var Store item in "var"
7382 * ... body ...
7383 * JUMP top Jump back to repeat
7384 * end: DROP Drop the result of "expr"
7385 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007386 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7387 * UNPACK 2 Split item in 2
7388 * STORE var1 Store item in "var1"
7389 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 */
7391 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007392compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007394 char_u *arg;
7395 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007396 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007398 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007399 int var_count = 0;
7400 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 garray_T *stack = &cctx->ctx_type_stack;
7403 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007404 lvar_T *loop_lvar; // loop iteration variable
7405 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007407 type_T *item_type = &t_any;
7408 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409
Bram Moolenaar792f7862020-11-23 08:31:18 +01007410 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007411 if (p == NULL)
7412 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007413 if (var_count == 0)
7414 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007415
7416 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007417 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007418 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7419 return NULL;
7420 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 {
7422 emsg(_(e_missing_in));
7423 return NULL;
7424 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007425 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007426 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7427 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 scope = new_scope(cctx, FOR_SCOPE);
7430 if (scope == NULL)
7431 return NULL;
7432
Bram Moolenaar792f7862020-11-23 08:31:18 +01007433 // Reserve a variable to store the loop iteration counter and initialize it
7434 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007435 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7436 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007437 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007438 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007439 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007441 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007442 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443
7444 // compile "expr", it remains on the stack until "endfor"
7445 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007446 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007447 {
7448 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007450 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007451 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007452
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007453 // If we know the type of "var" and it is a not a list or string we can
7454 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007456 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7457 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007458 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007459 // TODO: support Blob
7460 semsg(_(e_for_loop_on_str_not_supported),
7461 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007462 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007463 return NULL;
7464 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007465
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007466 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007467 {
7468 if (var_count == 1)
7469 item_type = vartype->tt_member;
7470 else if (vartype->tt_member->tt_type == VAR_LIST
7471 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7472 item_type = vartype->tt_member->tt_member;
7473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474
Bram Moolenaara91a7132021-03-25 21:12:15 +01007475 // CMDMOD_REV must come before the FOR instruction
7476 generate_undo_cmdmods(cctx);
7477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007479 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007480 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481
Bram Moolenaar792f7862020-11-23 08:31:18 +01007482 arg = arg_start;
7483 if (var_count > 1)
7484 {
7485 generate_UNPACK(cctx, var_count, semicolon);
7486 arg = skipwhite(arg + 1); // skip white after '['
7487
7488 // the list item is replaced by a number of items
7489 if (ga_grow(stack, var_count - 1) == FAIL)
7490 {
7491 drop_scope(cctx);
7492 return NULL;
7493 }
7494 --stack->ga_len;
7495 for (idx = 0; idx < var_count; ++idx)
7496 {
7497 ((type_T **)stack->ga_data)[stack->ga_len] =
7498 (semicolon && idx == 0) ? vartype : item_type;
7499 ++stack->ga_len;
7500 }
7501 }
7502
7503 for (idx = 0; idx < var_count; ++idx)
7504 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007505 assign_dest_T dest = dest_local;
7506 int opt_flags = 0;
7507 int vimvaridx = -1;
7508 type_T *type = &t_any;
7509
7510 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007511 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007512 name = vim_strnsave(arg, varlen);
7513 if (name == NULL)
7514 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007515
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007516 // TODO: script var not supported?
7517 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7518 &vimvaridx, &type, cctx) == FAIL)
7519 goto failed;
7520 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007521 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007522 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7523 0, 0, type, name) == FAIL)
7524 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007525 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007526 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007527 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007528 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007529 {
7530 semsg(_(e_variable_already_declared), arg);
7531 goto failed;
7532 }
7533
Bram Moolenaarea870692020-12-02 14:24:30 +01007534 if (STRNCMP(name, "s:", 2) == 0)
7535 {
7536 semsg(_(e_cannot_declare_script_variable_in_function), name);
7537 goto failed;
7538 }
7539
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007540 // Reserve a variable to store "var".
7541 // TODO: check for type
7542 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7543 if (var_lvar == NULL)
7544 // out of memory or used as an argument
7545 goto failed;
7546
7547 if (semicolon && idx == var_count - 1)
7548 var_lvar->lv_type = vartype;
7549 else
7550 var_lvar->lv_type = item_type;
7551 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7552 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007553
Bram Moolenaar036d0712021-01-17 20:23:38 +01007554 if (*p == ':')
7555 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007556 if (*p == ',' || *p == ';')
7557 ++p;
7558 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007559 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007560 }
7561
7562 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007563
7564failed:
7565 vim_free(name);
7566 drop_scope(cctx);
7567 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568}
7569
7570/*
7571 * compile "endfor"
7572 */
7573 static char_u *
7574compile_endfor(char_u *arg, cctx_T *cctx)
7575{
7576 garray_T *instr = &cctx->ctx_instr;
7577 scope_T *scope = cctx->ctx_scope;
7578 forscope_T *forscope;
7579 isn_T *isn;
7580
Bram Moolenaarfa984412021-03-25 22:15:28 +01007581 if (misplaced_cmdmod(cctx))
7582 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 if (scope == NULL || scope->se_type != FOR_SCOPE)
7585 {
7586 emsg(_(e_for));
7587 return NULL;
7588 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007589 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007591 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007592
7593 // At end of ":for" scope jump back to the FOR instruction.
7594 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7595
7596 // Fill in the "end" label in the FOR statement so it can jump here
7597 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7598 isn->isn_arg.forloop.for_end = instr->ga_len;
7599
7600 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007601 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007602
7603 // Below the ":for" scope drop the "expr" list from the stack.
7604 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7605 return NULL;
7606
7607 vim_free(scope);
7608
7609 return arg;
7610}
7611
7612/*
7613 * compile "while expr"
7614 *
7615 * Produces instructions:
7616 * top: EVAL expr Push result of "expr"
7617 * JUMP_IF_FALSE end jump if false
7618 * ... body ...
7619 * JUMP top Jump back to repeat
7620 * end:
7621 *
7622 */
7623 static char_u *
7624compile_while(char_u *arg, cctx_T *cctx)
7625{
7626 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 scope_T *scope;
7628
7629 scope = new_scope(cctx, WHILE_SCOPE);
7630 if (scope == NULL)
7631 return NULL;
7632
Bram Moolenaara91a7132021-03-25 21:12:15 +01007633 // "endwhile" jumps back here, one before when profiling or using cmdmods
7634 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635
7636 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007637 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007639 if (!ends_excmd2(arg, skipwhite(p)))
7640 {
7641 semsg(_(e_trailing_arg), p);
7642 return NULL;
7643 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007644
Bram Moolenaar13106602020-10-04 16:06:05 +02007645 if (bool_on_stack(cctx) == FAIL)
7646 return FAIL;
7647
Bram Moolenaara91a7132021-03-25 21:12:15 +01007648 // CMDMOD_REV must come before the jump
7649 generate_undo_cmdmods(cctx);
7650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007652 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007653 JUMP_IF_FALSE, cctx) == FAIL)
7654 return FAIL;
7655
7656 return p;
7657}
7658
7659/*
7660 * compile "endwhile"
7661 */
7662 static char_u *
7663compile_endwhile(char_u *arg, cctx_T *cctx)
7664{
7665 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007666 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
Bram Moolenaarfa984412021-03-25 22:15:28 +01007668 if (misplaced_cmdmod(cctx))
7669 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7671 {
7672 emsg(_(e_while));
7673 return NULL;
7674 }
7675 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007676 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007677
Bram Moolenaarf002a412021-01-24 13:34:18 +01007678#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007679 // count the endwhile before jumping
7680 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007681#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007684 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685
7686 // Fill in the "end" label in the WHILE statement so it can jump here.
7687 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007688 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7689 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007690
7691 vim_free(scope);
7692
7693 return arg;
7694}
7695
7696/*
7697 * compile "continue"
7698 */
7699 static char_u *
7700compile_continue(char_u *arg, cctx_T *cctx)
7701{
7702 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007703 int try_scopes = 0;
7704 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705
7706 for (;;)
7707 {
7708 if (scope == NULL)
7709 {
7710 emsg(_(e_continue));
7711 return NULL;
7712 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007713 if (scope->se_type == FOR_SCOPE)
7714 {
7715 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007717 }
7718 if (scope->se_type == WHILE_SCOPE)
7719 {
7720 loop_label = scope->se_u.se_while.ws_top_label;
7721 break;
7722 }
7723 if (scope->se_type == TRY_SCOPE)
7724 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007725 scope = scope->se_outer;
7726 }
7727
Bram Moolenaarc150c092021-02-13 15:02:46 +01007728 if (try_scopes > 0)
7729 // Inside one or more try/catch blocks we first need to jump to the
7730 // "finally" or "endtry" to cleanup.
7731 generate_TRYCONT(cctx, try_scopes, loop_label);
7732 else
7733 // Jump back to the FOR or WHILE instruction.
7734 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 return arg;
7737}
7738
7739/*
7740 * compile "break"
7741 */
7742 static char_u *
7743compile_break(char_u *arg, cctx_T *cctx)
7744{
7745 scope_T *scope = cctx->ctx_scope;
7746 endlabel_T **el;
7747
7748 for (;;)
7749 {
7750 if (scope == NULL)
7751 {
7752 emsg(_(e_break));
7753 return NULL;
7754 }
7755 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7756 break;
7757 scope = scope->se_outer;
7758 }
7759
7760 // Jump to the end of the FOR or WHILE loop.
7761 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007762 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007763 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007764 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007765 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7766 return FAIL;
7767
7768 return arg;
7769}
7770
7771/*
7772 * compile "{" start of block
7773 */
7774 static char_u *
7775compile_block(char_u *arg, cctx_T *cctx)
7776{
7777 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7778 return NULL;
7779 return skipwhite(arg + 1);
7780}
7781
7782/*
7783 * compile end of block: drop one scope
7784 */
7785 static void
7786compile_endblock(cctx_T *cctx)
7787{
7788 scope_T *scope = cctx->ctx_scope;
7789
7790 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007791 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007792 vim_free(scope);
7793}
7794
7795/*
7796 * compile "try"
7797 * Creates a new scope for the try-endtry, pointing to the first catch and
7798 * finally.
7799 * Creates another scope for the "try" block itself.
7800 * TRY instruction sets up exception handling at runtime.
7801 *
7802 * "try"
7803 * TRY -> catch1, -> finally push trystack entry
7804 * ... try block
7805 * "throw {exception}"
7806 * EVAL {exception}
7807 * THROW create exception
7808 * ... try block
7809 * " catch {expr}"
7810 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007811 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007812 * EVAL {expr}
7813 * MATCH
7814 * JUMP nomatch -> catch2
7815 * CATCH remove exception
7816 * ... catch block
7817 * " catch"
7818 * JUMP -> finally
7819 * catch2: CATCH remove exception
7820 * ... catch block
7821 * " finally"
7822 * finally:
7823 * ... finally block
7824 * " endtry"
7825 * ENDTRY pop trystack entry, may rethrow
7826 */
7827 static char_u *
7828compile_try(char_u *arg, cctx_T *cctx)
7829{
7830 garray_T *instr = &cctx->ctx_instr;
7831 scope_T *try_scope;
7832 scope_T *scope;
7833
Bram Moolenaarfa984412021-03-25 22:15:28 +01007834 if (misplaced_cmdmod(cctx))
7835 return NULL;
7836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007837 // scope that holds the jumps that go to catch/finally/endtry
7838 try_scope = new_scope(cctx, TRY_SCOPE);
7839 if (try_scope == NULL)
7840 return NULL;
7841
Bram Moolenaar69f70502021-01-01 16:10:46 +01007842 if (cctx->ctx_skip != SKIP_YES)
7843 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007844 isn_T *isn;
7845
7846 // "try_catch" is set when the first ":catch" is found or when no catch
7847 // is found and ":finally" is found.
7848 // "try_finally" is set when ":finally" is found
7849 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007850 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007851 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7852 return NULL;
7853 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7854 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007855 return NULL;
7856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857
7858 // scope for the try block itself
7859 scope = new_scope(cctx, BLOCK_SCOPE);
7860 if (scope == NULL)
7861 return NULL;
7862
7863 return arg;
7864}
7865
7866/*
7867 * compile "catch {expr}"
7868 */
7869 static char_u *
7870compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7871{
7872 scope_T *scope = cctx->ctx_scope;
7873 garray_T *instr = &cctx->ctx_instr;
7874 char_u *p;
7875 isn_T *isn;
7876
Bram Moolenaarfa984412021-03-25 22:15:28 +01007877 if (misplaced_cmdmod(cctx))
7878 return NULL;
7879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007880 // end block scope from :try or :catch
7881 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7882 compile_endblock(cctx);
7883 scope = cctx->ctx_scope;
7884
7885 // Error if not in a :try scope
7886 if (scope == NULL || scope->se_type != TRY_SCOPE)
7887 {
7888 emsg(_(e_catch));
7889 return NULL;
7890 }
7891
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007892 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007894 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007895 return NULL;
7896 }
7897
Bram Moolenaar69f70502021-01-01 16:10:46 +01007898 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007900#ifdef FEAT_PROFILE
7901 // the profile-start should be after the jump
7902 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7903 .isn_type == ISN_PROF_START)
7904 --instr->ga_len;
7905#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007906 // Jump from end of previous block to :finally or :endtry
7907 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7908 JUMP_ALWAYS, cctx) == FAIL)
7909 return NULL;
7910
7911 // End :try or :catch scope: set value in ISN_TRY instruction
7912 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007913 if (isn->isn_arg.try.try_ref->try_catch == 0)
7914 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007915 if (scope->se_u.se_try.ts_catch_label != 0)
7916 {
7917 // Previous catch without match jumps here
7918 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7919 isn->isn_arg.jump.jump_where = instr->ga_len;
7920 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007921#ifdef FEAT_PROFILE
7922 if (cctx->ctx_profiling)
7923 {
7924 // a "throw" that jumps here needs to be counted
7925 generate_instr(cctx, ISN_PROF_END);
7926 // the "catch" is also counted
7927 generate_instr(cctx, ISN_PROF_START);
7928 }
7929#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007930 }
7931
7932 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007933 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007934 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007935 scope->se_u.se_try.ts_caught_all = TRUE;
7936 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007937 }
7938 else
7939 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007940 char_u *end;
7941 char_u *pat;
7942 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007943 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007944 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007945
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007946 // Push v:exception, push {expr} and MATCH
7947 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7948
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007949 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007950 if (*end != *p)
7951 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007952 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007953 vim_free(tofree);
7954 return FAIL;
7955 }
7956 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007957 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007958 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007959 len = (int)(end - tofree);
7960 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007961 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007962 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007963 if (pat == NULL)
7964 return FAIL;
7965 if (generate_PUSHS(cctx, pat) == FAIL)
7966 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007968 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7969 return NULL;
7970
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007971 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007972 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7973 return NULL;
7974 }
7975
Bram Moolenaar69f70502021-01-01 16:10:46 +01007976 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007977 return NULL;
7978
7979 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7980 return NULL;
7981 return p;
7982}
7983
7984 static char_u *
7985compile_finally(char_u *arg, cctx_T *cctx)
7986{
7987 scope_T *scope = cctx->ctx_scope;
7988 garray_T *instr = &cctx->ctx_instr;
7989 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007990 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991
Bram Moolenaarfa984412021-03-25 22:15:28 +01007992 if (misplaced_cmdmod(cctx))
7993 return NULL;
7994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 // end block scope from :try or :catch
7996 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7997 compile_endblock(cctx);
7998 scope = cctx->ctx_scope;
7999
8000 // Error if not in a :try scope
8001 if (scope == NULL || scope->se_type != TRY_SCOPE)
8002 {
8003 emsg(_(e_finally));
8004 return NULL;
8005 }
8006
8007 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008008 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008009 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 {
8011 emsg(_(e_finally_dup));
8012 return NULL;
8013 }
8014
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008015 this_instr = instr->ga_len;
8016#ifdef FEAT_PROFILE
8017 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8018 .isn_type == ISN_PROF_START)
8019 // jump to the profile start of the "finally"
8020 --this_instr;
8021#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008023 // Fill in the "end" label in jumps at the end of the blocks.
8024 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8025 this_instr, cctx);
8026
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008027 // If there is no :catch then an exception jumps to :finally.
8028 if (isn->isn_arg.try.try_ref->try_catch == 0)
8029 isn->isn_arg.try.try_ref->try_catch = this_instr;
8030 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008031 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032 {
8033 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01008034 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008035 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02008036 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008038 if (generate_instr(cctx, ISN_FINALLY) == NULL)
8039 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008041 // TODO: set index in ts_finally_label jumps
8042
8043 return arg;
8044}
8045
8046 static char_u *
8047compile_endtry(char_u *arg, cctx_T *cctx)
8048{
8049 scope_T *scope = cctx->ctx_scope;
8050 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01008051 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052
Bram Moolenaarfa984412021-03-25 22:15:28 +01008053 if (misplaced_cmdmod(cctx))
8054 return NULL;
8055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008056 // end block scope from :catch or :finally
8057 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
8058 compile_endblock(cctx);
8059 scope = cctx->ctx_scope;
8060
8061 // Error if not in a :try scope
8062 if (scope == NULL || scope->se_type != TRY_SCOPE)
8063 {
8064 if (scope == NULL)
8065 emsg(_(e_no_endtry));
8066 else if (scope->se_type == WHILE_SCOPE)
8067 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01008068 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008069 emsg(_(e_endfor));
8070 else
8071 emsg(_(e_endif));
8072 return NULL;
8073 }
8074
Bram Moolenaarc150c092021-02-13 15:02:46 +01008075 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008076 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008078 if (try_isn->isn_arg.try.try_ref->try_catch == 0
8079 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01008080 {
8081 emsg(_(e_missing_catch_or_finally));
8082 return NULL;
8083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008085#ifdef FEAT_PROFILE
8086 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
8087 .isn_type == ISN_PROF_START)
8088 // move the profile start after "endtry" so that it's not counted when
8089 // the exception is rethrown.
8090 --instr->ga_len;
8091#endif
8092
Bram Moolenaar69f70502021-01-01 16:10:46 +01008093 // Fill in the "end" label in jumps at the end of the blocks, if not
8094 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008095 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
8096 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097
Bram Moolenaar69f70502021-01-01 16:10:46 +01008098 if (scope->se_u.se_try.ts_catch_label != 0)
8099 {
8100 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01008101 isn_T *isn = ((isn_T *)instr->ga_data)
8102 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01008103 isn->isn_arg.jump.jump_where = instr->ga_len;
8104 }
Bram Moolenaare8593122020-07-18 15:17:02 +02008105 }
8106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008107 compile_endblock(cctx);
8108
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008109 if (cctx->ctx_skip != SKIP_YES)
8110 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01008111 // End :catch or :finally scope: set instruction index in ISN_TRY
8112 // instruction
8113 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008114 if (cctx->ctx_skip != SKIP_YES
8115 && generate_instr(cctx, ISN_ENDTRY) == NULL)
8116 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01008117#ifdef FEAT_PROFILE
8118 if (cctx->ctx_profiling)
8119 generate_instr(cctx, ISN_PROF_START);
8120#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01008121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 return arg;
8123}
8124
8125/*
8126 * compile "throw {expr}"
8127 */
8128 static char_u *
8129compile_throw(char_u *arg, cctx_T *cctx UNUSED)
8130{
8131 char_u *p = skipwhite(arg);
8132
Bram Moolenaara5565e42020-05-09 15:44:01 +02008133 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01008135 if (cctx->ctx_skip == SKIP_YES)
8136 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 if (may_generate_2STRING(-1, cctx) == FAIL)
8138 return NULL;
8139 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
8140 return NULL;
8141
8142 return p;
8143}
8144
8145/*
8146 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008147 * compile "echomsg expr"
8148 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008149 * compile "execute expr"
8150 */
8151 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008152compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008153{
8154 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008155 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008156 int count = 0;
8157
8158 for (;;)
8159 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008160 if (ends_excmd2(prev, p))
8161 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008162 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008163 return NULL;
8164 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008165 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008166 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008167 }
8168
Bram Moolenaare4984292020-12-13 14:19:25 +01008169 if (count > 0)
8170 {
8171 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8172 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8173 else if (cmdidx == CMD_execute)
8174 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8175 else if (cmdidx == CMD_echomsg)
8176 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8177 else
8178 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008180 return p;
8181}
8182
8183/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008184 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008185 * instruction to compute it and return OK.
8186 * Otherwise return FAIL, the caller must deal with any range.
8187 */
8188 static int
8189compile_variable_range(exarg_T *eap, cctx_T *cctx)
8190{
8191 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8192 char_u *p = skipdigits(eap->cmd);
8193
8194 if (p == range_end)
8195 return FAIL;
8196 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8197}
8198
8199/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008200 * :put r
8201 * :put ={expr}
8202 */
8203 static char_u *
8204compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8205{
8206 char_u *line = arg;
8207 linenr_T lnum;
8208 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008209 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008210
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008211 eap->regname = *line;
8212
8213 if (eap->regname == '=')
8214 {
8215 char_u *p = line + 1;
8216
8217 if (compile_expr0(&p, cctx) == FAIL)
8218 return NULL;
8219 line = p;
8220 }
8221 else if (eap->regname != NUL)
8222 ++line;
8223
Bram Moolenaar08597872020-12-10 19:43:40 +01008224 if (compile_variable_range(eap, cctx) == OK)
8225 {
8226 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8227 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008228 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008229 {
8230 // Either no range or a number.
8231 // "errormsg" will not be set because the range is ADDR_LINES.
8232 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008233 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008234 return NULL;
8235 if (eap->addr_count == 0)
8236 lnum = -1;
8237 else
8238 lnum = eap->line2;
8239 if (above)
8240 --lnum;
8241 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008242
8243 generate_PUT(cctx, eap->regname, lnum);
8244 return line;
8245}
8246
8247/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008248 * A command that is not compiled, execute with legacy code.
8249 */
8250 static char_u *
8251compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8252{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008253 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008254 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008255 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008256
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008257 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008258 goto theend;
8259
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008260 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008261 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008262 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008263 int usefilter = FALSE;
8264
8265 has_expr = argt & (EX_XFILE | EX_EXPAND);
8266
8267 // If the command can be followed by a bar, find the bar and truncate
8268 // it, so that the following command can be compiled.
8269 // The '|' is overwritten with a NUL, it is put back below.
8270 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8271 && *eap->arg == '!')
8272 // :w !filter or :r !filter or :r! filter
8273 usefilter = TRUE;
8274 if ((argt & EX_TRLBAR) && !usefilter)
8275 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008276 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008277 separate_nextcmd(eap);
8278 if (eap->nextcmd != NULL)
8279 nextcmd = eap->nextcmd;
8280 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008281 else if (eap->cmdidx == CMD_wincmd)
8282 {
8283 p = eap->arg;
8284 if (*p != NUL)
8285 ++p;
8286 if (*p == 'g' || *p == Ctrl_G)
8287 ++p;
8288 p = skipwhite(p);
8289 if (*p == '|')
8290 {
8291 *p = NUL;
8292 nextcmd = p + 1;
8293 }
8294 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008295 }
8296
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008297 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8298 {
8299 // expand filename in "syntax include [@group] filename"
8300 has_expr = TRUE;
8301 eap->arg = skipwhite(eap->arg + 7);
8302 if (*eap->arg == '@')
8303 eap->arg = skiptowhite(eap->arg);
8304 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008305
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008306 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8307 && STRLEN(eap->arg) > 4)
8308 {
8309 int delim = *eap->arg;
8310
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008311 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008312 if (*p == delim)
8313 {
8314 eap->arg = p + 1;
8315 has_expr = TRUE;
8316 }
8317 }
8318
Bram Moolenaarecac5912021-01-05 19:23:28 +01008319 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8320 {
8321 // TODO: should only expand when appropriate for the command
8322 eap->arg = skiptowhite(eap->arg);
8323 has_expr = TRUE;
8324 }
8325
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008326 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008327 {
8328 int count = 0;
8329 char_u *start = skipwhite(line);
8330
8331 // :cmd xxx`=expr1`yyy`=expr2`zzz
8332 // PUSHS ":cmd xxx"
8333 // eval expr1
8334 // PUSHS "yyy"
8335 // eval expr2
8336 // PUSHS "zzz"
8337 // EXECCONCAT 5
8338 for (;;)
8339 {
8340 if (p > start)
8341 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008342 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008343 ++count;
8344 }
8345 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008346 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008347 return NULL;
8348 may_generate_2STRING(-1, cctx);
8349 ++count;
8350 p = skipwhite(p);
8351 if (*p != '`')
8352 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008353 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008354 return NULL;
8355 }
8356 start = p + 1;
8357
8358 p = (char_u *)strstr((char *)start, "`=");
8359 if (p == NULL)
8360 {
8361 if (*skipwhite(start) != NUL)
8362 {
8363 generate_PUSHS(cctx, vim_strsave(start));
8364 ++count;
8365 }
8366 break;
8367 }
8368 }
8369 generate_EXECCONCAT(cctx, count);
8370 }
8371 else
8372 generate_EXEC(cctx, line);
8373
8374theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008375 if (*nextcmd != NUL)
8376 {
8377 // the parser expects a pointer to the bar, put it back
8378 --nextcmd;
8379 *nextcmd = '|';
8380 }
8381
8382 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008383}
8384
8385/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008386 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008387 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008388 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008389 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008390add_def_function(ufunc_T *ufunc)
8391{
8392 dfunc_T *dfunc;
8393
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008394 if (def_functions.ga_len == 0)
8395 {
8396 // The first position is not used, so that a zero uf_dfunc_idx means it
8397 // wasn't set.
8398 if (ga_grow(&def_functions, 1) == FAIL)
8399 return FAIL;
8400 ++def_functions.ga_len;
8401 }
8402
Bram Moolenaar09689a02020-05-09 22:50:08 +02008403 // Add the function to "def_functions".
8404 if (ga_grow(&def_functions, 1) == FAIL)
8405 return FAIL;
8406 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8407 CLEAR_POINTER(dfunc);
8408 dfunc->df_idx = def_functions.ga_len;
8409 ufunc->uf_dfunc_idx = dfunc->df_idx;
8410 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008411 dfunc->df_name = vim_strsave(ufunc->uf_name);
8412 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008413 ++def_functions.ga_len;
8414 return OK;
8415}
8416
8417/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008418 * After ex_function() has collected all the function lines: parse and compile
8419 * the lines into instructions.
8420 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008421 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8422 * the return statement (used for lambda). When uf_ret_type is already set
8423 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008424 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008425 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008426 * This can be used recursively through compile_lambda(), which may reallocate
8427 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008428 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008430 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008431compile_def_function(
8432 ufunc_T *ufunc,
8433 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008434 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008435 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008436{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 char_u *line = NULL;
8438 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008440 cctx_T cctx;
8441 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008442 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02008443 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008444 int ret = FAIL;
8445 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008446 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008447 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008448 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008449#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008450 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008451#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008453 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008454 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008455 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008456 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008457 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8458 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008459 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008461 else
8462 {
8463 if (add_def_function(ufunc) == FAIL)
8464 return FAIL;
8465 new_def_function = TRUE;
8466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008467
Bram Moolenaar985116a2020-07-12 17:31:09 +02008468 ufunc->uf_def_status = UF_COMPILING;
8469
Bram Moolenaara80faa82020-04-12 19:37:17 +02008470 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008471
Bram Moolenaarf002a412021-01-24 13:34:18 +01008472#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008473 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008474#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008475 cctx.ctx_ufunc = ufunc;
8476 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008477 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008478 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8479 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8480 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8481 cctx.ctx_type_list = &ufunc->uf_type_list;
8482 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8483 instr = &cctx.ctx_instr;
8484
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008485 // Set the context to the function, it may be compiled when called from
8486 // another script. Set the script version to the most modern one.
8487 // The line number will be set in next_line_from_context().
8488 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008489 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8490
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008491 // Make sure error messages are OK.
8492 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8493 if (do_estack_push)
8494 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008495 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008496
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008497 if (ufunc->uf_def_args.ga_len > 0)
8498 {
8499 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008500 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008501 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008502 int i;
8503 char_u *arg;
8504 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008505 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008506
8507 // Produce instructions for the default values of optional arguments.
Bram Moolenaar12bce952021-03-11 20:04:04 +01008508 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008509 for (i = 0; i < count; ++i)
8510 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008511 garray_T *stack = &cctx.ctx_type_stack;
8512 type_T *val_type;
8513 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008514 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008515 int r;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008516 int jump_instr_idx = instr->ga_len;
8517 isn_T *isn;
8518
8519 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
8520 if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
8521 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008522
8523 // Make sure later arguments are not found.
8524 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008525
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008526 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008527 r = compile_expr0(&arg, &cctx);
8528
8529 ufunc->uf_args.ga_len = uf_args_len;
8530 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008531 goto erret;
8532
8533 // If no type specified use the type of the default value.
8534 // Otherwise check that the default value type matches the
8535 // specified type.
8536 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008537 where.wt_index = arg_idx + 1;
8538 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008539 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008540 {
8541 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008542 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008543 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008544 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008545 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008546 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008547
8548 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008549 goto erret;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02008550
8551 // set instruction index in JUMP_IF_ARG_SET to here
8552 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
8553 isn->isn_arg.jumparg.jump_where = instr->ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008554 }
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008555
8556 if (did_set_arg_type)
8557 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008558 }
8559
8560 /*
8561 * Loop over all the lines of the function and generate instructions.
8562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008563 for (;;)
8564 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008565 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008566 int starts_with_colon = FALSE;
8567 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008568 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008569
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008570 // Bail out on the first error to avoid a flood of errors and report
8571 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008572 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008573 goto erret;
8574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 if (line != NULL && *line == '|')
8576 // the line continues after a '|'
8577 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008578 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008579 && !(*line == '#' && (line == cctx.ctx_line_start
8580 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008581 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008582 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008583 goto erret;
8584 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008585 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8586 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008587 else
8588 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008589 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008590 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008591 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008592 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008593#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008594 if (cctx.ctx_skip != SKIP_YES)
8595 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008596#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008599 }
8600
Bram Moolenaara80faa82020-04-12 19:37:17 +02008601 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008602 ea.cmdlinep = &line;
8603 ea.cmd = skipwhite(line);
8604
Bram Moolenaarb2049902021-01-24 12:53:53 +01008605 if (*ea.cmd == '#')
8606 {
8607 // "#" starts a comment
8608 line = (char_u *)"";
8609 continue;
8610 }
8611
Bram Moolenaarf002a412021-01-24 13:34:18 +01008612#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008613 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8614 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008615 {
8616 may_generate_prof_end(&cctx, prof_lnum);
8617
8618 prof_lnum = cctx.ctx_lnum;
8619 generate_instr(&cctx, ISN_PROF_START);
8620 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008621#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008622
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008623 // Some things can be recognized by the first character.
8624 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008625 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008626 case '}':
8627 {
8628 // "}" ends a block scope
8629 scopetype_T stype = cctx.ctx_scope == NULL
8630 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008631
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008632 if (stype == BLOCK_SCOPE)
8633 {
8634 compile_endblock(&cctx);
8635 line = ea.cmd;
8636 }
8637 else
8638 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008639 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008640 goto erret;
8641 }
8642 if (line != NULL)
8643 line = skipwhite(ea.cmd + 1);
8644 continue;
8645 }
8646
8647 case '{':
8648 // "{" starts a block scope
8649 // "{'a': 1}->func() is something else
8650 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8651 {
8652 line = compile_block(ea.cmd, &cctx);
8653 continue;
8654 }
8655 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656 }
8657
8658 /*
8659 * COMMAND MODIFIERS
8660 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008661 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008662 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8663 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008664 {
8665 if (errormsg != NULL)
8666 goto erret;
8667 // empty line or comment
8668 line = (char_u *)"";
8669 continue;
8670 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008671 generate_cmdmods(&cctx, &local_cmdmod);
8672 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008674 // Check if there was a colon after the last command modifier or before
8675 // the current position.
8676 for (p = ea.cmd; p >= line; --p)
8677 {
8678 if (*p == ':')
8679 starts_with_colon = TRUE;
8680 if (p < ea.cmd && !VIM_ISWHITE(*p))
8681 break;
8682 }
8683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008684 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008685 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008686 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008687 {
8688 if (*ea.cmd == '(')
8689 // not for "call()"
8690 ea.cmd = p;
8691 else
8692 ea.cmd = skipwhite(ea.cmd);
8693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008695 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008696 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008697 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008698
Bram Moolenaar17126b12021-01-07 22:03:02 +01008699 // Check for assignment after command modifiers.
8700 assign = may_compile_assignment(&ea, &line, &cctx);
8701 if (assign == OK)
8702 goto nextline;
8703 if (assign == FAIL)
8704 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705 }
8706
8707 /*
8708 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008709 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008710 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008711 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008712 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008713 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008714 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008715 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008716 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008717 if (!starts_with_colon)
8718 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008719 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008720 goto erret;
8721 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008722 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008723 if (ends_excmd2(line, ea.cmd))
8724 {
8725 // A range without a command: jump to the line.
8726 // TODO: compile to a more efficient command, possibly
8727 // calling parse_cmd_address().
8728 ea.cmdidx = CMD_SIZE;
8729 line = compile_exec(line, &ea, &cctx);
8730 goto nextline;
8731 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008732 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008733 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008734 p = find_ex_command(&ea, NULL, starts_with_colon
8735 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008736
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008737 if (p == NULL)
8738 {
8739 if (cctx.ctx_skip != SKIP_YES)
8740 emsg(_(e_ambiguous_use_of_user_defined_command));
8741 goto erret;
8742 }
8743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008744 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8745 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008746 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008747 {
8748 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008749 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008750 }
8751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008752 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008753 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008755 // CMD_var cannot happen, compile_assignment() above would be
8756 // used. Most likely an assignment to a non-existing variable.
8757 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008758 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008760 }
8761
Bram Moolenaar3988f642020-08-27 22:43:03 +02008762 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008763 && ea.cmdidx != CMD_elseif
8764 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008765 && ea.cmdidx != CMD_endif
8766 && ea.cmdidx != CMD_endfor
8767 && ea.cmdidx != CMD_endwhile
8768 && ea.cmdidx != CMD_catch
8769 && ea.cmdidx != CMD_finally
8770 && ea.cmdidx != CMD_endtry)
8771 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008772 emsg(_(e_unreachable_code_after_return));
8773 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008774 }
8775
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008776 p = skipwhite(p);
8777 if (ea.cmdidx != CMD_SIZE
8778 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8779 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008780 if (ea.cmdidx >= 0)
8781 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008782 if ((ea.argt & EX_BANG) && *p == '!')
8783 {
8784 ea.forceit = TRUE;
8785 p = skipwhite(p + 1);
8786 }
8787 }
8788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 switch (ea.cmdidx)
8790 {
8791 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008792 ea.arg = p;
8793 line = compile_nested_function(&ea, &cctx);
8794 break;
8795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008797 // TODO: should we allow this, e.g. to declare a global
8798 // function?
8799 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008800 goto erret;
8801
8802 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008803 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008804 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 break;
8806
8807 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008808 emsg(_(e_cannot_use_let_in_vim9_script));
8809 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008810 case CMD_var:
8811 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008812 case CMD_const:
8813 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008814 if (line == p)
8815 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008816 break;
8817
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008818 case CMD_unlet:
8819 case CMD_unlockvar:
8820 case CMD_lockvar:
8821 line = compile_unletlock(p, &ea, &cctx);
8822 break;
8823
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824 case CMD_import:
8825 line = compile_import(p, &cctx);
8826 break;
8827
8828 case CMD_if:
8829 line = compile_if(p, &cctx);
8830 break;
8831 case CMD_elseif:
8832 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008833 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 break;
8835 case CMD_else:
8836 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008837 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 break;
8839 case CMD_endif:
8840 line = compile_endif(p, &cctx);
8841 break;
8842
8843 case CMD_while:
8844 line = compile_while(p, &cctx);
8845 break;
8846 case CMD_endwhile:
8847 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008848 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008849 break;
8850
8851 case CMD_for:
8852 line = compile_for(p, &cctx);
8853 break;
8854 case CMD_endfor:
8855 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008856 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008857 break;
8858 case CMD_continue:
8859 line = compile_continue(p, &cctx);
8860 break;
8861 case CMD_break:
8862 line = compile_break(p, &cctx);
8863 break;
8864
8865 case CMD_try:
8866 line = compile_try(p, &cctx);
8867 break;
8868 case CMD_catch:
8869 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008870 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008871 break;
8872 case CMD_finally:
8873 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008874 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008875 break;
8876 case CMD_endtry:
8877 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008878 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879 break;
8880 case CMD_throw:
8881 line = compile_throw(p, &cctx);
8882 break;
8883
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008884 case CMD_eval:
8885 if (compile_expr0(&p, &cctx) == FAIL)
8886 goto erret;
8887
Bram Moolenaar3988f642020-08-27 22:43:03 +02008888 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008889 generate_instr_drop(&cctx, ISN_DROP, 1);
8890
8891 line = skipwhite(p);
8892 break;
8893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008895 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008896 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008897 case CMD_echomsg:
8898 case CMD_echoerr:
8899 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008900 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008902 case CMD_put:
8903 ea.cmd = cmd;
8904 line = compile_put(p, &ea, &cctx);
8905 break;
8906
Bram Moolenaar3988f642020-08-27 22:43:03 +02008907 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008908
Bram Moolenaarae616492020-07-28 20:07:27 +02008909 case CMD_append:
8910 case CMD_change:
8911 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008912 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008913 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008914 case CMD_xit:
8915 not_in_vim9(&ea);
8916 goto erret;
8917
Bram Moolenaar002262f2020-07-08 17:47:57 +02008918 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008919 if (cctx.ctx_skip != SKIP_YES)
8920 {
8921 semsg(_(e_invalid_command_str), ea.cmd);
8922 goto erret;
8923 }
8924 // We don't check for a next command here.
8925 line = (char_u *)"";
8926 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008928 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008929 if (cctx.ctx_skip == SKIP_YES)
8930 {
8931 // We don't check for a next command here.
8932 line = (char_u *)"";
8933 }
8934 else
8935 {
8936 // Not recognized, execute with do_cmdline_cmd().
8937 ea.arg = p;
8938 line = compile_exec(line, &ea, &cctx);
8939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008940 break;
8941 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008942nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008943 if (line == NULL)
8944 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008945 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008946
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008947 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008948 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008950 if (cctx.ctx_type_stack.ga_len < 0)
8951 {
8952 iemsg("Type stack underflow");
8953 goto erret;
8954 }
8955 }
8956
8957 if (cctx.ctx_scope != NULL)
8958 {
8959 if (cctx.ctx_scope->se_type == IF_SCOPE)
8960 emsg(_(e_endif));
8961 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8962 emsg(_(e_endwhile));
8963 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8964 emsg(_(e_endfor));
8965 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008966 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 goto erret;
8968 }
8969
Bram Moolenaarefd88552020-06-18 20:50:10 +02008970 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008971 {
8972 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8973 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008974 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 goto erret;
8976 }
8977
8978 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008979 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008980 }
8981
Bram Moolenaar599410c2021-04-10 14:03:43 +02008982 // When compiled with ":silent!" and there was an error don't consider the
8983 // function compiled.
8984 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008985 {
8986 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8987 + ufunc->uf_dfunc_idx;
8988 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008989 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008990#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008991 if (cctx.ctx_profiling)
8992 {
8993 dfunc->df_instr_prof = instr->ga_data;
8994 dfunc->df_instr_prof_count = instr->ga_len;
8995 }
8996 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008997#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008998 {
8999 dfunc->df_instr = instr->ga_data;
9000 dfunc->df_instr_count = instr->ga_len;
9001 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009002 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009003 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009004 if (cctx.ctx_outer_used)
9005 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009006 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009008
9009 ret = OK;
9010
9011erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02009012 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009013 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009014 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02009015 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9016 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009017
9018 for (idx = 0; idx < instr->ga_len; ++idx)
9019 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009020 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009021 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009022
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009023 // If using the last entry in the table and it was added above, we
9024 // might as well remove it.
9025 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02009026 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009027 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01009028 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02009029 ufunc->uf_dfunc_idx = 0;
9030 }
Bram Moolenaar701cc6c2021-04-10 13:33:48 +02009031 ufunc->uf_def_status = UF_COMPILE_ERROR;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009032
Bram Moolenaar3cca2992020-04-02 22:57:36 +02009033 while (cctx.ctx_scope != NULL)
9034 drop_scope(&cctx);
9035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009036 if (errormsg != NULL)
9037 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01009038 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02009039 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009040 }
9041
9042 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02009043 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02009044 if (do_estack_push)
9045 estack_pop();
9046
Bram Moolenaar20431c92020-03-20 18:39:46 +01009047 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02009048 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009049 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02009050 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009051}
9052
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009053 void
9054set_function_type(ufunc_T *ufunc)
9055{
9056 int varargs = ufunc->uf_va_name != NULL;
9057 int argcount = ufunc->uf_args.ga_len;
9058
9059 // Create a type for the function, with the return type and any
9060 // argument types.
9061 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
9062 // The type is included in "tt_args".
9063 if (argcount > 0 || varargs)
9064 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01009065 if (ufunc->uf_type_list.ga_itemsize == 0)
9066 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009067 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
9068 argcount, &ufunc->uf_type_list);
9069 // Add argument types to the function type.
9070 if (func_type_add_arg_types(ufunc->uf_func_type,
9071 argcount + varargs,
9072 &ufunc->uf_type_list) == FAIL)
9073 return;
9074 ufunc->uf_func_type->tt_argcount = argcount + varargs;
9075 ufunc->uf_func_type->tt_min_argcount =
9076 argcount - ufunc->uf_def_args.ga_len;
9077 if (ufunc->uf_arg_types == NULL)
9078 {
9079 int i;
9080
9081 // lambda does not have argument types.
9082 for (i = 0; i < argcount; ++i)
9083 ufunc->uf_func_type->tt_args[i] = &t_any;
9084 }
9085 else
9086 mch_memmove(ufunc->uf_func_type->tt_args,
9087 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
9088 if (varargs)
9089 {
9090 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02009091 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02009092 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
9093 }
9094 }
9095 else
9096 // No arguments, can use a predefined type.
9097 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
9098 argcount, &ufunc->uf_type_list);
9099}
9100
9101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009102/*
9103 * Delete an instruction, free what it contains.
9104 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01009105 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009106delete_instr(isn_T *isn)
9107{
9108 switch (isn->isn_type)
9109 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009110 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009111 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009112 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009113 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009114 case ISN_LOADENV:
9115 case ISN_LOADG:
9116 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009117 case ISN_LOADT:
9118 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009119 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009120 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009121 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01009122 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01009123 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009124 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009125 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009126 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02009127 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01009128 case ISN_STOREW:
9129 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009130 vim_free(isn->isn_arg.string);
9131 break;
9132
9133 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01009134 case ISN_STORES:
9135 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009136 break;
9137
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009138 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02009139 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02009140 vim_free(isn->isn_arg.unlet.ul_name);
9141 break;
9142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009143 case ISN_STOREOPT:
9144 vim_free(isn->isn_arg.storeopt.so_name);
9145 break;
9146
9147 case ISN_PUSHBLOB: // push blob isn_arg.blob
9148 blob_unref(isn->isn_arg.blob);
9149 break;
9150
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009151 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009152#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009153 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009154#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009155 break;
9156
9157 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009158#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009159 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009160#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009161 break;
9162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009163 case ISN_UCALL:
9164 vim_free(isn->isn_arg.ufunc.cuf_name);
9165 break;
9166
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009167 case ISN_FUNCREF:
9168 {
9169 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9170 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009171 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009172
Bram Moolenaar077a4232020-12-22 18:33:27 +01009173 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9174 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009175 }
9176 break;
9177
9178 case ISN_DCALL:
9179 {
9180 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9181 + isn->isn_arg.dfunc.cdf_idx;
9182
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009183 if (dfunc->df_ufunc != NULL
9184 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009185 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009186 }
9187 break;
9188
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009189 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009190 {
9191 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9192 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9193
9194 if (ufunc != NULL)
9195 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009196 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009197 func_ptr_unref(ufunc);
9198 }
9199
9200 vim_free(lambda);
9201 vim_free(isn->isn_arg.newfunc.nf_global);
9202 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009203 break;
9204
Bram Moolenaar5e654232020-09-16 15:22:00 +02009205 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009206 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009207 free_type(isn->isn_arg.type.ct_type);
9208 break;
9209
Bram Moolenaar02194d22020-10-24 23:08:38 +02009210 case ISN_CMDMOD:
9211 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9212 ->cmod_filter_regmatch.regprog);
9213 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9214 break;
9215
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009216 case ISN_LOADSCRIPT:
9217 case ISN_STORESCRIPT:
9218 vim_free(isn->isn_arg.script.scriptref);
9219 break;
9220
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009221 case ISN_TRY:
9222 vim_free(isn->isn_arg.try.try_ref);
9223 break;
9224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009225 case ISN_2BOOL:
9226 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009227 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009228 case ISN_ADDBLOB:
9229 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009230 case ISN_ANYINDEX:
9231 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009232 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009233 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009234 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009235 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009236 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009237 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009238 case ISN_COMPAREANY:
9239 case ISN_COMPAREBLOB:
9240 case ISN_COMPAREBOOL:
9241 case ISN_COMPAREDICT:
9242 case ISN_COMPAREFLOAT:
9243 case ISN_COMPAREFUNC:
9244 case ISN_COMPARELIST:
9245 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009246 case ISN_COMPARESPECIAL:
9247 case ISN_COMPARESTRING:
9248 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009249 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009250 case ISN_DROP:
9251 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009252 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009253 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009254 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009255 case ISN_EXECCONCAT:
9256 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009257 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009258 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009259 case ISN_GETITEM:
9260 case ISN_JUMP:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02009261 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009262 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009263 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009264 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009265 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009266 case ISN_LOADBDICT:
9267 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009268 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009269 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009270 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009271 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009272 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009273 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009274 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009275 case ISN_NEGATENR:
9276 case ISN_NEWDICT:
9277 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009278 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009279 case ISN_OPFLOAT:
9280 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009281 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009282 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009283 case ISN_PROF_END:
9284 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009285 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009286 case ISN_PUSHF:
9287 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009288 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009289 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009290 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009291 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009292 case ISN_SHUFFLE:
9293 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009294 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009295 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009296 case ISN_STORENR:
9297 case ISN_STOREOUTER:
9298 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009299 case ISN_STOREV:
9300 case ISN_STRINDEX:
9301 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009302 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009303 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009304 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009305 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009306 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009307 // nothing allocated
9308 break;
9309 }
9310}
9311
9312/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009313 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009314 */
9315 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009316delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009317{
9318 int idx;
9319
9320 ga_clear(&dfunc->df_def_args_isn);
9321
9322 if (dfunc->df_instr != NULL)
9323 {
9324 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9325 delete_instr(dfunc->df_instr + idx);
9326 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009327 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009328 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009329#ifdef FEAT_PROFILE
9330 if (dfunc->df_instr_prof != NULL)
9331 {
9332 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9333 delete_instr(dfunc->df_instr_prof + idx);
9334 VIM_CLEAR(dfunc->df_instr_prof);
9335 dfunc->df_instr_prof = NULL;
9336 }
9337#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009338
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009339 if (mark_deleted)
9340 dfunc->df_deleted = TRUE;
9341 if (dfunc->df_ufunc != NULL)
9342 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009343}
9344
9345/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009346 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009347 * function, unless another user function still uses it.
9348 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009349 */
9350 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009351unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009352{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009353 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009354 {
9355 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9356 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009357
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009358 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009359 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009360 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009361 ufunc->uf_dfunc_idx = 0;
9362 if (dfunc->df_ufunc == ufunc)
9363 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009364 }
9365}
9366
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009367/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009368 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009369 */
9370 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009371link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009372{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009373 if (ufunc->uf_dfunc_idx > 0)
9374 {
9375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9376 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009377
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009378 ++dfunc->df_refcount;
9379 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009380}
9381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009382#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009383/*
9384 * Free all functions defined with ":def".
9385 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009386 void
9387free_def_functions(void)
9388{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009389 int idx;
9390
9391 for (idx = 0; idx < def_functions.ga_len; ++idx)
9392 {
9393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9394
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009395 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009396 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009397 }
9398
9399 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009400}
9401#endif
9402
9403
9404#endif // FEAT_EVAL