blob: 26d346f4344323ec854c77ea3d45dbcfd5754314 [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 Moolenaar53900992020-08-22 19:02:02 +0200335 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
336 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 * Returns OK or FAIL.
339 */
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100340 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200341script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200345 if (current_sctx.sc_sid <= 0)
346 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200347 is_vim9_script = script_is_vim9();
348 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200349 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200350 if (is_vim9_script)
351 {
352 // Check script variables that were visible where the function was
353 // defined.
354 if (find_script_var(name, len, cctx) != NULL)
355 return OK;
356 }
357 else
358 {
359 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
360 dictitem_T *di;
361 int cc;
362
363 // Check script variables that are currently visible
364 cc = name[len];
365 name[len] = NUL;
366 di = find_var_in_ht(ht, 0, name, TRUE);
367 name[len] = cc;
368 if (di != NULL)
369 return OK;
370 }
371
372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373}
374
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100376 * Return TRUE if "name" is a local variable, argument, script variable or
377 * imported.
378 */
379 static int
380variable_exists(char_u *name, size_t len, cctx_T *cctx)
381{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100382 return (cctx != NULL
383 && (lookup_local(name, len, NULL, cctx) == OK
384 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaare0890d62021-02-17 14:52:14 +0100385 || script_var_exists(name, len, FALSE, cctx) == OK
386 || find_imported(name, len, cctx) != NULL;
387}
388
389/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100390 * Return TRUE if "name" is a local variable, argument, script variable,
391 * imported or function.
392 */
393 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100394item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100395{
396 int is_global;
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100397 char_u *p;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100398
399 if (variable_exists(name, len, cctx))
400 return TRUE;
401
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100402 // This is similar to what is in lookup_scriptitem():
403 // Find a function, so that a following "->" works.
404 // Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
405 // a function call.
406 p = skipwhite(name + len);
407
408 if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
409 {
410 // Do not check for an internal function, since it might also be a
411 // valid command, such as ":split" versuse "split()".
412 // Skip "g:" before a function name.
413 is_global = (name[0] == 'g' && name[1] == ':');
414 return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
415 }
416 return FALSE;
Bram Moolenaar6914e872021-03-06 21:01:09 +0100417}
418
419/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100420 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200421 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200422 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100423 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100424 * Return FAIL and give an error if it defined.
425 */
426 int
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100427check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100428{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200429 int c = p[len];
430 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200431
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100432 if (script_var_exists(p, len, FALSE, cctx) == OK)
433 {
434 if (is_arg)
435 semsg(_(e_argument_already_declared_in_script_str), p);
436 else
437 semsg(_(e_variable_already_declared_in_script_str), p);
438 return FAIL;
439 }
440
Bram Moolenaarad486a02020-08-01 23:22:18 +0200441 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100442 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100443 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200444 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200445 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200446 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100447 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200448 // A local or script-local function can shadow a global function.
449 if (ufunc == NULL || !func_is_global(ufunc)
450 || (p[0] == 'g' && p[1] == ':'))
451 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100452 if (is_arg)
453 semsg(_(e_argument_name_shadows_existing_variable_str), p);
454 else
455 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200456 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200457 return FAIL;
458 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100459 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200460 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100461 return OK;
462}
463
Bram Moolenaar65b95452020-07-19 14:03:09 +0200464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465/////////////////////////////////////////////////////////////////////
466// Following generate_ functions expect the caller to call ga_grow().
467
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200468#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
469#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471/*
472 * Generate an instruction without arguments.
473 * Returns a pointer to the new instruction, NULL if failed.
474 */
475 static isn_T *
476generate_instr(cctx_T *cctx, isntype_T isn_type)
477{
478 garray_T *instr = &cctx->ctx_instr;
479 isn_T *isn;
480
Bram Moolenaar080457c2020-03-03 21:53:32 +0100481 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 if (ga_grow(instr, 1) == FAIL)
483 return NULL;
484 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
485 isn->isn_type = isn_type;
486 isn->isn_lnum = cctx->ctx_lnum + 1;
487 ++instr->ga_len;
488
489 return isn;
490}
491
492/*
493 * Generate an instruction without arguments.
494 * "drop" will be removed from the stack.
495 * Returns a pointer to the new instruction, NULL if failed.
496 */
497 static isn_T *
498generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
499{
500 garray_T *stack = &cctx->ctx_type_stack;
501
Bram Moolenaar080457c2020-03-03 21:53:32 +0100502 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 stack->ga_len -= drop;
504 return generate_instr(cctx, isn_type);
505}
506
507/*
508 * Generate instruction "isn_type" and put "type" on the type stack.
509 */
510 static isn_T *
511generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
512{
513 isn_T *isn;
514 garray_T *stack = &cctx->ctx_type_stack;
515
516 if ((isn = generate_instr(cctx, isn_type)) == NULL)
517 return NULL;
518
519 if (ga_grow(stack, 1) == FAIL)
520 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200521 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 ++stack->ga_len;
523
524 return isn;
525}
526
527/*
528 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200529 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 */
531 static int
532may_generate_2STRING(int offset, cctx_T *cctx)
533{
534 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200535 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100537 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100538
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100539 RETURN_OK_IF_SKIP(cctx);
540 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200541 switch ((*type)->tt_type)
542 {
543 // nothing to be done
544 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200546 // conversion possible
547 case VAR_SPECIAL:
548 case VAR_BOOL:
549 case VAR_NUMBER:
550 case VAR_FLOAT:
551 break;
552
553 // conversion possible (with runtime check)
554 case VAR_ANY:
555 case VAR_UNKNOWN:
556 isntype = ISN_2STRING_ANY;
557 break;
558
559 // conversion not possible
560 case VAR_VOID:
561 case VAR_BLOB:
562 case VAR_FUNC:
563 case VAR_PARTIAL:
564 case VAR_LIST:
565 case VAR_DICT:
566 case VAR_JOB:
567 case VAR_CHANNEL:
568 to_string_error((*type)->tt_type);
569 return FAIL;
570 }
571
572 *type = &t_string;
573 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 return FAIL;
575 isn->isn_arg.number = offset;
576
577 return OK;
578}
579
580 static int
581check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
582{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200583 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200585 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 {
587 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200588 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200590 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 return FAIL;
592 }
593 return OK;
594}
595
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200596 static int
597generate_add_instr(
598 cctx_T *cctx,
599 vartype_T vartype,
600 type_T *type1,
601 type_T *type2)
602{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100603 garray_T *stack = &cctx->ctx_type_stack;
604 isn_T *isn = generate_instr_drop(cctx,
605 vartype == VAR_NUMBER ? ISN_OPNR
606 : vartype == VAR_LIST ? ISN_ADDLIST
607 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100609 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100611 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612
613 if (vartype != VAR_LIST && vartype != VAR_BLOB
614 && type1->tt_type != VAR_ANY
615 && type2->tt_type != VAR_ANY
616 && check_number_or_float(
617 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
618 return FAIL;
619
620 if (isn != NULL)
621 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100622
623 // When concatenating two lists with different member types the member type
624 // becomes "any".
625 if (vartype == VAR_LIST
626 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
627 && type1->tt_member != type2->tt_member)
628 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
629
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200630 return isn == NULL ? FAIL : OK;
631}
632
633/*
634 * Get the type to use for an instruction for an operation on "type1" and
635 * "type2". If they are matching use a type-specific instruction. Otherwise
636 * fall back to runtime type checking.
637 */
638 static vartype_T
639operator_type(type_T *type1, type_T *type2)
640{
641 if (type1->tt_type == type2->tt_type
642 && (type1->tt_type == VAR_NUMBER
643 || type1->tt_type == VAR_LIST
644#ifdef FEAT_FLOAT
645 || type1->tt_type == VAR_FLOAT
646#endif
647 || type1->tt_type == VAR_BLOB))
648 return type1->tt_type;
649 return VAR_ANY;
650}
651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652/*
653 * Generate an instruction with two arguments. The instruction depends on the
654 * type of the arguments.
655 */
656 static int
657generate_two_op(cctx_T *cctx, char_u *op)
658{
659 garray_T *stack = &cctx->ctx_type_stack;
660 type_T *type1;
661 type_T *type2;
662 vartype_T vartype;
663 isn_T *isn;
664
Bram Moolenaar080457c2020-03-03 21:53:32 +0100665 RETURN_OK_IF_SKIP(cctx);
666
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200667 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
669 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200670 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
672 switch (*op)
673 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200674 case '+':
675 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 break;
678
679 case '-':
680 case '*':
681 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
682 op) == FAIL)
683 return FAIL;
684 if (vartype == VAR_NUMBER)
685 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
686#ifdef FEAT_FLOAT
687 else if (vartype == VAR_FLOAT)
688 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
689#endif
690 else
691 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
692 if (isn != NULL)
693 isn->isn_arg.op.op_type = *op == '*'
694 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
695 break;
696
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200699 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 && type2->tt_type != VAR_NUMBER))
701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200702 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 return FAIL;
704 }
705 isn = generate_instr_drop(cctx,
706 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
707 if (isn != NULL)
708 isn->isn_arg.op.op_type = EXPR_REM;
709 break;
710 }
711
712 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200713 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 {
715 type_T *type = &t_any;
716
717#ifdef FEAT_FLOAT
718 // float+number and number+float results in float
719 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
720 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
721 type = &t_float;
722#endif
723 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
724 }
725
726 return OK;
727}
728
729/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 * Get the instruction to use for comparing "type1" with "type2"
731 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200733 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100734get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735{
736 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737
Bram Moolenaar4c683752020-04-05 21:38:23 +0200738 if (type1 == VAR_UNKNOWN)
739 type1 = VAR_ANY;
740 if (type2 == VAR_UNKNOWN)
741 type2 = VAR_ANY;
742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 if (type1 == type2)
744 {
745 switch (type1)
746 {
747 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
748 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
749 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
750 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
751 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
752 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
753 case VAR_LIST: isntype = ISN_COMPARELIST; break;
754 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
755 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 default: isntype = ISN_COMPAREANY; break;
757 }
758 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200759 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100761 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 isntype = ISN_COMPAREANY;
763
Bram Moolenaar657137c2021-01-09 15:45:23 +0100764 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 && (isntype == ISN_COMPAREBOOL
766 || isntype == ISN_COMPARESPECIAL
767 || isntype == ISN_COMPARENR
768 || isntype == ISN_COMPAREFLOAT))
769 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200770 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100771 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200772 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 }
774 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100775 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
777 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100778 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
779 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 && (type1 == VAR_BLOB || type2 == VAR_BLOB
781 || type1 == VAR_LIST || type2 == VAR_LIST))))
782 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200783 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200785 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200787 return isntype;
788}
789
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200790 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100791check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200792{
793 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
794 return FAIL;
795 return OK;
796}
797
Bram Moolenaara5565e42020-05-09 15:44:01 +0200798/*
799 * Generate an ISN_COMPARE* instruction with a boolean result.
800 */
801 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100802generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200803{
804 isntype_T isntype;
805 isn_T *isn;
806 garray_T *stack = &cctx->ctx_type_stack;
807 vartype_T type1;
808 vartype_T type2;
809
810 RETURN_OK_IF_SKIP(cctx);
811
812 // Get the known type of the two items on the stack. If they are matching
813 // use a type-specific instruction. Otherwise fall back to runtime type
814 // checking.
815 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
816 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100817 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200818 if (isntype == ISN_DROP)
819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820
821 if ((isn = generate_instr(cctx, isntype)) == NULL)
822 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100823 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 isn->isn_arg.op.op_ic = ic;
825
826 // takes two arguments, puts one bool back
827 if (stack->ga_len >= 2)
828 {
829 --stack->ga_len;
830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
831 }
832
833 return OK;
834}
835
836/*
837 * Generate an ISN_2BOOL instruction.
838 */
839 static int
840generate_2BOOL(cctx_T *cctx, int invert)
841{
842 isn_T *isn;
843 garray_T *stack = &cctx->ctx_type_stack;
844
Bram Moolenaar080457c2020-03-03 21:53:32 +0100845 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
847 return FAIL;
848 isn->isn_arg.number = invert;
849
850 // type becomes bool
851 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
852
853 return OK;
854}
855
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200856/*
857 * Generate an ISN_COND2BOOL instruction.
858 */
859 static int
860generate_COND2BOOL(cctx_T *cctx)
861{
862 isn_T *isn;
863 garray_T *stack = &cctx->ctx_type_stack;
864
865 RETURN_OK_IF_SKIP(cctx);
866 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
867 return FAIL;
868
869 // type becomes bool
870 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
871
872 return OK;
873}
874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200876generate_TYPECHECK(
877 cctx_T *cctx,
878 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100879 int offset,
880 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881{
882 isn_T *isn;
883 garray_T *stack = &cctx->ctx_type_stack;
884
Bram Moolenaar080457c2020-03-03 21:53:32 +0100885 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
887 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100889 isn->isn_arg.type.ct_off = (int8_T)offset;
890 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaar5e654232020-09-16 15:22:00 +0200892 // type becomes expected
893 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 return OK;
896}
897
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100898 static int
899generate_SETTYPE(
900 cctx_T *cctx,
901 type_T *expected)
902{
903 isn_T *isn;
904
905 RETURN_OK_IF_SKIP(cctx);
906 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
907 return FAIL;
908 isn->isn_arg.type.ct_type = alloc_type(expected);
909 return OK;
910}
911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200913 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
914 * used. Return FALSE if the types will never match.
915 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100916 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200917use_typecheck(type_T *actual, type_T *expected)
918{
919 if (actual->tt_type == VAR_ANY
920 || actual->tt_type == VAR_UNKNOWN
921 || (actual->tt_type == VAR_FUNC
922 && (expected->tt_type == VAR_FUNC
923 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100924 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
925 && ((actual->tt_member == &t_void)
926 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200927 return TRUE;
928 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
929 && actual->tt_type == expected->tt_type)
930 // This takes care of a nested list or dict.
931 return use_typecheck(actual->tt_member, expected->tt_member);
932 return FALSE;
933}
934
935/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200936 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200937 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200938 * - "actual" is a type that can be "expected" type: add a runtime check; or
939 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200940 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
941 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200942 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100943 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200944need_type(
945 type_T *actual,
946 type_T *expected,
947 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100948 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200949 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200950 int silent,
951 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200952{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100953 where_T where;
954
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200955 if (expected == &t_bool && actual != &t_bool
956 && (actual->tt_flags & TTFLAG_BOOL_OK))
957 {
958 // Using "0", "1" or the result of an expression with "&&" or "||" as a
959 // boolean is OK but requires a conversion.
960 generate_2BOOL(cctx, FALSE);
961 return OK;
962 }
963
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100964 where.wt_index = arg_idx;
965 where.wt_variable = FALSE;
966 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200967 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200968
969 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200970 // If it's a constant a runtime check makes no sense.
971 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200972 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100973 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200974 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200975 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200976
977 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100978 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200979 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200980}
981
982/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100983 * Check that the top of the type stack has a type that can be used as a
984 * condition. Give an error and return FAIL if not.
985 */
986 static int
987bool_on_stack(cctx_T *cctx)
988{
989 garray_T *stack = &cctx->ctx_type_stack;
990 type_T *type;
991
992 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
993 if (type == &t_bool)
994 return OK;
995
996 if (type == &t_any || type == &t_number)
997 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
998 // This requires a runtime type check.
999 return generate_COND2BOOL(cctx);
1000
Bram Moolenaar351ead02021-01-16 16:07:01 +01001001 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +01001002}
1003
1004/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 * Generate an ISN_PUSHNR instruction.
1006 */
1007 static int
1008generate_PUSHNR(cctx_T *cctx, varnumber_T number)
1009{
1010 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001011 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012
Bram Moolenaar080457c2020-03-03 21:53:32 +01001013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
1015 return FAIL;
1016 isn->isn_arg.number = number;
1017
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001018 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +02001019 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +01001020 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_PUSHBOOL instruction.
1026 */
1027 static int
1028generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
1034 return FAIL;
1035 isn->isn_arg.number = number;
1036
1037 return OK;
1038}
1039
1040/*
1041 * Generate an ISN_PUSHSPEC instruction.
1042 */
1043 static int
1044generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1050 return FAIL;
1051 isn->isn_arg.number = number;
1052
1053 return OK;
1054}
1055
1056#ifdef FEAT_FLOAT
1057/*
1058 * Generate an ISN_PUSHF instruction.
1059 */
1060 static int
1061generate_PUSHF(cctx_T *cctx, float_T fnumber)
1062{
1063 isn_T *isn;
1064
Bram Moolenaar080457c2020-03-03 21:53:32 +01001065 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1067 return FAIL;
1068 isn->isn_arg.fnumber = fnumber;
1069
1070 return OK;
1071}
1072#endif
1073
1074/*
1075 * Generate an ISN_PUSHS instruction.
1076 * Consumes "str".
1077 */
1078 static int
1079generate_PUSHS(cctx_T *cctx, char_u *str)
1080{
1081 isn_T *isn;
1082
Bram Moolenaar508b5612021-01-02 18:17:26 +01001083 if (cctx->ctx_skip == SKIP_YES)
1084 {
1085 vim_free(str);
1086 return OK;
1087 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.string = str;
1091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001096 * Generate an ISN_PUSHCHANNEL instruction.
1097 * Consumes "channel".
1098 */
1099 static int
1100generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1101{
1102 isn_T *isn;
1103
Bram Moolenaar080457c2020-03-03 21:53:32 +01001104 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001105 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1106 return FAIL;
1107 isn->isn_arg.channel = channel;
1108
1109 return OK;
1110}
1111
1112/*
1113 * Generate an ISN_PUSHJOB instruction.
1114 * Consumes "job".
1115 */
1116 static int
1117generate_PUSHJOB(cctx_T *cctx, job_T *job)
1118{
1119 isn_T *isn;
1120
Bram Moolenaar080457c2020-03-03 21:53:32 +01001121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001122 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001123 return FAIL;
1124 isn->isn_arg.job = job;
1125
1126 return OK;
1127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Generate an ISN_PUSHBLOB instruction.
1131 * Consumes "blob".
1132 */
1133 static int
1134generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1135{
1136 isn_T *isn;
1137
Bram Moolenaar080457c2020-03-03 21:53:32 +01001138 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001139 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1140 return FAIL;
1141 isn->isn_arg.blob = blob;
1142
1143 return OK;
1144}
1145
1146/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001147 * Generate an ISN_PUSHFUNC instruction with name "name".
1148 * Consumes "name".
1149 */
1150 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001151generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001152{
1153 isn_T *isn;
1154
Bram Moolenaar080457c2020-03-03 21:53:32 +01001155 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001156 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001157 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001158 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001159
1160 return OK;
1161}
1162
1163/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001164 * Generate an ISN_GETITEM instruction with "index".
1165 */
1166 static int
1167generate_GETITEM(cctx_T *cctx, int index)
1168{
1169 isn_T *isn;
1170 garray_T *stack = &cctx->ctx_type_stack;
1171 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1172 type_T *item_type = &t_any;
1173
1174 RETURN_OK_IF_SKIP(cctx);
1175
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001176 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001177 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001178 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001179 emsg(_(e_listreq));
1180 return FAIL;
1181 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001182 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001183 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1184 return FAIL;
1185 isn->isn_arg.number = index;
1186
1187 // add the item type to the type stack
1188 if (ga_grow(stack, 1) == FAIL)
1189 return FAIL;
1190 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1191 ++stack->ga_len;
1192 return OK;
1193}
1194
1195/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001196 * Generate an ISN_SLICE instruction with "count".
1197 */
1198 static int
1199generate_SLICE(cctx_T *cctx, int count)
1200{
1201 isn_T *isn;
1202
1203 RETURN_OK_IF_SKIP(cctx);
1204 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1205 return FAIL;
1206 isn->isn_arg.number = count;
1207 return OK;
1208}
1209
1210/*
1211 * Generate an ISN_CHECKLEN instruction with "min_len".
1212 */
1213 static int
1214generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1215{
1216 isn_T *isn;
1217
1218 RETURN_OK_IF_SKIP(cctx);
1219
1220 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1221 return FAIL;
1222 isn->isn_arg.checklen.cl_min_len = min_len;
1223 isn->isn_arg.checklen.cl_more_OK = more_OK;
1224
1225 return OK;
1226}
1227
1228/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 * Generate an ISN_STORE instruction.
1230 */
1231 static int
1232generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1233{
1234 isn_T *isn;
1235
Bram Moolenaar080457c2020-03-03 21:53:32 +01001236 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1238 return FAIL;
1239 if (name != NULL)
1240 isn->isn_arg.string = vim_strsave(name);
1241 else
1242 isn->isn_arg.number = idx;
1243
1244 return OK;
1245}
1246
1247/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001248 * Generate an ISN_STOREOUTER instruction.
1249 */
1250 static int
1251generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1252{
1253 isn_T *isn;
1254
1255 RETURN_OK_IF_SKIP(cctx);
1256 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.outer.outer_idx = idx;
1259 isn->isn_arg.outer.outer_depth = level;
1260
1261 return OK;
1262}
1263
1264/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1266 */
1267 static int
1268generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1269{
1270 isn_T *isn;
1271
Bram Moolenaar080457c2020-03-03 21:53:32 +01001272 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1274 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001275 isn->isn_arg.storenr.stnr_idx = idx;
1276 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
1278 return OK;
1279}
1280
1281/*
1282 * Generate an ISN_STOREOPT instruction
1283 */
1284 static int
1285generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1286{
1287 isn_T *isn;
1288
Bram Moolenaar080457c2020-03-03 21:53:32 +01001289 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001290 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
1292 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1293 isn->isn_arg.storeopt.so_flags = opt_flags;
1294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOAD or similar instruction.
1300 */
1301 static int
1302generate_LOAD(
1303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int idx,
1306 char_u *name,
1307 type_T *type)
1308{
1309 isn_T *isn;
1310
Bram Moolenaar080457c2020-03-03 21:53:32 +01001311 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1313 return FAIL;
1314 if (name != NULL)
1315 isn->isn_arg.string = vim_strsave(name);
1316 else
1317 isn->isn_arg.number = idx;
1318
1319 return OK;
1320}
1321
1322/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001323 * Generate an ISN_LOADOUTER instruction
1324 */
1325 static int
1326generate_LOADOUTER(
1327 cctx_T *cctx,
1328 int idx,
1329 int nesting,
1330 type_T *type)
1331{
1332 isn_T *isn;
1333
1334 RETURN_OK_IF_SKIP(cctx);
1335 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1336 return FAIL;
1337 isn->isn_arg.outer.outer_idx = idx;
1338 isn->isn_arg.outer.outer_depth = nesting;
1339
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001344 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 */
1346 static int
1347generate_LOADV(
1348 cctx_T *cctx,
1349 char_u *name,
1350 int error)
1351{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001352 int di_flags;
1353 int vidx = find_vim_var(name, &di_flags);
1354 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 if (vidx < 0)
1358 {
1359 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001360 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361 return FAIL;
1362 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001365 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001366}
1367
1368/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001369 * Generate an ISN_UNLET instruction.
1370 */
1371 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001372generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373{
1374 isn_T *isn;
1375
1376 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001377 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001378 return FAIL;
1379 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1380 isn->isn_arg.unlet.ul_forceit = forceit;
1381
1382 return OK;
1383}
1384
1385/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001386 * Generate an ISN_LOCKCONST instruction.
1387 */
1388 static int
1389generate_LOCKCONST(cctx_T *cctx)
1390{
1391 isn_T *isn;
1392
1393 RETURN_OK_IF_SKIP(cctx);
1394 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1395 return FAIL;
1396 return OK;
1397}
1398
1399/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Generate an ISN_LOADS instruction.
1401 */
1402 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001403generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001407 int sid,
1408 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409{
1410 isn_T *isn;
1411
Bram Moolenaar080457c2020-03-03 21:53:32 +01001412 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001413 if (isn_type == ISN_LOADS)
1414 isn = generate_instr_type(cctx, isn_type, type);
1415 else
1416 isn = generate_instr_drop(cctx, isn_type, 1);
1417 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001419 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1420 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421
1422 return OK;
1423}
1424
1425/*
1426 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1427 */
1428 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001429generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 cctx_T *cctx,
1431 isntype_T isn_type,
1432 int sid,
1433 int idx,
1434 type_T *type)
1435{
1436 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001437 scriptref_T *sref;
1438 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439
Bram Moolenaar080457c2020-03-03 21:53:32 +01001440 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 if (isn_type == ISN_LOADSCRIPT)
1442 isn = generate_instr_type(cctx, isn_type, type);
1443 else
1444 isn = generate_instr_drop(cctx, isn_type, 1);
1445 if (isn == NULL)
1446 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001447
1448 // This requires three arguments, which doesn't fit in an instruction, thus
1449 // we need to allocate a struct for this.
1450 sref = ALLOC_ONE(scriptref_T);
1451 if (sref == NULL)
1452 return FAIL;
1453 isn->isn_arg.script.scriptref = sref;
1454 sref->sref_sid = sid;
1455 sref->sref_idx = idx;
1456 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001457 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 return OK;
1459}
1460
1461/*
1462 * Generate an ISN_NEWLIST instruction.
1463 */
1464 static int
1465generate_NEWLIST(cctx_T *cctx, int count)
1466{
1467 isn_T *isn;
1468 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 type_T *type;
1470 type_T *member;
1471
Bram Moolenaar080457c2020-03-03 21:53:32 +01001472 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1474 return FAIL;
1475 isn->isn_arg.number = count;
1476
Bram Moolenaar127542b2020-08-09 17:22:04 +02001477 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001478 if (count == 0)
1479 member = &t_void;
1480 else
1481 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001482 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1483 cctx->ctx_type_list);
1484 type = get_list_type(member, cctx->ctx_type_list);
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // drop the value types
1487 stack->ga_len -= count;
1488
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 // add the list type to the type stack
1490 if (ga_grow(stack, 1) == FAIL)
1491 return FAIL;
1492 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1493 ++stack->ga_len;
1494
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_NEWDICT instruction.
1500 */
1501 static int
1502generate_NEWDICT(cctx_T *cctx, int count)
1503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 type_T *type;
1507 type_T *member;
1508
Bram Moolenaar080457c2020-03-03 21:53:32 +01001509 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1511 return FAIL;
1512 isn->isn_arg.number = count;
1513
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001514 if (count == 0)
1515 member = &t_void;
1516 else
1517 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001518 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1519 cctx->ctx_type_list);
1520 type = get_dict_type(member, cctx->ctx_type_list);
1521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 // drop the key and value types
1523 stack->ga_len -= 2 * count;
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // add the dict type to the type stack
1526 if (ga_grow(stack, 1) == FAIL)
1527 return FAIL;
1528 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1529 ++stack->ga_len;
1530
1531 return OK;
1532}
1533
1534/*
1535 * Generate an ISN_FUNCREF instruction.
1536 */
1537 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001538generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539{
1540 isn_T *isn;
1541 garray_T *stack = &cctx->ctx_type_stack;
1542
Bram Moolenaar080457c2020-03-03 21:53:32 +01001543 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1545 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001546 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001547 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01001549 // If the referenced function is a closure, it may use items further up in
Bram Moolenaarab360522021-01-10 14:02:28 +01001550 // the nested context, including this one.
1551 if (ufunc->uf_flags & FC_CLOSURE)
1552 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if (ga_grow(stack, 1) == FAIL)
1555 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001556 ((type_T **)stack->ga_data)[stack->ga_len] =
1557 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 ++stack->ga_len;
1559
1560 return OK;
1561}
1562
1563/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001564 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001565 * "lambda_name" and "func_name" must be in allocated memory and will be
1566 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001567 */
1568 static int
1569generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1570{
1571 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001572
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001573 if (cctx->ctx_skip == SKIP_YES)
1574 {
1575 vim_free(lambda_name);
1576 vim_free(func_name);
1577 return OK;
1578 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001579 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001580 {
1581 vim_free(lambda_name);
1582 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001583 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001584 }
1585 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001586 isn->isn_arg.newfunc.nf_global = func_name;
1587
1588 return OK;
1589}
1590
1591/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001592 * Generate an ISN_DEF instruction: list functions
1593 */
1594 static int
1595generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1596{
1597 isn_T *isn;
1598
1599 RETURN_OK_IF_SKIP(cctx);
1600 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1601 return FAIL;
1602 if (len > 0)
1603 {
1604 isn->isn_arg.string = vim_strnsave(name, len);
1605 if (isn->isn_arg.string == NULL)
1606 return FAIL;
1607 }
1608 return OK;
1609}
1610
1611/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 * Generate an ISN_JUMP instruction.
1613 */
1614 static int
1615generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1616{
1617 isn_T *isn;
1618 garray_T *stack = &cctx->ctx_type_stack;
1619
Bram Moolenaar080457c2020-03-03 21:53:32 +01001620 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1622 return FAIL;
1623 isn->isn_arg.jump.jump_when = when;
1624 isn->isn_arg.jump.jump_where = where;
1625
1626 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1627 --stack->ga_len;
1628
1629 return OK;
1630}
1631
1632 static int
1633generate_FOR(cctx_T *cctx, int loop_idx)
1634{
1635 isn_T *isn;
1636 garray_T *stack = &cctx->ctx_type_stack;
1637
Bram Moolenaar080457c2020-03-03 21:53:32 +01001638 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1640 return FAIL;
1641 isn->isn_arg.forloop.for_idx = loop_idx;
1642
1643 if (ga_grow(stack, 1) == FAIL)
1644 return FAIL;
1645 // type doesn't matter, will be stored next
1646 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1647 ++stack->ga_len;
1648
1649 return OK;
1650}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001651/*
1652 * Generate an ISN_TRYCONT instruction.
1653 */
1654 static int
1655generate_TRYCONT(cctx_T *cctx, int levels, int where)
1656{
1657 isn_T *isn;
1658
1659 RETURN_OK_IF_SKIP(cctx);
1660 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.trycont.tct_levels = levels;
1663 isn->isn_arg.trycont.tct_where = where;
1664
1665 return OK;
1666}
1667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
1669/*
1670 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001671 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 * Return FAIL if the number of arguments is wrong.
1673 */
1674 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001675generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676{
1677 isn_T *isn;
1678 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001679 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001680 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001681 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682
Bram Moolenaar080457c2020-03-03 21:53:32 +01001683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001684 argoff = check_internal_func(func_idx, argcount);
1685 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 return FAIL;
1687
Bram Moolenaar389df252020-07-09 21:20:47 +02001688 if (method_call && argoff > 1)
1689 {
1690 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1691 return FAIL;
1692 isn->isn_arg.shuffle.shfl_item = argcount;
1693 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1694 }
1695
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001696 if (argcount > 0)
1697 {
1698 // Check the types of the arguments.
1699 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001700 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1701 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001702 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001703 if (internal_func_is_map(func_idx))
1704 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001705 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.bfunc.cbf_idx = func_idx;
1710 isn->isn_arg.bfunc.cbf_argcount = argcount;
1711
Bram Moolenaar94738d82020-10-21 14:25:07 +02001712 // Drop the argument types and push the return type.
1713 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 if (ga_grow(stack, 1) == FAIL)
1715 return FAIL;
1716 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001717 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001718 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001720 if (maptype != NULL && maptype->tt_member != NULL
1721 && maptype->tt_member != &t_any)
1722 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001723 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 return OK;
1726}
1727
1728/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001729 * Generate an ISN_LISTAPPEND instruction. Works like add().
1730 * Argument count is already checked.
1731 */
1732 static int
1733generate_LISTAPPEND(cctx_T *cctx)
1734{
1735 garray_T *stack = &cctx->ctx_type_stack;
1736 type_T *list_type;
1737 type_T *item_type;
1738 type_T *expected;
1739
1740 // Caller already checked that list_type is a list.
1741 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1742 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1743 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001744 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001745 return FAIL;
1746
1747 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1748 return FAIL;
1749
1750 --stack->ga_len; // drop the argument
1751 return OK;
1752}
1753
1754/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001755 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1756 * Argument count is already checked.
1757 */
1758 static int
1759generate_BLOBAPPEND(cctx_T *cctx)
1760{
1761 garray_T *stack = &cctx->ctx_type_stack;
1762 type_T *item_type;
1763
1764 // Caller already checked that blob_type is a blob.
1765 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001766 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001767 return FAIL;
1768
1769 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1770 return FAIL;
1771
1772 --stack->ga_len; // drop the argument
1773 return OK;
1774}
1775
1776/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001777 * Return TRUE if "ufunc" should be compiled, taking into account whether
1778 * "profile" indicates profiling is to be done.
1779 */
1780 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001781func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001782{
1783 switch (ufunc->uf_def_status)
1784 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001785 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 case UF_TO_BE_COMPILED: return TRUE;
1787 case UF_COMPILED:
1788 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001789#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001790 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1791 + ufunc->uf_dfunc_idx;
1792
1793 return profile ? dfunc->df_instr_prof == NULL
1794 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001795#else
1796 break;
1797#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001798 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001799 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001800 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001801 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001802}
1803
1804/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 * Generate an ISN_DCALL or ISN_UCALL instruction.
1806 * Return FAIL if the number of arguments is wrong.
1807 */
1808 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001809generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810{
1811 isn_T *isn;
1812 garray_T *stack = &cctx->ctx_type_stack;
1813 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001814 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 if (argcount > regular_args && !has_varargs(ufunc))
1818 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001819 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 return FAIL;
1821 }
1822 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1823 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001824 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 return FAIL;
1826 }
1827
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001828 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001829 {
1830 int i;
1831
1832 for (i = 0; i < argcount; ++i)
1833 {
1834 type_T *expected;
1835 type_T *actual;
1836
1837 if (i < regular_args)
1838 {
1839 if (ufunc->uf_arg_types == NULL)
1840 continue;
1841 expected = ufunc->uf_arg_types[i];
1842 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001843 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1844 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001845 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001846 else
1847 expected = ufunc->uf_va_type->tt_member;
1848 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001849 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001850 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001851 {
1852 arg_type_mismatch(expected, actual, i + 1);
1853 return FAIL;
1854 }
1855 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001856 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001857 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001858 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001859 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001860 }
1861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001863 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001864 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001866 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 {
1868 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1869 isn->isn_arg.dfunc.cdf_argcount = argcount;
1870 }
1871 else
1872 {
1873 // A user function may be deleted and redefined later, can't use the
1874 // ufunc pointer, need to look it up again at runtime.
1875 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1876 isn->isn_arg.ufunc.cuf_argcount = argcount;
1877 }
1878
1879 stack->ga_len -= argcount; // drop the arguments
1880 if (ga_grow(stack, 1) == FAIL)
1881 return FAIL;
1882 // add return value
1883 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1884 ++stack->ga_len;
1885
1886 return OK;
1887}
1888
1889/*
1890 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1891 */
1892 static int
1893generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1894{
1895 isn_T *isn;
1896 garray_T *stack = &cctx->ctx_type_stack;
1897
Bram Moolenaar080457c2020-03-03 21:53:32 +01001898 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1902 isn->isn_arg.ufunc.cuf_argcount = argcount;
1903
1904 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001905 if (ga_grow(stack, 1) == FAIL)
1906 return FAIL;
1907 // add return value
1908 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1909 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910
1911 return OK;
1912}
1913
1914/*
1915 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001916 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 */
1918 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001919generate_PCALL(
1920 cctx_T *cctx,
1921 int argcount,
1922 char_u *name,
1923 type_T *type,
1924 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925{
1926 isn_T *isn;
1927 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001928 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaar080457c2020-03-03 21:53:32 +01001930 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001931
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001932 if (type->tt_type == VAR_ANY)
1933 ret_type = &t_any;
1934 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001935 {
1936 if (type->tt_argcount != -1)
1937 {
1938 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1939
1940 if (argcount < type->tt_min_argcount - varargs)
1941 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001942 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001943 return FAIL;
1944 }
1945 if (!varargs && argcount > type->tt_argcount)
1946 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001947 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001948 return FAIL;
1949 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001950 if (type->tt_args != NULL)
1951 {
1952 int i;
1953
1954 for (i = 0; i < argcount; ++i)
1955 {
1956 int offset = -argcount + i - 1;
1957 type_T *actual = ((type_T **)stack->ga_data)[
1958 stack->ga_len + offset];
1959 type_T *expected;
1960
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001961 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001962 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001963 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001964 else
1965 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001966 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001967 cctx, TRUE, FALSE) == FAIL)
1968 {
1969 arg_type_mismatch(expected, actual, i + 1);
1970 return FAIL;
1971 }
1972 }
1973 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001974 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001975 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001976 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001977 else
1978 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001979 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001980 return FAIL;
1981 }
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.pfunc.cpf_top = at_top;
1986 isn->isn_arg.pfunc.cpf_argcount = argcount;
1987
1988 stack->ga_len -= argcount; // drop the arguments
1989
1990 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001991 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001993 // If partial is above the arguments it must be cleared and replaced with
1994 // the return value.
1995 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1996 return FAIL;
1997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 return OK;
1999}
2000
2001/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002002 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002005generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006{
2007 isn_T *isn;
2008 garray_T *stack = &cctx->ctx_type_stack;
2009 type_T *type;
2010
Bram Moolenaar080457c2020-03-03 21:53:32 +01002011 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002012 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002014 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002016 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01002018 if (type->tt_type != VAR_DICT && type != &t_any)
2019 {
2020 emsg(_(e_dictreq));
2021 return FAIL;
2022 }
2023 // change dict type to dict member type
2024 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01002025 {
2026 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
2027 type->tt_member == &t_unknown ? &t_any : type->tt_member;
2028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029
2030 return OK;
2031}
2032
2033/*
2034 * Generate an ISN_ECHO instruction.
2035 */
2036 static int
2037generate_ECHO(cctx_T *cctx, int with_white, int count)
2038{
2039 isn_T *isn;
2040
Bram Moolenaar080457c2020-03-03 21:53:32 +01002041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.echo.echo_with_white = with_white;
2045 isn->isn_arg.echo.echo_count = count;
2046
2047 return OK;
2048}
2049
Bram Moolenaarad39c092020-02-26 18:23:43 +01002050/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002051 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002052 */
2053 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002054generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002055{
2056 isn_T *isn;
2057
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002058 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002059 return FAIL;
2060 isn->isn_arg.number = count;
2061
2062 return OK;
2063}
2064
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002065/*
2066 * Generate an ISN_PUT instruction.
2067 */
2068 static int
2069generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2070{
2071 isn_T *isn;
2072
2073 RETURN_OK_IF_SKIP(cctx);
2074 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.put.put_regname = regname;
2077 isn->isn_arg.put.put_lnum = lnum;
2078 return OK;
2079}
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 static int
2082generate_EXEC(cctx_T *cctx, char_u *line)
2083{
2084 isn_T *isn;
2085
Bram Moolenaar080457c2020-03-03 21:53:32 +01002086 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2088 return FAIL;
2089 isn->isn_arg.string = vim_strsave(line);
2090 return OK;
2091}
2092
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002093 static int
2094generate_EXECCONCAT(cctx_T *cctx, int count)
2095{
2096 isn_T *isn;
2097
2098 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2099 return FAIL;
2100 isn->isn_arg.number = count;
2101 return OK;
2102}
2103
Bram Moolenaar08597872020-12-10 19:43:40 +01002104/*
2105 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2106 */
2107 static int
2108generate_RANGE(cctx_T *cctx, char_u *range)
2109{
2110 isn_T *isn;
2111 garray_T *stack = &cctx->ctx_type_stack;
2112
2113 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2114 return FAIL;
2115 isn->isn_arg.string = range;
2116
2117 if (ga_grow(stack, 1) == FAIL)
2118 return FAIL;
2119 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2120 ++stack->ga_len;
2121 return OK;
2122}
2123
Bram Moolenaar792f7862020-11-23 08:31:18 +01002124 static int
2125generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2126{
2127 isn_T *isn;
2128
2129 RETURN_OK_IF_SKIP(cctx);
2130 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2131 return FAIL;
2132 isn->isn_arg.unpack.unp_count = var_count;
2133 isn->isn_arg.unpack.unp_semicolon = semicolon;
2134 return OK;
2135}
2136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002138 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002139 */
2140 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002141generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002142{
2143 isn_T *isn;
2144
Bram Moolenaarfa984412021-03-25 22:15:28 +01002145 if (has_cmdmod(cmod))
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002146 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002147 cctx->ctx_has_cmdmod = TRUE;
2148
2149 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002150 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002151 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2152 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2153 return FAIL;
2154 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002155 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002156 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002157 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002158
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002159 return OK;
2160}
2161
2162 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002163generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002164{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002165 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2166 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002167 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002168 return OK;
2169}
2170
Bram Moolenaarfa984412021-03-25 22:15:28 +01002171 static int
2172misplaced_cmdmod(cctx_T *cctx)
Bram Moolenaara91a7132021-03-25 21:12:15 +01002173{
2174 garray_T *instr = &cctx->ctx_instr;
2175
Bram Moolenaara91a7132021-03-25 21:12:15 +01002176 if (cctx->ctx_has_cmdmod
2177 && ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
2178 == ISN_CMDMOD)
2179 {
Bram Moolenaarfa984412021-03-25 22:15:28 +01002180 emsg(_(e_misplaced_command_modifier));
2181 return TRUE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002182 }
Bram Moolenaarfa984412021-03-25 22:15:28 +01002183 return FALSE;
Bram Moolenaara91a7132021-03-25 21:12:15 +01002184}
2185
2186/*
2187 * Get the index of the current instruction.
2188 * This compenstates for a preceding ISN_CMDMOD and ISN_PROF_START.
2189 */
2190 static int
2191current_instr_idx(cctx_T *cctx)
2192{
2193 garray_T *instr = &cctx->ctx_instr;
2194 int idx = instr->ga_len;
2195
2196 if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
2197 .isn_type == ISN_CMDMOD)
2198 --idx;
2199#ifdef FEAT_PROFILE
2200 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[idx - 1]
2201 .isn_type == ISN_PROF_START)
2202 --idx;
2203#endif
2204 return idx;
2205}
2206
Bram Moolenaarf002a412021-01-24 13:34:18 +01002207#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002208 static void
2209may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2210{
2211 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002212 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002213}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002214#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002215
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002216/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002218 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002220 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002221reserve_local(
2222 cctx_T *cctx,
2223 char_u *name,
2224 size_t len,
2225 int isConst,
2226 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 lvar_T *lvar;
2229
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002230 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002232 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002233 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 }
2235
2236 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002237 return NULL;
2238 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002239 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002241 // Every local variable uses the next entry on the stack. We could re-use
2242 // the last ones when leaving a scope, but then variables used in a closure
2243 // might get overwritten. To keep things simple do not re-use stack
2244 // entries. This is less efficient, but memory is cheap these days.
2245 lvar->lv_idx = cctx->ctx_locals_count++;
2246
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002247 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 lvar->lv_const = isConst;
2249 lvar->lv_type = type;
2250
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002251 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252}
2253
2254/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002255 * Remove local variables above "new_top".
2256 */
2257 static void
2258unwind_locals(cctx_T *cctx, int new_top)
2259{
2260 if (cctx->ctx_locals.ga_len > new_top)
2261 {
2262 int idx;
2263 lvar_T *lvar;
2264
2265 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2266 {
2267 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2268 vim_free(lvar->lv_name);
2269 }
2270 }
2271 cctx->ctx_locals.ga_len = new_top;
2272}
2273
2274/*
2275 * Free all local variables.
2276 */
2277 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002278free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002279{
2280 unwind_locals(cctx, 0);
2281 ga_clear(&cctx->ctx_locals);
2282}
2283
2284/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002285 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2286 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2287 * error if the variable was defined with :const.
2288 */
2289 static int
2290check_item_writable(svar_T *sv, int check_writable, char_u *name)
2291{
2292 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2293 || (check_writable == ASSIGN_FINAL
2294 && sv->sv_const == ASSIGN_CONST))
2295 {
2296 semsg(_(e_readonlyvar), name);
2297 return FAIL;
2298 }
2299 return OK;
2300}
2301
2302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002304 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 * Returns the index in "sn_var_vals" if found.
2306 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002307 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 */
2309 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002310get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311{
2312 hashtab_T *ht;
2313 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002314 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002315 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 int idx;
2317
Bram Moolenaare3d46852020-08-29 13:39:17 +02002318 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002320 if (sid == current_sctx.sc_sid)
2321 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002322 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002323
2324 if (sav == NULL)
2325 return -2;
2326 idx = sav->sav_var_vals_idx;
2327 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002328 if (check_item_writable(sv, check_writable, name) == FAIL)
2329 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002330 return idx;
2331 }
2332
2333 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 ht = &SCRIPT_VARS(sid);
2335 di = find_var_in_ht(ht, 0, name, TRUE);
2336 if (di == NULL)
2337 return -2;
2338
2339 // Now find the svar_T index in sn_var_vals.
2340 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2341 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002342 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 if (sv->sv_tv == &di->di_tv)
2344 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002345 if (check_item_writable(sv, check_writable, name) == FAIL)
2346 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 return idx;
2348 }
2349 }
2350 return -1;
2351}
2352
2353/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002354 * Find "name" in imported items of the current script or in "cctx" if not
2355 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 */
2357 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002358find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 int idx;
2361
Bram Moolenaare3d46852020-08-29 13:39:17 +02002362 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002363 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 if (cctx != NULL)
2365 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2366 {
2367 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2368 + idx;
2369
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002370 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2371 : STRLEN(import->imp_name) == len
2372 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 return import;
2374 }
2375
Bram Moolenaarefa94442020-08-08 22:16:00 +02002376 return find_imported_in_script(name, len, current_sctx.sc_sid);
2377}
2378
2379 imported_T *
2380find_imported_in_script(char_u *name, size_t len, int sid)
2381{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002382 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002383 int idx;
2384
Bram Moolenaare3d46852020-08-29 13:39:17 +02002385 if (!SCRIPT_ID_VALID(sid))
2386 return NULL;
2387 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2389 {
2390 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2391
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002392 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2393 : STRLEN(import->imp_name) == len
2394 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 return import;
2396 }
2397 return NULL;
2398}
2399
2400/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002401 * Free all imported variables.
2402 */
2403 static void
2404free_imported(cctx_T *cctx)
2405{
2406 int idx;
2407
2408 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2409 {
2410 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2411
2412 vim_free(import->imp_name);
2413 }
2414 ga_clear(&cctx->ctx_imports);
2415}
2416
2417/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002418 * Return a pointer to the next line that isn't empty or only contains a
2419 * comment. Skips over white space.
2420 * Returns NULL if there is none.
2421 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002422 char_u *
2423peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002424{
2425 int lnum = cctx->ctx_lnum;
2426
2427 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2428 {
2429 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002430 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002431
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002432 // ignore NULLs inserted for continuation lines
2433 if (line != NULL)
2434 {
2435 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002436 if (vim9_bad_comment(p))
2437 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002438 if (*p != NUL && !vim9_comment_start(p))
2439 return p;
2440 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002441 }
2442 return NULL;
2443}
2444
2445/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002446 * Called when checking for a following operator at "arg". When the rest of
2447 * the line is empty or only a comment, peek the next line. If there is a next
2448 * line return a pointer to it and set "nextp".
2449 * Otherwise skip over white space.
2450 */
2451 static char_u *
2452may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2453{
2454 char_u *p = skipwhite(arg);
2455
2456 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002457 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002458 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002459 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002460 if (*nextp != NULL)
2461 return *nextp;
2462 }
2463 return p;
2464}
2465
2466/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002467 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002468 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002469 * Returns NULL when at the end.
2470 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002471 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002472next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002473{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002474 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002475
2476 do
2477 {
2478 ++cctx->ctx_lnum;
2479 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002480 {
2481 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002482 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002483 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002484 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002485 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002486 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002487 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002488 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002489 return line;
2490}
2491
2492/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002493 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002494 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002495 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002496 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2497 */
2498 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002499may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002500{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002501 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01002502 if (vim9_bad_comment(*arg))
2503 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002504 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002505 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002506 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002507
2508 if (next == NULL)
2509 return FAIL;
2510 *arg = skipwhite(next);
2511 }
2512 return OK;
2513}
2514
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002515/*
2516 * Idem, and give an error when failed.
2517 */
2518 static int
2519may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2520{
2521 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2522 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002523 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002524 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002525 return FAIL;
2526 }
2527 return OK;
2528}
2529
2530
Bram Moolenaara5565e42020-05-09 15:44:01 +02002531// Structure passed between the compile_expr* functions to keep track of
2532// constants that have been parsed but for which no code was produced yet. If
2533// possible expressions on these constants are applied at compile time. If
2534// that is not possible, the code to push the constants needs to be generated
2535// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002536// Using 50 should be more than enough of 5 levels of ().
2537#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002538typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002539 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002540 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002541 int pp_is_const; // all generated code was constants, used for a
2542 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002543} ppconst_T;
2544
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002545static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002546static int compile_expr0(char_u **arg, cctx_T *cctx);
2547static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2548
Bram Moolenaara5565e42020-05-09 15:44:01 +02002549/*
2550 * Generate a PUSH instruction for "tv".
2551 * "tv" will be consumed or cleared.
2552 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2553 */
2554 static int
2555generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2556{
2557 if (tv != NULL)
2558 {
2559 switch (tv->v_type)
2560 {
2561 case VAR_UNKNOWN:
2562 break;
2563 case VAR_BOOL:
2564 generate_PUSHBOOL(cctx, tv->vval.v_number);
2565 break;
2566 case VAR_SPECIAL:
2567 generate_PUSHSPEC(cctx, tv->vval.v_number);
2568 break;
2569 case VAR_NUMBER:
2570 generate_PUSHNR(cctx, tv->vval.v_number);
2571 break;
2572#ifdef FEAT_FLOAT
2573 case VAR_FLOAT:
2574 generate_PUSHF(cctx, tv->vval.v_float);
2575 break;
2576#endif
2577 case VAR_BLOB:
2578 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2579 tv->vval.v_blob = NULL;
2580 break;
2581 case VAR_STRING:
2582 generate_PUSHS(cctx, tv->vval.v_string);
2583 tv->vval.v_string = NULL;
2584 break;
2585 default:
2586 iemsg("constant type not supported");
2587 clear_tv(tv);
2588 return FAIL;
2589 }
2590 tv->v_type = VAR_UNKNOWN;
2591 }
2592 return OK;
2593}
2594
2595/*
2596 * Generate code for any ppconst entries.
2597 */
2598 static int
2599generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2600{
2601 int i;
2602 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002603 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002604
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002605 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002606 for (i = 0; i < ppconst->pp_used; ++i)
2607 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2608 ret = FAIL;
2609 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002610 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002611 return ret;
2612}
2613
2614/*
2615 * Clear ppconst constants. Used when failing.
2616 */
2617 static void
2618clear_ppconst(ppconst_T *ppconst)
2619{
2620 int i;
2621
2622 for (i = 0; i < ppconst->pp_used; ++i)
2623 clear_tv(&ppconst->pp_tv[i]);
2624 ppconst->pp_used = 0;
2625}
2626
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002627/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002628 * Generate an instruction to load script-local variable "name", without the
2629 * leading "s:".
2630 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 */
2632 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002633compile_load_scriptvar(
2634 cctx_T *cctx,
2635 char_u *name, // variable NUL terminated
2636 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002637 char_u **end, // end of variable
2638 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002640 scriptitem_T *si;
2641 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 imported_T *import;
2643
Bram Moolenaare3d46852020-08-29 13:39:17 +02002644 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2645 return FAIL;
2646 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002647 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002648 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002650 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002651 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2652 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 }
2654 if (idx >= 0)
2655 {
2656 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2657
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002658 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 current_sctx.sc_sid, idx, sv->sv_type);
2660 return OK;
2661 }
2662
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002663 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 if (import != NULL)
2665 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002666 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002667 {
2668 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002669 char_u *exp_name;
2670 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002671 ufunc_T *ufunc;
2672 type_T *type;
2673
2674 // Used "import * as Name", need to lookup the member.
2675 if (*p != '.')
2676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002677 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002678 return FAIL;
2679 }
2680 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002681 if (VIM_ISWHITE(*p))
2682 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002683 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002684 return FAIL;
2685 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002686
Bram Moolenaar1c991142020-07-04 13:15:31 +02002687 // isolate one name
2688 exp_name = p;
2689 while (eval_isnamec(*p))
2690 ++p;
2691 cc = *p;
2692 *p = NUL;
2693
Bram Moolenaaredba7072021-03-13 21:14:18 +01002694 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
2695 cctx, TRUE);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002696 *p = cc;
2697 p = skipwhite(p);
2698
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002699 // TODO: what if it is a function?
2700 if (idx < 0)
2701 return FAIL;
2702 *end = p;
2703
2704 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2705 import->imp_sid,
2706 idx,
2707 type);
2708 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002709 else if (import->imp_funcname != NULL)
2710 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002711 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002712 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2713 import->imp_sid,
2714 import->imp_var_vals_idx,
2715 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 return OK;
2717 }
2718
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002719 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002720 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 return FAIL;
2722}
2723
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002724 static int
2725generate_funcref(cctx_T *cctx, char_u *name)
2726{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002727 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002728
2729 if (ufunc == NULL)
2730 return FAIL;
2731
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002732 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002733 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2734 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002735 == FAIL)
2736 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002737 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002738}
2739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740/*
2741 * Compile a variable name into a load instruction.
2742 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002743 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 * When "error" is FALSE do not give an error when not found.
2745 */
2746 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002747compile_load(
2748 char_u **arg,
2749 char_u *end_arg,
2750 cctx_T *cctx,
2751 int is_expr,
2752 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753{
2754 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002755 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002756 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002758 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759
2760 if (*(*arg + 1) == ':')
2761 {
2762 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002763 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002764 {
2765 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002767 switch (**arg)
2768 {
2769 case 'g': isn_type = ISN_LOADGDICT; break;
2770 case 'w': isn_type = ISN_LOADWDICT; break;
2771 case 't': isn_type = ISN_LOADTDICT; break;
2772 case 'b': isn_type = ISN_LOADBDICT; break;
2773 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002774 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002775 goto theend;
2776 }
2777 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2778 goto theend;
2779 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 else
2782 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002783 isntype_T isn_type = ISN_DROP;
2784
2785 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2786 if (name == NULL)
2787 return FAIL;
2788
2789 switch (**arg)
2790 {
2791 case 'v': res = generate_LOADV(cctx, name, error);
2792 break;
2793 case 's': res = compile_load_scriptvar(cctx, name,
2794 NULL, NULL, error);
2795 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002796 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2797 isn_type = ISN_LOADG;
2798 else
2799 {
2800 isn_type = ISN_LOADAUTO;
2801 vim_free(name);
2802 name = vim_strnsave(*arg, end - *arg);
2803 if (name == NULL)
2804 return FAIL;
2805 }
2806 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002807 case 'w': isn_type = ISN_LOADW; break;
2808 case 't': isn_type = ISN_LOADT; break;
2809 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002810 default: // cannot happen, just in case
2811 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002812 goto theend;
2813 }
2814 if (isn_type != ISN_DROP)
2815 {
2816 // Global, Buffer-local, Window-local and Tabpage-local
2817 // variables can be defined later, thus we don't check if it
2818 // exists, give error at runtime.
2819 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
2822 }
2823 else
2824 {
2825 size_t len = end - *arg;
2826 int idx;
2827 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002828 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829
2830 name = vim_strnsave(*arg, end - *arg);
2831 if (name == NULL)
2832 return FAIL;
2833
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002834 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002836 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002837 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 }
2839 else
2840 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002841 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002842
Bram Moolenaar709664c2020-12-12 14:33:41 +01002843 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002845 type = lvar.lv_type;
2846 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002847 if (lvar.lv_from_outer != 0)
2848 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002849 else
2850 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 else
2853 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002854 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002855 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002856 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002857 || find_imported(name, 0, cctx) != NULL)
2858 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002859
Bram Moolenaar0f769812020-09-12 18:32:34 +02002860 // When evaluating an expression and the name starts with an
2861 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002862 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002863 if (res == FAIL && is_expr
2864 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002865 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 }
2867 }
2868 if (gen_load)
2869 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002870 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002871 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002872 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002873 cctx->ctx_outer_used = TRUE;
2874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 }
2876
2877 *arg = end;
2878
2879theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002880 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002881 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 vim_free(name);
2883 return res;
2884}
2885
2886/*
2887 * Compile the argument expressions.
2888 * "arg" points to just after the "(" and is advanced to after the ")"
2889 */
2890 static int
2891compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2892{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002893 char_u *p = *arg;
2894 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002895 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896
Bram Moolenaare6085c52020-04-12 20:19:16 +02002897 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002899 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2900 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002901 if (*p == ')')
2902 {
2903 *arg = p + 1;
2904 return OK;
2905 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002906 if (must_end)
2907 {
2908 semsg(_(e_missing_comma_before_argument_str), p);
2909 return FAIL;
2910 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002911
Bram Moolenaara5565e42020-05-09 15:44:01 +02002912 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 return FAIL;
2914 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002915
2916 if (*p != ',' && *skipwhite(p) == ',')
2917 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002918 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002919 p = skipwhite(p);
2920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002922 {
2923 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002924 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002925 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002926 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002927 else
2928 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002929 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002930 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002932failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002933 emsg(_(e_missing_close));
2934 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935}
2936
2937/*
2938 * Compile a function call: name(arg1, arg2)
2939 * "arg" points to "name", "arg + varlen" to the "(".
2940 * "argcount_init" is 1 for "value->method()"
2941 * Instructions:
2942 * EVAL arg1
2943 * EVAL arg2
2944 * BCALL / DCALL / UCALL
2945 */
2946 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002947compile_call(
2948 char_u **arg,
2949 size_t varlen,
2950 cctx_T *cctx,
2951 ppconst_T *ppconst,
2952 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953{
2954 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002955 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 int argcount = argcount_init;
2957 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002958 char_u fname_buf[FLEN_FIXED + 1];
2959 char_u *tofree = NULL;
2960 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002961 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002962 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002963 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964
Bram Moolenaara5565e42020-05-09 15:44:01 +02002965 // we can evaluate "has('name')" at compile time
2966 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2967 {
2968 char_u *s = skipwhite(*arg + varlen + 1);
2969 typval_T argvars[2];
2970
2971 argvars[0].v_type = VAR_UNKNOWN;
2972 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002973 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002974 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002975 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002976 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002977 if (*s == ')' && argvars[0].v_type == VAR_STRING
2978 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002979 {
2980 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2981
2982 *arg = s + 1;
2983 argvars[1].v_type = VAR_UNKNOWN;
2984 tv->v_type = VAR_NUMBER;
2985 tv->vval.v_number = 0;
2986 f_has(argvars, tv);
2987 clear_tv(&argvars[0]);
2988 ++ppconst->pp_used;
2989 return OK;
2990 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002991 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002992 }
2993
2994 if (generate_ppconst(cctx, ppconst) == FAIL)
2995 return FAIL;
2996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 if (varlen >= sizeof(namebuf))
2998 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002999 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 return FAIL;
3001 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003002 vim_strncpy(namebuf, *arg, varlen);
3003 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004
3005 *arg = skipwhite(*arg + varlen + 1);
3006 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003007 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008
Bram Moolenaar03290b82020-12-19 16:30:44 +01003009 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02003010 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 {
3012 int idx;
3013
3014 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003015 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003017 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01003018 if (STRCMP(name, "flatten") == 0)
3019 {
3020 emsg(_(e_cannot_use_flatten_in_vim9_script));
3021 goto theend;
3022 }
3023
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003024 if (STRCMP(name, "add") == 0 && argcount == 2)
3025 {
3026 garray_T *stack = &cctx->ctx_type_stack;
3027 type_T *type = ((type_T **)stack->ga_data)[
3028 stack->ga_len - 2];
3029
Bram Moolenaare88c8e82020-11-01 17:03:37 +01003030 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003031 if (type->tt_type == VAR_LIST)
3032 {
3033 // inline "add(list, item)" so that the type can be checked
3034 res = generate_LISTAPPEND(cctx);
3035 idx = -1;
3036 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003037 else if (type->tt_type == VAR_BLOB)
3038 {
3039 // inline "add(blob, nr)" so that the type can be checked
3040 res = generate_BLOBAPPEND(cctx);
3041 idx = -1;
3042 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003043 }
3044
3045 if (idx >= 0)
3046 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
3047 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003048 else
3049 semsg(_(e_unknownfunc), namebuf);
3050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 }
3052
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003053 // An argument or local variable can be a function reference, this
3054 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01003055 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003056 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003057 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01003058 // If we can find the function by name generate the right call.
3059 // Skip global functions here, a local funcref takes precedence.
3060 ufunc = find_func(name, FALSE, cctx);
3061 if (ufunc != NULL && !func_is_global(ufunc))
3062 {
3063 res = generate_CALL(cctx, ufunc, argcount);
3064 goto theend;
3065 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067
3068 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02003069 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02003070 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02003072 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02003073 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003074 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003075 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003076 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003077
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003078 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003079 goto theend;
3080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081
Bram Moolenaar0f769812020-09-12 18:32:34 +02003082 // If we can find a global function by name generate the right call.
3083 if (ufunc != NULL)
3084 {
3085 res = generate_CALL(cctx, ufunc, argcount);
3086 goto theend;
3087 }
3088
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003089 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003090 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003091 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003092 res = generate_UCALL(cctx, name, argcount);
3093 else
3094 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003095
3096theend:
3097 vim_free(tofree);
3098 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099}
3100
3101// like NAMESPACE_CHAR but with 'a' and 'l'.
3102#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3103
3104/*
3105 * Find the end of a variable or function name. Unlike find_name_end() this
3106 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003107 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 * Return a pointer to just after the name. Equal to "arg" if there is no
3109 * valid name.
3110 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003111 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003112to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113{
3114 char_u *p;
3115
3116 // Quick check for valid starting character.
3117 if (!eval_isnamec1(*arg))
3118 return arg;
3119
3120 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3121 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3122 // and can be used in slice "[n:]".
3123 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003124 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3126 break;
3127 return p;
3128}
3129
3130/*
3131 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003132 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 */
3134 char_u *
3135to_name_const_end(char_u *arg)
3136{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003137 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 typval_T rettv;
3139
3140 if (p == arg && *arg == '[')
3141 {
3142
3143 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003144 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 p = arg;
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 return p;
3148}
3149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150/*
3151 * parse a list: [expr, expr]
3152 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003153 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 */
3155 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003156compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157{
3158 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003159 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003161 int is_const;
3162 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003167 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003168 semsg(_(e_list_end), *arg);
3169 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003170 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003171 if (*p == ',')
3172 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003173 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003174 return FAIL;
3175 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003176 if (*p == ']')
3177 {
3178 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003179 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003180 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003181 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003182 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003183 if (!is_const)
3184 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 ++count;
3186 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003187 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003189 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3190 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003191 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003192 return FAIL;
3193 }
3194 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003195 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 p = skipwhite(p);
3197 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003198 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003200 ppconst->pp_is_const = is_all_const;
3201 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202}
3203
3204/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003205 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003206 * "*arg" points to the '('.
Bram Moolenaare462f522020-12-27 14:43:30 +01003207 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 */
3209 static int
3210compile_lambda(char_u **arg, cctx_T *cctx)
3211{
Bram Moolenaare462f522020-12-27 14:43:30 +01003212 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 typval_T rettv;
3214 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003215 evalarg_T evalarg;
3216
3217 CLEAR_FIELD(evalarg);
3218 evalarg.eval_flags = EVAL_EVALUATE;
3219 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220
3221 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003222 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3223 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003224 {
3225 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003226 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003227 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003228
Bram Moolenaar65c44152020-12-24 15:14:01 +01003229 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003231 ++ufunc->uf_refcount;
3232 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233
Bram Moolenaar65c44152020-12-24 15:14:01 +01003234 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003235 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
Bram Moolenaar67da21a2021-03-21 22:12:34 +01003237 // evalarg.eval_tofree_cmdline may have a copy of the last line and "*arg"
3238 // points into it. Point to the original line to avoid a dangling pointer.
3239 if (evalarg.eval_tofree_cmdline != NULL)
3240 {
3241 size_t off = *arg - evalarg.eval_tofree_cmdline;
3242
3243 *arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
3244 + off;
3245 }
3246
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003247 clear_evalarg(&evalarg, NULL);
3248
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003249 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003250 {
3251 // The return type will now be known.
3252 set_function_type(ufunc);
3253
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003254 // The function reference count will be 1. When the ISN_FUNCREF
3255 // instruction is deleted the reference count is decremented and the
3256 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003257 return generate_FUNCREF(cctx, ufunc);
3258 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003259
3260 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 return FAIL;
3262}
3263
3264/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003265 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003267 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 */
3269 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003270compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271{
3272 garray_T *instr = &cctx->ctx_instr;
3273 int count = 0;
3274 dict_T *d = dict_alloc();
3275 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003276 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003277 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003278 int is_const;
3279 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280
3281 if (d == NULL)
3282 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003283 if (generate_ppconst(cctx, ppconst) == FAIL)
3284 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003285 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003287 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288
Bram Moolenaar23c55272020-06-21 16:58:13 +02003289 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003291 *arg = NULL;
3292 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003293 }
3294
3295 if (**arg == '}')
3296 break;
3297
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003298 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003300 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003302 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003303 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003304 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003307 if (isn->isn_type == ISN_PUSHNR)
3308 {
3309 char buf[NUMBUFLEN];
3310
3311 // Convert to string at compile time.
3312 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3313 isn->isn_type = ISN_PUSHS;
3314 isn->isn_arg.string = vim_strsave((char_u *)buf);
3315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 if (isn->isn_type == ISN_PUSHS)
3317 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003318 else if (may_generate_2STRING(-1, cctx) == FAIL)
3319 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003320 *arg = skipwhite(*arg);
3321 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003322 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003323 emsg(_(e_missing_matching_bracket_after_dict_key));
3324 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003325 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003326 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003328 else
3329 {
3330 // {"name": value},
3331 // {'name': value},
3332 // {name: value} use "name" as a literal key
3333 key = get_literal_key(arg);
3334 if (key == NULL)
3335 return FAIL;
3336 if (generate_PUSHS(cctx, key) == FAIL)
3337 return FAIL;
3338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339
3340 // Check for duplicate keys, if using string keys.
3341 if (key != NULL)
3342 {
3343 item = dict_find(d, key, -1);
3344 if (item != NULL)
3345 {
3346 semsg(_(e_duplicate_key), key);
3347 goto failret;
3348 }
3349 item = dictitem_alloc(key);
3350 if (item != NULL)
3351 {
3352 item->di_tv.v_type = VAR_UNKNOWN;
3353 item->di_tv.v_lock = 0;
3354 if (dict_add(d, item) == FAIL)
3355 dictitem_free(item);
3356 }
3357 }
3358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 if (**arg != ':')
3360 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003361 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003362 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003363 else
3364 semsg(_(e_missing_dict_colon), *arg);
3365 return FAIL;
3366 }
3367 whitep = *arg + 1;
3368 if (!IS_WHITE_OR_NUL(*whitep))
3369 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003370 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 return FAIL;
3372 }
3373
Bram Moolenaar23c55272020-06-21 16:58:13 +02003374 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003375 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003376 *arg = NULL;
3377 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003378 }
3379
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003380 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003382 if (!is_const)
3383 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 ++count;
3385
Bram Moolenaar2c330432020-04-13 14:41:35 +02003386 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003387 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003388 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003389 *arg = NULL;
3390 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003391 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 if (**arg == '}')
3393 break;
3394 if (**arg != ',')
3395 {
3396 semsg(_(e_missing_dict_comma), *arg);
3397 goto failret;
3398 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003399 if (IS_WHITE_OR_NUL(*whitep))
3400 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003401 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003402 return FAIL;
3403 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003404 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003405 if (!IS_WHITE_OR_NUL(*whitep))
3406 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003407 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003408 return FAIL;
3409 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003410 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 }
3412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 *arg = *arg + 1;
3414
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003415 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003416 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003417 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003418 *arg += STRLEN(*arg);
3419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003421 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 return generate_NEWDICT(cctx, count);
3423
3424failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003425 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003426 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003427 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003428 *arg = (char_u *)"";
3429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 dict_unref(d);
3431 return FAIL;
3432}
3433
3434/*
3435 * Compile "&option".
3436 */
3437 static int
3438compile_get_option(char_u **arg, cctx_T *cctx)
3439{
3440 typval_T rettv;
3441 char_u *start = *arg;
3442 int ret;
3443
3444 // parse the option and get the current value to get the type.
3445 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003446 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 if (ret == OK)
3448 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003449 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003450 char_u *name = vim_strnsave(start, *arg - start);
3451 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3452 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453
3454 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3455 vim_free(name);
3456 }
3457 clear_tv(&rettv);
3458
3459 return ret;
3460}
3461
3462/*
3463 * Compile "$VAR".
3464 */
3465 static int
3466compile_get_env(char_u **arg, cctx_T *cctx)
3467{
3468 char_u *start = *arg;
3469 int len;
3470 int ret;
3471 char_u *name;
3472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 ++*arg;
3474 len = get_env_len(arg);
3475 if (len == 0)
3476 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003477 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 return FAIL;
3479 }
3480
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003481 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 name = vim_strnsave(start, len + 1);
3483 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3484 vim_free(name);
3485 return ret;
3486}
3487
3488/*
3489 * Compile "@r".
3490 */
3491 static int
3492compile_get_register(char_u **arg, cctx_T *cctx)
3493{
3494 int ret;
3495
3496 ++*arg;
3497 if (**arg == NUL)
3498 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003499 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 return FAIL;
3501 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003502 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 {
3504 emsg_invreg(**arg);
3505 return FAIL;
3506 }
3507 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3508 ++*arg;
3509 return ret;
3510}
3511
3512/*
3513 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003514 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 */
3516 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003517apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003519 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520
3521 // this works from end to start
3522 while (p > start)
3523 {
3524 --p;
3525 if (*p == '-' || *p == '+')
3526 {
3527 // only '-' has an effect, for '+' we only check the type
3528#ifdef FEAT_FLOAT
3529 if (rettv->v_type == VAR_FLOAT)
3530 {
3531 if (*p == '-')
3532 rettv->vval.v_float = -rettv->vval.v_float;
3533 }
3534 else
3535#endif
3536 {
3537 varnumber_T val;
3538 int error = FALSE;
3539
3540 // tv_get_number_chk() accepts a string, but we don't want that
3541 // here
3542 if (check_not_string(rettv) == FAIL)
3543 return FAIL;
3544 val = tv_get_number_chk(rettv, &error);
3545 clear_tv(rettv);
3546 if (error)
3547 return FAIL;
3548 if (*p == '-')
3549 val = -val;
3550 rettv->v_type = VAR_NUMBER;
3551 rettv->vval.v_number = val;
3552 }
3553 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003554 else if (numeric_only)
3555 {
3556 ++p;
3557 break;
3558 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003559 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 {
3561 int v = tv2bool(rettv);
3562
3563 // '!' is permissive in the type.
3564 clear_tv(rettv);
3565 rettv->v_type = VAR_BOOL;
3566 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3567 }
3568 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003569 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 return OK;
3571}
3572
3573/*
3574 * Recognize v: variables that are constants and set "rettv".
3575 */
3576 static void
3577get_vim_constant(char_u **arg, typval_T *rettv)
3578{
3579 if (STRNCMP(*arg, "v:true", 6) == 0)
3580 {
3581 rettv->v_type = VAR_BOOL;
3582 rettv->vval.v_number = VVAL_TRUE;
3583 *arg += 6;
3584 }
3585 else if (STRNCMP(*arg, "v:false", 7) == 0)
3586 {
3587 rettv->v_type = VAR_BOOL;
3588 rettv->vval.v_number = VVAL_FALSE;
3589 *arg += 7;
3590 }
3591 else if (STRNCMP(*arg, "v:null", 6) == 0)
3592 {
3593 rettv->v_type = VAR_SPECIAL;
3594 rettv->vval.v_number = VVAL_NULL;
3595 *arg += 6;
3596 }
3597 else if (STRNCMP(*arg, "v:none", 6) == 0)
3598 {
3599 rettv->v_type = VAR_SPECIAL;
3600 rettv->vval.v_number = VVAL_NONE;
3601 *arg += 6;
3602 }
3603}
3604
Bram Moolenaar657137c2021-01-09 15:45:23 +01003605 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003606get_compare_type(char_u *p, int *len, int *type_is)
3607{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003608 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003609 int i;
3610
3611 switch (p[0])
3612 {
3613 case '=': if (p[1] == '=')
3614 type = EXPR_EQUAL;
3615 else if (p[1] == '~')
3616 type = EXPR_MATCH;
3617 break;
3618 case '!': if (p[1] == '=')
3619 type = EXPR_NEQUAL;
3620 else if (p[1] == '~')
3621 type = EXPR_NOMATCH;
3622 break;
3623 case '>': if (p[1] != '=')
3624 {
3625 type = EXPR_GREATER;
3626 *len = 1;
3627 }
3628 else
3629 type = EXPR_GEQUAL;
3630 break;
3631 case '<': if (p[1] != '=')
3632 {
3633 type = EXPR_SMALLER;
3634 *len = 1;
3635 }
3636 else
3637 type = EXPR_SEQUAL;
3638 break;
3639 case 'i': if (p[1] == 's')
3640 {
3641 // "is" and "isnot"; but not a prefix of a name
3642 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3643 *len = 5;
3644 i = p[*len];
3645 if (!isalnum(i) && i != '_')
3646 {
3647 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3648 *type_is = TRUE;
3649 }
3650 }
3651 break;
3652 }
3653 return type;
3654}
3655
3656/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003657 * Skip over an expression, ignoring most errors.
3658 */
3659 static void
3660skip_expr_cctx(char_u **arg, cctx_T *cctx)
3661{
3662 evalarg_T evalarg;
3663
3664 CLEAR_FIELD(evalarg);
3665 evalarg.eval_cctx = cctx;
3666 skip_expr(arg, &evalarg);
3667}
3668
3669/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003671 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 */
3673 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003674compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003676 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
3678 // this works from end to start
3679 while (p > start)
3680 {
3681 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003682 while (VIM_ISWHITE(*p))
3683 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 if (*p == '-' || *p == '+')
3685 {
3686 int negate = *p == '-';
3687 isn_T *isn;
3688
3689 // TODO: check type
3690 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3691 {
3692 --p;
3693 if (*p == '-')
3694 negate = !negate;
3695 }
3696 // only '-' has an effect, for '+' we only check the type
3697 if (negate)
3698 isn = generate_instr(cctx, ISN_NEGATENR);
3699 else
3700 isn = generate_instr(cctx, ISN_CHECKNR);
3701 if (isn == NULL)
3702 return FAIL;
3703 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003704 else if (numeric_only)
3705 {
3706 ++p;
3707 break;
3708 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 else
3710 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003711 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003713 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003715 if (p[-1] == '!')
3716 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 }
3719 if (generate_2BOOL(cctx, invert) == FAIL)
3720 return FAIL;
3721 }
3722 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003723 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 return OK;
3725}
3726
3727/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003728 * Compile "(expression)": recursive!
3729 * Return FAIL/OK.
3730 */
3731 static int
3732compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3733{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003734 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003735 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003736
Bram Moolenaar24156692021-01-14 20:35:49 +01003737 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3738 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003739 if (ppconst->pp_used <= PPSIZE - 10)
3740 {
3741 ret = compile_expr1(arg, cctx, ppconst);
3742 }
3743 else
3744 {
3745 // Not enough space in ppconst, flush constants.
3746 if (generate_ppconst(cctx, ppconst) == FAIL)
3747 return FAIL;
3748 ret = compile_expr0(arg, cctx);
3749 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003750 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3751 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003752 if (**arg == ')')
3753 ++*arg;
3754 else if (ret == OK)
3755 {
3756 emsg(_(e_missing_close));
3757 ret = FAIL;
3758 }
3759 return ret;
3760}
3761
3762/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003764 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 */
3766 static int
3767compile_subscript(
3768 char_u **arg,
3769 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003770 char_u *start_leader,
3771 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003772 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003774 char_u *name_start = *end_leader;
3775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 for (;;)
3777 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003778 char_u *p = skipwhite(*arg);
3779
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003780 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003781 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003782 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003783
3784 // If a following line starts with "->{" or "->X" advance to that
3785 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003786 // Also if a following line starts with ".x".
3787 if (next != NULL &&
3788 ((next[0] == '-' && next[1] == '>'
3789 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003790 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003791 {
3792 next = next_line_from_context(cctx, TRUE);
3793 if (next == NULL)
3794 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003795 *arg = next;
3796 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003797 }
3798 }
3799
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003800 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003801 // not a function call.
3802 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003804 garray_T *stack = &cctx->ctx_type_stack;
3805 type_T *type;
3806 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003808 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003809 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003810 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003813 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3814
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003815 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3817 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003818 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 return FAIL;
3820 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003821 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003823 char_u *pstart = p;
3824
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003825 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003826 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003827 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 // something->method()
3830 // Apply the '!', '-' and '+' first:
3831 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003832 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003835 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003836 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003837 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003838 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003839 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003840 int argcount = 1;
3841 char_u *expr;
3842 garray_T *stack;
3843 type_T *type;
3844
3845 // Funcref call: list->(Refs[2])(arg)
3846 // or lambda: list->((arg) => expr)(arg)
3847 // Fist compile the arguments.
3848 expr = *arg;
3849 *arg = skipwhite(*arg + 1);
3850 skip_expr_cctx(arg, cctx);
3851 *arg = skipwhite(*arg);
3852 if (**arg != ')')
3853 {
3854 semsg(_(e_missing_paren), *arg);
3855 return FAIL;
3856 }
3857 ++*arg;
3858 if (**arg != '(')
3859 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003860 if (*skipwhite(*arg) == '(')
3861 emsg(_(e_nowhitespace));
3862 else
3863 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003864 return FAIL;
3865 }
3866
3867 *arg = skipwhite(*arg + 1);
3868 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3869 return FAIL;
3870
3871 // Compile the function expression.
3872 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3873 return FAIL;
3874
3875 stack = &cctx->ctx_type_stack;
3876 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3877 if (generate_PCALL(cctx, argcount,
3878 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 return FAIL;
3880 }
3881 else
3882 {
3883 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003884 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003885 if (!eval_isnamec1(*p))
3886 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003887 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003888 return FAIL;
3889 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003890 if (ASCII_ISALPHA(*p) && p[1] == ':')
3891 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003892 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 ;
3894 if (*p != '(')
3895 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003896 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 return FAIL;
3898 }
3899 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003900 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 return FAIL;
3902 }
3903 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003904 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003906 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003907 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003908 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003909 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003910 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003911
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003912 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003913 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003914 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003915 // TODO: blob index
3916 // TODO: more arguments
3917 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003918 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003919 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003920 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003921
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003922 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003923 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003924 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003925 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003926 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003927 // missing first index is equal to zero
3928 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003929 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003930 else
3931 {
3932 if (compile_expr0(arg, cctx) == FAIL)
3933 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003934 if (**arg == ':')
3935 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003936 semsg(_(e_white_space_required_before_and_after_str_at_str),
3937 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003938 return FAIL;
3939 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003940 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003941 return FAIL;
3942 *arg = skipwhite(*arg);
3943 }
3944 if (**arg == ':')
3945 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003946 is_slice = TRUE;
3947 ++*arg;
3948 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3949 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003950 semsg(_(e_white_space_required_before_and_after_str_at_str),
3951 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003952 return FAIL;
3953 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003954 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003955 return FAIL;
3956 if (**arg == ']')
3957 // missing second index is equal to end of string
3958 generate_PUSHNR(cctx, -1);
3959 else
3960 {
3961 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003962 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003963 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003964 return FAIL;
3965 *arg = skipwhite(*arg);
3966 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968
3969 if (**arg != ']')
3970 {
3971 emsg(_(e_missbrac));
3972 return FAIL;
3973 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003974 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003976 // We can index a list and a dict. If we don't know the type
3977 // we can use the index value type.
3978 // TODO: If we don't know use an instruction to figure it out at
3979 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003980 typep = ((type_T **)stack->ga_data) + stack->ga_len
3981 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003982 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003983 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3984 // If the index is a string, the variable must be a Dict.
3985 if (*typep == &t_any && valtype == &t_string)
3986 vtype = VAR_DICT;
3987 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003988 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003989 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003990 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003991 return FAIL;
3992 if (is_slice)
3993 {
3994 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003995 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003996 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003997 return FAIL;
3998 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003999 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02004000
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004001 if (vtype == VAR_DICT)
4002 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004003 if (is_slice)
4004 {
4005 emsg(_(e_cannot_slice_dictionary));
4006 return FAIL;
4007 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004008 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004009 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004010 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004011 if (*typep == &t_unknown)
4012 // empty dict was used
4013 *typep = &t_any;
4014 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02004015 else
4016 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004017 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004018 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02004019 return FAIL;
4020 *typep = &t_any;
4021 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004022 if (may_generate_2STRING(-1, cctx) == FAIL)
4023 return FAIL;
4024 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
4025 return FAIL;
4026 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004027 else if (vtype == VAR_STRING)
4028 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004029 *typep = &t_string;
4030 if ((is_slice
4031 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
4032 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004033 return FAIL;
4034 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004035 else if (vtype == VAR_BLOB)
4036 {
4037 emsg("Sorry, blob index and slice not implemented yet");
4038 return FAIL;
4039 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02004040 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01004041 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004042 if (is_slice)
4043 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004044 if (generate_instr_drop(cctx,
4045 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
4046 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004047 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004048 }
Bram Moolenaared591872020-08-15 22:14:53 +02004049 else
4050 {
4051 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004052 {
Bram Moolenaared591872020-08-15 22:14:53 +02004053 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01004054 if (*typep == &t_unknown)
4055 // empty list was used
4056 *typep = &t_any;
4057 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004058 if (generate_instr_drop(cctx,
4059 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
4060 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02004061 return FAIL;
4062 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004063 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004064 else
4065 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02004066 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01004067 return FAIL;
4068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004070 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004072 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004074 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004075 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004076
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02004077 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004078 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004079 {
4080 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004081 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02004082 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02004083 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004084 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 while (eval_isnamec(*p))
4086 MB_PTR_ADV(p);
4087 if (p == *arg)
4088 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004089 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 return FAIL;
4091 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004092 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 return FAIL;
4094 *arg = p;
4095 }
4096 else
4097 break;
4098 }
4099
4100 // TODO - see handle_subscript():
4101 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4102 // Don't do this when "Func" is already a partial that was bound
4103 // explicitly (pt_auto is FALSE).
4104
4105 return OK;
4106}
4107
4108/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004109 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4110 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004112 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004113 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004114 *
4115 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 */
4117
4118/*
4119 * number number constant
4120 * 0zFFFFFFFF Blob constant
4121 * "string" string constant
4122 * 'string' literal string constant
4123 * &option-name option value
4124 * @r register contents
4125 * identifier variable value
4126 * function() function call
4127 * $VAR environment variable
4128 * (expression) nested expression
4129 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004130 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 *
4132 * Also handle:
4133 * ! in front logical NOT
4134 * - in front unary minus
4135 * + in front unary plus (ignored)
4136 * trailing (arg) funcref/partial call
4137 * trailing [] subscript in String or List
4138 * trailing .name entry in Dictionary
4139 * trailing ->name() method call
4140 */
4141 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004142compile_expr7(
4143 char_u **arg,
4144 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004145 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 char_u *start_leader, *end_leader;
4148 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004149 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004150 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004152 ppconst->pp_is_const = FALSE;
4153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 /*
4155 * Skip '!', '-' and '+' characters. They are handled later.
4156 */
4157 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004158 if (eval_leader(arg, TRUE) == FAIL)
4159 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 end_leader = *arg;
4161
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004162 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 switch (**arg)
4164 {
4165 /*
4166 * Number constant.
4167 */
4168 case '0': // also for blob starting with 0z
4169 case '1':
4170 case '2':
4171 case '3':
4172 case '4':
4173 case '5':
4174 case '6':
4175 case '7':
4176 case '8':
4177 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004178 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004180 // Apply "-" and "+" just before the number now, right to
4181 // left. Matters especially when "->" follows. Stops at
4182 // '!'.
4183 if (apply_leader(rettv, TRUE,
4184 start_leader, &end_leader) == FAIL)
4185 {
4186 clear_tv(rettv);
4187 return FAIL;
4188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 break;
4190
4191 /*
4192 * String constant: "string".
4193 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004194 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 return FAIL;
4196 break;
4197
4198 /*
4199 * Literal string constant: 'str''ing'.
4200 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004201 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 return FAIL;
4203 break;
4204
4205 /*
4206 * Constant Vim variable.
4207 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004208 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 ret = NOTDONE;
4210 break;
4211
4212 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004213 * "true" constant
4214 */
4215 case 't': if (STRNCMP(*arg, "true", 4) == 0
4216 && !eval_isnamec((*arg)[4]))
4217 {
4218 *arg += 4;
4219 rettv->v_type = VAR_BOOL;
4220 rettv->vval.v_number = VVAL_TRUE;
4221 }
4222 else
4223 ret = NOTDONE;
4224 break;
4225
4226 /*
4227 * "false" constant
4228 */
4229 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4230 && !eval_isnamec((*arg)[5]))
4231 {
4232 *arg += 5;
4233 rettv->v_type = VAR_BOOL;
4234 rettv->vval.v_number = VVAL_FALSE;
4235 }
4236 else
4237 ret = NOTDONE;
4238 break;
4239
4240 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004241 * "null" constant
4242 */
4243 case 'n': if (STRNCMP(*arg, "null", 4) == 0
Bram Moolenaarc23555d2021-03-10 19:04:07 +01004244 && !eval_isnamec((*arg)[4]))
Bram Moolenaar67977822021-01-03 21:53:53 +01004245 {
4246 *arg += 4;
4247 rettv->v_type = VAR_SPECIAL;
4248 rettv->vval.v_number = VVAL_NULL;
4249 }
4250 else
4251 ret = NOTDONE;
4252 break;
4253
4254 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 * List: [expr, expr]
4256 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004257 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4258 return FAIL;
4259 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261
4262 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004263 * Dictionary: {'key': val, 'key': val}
4264 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004265 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4266 return FAIL;
4267 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 break;
4269
4270 /*
4271 * Option value: &name
4272 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004273 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4274 return FAIL;
4275 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 break;
4277
4278 /*
4279 * Environment variable: $VAR.
4280 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004281 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4282 return FAIL;
4283 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 break;
4285
4286 /*
4287 * Register contents: @r.
4288 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004289 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4290 return FAIL;
4291 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 break;
4293 /*
4294 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004295 * lambda: (arg, arg) => expr
4296 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004298 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4299 ret = compile_lambda(arg, cctx);
4300 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004301 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 break;
4303
4304 default: ret = NOTDONE;
4305 break;
4306 }
4307 if (ret == FAIL)
4308 return FAIL;
4309
Bram Moolenaar1c747212020-05-09 18:28:34 +02004310 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004312 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004313 clear_tv(rettv);
4314 else
4315 // A constant expression can possibly be handled compile time,
4316 // return the value instead of generating code.
4317 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319 else if (ret == NOTDONE)
4320 {
4321 char_u *p;
4322 int r;
4323
4324 if (!eval_isnamec1(**arg))
4325 {
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01004326 if (!vim9_bad_comment(*arg))
4327 {
4328 if (ends_excmd(*skipwhite(*arg)))
4329 semsg(_(e_empty_expression_str), *arg);
4330 else
4331 semsg(_(e_name_expected_str), *arg);
4332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 return FAIL;
4334 }
4335
4336 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004337 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004339 {
4340 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004343 {
4344 if (generate_ppconst(cctx, ppconst) == FAIL)
4345 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004346 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 if (r == FAIL)
4349 return FAIL;
4350 }
4351
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004352 // Handle following "[]", ".member", etc.
4353 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004354 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004355 ppconst) == FAIL)
4356 return FAIL;
4357 if (ppconst->pp_used > 0)
4358 {
4359 // apply the '!', '-' and '+' before the constant
4360 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004361 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004362 return FAIL;
4363 return OK;
4364 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004365 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004367 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368}
4369
4370/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004371 * Give the "white on both sides" error, taking the operator from "p[len]".
4372 */
4373 void
4374error_white_both(char_u *op, int len)
4375{
4376 char_u buf[10];
4377
4378 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004379 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004380}
4381
4382/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004383 * <type>expr7: runtime type check / conversion
4384 */
4385 static int
4386compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4387{
4388 type_T *want_type = NULL;
4389
4390 // Recognize <type>
4391 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4392 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004393 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004394 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4395 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004396 return FAIL;
4397
4398 if (**arg != '>')
4399 {
4400 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004401 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004402 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004403 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004404 return FAIL;
4405 }
4406 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004407 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004408 return FAIL;
4409 }
4410
4411 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4412 return FAIL;
4413
4414 if (want_type != NULL)
4415 {
4416 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004417 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004418 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004419
Bram Moolenaard1103582020-08-14 22:44:25 +02004420 generate_ppconst(cctx, ppconst);
4421 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004422 where.wt_index = 0;
4423 where.wt_variable = FALSE;
4424 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004425 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004426 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004427 return FAIL;
4428 }
4429 }
4430
4431 return OK;
4432}
4433
4434/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 * * number multiplication
4436 * / number division
4437 * % number modulo
4438 */
4439 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441{
4442 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004443 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004444 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004446 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004447 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 return FAIL;
4449
4450 /*
4451 * Repeat computing, until no "*", "/" or "%" is following.
4452 */
4453 for (;;)
4454 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004455 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 if (*op != '*' && *op != '/' && *op != '%')
4457 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004458 if (next != NULL)
4459 {
4460 *arg = next_line_from_context(cctx, TRUE);
4461 op = skipwhite(*arg);
4462 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004463
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004464 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004466 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004467 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004469 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004470 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004472 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004473 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004475
4476 if (ppconst->pp_used == ppconst_used + 2
4477 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4478 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004479 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004480 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4481 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4482 varnumber_T res = 0;
4483 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004485 // both are numbers: compute the result
4486 switch (*op)
4487 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004488 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004489 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004490 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004491 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004492 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004493 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004494 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004495 break;
4496 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004497 if (failed)
4498 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004499 tv1->vval.v_number = res;
4500 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004501 }
4502 else
4503 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004504 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004505 generate_two_op(cctx, op);
4506 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 }
4508
4509 return OK;
4510}
4511
4512/*
Bram Moolenaard345fb92021-03-10 18:43:09 +01004513 * + number addition or list/blobl concatenation
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 * - number subtraction
4515 * .. string concatenation
4516 */
4517 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004518compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519{
4520 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004521 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004523 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524
4525 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return FAIL;
4528
4529 /*
4530 * Repeat computing, until no "+", "-" or ".." is following.
4531 */
4532 for (;;)
4533 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004534 op = may_peek_next_line(cctx, *arg, &next);
4535 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536 break;
4537 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004538 if (next != NULL)
4539 {
4540 *arg = next_line_from_context(cctx, TRUE);
4541 op = skipwhite(*arg);
4542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004544 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004546 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004547 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 }
4549
Bram Moolenaare0de1712020-12-02 17:36:54 +01004550 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004553 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 return FAIL;
4556
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004558 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4560 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4561 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4562 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4565 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004566
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004567 // concat/subtract/add constant numbers
4568 if (*op == '+')
4569 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4570 else if (*op == '-')
4571 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4572 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004573 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004574 // concatenate constant strings
4575 char_u *s1 = tv1->vval.v_string;
4576 char_u *s2 = tv2->vval.v_string;
4577 size_t len1 = STRLEN(s1);
4578
4579 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4580 if (tv1->vval.v_string == NULL)
4581 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004583 return FAIL;
4584 }
4585 mch_memmove(tv1->vval.v_string, s1, len1);
4586 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004587 vim_free(s1);
4588 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004589 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
4592 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004593 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004594 generate_ppconst(cctx, ppconst);
Bram Moolenaard345fb92021-03-10 18:43:09 +01004595 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004596 if (*op == '.')
4597 {
4598 if (may_generate_2STRING(-2, cctx) == FAIL
4599 || may_generate_2STRING(-1, cctx) == FAIL)
4600 return FAIL;
4601 generate_instr_drop(cctx, ISN_CONCAT, 1);
4602 }
4603 else
4604 generate_two_op(cctx, op);
4605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * expr5a == expr5b
4613 * expr5a =~ expr5b
4614 * expr5a != expr5b
4615 * expr5a !~ expr5b
4616 * expr5a > expr5b
4617 * expr5a >= expr5b
4618 * expr5a < expr5b
4619 * expr5a <= expr5b
4620 * expr5a is expr5b
4621 * expr5a isnot expr5b
4622 *
4623 * Produces instructions:
4624 * EVAL expr5a Push result of "expr5a"
4625 * EVAL expr5b Push result of "expr5b"
4626 * COMPARE one of the compare instructions
4627 */
4628 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004629compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004631 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004633 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
4638 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 return FAIL;
4641
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004642 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004643 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
4645 /*
4646 * If there is a comparative operator, use it.
4647 */
4648 if (type != EXPR_UNKNOWN)
4649 {
4650 int ic = FALSE; // Default: do not ignore case
4651
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004652 if (next != NULL)
4653 {
4654 *arg = next_line_from_context(cctx, TRUE);
4655 p = skipwhite(*arg);
4656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 if (type_is && (p[len] == '?' || p[len] == '#'))
4658 {
4659 semsg(_(e_invexpr2), *arg);
4660 return FAIL;
4661 }
4662 // extra question mark appended: ignore case
4663 if (p[len] == '?')
4664 {
4665 ic = TRUE;
4666 ++len;
4667 }
4668 // extra '#' appended: match case (ignored)
4669 else if (p[len] == '#')
4670 ++len;
4671 // nothing appended: match case
4672
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004673 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004675 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
4678
4679 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004680 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004681 return FAIL;
4682
Bram Moolenaara5565e42020-05-09 15:44:01 +02004683 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 return FAIL;
4685
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686 if (ppconst->pp_used == ppconst_used + 2)
4687 {
4688 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4689 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4690 int ret;
4691
4692 // Both sides are a constant, compute the result now.
4693 // First check for a valid combination of types, this is more
4694 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004695 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 ret = FAIL;
4697 else
4698 {
4699 ret = typval_compare(tv1, tv2, type, ic);
4700 tv1->v_type = VAR_BOOL;
4701 tv1->vval.v_number = tv1->vval.v_number
4702 ? VVAL_TRUE : VVAL_FALSE;
4703 clear_tv(tv2);
4704 --ppconst->pp_used;
4705 }
4706 return ret;
4707 }
4708
4709 generate_ppconst(cctx, ppconst);
4710 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 }
4712
4713 return OK;
4714}
4715
Bram Moolenaar7f141552020-05-09 17:35:53 +02004716static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718/*
4719 * Compile || or &&.
4720 */
4721 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004722compile_and_or(
4723 char_u **arg,
4724 cctx_T *cctx,
4725 char *op,
4726 ppconst_T *ppconst,
4727 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004729 char_u *next;
4730 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 int opchar = *op;
4732
4733 if (p[0] == opchar && p[1] == opchar)
4734 {
4735 garray_T *instr = &cctx->ctx_instr;
4736 garray_T end_ga;
4737
4738 /*
4739 * Repeat until there is no following "||" or "&&"
4740 */
4741 ga_init2(&end_ga, sizeof(int), 10);
4742 while (p[0] == opchar && p[1] == opchar)
4743 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004744 if (next != NULL)
4745 {
4746 *arg = next_line_from_context(cctx, TRUE);
4747 p = skipwhite(*arg);
4748 }
4749
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004750 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4751 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004752 semsg(_(e_white_space_required_before_and_after_str_at_str),
4753 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004754 return FAIL;
4755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004757 // TODO: use ppconst if the value is a constant and check
4758 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004759 generate_ppconst(cctx, ppconst);
4760
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004761 // Every part must evaluate to a bool.
4762 if (bool_on_stack(cctx) == FAIL)
4763 {
4764 ga_clear(&end_ga);
4765 return FAIL;
4766 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768 if (ga_grow(&end_ga, 1) == FAIL)
4769 {
4770 ga_clear(&end_ga);
4771 return FAIL;
4772 }
4773 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4774 ++end_ga.ga_len;
4775 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004776 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777
4778 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004779 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004780 {
4781 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004782 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004783 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004784
Bram Moolenaara5565e42020-05-09 15:44:01 +02004785 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4786 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 {
4788 ga_clear(&end_ga);
4789 return FAIL;
4790 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004791
4792 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004794 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004796 // Every part must evaluate to a bool.
4797 if (bool_on_stack(cctx) == FAIL)
4798 {
4799 ga_clear(&end_ga);
4800 return FAIL;
4801 }
4802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803 // Fill in the end label in all jumps.
4804 while (end_ga.ga_len > 0)
4805 {
4806 isn_T *isn;
4807
4808 --end_ga.ga_len;
4809 isn = ((isn_T *)instr->ga_data)
4810 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4811 isn->isn_arg.jump.jump_where = instr->ga_len;
4812 }
4813 ga_clear(&end_ga);
4814 }
4815
4816 return OK;
4817}
4818
4819/*
4820 * expr4a && expr4a && expr4a logical AND
4821 *
4822 * Produces instructions:
4823 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004824 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004825 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004827 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 * EVAL expr4c Push result of "expr4c"
4829 * end:
4830 */
4831 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004832compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 int ppconst_used = ppconst->pp_used;
4835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 return FAIL;
4839
4840 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842}
4843
4844/*
4845 * expr3a || expr3b || expr3c logical OR
4846 *
4847 * Produces instructions:
4848 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004849 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004850 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004852 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 * EVAL expr3c Push result of "expr3c"
4854 * end:
4855 */
4856 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004857compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004859 int ppconst_used = ppconst->pp_used;
4860
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004862 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 return FAIL;
4864
4865 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867}
4868
4869/*
4870 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004872 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 * JUMP_IF_FALSE alt jump if false
4874 * EVAL expr1a
4875 * JUMP_ALWAYS end
4876 * alt: EVAL expr1b
4877 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004878 *
4879 * Toplevel expression: expr2 ?? expr1
4880 * Produces instructions:
4881 * EVAL expr2 Push result of "expr2"
4882 * JUMP_AND_KEEP_IF_TRUE end jump if true
4883 * EVAL expr1
4884 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 */
4886 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004887compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888{
4889 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004891 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
Bram Moolenaar3988f642020-08-27 22:43:03 +02004893 // Ignore all kinds of errors when not producing code.
4894 if (cctx->ctx_skip == SKIP_YES)
4895 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004896 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004897 return OK;
4898 }
4899
Bram Moolenaar61a89812020-05-07 16:58:17 +02004900 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 return FAIL;
4903
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004904 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 if (*p == '?')
4906 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004907 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 garray_T *instr = &cctx->ctx_instr;
4909 garray_T *stack = &cctx->ctx_type_stack;
4910 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004911 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004913 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004914 int has_const_expr = FALSE;
4915 int const_value = FALSE;
4916 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004918 if (next != NULL)
4919 {
4920 *arg = next_line_from_context(cctx, TRUE);
4921 p = skipwhite(*arg);
4922 }
4923
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004924 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004925 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004926 semsg(_(e_white_space_required_before_and_after_str_at_str),
4927 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004928 return FAIL;
4929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931 if (ppconst->pp_used == ppconst_used + 1)
4932 {
4933 // the condition is a constant, we know whether the ? or the :
4934 // expression is to be evaluated.
4935 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004936 if (op_falsy)
4937 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4938 else
4939 {
4940 int error = FALSE;
4941
4942 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4943 &error);
4944 if (error)
4945 return FAIL;
4946 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004947 cctx->ctx_skip = save_skip == SKIP_YES ||
4948 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4949
4950 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4951 // "left ?? right" and "left" is truthy: produce "left"
4952 generate_ppconst(cctx, ppconst);
4953 else
4954 {
4955 clear_tv(&ppconst->pp_tv[ppconst_used]);
4956 --ppconst->pp_used;
4957 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004958 }
4959 else
4960 {
4961 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004962 if (op_falsy)
4963 end_idx = instr->ga_len;
4964 generate_JUMP(cctx, op_falsy
4965 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4966 if (op_falsy)
4967 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969
4970 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004971 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004972 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004973 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004974 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975
Bram Moolenaara5565e42020-05-09 15:44:01 +02004976 if (!has_const_expr)
4977 {
4978 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004980 if (!op_falsy)
4981 {
4982 // remember the type and drop it
4983 --stack->ga_len;
4984 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004986 end_idx = instr->ga_len;
4987 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004988
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004989 // jump here from JUMP_IF_FALSE
4990 isn = ((isn_T *)instr->ga_data) + alt_idx;
4991 isn->isn_arg.jump.jump_where = instr->ga_len;
4992 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004995 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004997 // Check for the ":".
4998 p = may_peek_next_line(cctx, *arg, &next);
4999 if (*p != ':')
5000 {
5001 emsg(_(e_missing_colon));
5002 return FAIL;
5003 }
5004 if (next != NULL)
5005 {
5006 *arg = next_line_from_context(cctx, TRUE);
5007 p = skipwhite(*arg);
5008 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02005009
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005010 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
5011 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01005012 semsg(_(e_white_space_required_before_and_after_str_at_str),
5013 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005014 return FAIL;
5015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005017 // evaluate the third expression
5018 if (has_const_expr)
5019 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005020 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01005021 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005022 return FAIL;
5023 if (compile_expr1(arg, cctx, ppconst) == FAIL)
5024 return FAIL;
5025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026
Bram Moolenaara5565e42020-05-09 15:44:01 +02005027 if (!has_const_expr)
5028 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005029 type_T **typep;
5030
Bram Moolenaara5565e42020-05-09 15:44:01 +02005031 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032
Bram Moolenaara5565e42020-05-09 15:44:01 +02005033 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005034 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
5035 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02005036
Bram Moolenaar92f26c22020-10-03 20:17:30 +02005037 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02005038 isn = ((isn_T *)instr->ga_data) + end_idx;
5039 isn->isn_arg.jump.jump_where = instr->ga_len;
5040 }
5041
5042 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 }
5044 return OK;
5045}
5046
5047/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02005048 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005049 * Sets "is_const" (if not NULL) to indicate the value is a constant.
5050 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02005051 */
5052 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005053compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02005054{
5055 ppconst_T ppconst;
5056
5057 CLEAR_FIELD(ppconst);
5058 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
5059 {
5060 clear_ppconst(&ppconst);
5061 return FAIL;
5062 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005063 if (is_const != NULL)
5064 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02005065 if (generate_ppconst(cctx, &ppconst) == FAIL)
5066 return FAIL;
5067 return OK;
5068}
5069
5070/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005071 * Toplevel expression.
5072 */
5073 static int
5074compile_expr0(char_u **arg, cctx_T *cctx)
5075{
5076 return compile_expr0_ext(arg, cctx, NULL);
5077}
5078
5079/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 * compile "return [expr]"
5081 */
5082 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005083compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084{
5085 char_u *p = arg;
5086 garray_T *stack = &cctx->ctx_type_stack;
5087 type_T *stack_type;
5088
5089 if (*p != NUL && *p != '|' && *p != '\n')
5090 {
5091 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02005092 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 return NULL;
5094
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005095 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005096 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005097 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005098 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005099 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5100 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005101 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005102 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005103 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005104 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005105 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005106 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5107 && stack_type->tt_type != VAR_VOID
5108 && stack_type->tt_type != VAR_UNKNOWN)
5109 {
5110 emsg(_(e_returning_value_in_function_without_return_type));
5111 return NULL;
5112 }
5113 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005114 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005115 return NULL;
5116 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 }
5119 else
5120 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005121 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005122 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005123 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5124 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005126 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 return NULL;
5128 }
5129
5130 // No argument, return zero.
5131 generate_PUSHNR(cctx, 0);
5132 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005133
5134 // Undo any command modifiers.
5135 generate_undo_cmdmods(cctx);
5136
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005137 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 return NULL;
5139
5140 // "return val | endif" is possible
5141 return skipwhite(p);
5142}
5143
5144/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005145 * Get a line from the compilation context, compatible with exarg_T getline().
5146 * Return a pointer to the line in allocated memory.
5147 * Return NULL for end-of-file or some error.
5148 */
5149 static char_u *
5150exarg_getline(
5151 int c UNUSED,
5152 void *cookie,
5153 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005154 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005155{
5156 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005157 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005158
Bram Moolenaar66250c92020-08-20 15:02:42 +02005159 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005160 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005161 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005162 return NULL;
5163 ++cctx->ctx_lnum;
5164 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5165 // Comment lines result in NULL pointers, skip them.
5166 if (p != NULL)
5167 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005168 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005169}
5170
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005171 void
5172fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
5173{
5174 eap->getline = exarg_getline;
5175 eap->cookie = cctx;
5176}
5177
Bram Moolenaar04b12692020-05-04 23:24:44 +02005178/*
5179 * Compile a nested :def command.
5180 */
5181 static char_u *
5182compile_nested_function(exarg_T *eap, cctx_T *cctx)
5183{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005184 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005185 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005186 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005187 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005188 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005189 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005190
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005191 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005192 {
5193 emsg(_(e_cannot_use_bang_with_nested_def));
5194 return NULL;
5195 }
5196
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005197 if (*name_start == '/')
5198 {
5199 name_end = skip_regexp(name_start + 1, '/', TRUE);
5200 if (*name_end == '/')
5201 ++name_end;
5202 eap->nextcmd = check_nextcmd(name_end);
5203 }
5204 if (name_end == name_start || *skipwhite(name_end) != '(')
5205 {
5206 if (!ends_excmd2(name_start, name_end))
5207 {
5208 semsg(_(e_invalid_command_str), eap->cmd);
5209 return NULL;
5210 }
5211
5212 // "def" or "def Name": list functions
5213 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5214 return NULL;
5215 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5216 }
5217
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005218 // Only g:Func() can use a namespace.
5219 if (name_start[1] == ':' && !is_global)
5220 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005221 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005222 return NULL;
5223 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005224 if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02005225 return NULL;
5226
Bram Moolenaar04b12692020-05-04 23:24:44 +02005227 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005228 fill_exarg_from_cctx(eap, cctx);
5229
Bram Moolenaar04b12692020-05-04 23:24:44 +02005230 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005231 lambda_name = vim_strsave(get_lambda_name());
5232 if (lambda_name == NULL)
5233 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005234 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005235
Bram Moolenaar822ba242020-05-24 23:00:18 +02005236 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005237 {
5238 r = eap->skip ? OK : FAIL;
5239 goto theend;
5240 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01005241
5242 // copy over the block scope IDs before compiling
5243 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
5244 {
5245 int block_depth = cctx->ctx_ufunc->uf_block_depth;
5246
5247 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5248 if (ufunc->uf_block_ids != NULL)
5249 {
5250 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5251 sizeof(int) * block_depth);
5252 ufunc->uf_block_depth = block_depth;
5253 }
5254 }
5255
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005256 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5257 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005258 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005259 {
5260 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005261 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005262 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005263
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005264 if (is_global)
5265 {
5266 char_u *func_name = vim_strnsave(name_start + 2,
5267 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005268
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005269 if (func_name == NULL)
5270 r = FAIL;
5271 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005272 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005273 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005274 lambda_name = NULL;
5275 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005276 }
5277 else
5278 {
5279 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005280 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005281 TRUE, ufunc->uf_func_type);
Bram Moolenaare8211a32020-10-09 22:04:29 +02005282
Bram Moolenaareef21022020-08-01 22:16:43 +02005283 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005284 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005285 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005286 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005287 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
5288 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005289 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005290
5291theend:
5292 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005293 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005294}
5295
5296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 * Return the length of an assignment operator, or zero if there isn't one.
5298 */
5299 int
5300assignment_len(char_u *p, int *heredoc)
5301{
5302 if (*p == '=')
5303 {
5304 if (p[1] == '<' && p[2] == '<')
5305 {
5306 *heredoc = TRUE;
5307 return 3;
5308 }
5309 return 1;
5310 }
5311 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5312 return 2;
5313 if (STRNCMP(p, "..=", 3) == 0)
5314 return 3;
5315 return 0;
5316}
5317
5318// words that cannot be used as a variable
5319static char *reserved[] = {
5320 "true",
5321 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005322 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 NULL
5324};
5325
Bram Moolenaar752fc692021-01-04 21:57:11 +01005326// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005327typedef enum {
5328 dest_local,
5329 dest_option,
5330 dest_env,
5331 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005332 dest_buffer,
5333 dest_window,
5334 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005335 dest_vimvar,
5336 dest_script,
5337 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005338 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005339} assign_dest_T;
5340
Bram Moolenaar752fc692021-01-04 21:57:11 +01005341// Used by compile_lhs() to store information about the LHS of an assignment
5342// and one argument of ":unlet" with an index.
5343typedef struct {
5344 assign_dest_T lhs_dest; // type of destination
5345
5346 char_u *lhs_name; // allocated name including
5347 // "[expr]" or ".name".
5348 size_t lhs_varlen; // length of the variable without
5349 // "[expr]" or ".name"
5350 char_u *lhs_dest_end; // end of the destination, including
5351 // "[expr]" or ".name".
5352
5353 int lhs_has_index; // has "[expr]" or ".name"
5354
5355 int lhs_new_local; // create new local variable
5356 int lhs_opt_flags; // for when destination is an option
5357 int lhs_vimvaridx; // for when destination is a v:var
5358
5359 lvar_T lhs_local_lvar; // used for existing local destination
5360 lvar_T lhs_arg_lvar; // used for argument destination
5361 lvar_T *lhs_lvar; // points to destination lvar
5362 int lhs_scriptvar_sid;
5363 int lhs_scriptvar_idx;
5364
5365 int lhs_has_type; // type was specified
5366 type_T *lhs_type;
5367 type_T *lhs_member_type;
5368} lhs_T;
5369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005371 * Generate the load instruction for "name".
5372 */
5373 static void
5374generate_loadvar(
5375 cctx_T *cctx,
5376 assign_dest_T dest,
5377 char_u *name,
5378 lvar_T *lvar,
5379 type_T *type)
5380{
5381 switch (dest)
5382 {
5383 case dest_option:
5384 // TODO: check the option exists
5385 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5386 break;
5387 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005388 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5389 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5390 else
5391 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005392 break;
5393 case dest_buffer:
5394 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5395 break;
5396 case dest_window:
5397 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5398 break;
5399 case dest_tab:
5400 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5401 break;
5402 case dest_script:
5403 compile_load_scriptvar(cctx,
5404 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5405 break;
5406 case dest_env:
5407 // Include $ in the name here
5408 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5409 break;
5410 case dest_reg:
5411 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5412 break;
5413 case dest_vimvar:
5414 generate_LOADV(cctx, name + 2, TRUE);
5415 break;
5416 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005417 if (lvar->lv_from_outer > 0)
5418 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5419 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005420 else
5421 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5422 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005423 case dest_expr:
5424 // list or dict value should already be on the stack.
5425 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 }
5427}
5428
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005429/*
5430 * Skip over "[expr]" or ".member".
5431 * Does not check for any errors.
5432 */
5433 static char_u *
5434skip_index(char_u *start)
5435{
5436 char_u *p = start;
5437
5438 if (*p == '[')
5439 {
5440 p = skipwhite(p + 1);
5441 (void)skip_expr(&p, NULL);
5442 p = skipwhite(p);
5443 if (*p == ']')
5444 return p + 1;
5445 return p;
5446 }
5447 // if (*p == '.')
5448 return to_name_end(p + 1, TRUE);
5449}
5450
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005451 void
5452vim9_declare_error(char_u *name)
5453{
5454 char *scope = "";
5455
5456 switch (*name)
5457 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005458 case 'g': scope = _("global"); break;
5459 case 'b': scope = _("buffer"); break;
5460 case 'w': scope = _("window"); break;
5461 case 't': scope = _("tab"); break;
5462 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005463 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005464 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005465 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005466 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005467 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005468 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005469 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005470 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005471 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005472}
5473
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005474/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005475 * For one assignment figure out the type of destination. Return it in "dest".
5476 * When not recognized "dest" is not set.
5477 * For an option "opt_flags" is set.
5478 * For a v:var "vimvaridx" is set.
5479 * "type" is set to the destination type if known, unchanted otherwise.
5480 * Return FAIL if an error message was given.
5481 */
5482 static int
5483get_var_dest(
5484 char_u *name,
5485 assign_dest_T *dest,
5486 int cmdidx,
5487 int *opt_flags,
5488 int *vimvaridx,
5489 type_T **type,
5490 cctx_T *cctx)
5491{
5492 char_u *p;
5493
5494 if (*name == '&')
5495 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005496 int cc;
5497 long numval;
5498 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005499
5500 *dest = dest_option;
5501 if (cmdidx == CMD_final || cmdidx == CMD_const)
5502 {
5503 emsg(_(e_const_option));
5504 return FAIL;
5505 }
5506 p = name;
5507 p = find_option_end(&p, opt_flags);
5508 if (p == NULL)
5509 {
5510 // cannot happen?
5511 emsg(_(e_letunexp));
5512 return FAIL;
5513 }
5514 cc = *p;
5515 *p = NUL;
5516 opt_type = get_option_value(skip_option_env_lead(name),
5517 &numval, NULL, *opt_flags);
5518 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005519 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005520 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005521 case gov_unknown:
5522 semsg(_(e_unknown_option), name);
5523 return FAIL;
5524 case gov_string:
5525 case gov_hidden_string:
5526 *type = &t_string;
5527 break;
5528 case gov_bool:
5529 case gov_hidden_bool:
5530 *type = &t_bool;
5531 break;
5532 case gov_number:
5533 case gov_hidden_number:
5534 *type = &t_number;
5535 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005536 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005537 }
5538 else if (*name == '$')
5539 {
5540 *dest = dest_env;
5541 *type = &t_string;
5542 }
5543 else if (*name == '@')
5544 {
5545 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5546 {
5547 emsg_invreg(name[1]);
5548 return FAIL;
5549 }
5550 *dest = dest_reg;
5551 *type = &t_string;
5552 }
5553 else if (STRNCMP(name, "g:", 2) == 0)
5554 {
5555 *dest = dest_global;
5556 }
5557 else if (STRNCMP(name, "b:", 2) == 0)
5558 {
5559 *dest = dest_buffer;
5560 }
5561 else if (STRNCMP(name, "w:", 2) == 0)
5562 {
5563 *dest = dest_window;
5564 }
5565 else if (STRNCMP(name, "t:", 2) == 0)
5566 {
5567 *dest = dest_tab;
5568 }
5569 else if (STRNCMP(name, "v:", 2) == 0)
5570 {
5571 typval_T *vtv;
5572 int di_flags;
5573
5574 *vimvaridx = find_vim_var(name + 2, &di_flags);
5575 if (*vimvaridx < 0)
5576 {
5577 semsg(_(e_variable_not_found_str), name);
5578 return FAIL;
5579 }
5580 // We use the current value of "sandbox" here, is that OK?
5581 if (var_check_ro(di_flags, name, FALSE))
5582 return FAIL;
5583 *dest = dest_vimvar;
5584 vtv = get_vim_var_tv(*vimvaridx);
5585 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5586 }
5587 return OK;
5588}
5589
5590/*
5591 * Generate a STORE instruction for "dest", not being "dest_local".
5592 * Return FAIL when out of memory.
5593 */
5594 static int
5595generate_store_var(
5596 cctx_T *cctx,
5597 assign_dest_T dest,
5598 int opt_flags,
5599 int vimvaridx,
5600 int scriptvar_idx,
5601 int scriptvar_sid,
5602 type_T *type,
5603 char_u *name)
5604{
5605 switch (dest)
5606 {
5607 case dest_option:
5608 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5609 opt_flags);
5610 case dest_global:
5611 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005612 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5613 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005614 case dest_buffer:
5615 // include b: with the name, easier to execute that way
5616 return generate_STORE(cctx, ISN_STOREB, 0, name);
5617 case dest_window:
5618 // include w: with the name, easier to execute that way
5619 return generate_STORE(cctx, ISN_STOREW, 0, name);
5620 case dest_tab:
5621 // include t: with the name, easier to execute that way
5622 return generate_STORE(cctx, ISN_STORET, 0, name);
5623 case dest_env:
5624 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5625 case dest_reg:
5626 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5627 case dest_vimvar:
5628 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5629 case dest_script:
5630 if (scriptvar_idx < 0)
5631 {
5632 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005633 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005634
Bram Moolenaar41d61962020-12-06 20:12:43 +01005635 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005636 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5637 scriptvar_sid, type);
5638 if (name_s != name)
5639 vim_free(name_s);
5640 return r;
5641 }
5642 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5643 scriptvar_sid, scriptvar_idx, type);
5644 case dest_local:
5645 case dest_expr:
5646 // cannot happen
5647 break;
5648 }
5649 return FAIL;
5650}
5651
Bram Moolenaar752fc692021-01-04 21:57:11 +01005652 static int
5653is_decl_command(int cmdidx)
5654{
5655 return cmdidx == CMD_let || cmdidx == CMD_var
5656 || cmdidx == CMD_final || cmdidx == CMD_const;
5657}
5658
5659/*
5660 * Figure out the LHS type and other properties for an assignment or one item
5661 * of ":unlet" with an index.
5662 * Returns OK or FAIL.
5663 */
5664 static int
5665compile_lhs(
5666 char_u *var_start,
5667 lhs_T *lhs,
5668 int cmdidx,
5669 int heredoc,
5670 int oplen,
5671 cctx_T *cctx)
5672{
5673 char_u *var_end;
5674 int is_decl = is_decl_command(cmdidx);
5675
5676 CLEAR_POINTER(lhs);
5677 lhs->lhs_dest = dest_local;
5678 lhs->lhs_vimvaridx = -1;
5679 lhs->lhs_scriptvar_idx = -1;
5680
5681 // "dest_end" is the end of the destination, including "[expr]" or
5682 // ".name".
5683 // "var_end" is the end of the variable/option/etc. name.
5684 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5685 if (*var_start == '@')
5686 var_end = var_start + 2;
5687 else
5688 {
5689 // skip over the leading "&", "&l:", "&g:" and "$"
5690 var_end = skip_option_env_lead(var_start);
5691 var_end = to_name_end(var_end, TRUE);
5692 }
5693
5694 // "a: type" is declaring variable "a" with a type, not dict "a:".
5695 if (is_decl && lhs->lhs_dest_end == var_start + 2
5696 && lhs->lhs_dest_end[-1] == ':')
5697 --lhs->lhs_dest_end;
5698 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5699 --var_end;
5700
5701 // compute the length of the destination without "[expr]" or ".name"
5702 lhs->lhs_varlen = var_end - var_start;
5703 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5704 if (lhs->lhs_name == NULL)
5705 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005706
5707 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5708 // Something follows after the variable: "var[idx]" or "var.key".
5709 lhs->lhs_has_index = TRUE;
5710
Bram Moolenaar752fc692021-01-04 21:57:11 +01005711 if (heredoc)
5712 lhs->lhs_type = &t_list_string;
5713 else
5714 lhs->lhs_type = &t_any;
5715
5716 if (cctx->ctx_skip != SKIP_YES)
5717 {
5718 int declare_error = FALSE;
5719
5720 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5721 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5722 &lhs->lhs_type, cctx) == FAIL)
5723 return FAIL;
5724 if (lhs->lhs_dest != dest_local)
5725 {
5726 // Specific kind of variable recognized.
5727 declare_error = is_decl;
5728 }
5729 else
5730 {
5731 int idx;
5732
5733 // No specific kind of variable recognized, just a name.
5734 for (idx = 0; reserved[idx] != NULL; ++idx)
5735 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5736 {
5737 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5738 return FAIL;
5739 }
5740
5741
5742 if (lookup_local(var_start, lhs->lhs_varlen,
5743 &lhs->lhs_local_lvar, cctx) == OK)
5744 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5745 else
5746 {
5747 CLEAR_FIELD(lhs->lhs_arg_lvar);
5748 if (arg_exists(var_start, lhs->lhs_varlen,
5749 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5750 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5751 {
5752 if (is_decl)
5753 {
5754 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5755 return FAIL;
5756 }
5757 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5758 }
5759 }
5760 if (lhs->lhs_lvar != NULL)
5761 {
5762 if (is_decl)
5763 {
5764 semsg(_(e_variable_already_declared), lhs->lhs_name);
5765 return FAIL;
5766 }
5767 }
5768 else
5769 {
5770 int script_namespace = lhs->lhs_varlen > 1
5771 && STRNCMP(var_start, "s:", 2) == 0;
5772 int script_var = (script_namespace
5773 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5774 FALSE, cctx)
5775 : script_var_exists(var_start, lhs->lhs_varlen,
5776 TRUE, cctx)) == OK;
5777 imported_T *import =
5778 find_imported(var_start, lhs->lhs_varlen, cctx);
5779
5780 if (script_namespace || script_var || import != NULL)
5781 {
5782 char_u *rawname = lhs->lhs_name
5783 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5784
5785 if (is_decl)
5786 {
5787 if (script_namespace)
5788 semsg(_(e_cannot_declare_script_variable_in_function),
5789 lhs->lhs_name);
5790 else
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005791 semsg(_(e_variable_already_declared_in_script_str),
Bram Moolenaar752fc692021-01-04 21:57:11 +01005792 lhs->lhs_name);
5793 return FAIL;
5794 }
5795 else if (cctx->ctx_ufunc->uf_script_ctx_version
5796 == SCRIPT_VERSION_VIM9
5797 && script_namespace
5798 && !script_var && import == NULL)
5799 {
5800 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5801 return FAIL;
5802 }
5803
5804 lhs->lhs_dest = dest_script;
5805
5806 // existing script-local variables should have a type
5807 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5808 if (import != NULL)
5809 lhs->lhs_scriptvar_sid = import->imp_sid;
5810 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5811 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005812 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005813 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005814 lhs->lhs_scriptvar_sid, rawname,
5815 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5816 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005817 if (lhs->lhs_scriptvar_idx >= 0)
5818 {
5819 scriptitem_T *si = SCRIPT_ITEM(
5820 lhs->lhs_scriptvar_sid);
5821 svar_T *sv =
5822 ((svar_T *)si->sn_var_vals.ga_data)
5823 + lhs->lhs_scriptvar_idx;
5824 lhs->lhs_type = sv->sv_type;
5825 }
5826 }
5827 }
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005828 else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005829 == FAIL)
5830 return FAIL;
5831 }
5832 }
5833
5834 if (declare_error)
5835 {
5836 vim9_declare_error(lhs->lhs_name);
5837 return FAIL;
5838 }
5839 }
5840
5841 // handle "a:name" as a name, not index "name" on "a"
5842 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5843 var_end = lhs->lhs_dest_end;
5844
5845 if (lhs->lhs_dest != dest_option)
5846 {
5847 if (is_decl && *var_end == ':')
5848 {
5849 char_u *p;
5850
5851 // parse optional type: "let var: type = expr"
5852 if (!VIM_ISWHITE(var_end[1]))
5853 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005854 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005855 return FAIL;
5856 }
5857 p = skipwhite(var_end + 1);
5858 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5859 if (lhs->lhs_type == NULL)
5860 return FAIL;
5861 lhs->lhs_has_type = TRUE;
5862 }
5863 else if (lhs->lhs_lvar != NULL)
5864 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5865 }
5866
5867 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5868 && lhs->lhs_type->tt_type != VAR_STRING
5869 && lhs->lhs_type->tt_type != VAR_ANY)
5870 {
5871 emsg(_(e_can_only_concatenate_to_string));
5872 return FAIL;
5873 }
5874
5875 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5876 && cctx->ctx_skip != SKIP_YES)
5877 {
5878 if (oplen > 1 && !heredoc)
5879 {
5880 // +=, /=, etc. require an existing variable
5881 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5882 return FAIL;
5883 }
5884 if (!is_decl)
5885 {
5886 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5887 return FAIL;
5888 }
5889
Bram Moolenaar3f327882021-03-17 20:56:38 +01005890 // Check the name is valid for a funcref.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005891 if ((lhs->lhs_type->tt_type == VAR_FUNC
5892 || lhs->lhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01005893 && var_wrong_func_name(lhs->lhs_name, TRUE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01005894 return FAIL;
Bram Moolenaar3f327882021-03-17 20:56:38 +01005895
5896 // New local variable.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005897 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5898 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5899 if (lhs->lhs_lvar == NULL)
5900 return FAIL;
5901 lhs->lhs_new_local = TRUE;
5902 }
5903
5904 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005905 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 {
5907 // Something follows after the variable: "var[idx]" or "var.key".
5908 // TODO: should we also handle "->func()" here?
5909 if (is_decl)
5910 {
5911 emsg(_(e_cannot_use_index_when_declaring_variable));
5912 return FAIL;
5913 }
5914
5915 if (var_start[lhs->lhs_varlen] == '['
5916 || var_start[lhs->lhs_varlen] == '.')
5917 {
5918 char_u *after = var_start + lhs->lhs_varlen;
5919 char_u *p;
5920
5921 // Only the last index is used below, if there are others
5922 // before it generate code for the expression. Thus for
5923 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5924 for (;;)
5925 {
5926 p = skip_index(after);
5927 if (*p != '[' && *p != '.')
5928 break;
5929 after = p;
5930 }
5931 if (after > var_start + lhs->lhs_varlen)
5932 {
5933 lhs->lhs_varlen = after - var_start;
5934 lhs->lhs_dest = dest_expr;
5935 // We don't know the type before evaluating the expression,
5936 // use "any" until then.
5937 lhs->lhs_type = &t_any;
5938 }
5939
Bram Moolenaar752fc692021-01-04 21:57:11 +01005940 if (lhs->lhs_type->tt_member == NULL)
5941 lhs->lhs_member_type = &t_any;
5942 else
5943 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5944 }
5945 else
5946 {
5947 semsg("Not supported yet: %s", var_start);
5948 return FAIL;
5949 }
5950 }
5951 return OK;
5952}
5953
5954/*
5955 * Assignment to a list or dict member, or ":unlet" for the item, using the
5956 * information in "lhs".
5957 * Returns OK or FAIL.
5958 */
5959 static int
5960compile_assign_unlet(
5961 char_u *var_start,
5962 lhs_T *lhs,
5963 int is_assign,
5964 type_T *rhs_type,
5965 cctx_T *cctx)
5966{
5967 char_u *p;
5968 int r;
5969 vartype_T dest_type;
5970 size_t varlen = lhs->lhs_varlen;
5971 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005972 int range = FALSE;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005973
5974 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5975 p = var_start + varlen;
5976 if (*p == '[')
5977 {
5978 p = skipwhite(p + 1);
5979 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01005980
5981 if (r == OK && *skipwhite(p) == ':')
5982 {
5983 // unlet var[idx : idx]
5984 if (is_assign)
5985 {
5986 semsg(_(e_cannot_use_range_with_assignment_str), p);
5987 return FAIL;
5988 }
5989 range = TRUE;
5990 p = skipwhite(p);
5991 if (!IS_WHITE_OR_NUL(p[-1]) || !IS_WHITE_OR_NUL(p[1]))
5992 {
5993 semsg(_(e_white_space_required_before_and_after_str_at_str),
5994 ":", p);
5995 return FAIL;
5996 }
5997 p = skipwhite(p + 1);
5998 r = compile_expr0(&p, cctx);
5999 }
6000
Bram Moolenaar752fc692021-01-04 21:57:11 +01006001 if (r == OK && *skipwhite(p) != ']')
6002 {
6003 // this should not happen
6004 emsg(_(e_missbrac));
6005 r = FAIL;
6006 }
6007 }
6008 else // if (*p == '.')
6009 {
6010 char_u *key_end = to_name_end(p + 1, TRUE);
6011 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
6012
6013 r = generate_PUSHS(cctx, key);
6014 }
6015 if (r == FAIL)
6016 return FAIL;
6017
6018 if (lhs->lhs_type == &t_any)
6019 {
6020 // Index on variable of unknown type: check at runtime.
6021 dest_type = VAR_ANY;
6022 }
6023 else
6024 {
6025 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006026 if (dest_type == VAR_DICT && range)
6027 {
6028 emsg(e_cannot_use_range_with_dictionary);
6029 return FAIL;
6030 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006031 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
6032 return FAIL;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006033 if (dest_type == VAR_LIST)
6034 {
6035 if (range
6036 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01006037 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006038 return FAIL;
6039 if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6040 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6041 return FAIL;
6042 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006043 }
6044
6045 // Load the dict or list. On the stack we then have:
6046 // - value (for assignment, not for :unlet)
6047 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006048 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006049 // - variable
6050 if (lhs->lhs_dest == dest_expr)
6051 {
6052 int c = var_start[varlen];
6053
6054 // Evaluate "ll[expr]" of "ll[expr][idx]"
6055 p = var_start;
6056 var_start[varlen] = NUL;
6057 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6058 {
6059 // this should not happen
6060 emsg(_(e_missbrac));
6061 return FAIL;
6062 }
6063 var_start[varlen] = c;
6064
6065 lhs->lhs_type = stack->ga_len == 0 ? &t_void
6066 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6067 // now we can properly check the type
6068 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01006069 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006070 FALSE, FALSE) == FAIL)
6071 return FAIL;
6072 }
6073 else
6074 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
6075 lhs->lhs_lvar, lhs->lhs_type);
6076
6077 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
6078 {
6079 if (is_assign)
6080 {
6081 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6082
6083 if (isn == NULL)
6084 return FAIL;
6085 isn->isn_arg.vartype = dest_type;
6086 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01006087 else if (range)
6088 {
6089 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
6090 return FAIL;
6091 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006092 else
6093 {
6094 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
6095 return FAIL;
6096 }
6097 }
6098 else
6099 {
6100 emsg(_(e_indexable_type_required));
6101 return FAIL;
6102 }
6103
6104 return OK;
6105}
6106
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006107/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006108 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006109 * "let name"
6110 * "var name = expr"
6111 * "final name = expr"
6112 * "const name = expr"
6113 * "name = expr"
6114 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006115 * Return NULL for an error.
6116 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 */
6118 static char_u *
6119compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
6120{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006121 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006122 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006123 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006124 char_u *ret = NULL;
6125 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006126 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006127 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006128 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006129 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006130 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006131 int oplen = 0;
6132 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006133 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006135 int is_decl = is_decl_command(cmdidx);
6136 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006137
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006138 // Skip over the "var" or "[var, var]" to get to any "=".
6139 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
6140 if (p == NULL)
6141 return *arg == '[' ? arg : NULL;
6142
6143 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006144 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006145 // TODO: should we allow this, and figure out type inference from list
6146 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006147 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 return NULL;
6149 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006150 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 sp = p;
6153 p = skipwhite(p);
6154 op = p;
6155 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006156
6157 if (var_count > 0 && oplen == 0)
6158 // can be something like "[1, 2]->func()"
6159 return arg;
6160
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006161 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006162 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006163 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006164 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006167 if (heredoc)
6168 {
6169 list_T *l;
6170 listitem_T *li;
6171
6172 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006173 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006174 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006175 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006176 if (l == NULL)
6177 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006178
Bram Moolenaar078269b2020-09-21 20:35:55 +02006179 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006181 // Push each line and the create the list.
6182 FOR_ALL_LIST_ITEMS(l, li)
6183 {
6184 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6185 li->li_tv.vval.v_string = NULL;
6186 }
6187 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189 list_free(l);
6190 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006191 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006192 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006193 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006195 char_u *wp;
6196
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006197 // for "[var, var] = expr" evaluate the expression here, loop over the
6198 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006199 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006200
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006201 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006202 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006203 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006204 if (compile_expr0(&p, cctx) == FAIL)
6205 return NULL;
6206 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006208 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006210 type_T *stacktype;
6211
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006212 stacktype = stack->ga_len == 0 ? &t_void
6213 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006214 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006216 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006217 goto theend;
6218 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006219 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006220 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006221 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006222 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006223 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6224 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006225 if (stacktype->tt_member != NULL)
6226 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006227 }
6228 }
6229
6230 /*
6231 * Loop over variables in "[var, var] = expr".
6232 * For "var = expr" and "let var: type" this is done only once.
6233 */
6234 if (var_count > 0)
6235 var_start = skipwhite(arg + 1); // skip over the "["
6236 else
6237 var_start = arg;
6238 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6239 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 int instr_count = -1;
6241
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242 vim_free(lhs.lhs_name);
6243
6244 /*
6245 * Figure out the LHS type and other properties.
6246 */
6247 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6248 goto theend;
6249
6250 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006251 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006252 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006253 goto theend;
6254 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006255 if (!is_decl && lhs.lhs_lvar != NULL
6256 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006257 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006258 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006259 goto theend;
6260 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006261
6262 if (!heredoc)
6263 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006264 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006265 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006266 if (oplen > 0 && var_count == 0)
6267 {
6268 // skip over the "=" and the expression
6269 p = skipwhite(op + oplen);
6270 compile_expr0(&p, cctx);
6271 }
6272 }
6273 else if (oplen > 0)
6274 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006275 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006276 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006277
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006278 // For "var = expr" evaluate the expression.
6279 if (var_count == 0)
6280 {
6281 int r;
6282
6283 // for "+=", "*=", "..=" etc. first load the current value
6284 if (*op != '=')
6285 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006286 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6287 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006288
Bram Moolenaar752fc692021-01-04 21:57:11 +01006289 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006290 {
6291 // TODO: get member from list or dict
6292 emsg("Index with operation not supported yet");
6293 goto theend;
6294 }
6295 }
6296
6297 // Compile the expression. Temporarily hide the new local
6298 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006299 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006300 --cctx->ctx_locals.ga_len;
6301 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006302 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006303 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006304 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006306 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006307 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006308 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006309 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006310 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 ++cctx->ctx_locals.ga_len;
6312 if (r == FAIL)
6313 goto theend;
6314 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006315 else if (semicolon && var_idx == var_count - 1)
6316 {
6317 // For "[var; var] = expr" get the rest of the list
6318 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6319 goto theend;
6320 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006321 else
6322 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006323 // For "[var, var] = expr" get the "var_idx" item from the
6324 // list.
6325 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006326 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006327 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006328
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006329 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006330 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006332 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006333 if ((rhs_type->tt_type == VAR_FUNC
6334 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar3f327882021-03-17 20:56:38 +01006335 && !lhs.lhs_has_index
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006337 goto theend;
6338
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006340 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006341 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006342 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006343 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006344 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006345 }
6346 else
6347 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006348 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006349 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006350 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006351 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006352 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006354 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006355 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006356 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006357 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006358 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006359 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006360 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006361 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006363
Bram Moolenaar71abe482020-09-14 22:28:30 +02006364 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006365 if (lhs.lhs_has_index)
6366 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006367 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006368 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006369 goto theend;
6370 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006371 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006373 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006374 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006375 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006376 else if (cmdidx == CMD_final)
6377 {
6378 emsg(_(e_final_requires_a_value));
6379 goto theend;
6380 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006381 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006382 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006383 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006384 goto theend;
6385 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006386 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006387 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006388 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006389 goto theend;
6390 }
6391 else
6392 {
6393 // variables are always initialized
6394 if (ga_grow(instr, 1) == FAIL)
6395 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006396 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006397 {
6398 case VAR_BOOL:
6399 generate_PUSHBOOL(cctx, VVAL_FALSE);
6400 break;
6401 case VAR_FLOAT:
6402#ifdef FEAT_FLOAT
6403 generate_PUSHF(cctx, 0.0);
6404#endif
6405 break;
6406 case VAR_STRING:
6407 generate_PUSHS(cctx, NULL);
6408 break;
6409 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006410 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006411 break;
6412 case VAR_FUNC:
6413 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6414 break;
6415 case VAR_LIST:
6416 generate_NEWLIST(cctx, 0);
6417 break;
6418 case VAR_DICT:
6419 generate_NEWDICT(cctx, 0);
6420 break;
6421 case VAR_JOB:
6422 generate_PUSHJOB(cctx, NULL);
6423 break;
6424 case VAR_CHANNEL:
6425 generate_PUSHCHANNEL(cctx, NULL);
6426 break;
6427 case VAR_NUMBER:
6428 case VAR_UNKNOWN:
6429 case VAR_ANY:
6430 case VAR_PARTIAL:
6431 case VAR_VOID:
6432 case VAR_SPECIAL: // cannot happen
6433 generate_PUSHNR(cctx, 0);
6434 break;
6435 }
6436 }
6437 if (var_count == 0)
6438 end = p;
6439 }
6440
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006441 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006442 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006443 break;
6444
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006445 if (oplen > 0 && *op != '=')
6446 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006447 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006448 type_T *stacktype;
6449
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006450 if (*op == '.')
6451 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006452 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006453 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006454 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006455 if (
6456#ifdef FEAT_FLOAT
6457 // If variable is float operation with number is OK.
6458 !(expected == &t_float && stacktype == &t_number) &&
6459#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006460 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006461 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006462 goto theend;
6463
6464 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006465 {
6466 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6467 goto theend;
6468 }
6469 else if (*op == '+')
6470 {
6471 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006472 operator_type(lhs.lhs_member_type, stacktype),
6473 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006474 goto theend;
6475 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006476 else if (generate_two_op(cctx, op) == FAIL)
6477 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006481 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 // Use the info in "lhs" to store the value at the index in the
6483 // list or dict.
6484 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6485 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006486 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006487 }
6488 else
6489 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006490 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6491 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006492 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006493 generate_LOCKCONST(cctx);
6494
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006495 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006496 && (lhs.lhs_type->tt_type == VAR_DICT
6497 || lhs.lhs_type->tt_type == VAR_LIST)
6498 && lhs.lhs_type->tt_member != NULL
6499 && lhs.lhs_type->tt_member != &t_any
6500 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006501 // Set the type in the list or dict, so that it can be checked,
6502 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006503 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006504
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006506 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006507 if (generate_store_var(cctx, lhs.lhs_dest,
6508 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6509 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6510 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006511 goto theend;
6512 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006514 {
6515 isn_T *isn = ((isn_T *)instr->ga_data)
6516 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006517
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006518 // optimization: turn "var = 123" from ISN_PUSHNR +
6519 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006520 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006521 && instr->ga_len == instr_count + 1
6522 && isn->isn_type == ISN_PUSHNR)
6523 {
6524 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006525
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006526 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006528 isn->isn_arg.storenr.stnr_val = val;
6529 if (stack->ga_len > 0)
6530 --stack->ga_len;
6531 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006532 else if (lhs.lhs_lvar->lv_from_outer > 0)
6533 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6534 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006535 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006536 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006537 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006538 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006539
6540 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006541 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006543
6544 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006545 if (var_count > 0 && !semicolon)
6546 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006547 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006548 goto theend;
6549 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006550
Bram Moolenaarb2097502020-07-19 17:17:02 +02006551 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552
6553theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006554 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006555 return ret;
6556}
6557
6558/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006559 * Check for an assignment at "eap->cmd", compile it if found.
6560 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6561 */
6562 static int
6563may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6564{
6565 char_u *pskip;
6566 char_u *p;
6567
6568 // Assuming the command starts with a variable or function name,
6569 // find what follows.
6570 // Skip over "var.member", "var[idx]" and the like.
6571 // Also "&opt = val", "$ENV = val" and "@r = val".
6572 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6573 ? eap->cmd + 1 : eap->cmd;
6574 p = to_name_end(pskip, TRUE);
6575 if (p > eap->cmd && *p != NUL)
6576 {
6577 char_u *var_end;
6578 int oplen;
6579 int heredoc;
6580
6581 if (eap->cmd[0] == '@')
6582 var_end = eap->cmd + 2;
6583 else
6584 var_end = find_name_end(pskip, NULL, NULL,
6585 FNE_CHECK_START | FNE_INCL_BR);
6586 oplen = assignment_len(skipwhite(var_end), &heredoc);
6587 if (oplen > 0)
6588 {
6589 size_t len = p - eap->cmd;
6590
6591 // Recognize an assignment if we recognize the variable
6592 // name:
6593 // "g:var = expr"
6594 // "local = expr" where "local" is a local var.
6595 // "script = expr" where "script" is a script-local var.
6596 // "import = expr" where "import" is an imported var
6597 // "&opt = expr"
6598 // "$ENV = expr"
6599 // "@r = expr"
6600 if (*eap->cmd == '&'
6601 || *eap->cmd == '$'
6602 || *eap->cmd == '@'
6603 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006604 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006605 {
6606 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6607 if (*line == NULL || *line == eap->cmd)
6608 return FAIL;
6609 return OK;
6610 }
6611 }
6612 }
6613
6614 if (*eap->cmd == '[')
6615 {
6616 // [var, var] = expr
6617 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6618 if (*line == NULL)
6619 return FAIL;
6620 if (*line != eap->cmd)
6621 return OK;
6622 }
6623 return NOTDONE;
6624}
6625
6626/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006627 * Check if "name" can be "unlet".
6628 */
6629 int
6630check_vim9_unlet(char_u *name)
6631{
6632 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6633 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006634 // "unlet s:var" is allowed in legacy script.
6635 if (*name == 's' && !script_is_vim9())
6636 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006637 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006638 return FAIL;
6639 }
6640 return OK;
6641}
6642
6643/*
6644 * Callback passed to ex_unletlock().
6645 */
6646 static int
6647compile_unlet(
6648 lval_T *lvp,
6649 char_u *name_end,
6650 exarg_T *eap,
6651 int deep UNUSED,
6652 void *coookie)
6653{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006654 cctx_T *cctx = coookie;
6655 char_u *p = lvp->ll_name;
6656 int cc = *name_end;
6657 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006658
Bram Moolenaar752fc692021-01-04 21:57:11 +01006659 if (cctx->ctx_skip == SKIP_YES)
6660 return OK;
6661
6662 *name_end = NUL;
6663 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006664 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006665 // :unlet $ENV_VAR
6666 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6667 }
6668 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6669 {
6670 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006671
Bram Moolenaar752fc692021-01-04 21:57:11 +01006672 // This is similar to assigning: lookup the list/dict, compile the
6673 // idx/key. Then instead of storing the value unlet the item.
6674 // unlet {list}[idx]
6675 // unlet {dict}[key] dict.key
6676 //
6677 // Figure out the LHS type and other properties.
6678 //
6679 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6680
6681 // : unlet an indexed item
6682 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006683 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006684 iemsg("called compile_lhs() without an index");
6685 ret = FAIL;
6686 }
6687 else
6688 {
6689 // Use the info in "lhs" to unlet the item at the index in the
6690 // list or dict.
6691 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006692 }
6693
Bram Moolenaar752fc692021-01-04 21:57:11 +01006694 vim_free(lhs.lhs_name);
6695 }
6696 else if (check_vim9_unlet(p) == FAIL)
6697 {
6698 ret = FAIL;
6699 }
6700 else
6701 {
6702 // Normal name. Only supports g:, w:, t: and b: namespaces.
6703 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006704 }
6705
Bram Moolenaar752fc692021-01-04 21:57:11 +01006706 *name_end = cc;
6707 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006708}
6709
6710/*
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006711 * Callback passed to ex_unletlock().
6712 */
6713 static int
6714compile_lock_unlock(
6715 lval_T *lvp,
6716 char_u *name_end,
6717 exarg_T *eap,
6718 int deep UNUSED,
6719 void *coookie)
6720{
6721 cctx_T *cctx = coookie;
6722 int cc = *name_end;
6723 char_u *p = lvp->ll_name;
6724 int ret = OK;
6725 size_t len;
6726 char_u *buf;
6727
6728 if (cctx->ctx_skip == SKIP_YES)
6729 return OK;
6730
6731 // Cannot use :lockvar and :unlockvar on local variables.
6732 if (p[1] != ':')
6733 {
6734 char_u *end = skip_var_one(p, FALSE);
6735
6736 if (lookup_local(p, end - p, NULL, cctx) == OK)
6737 {
6738 emsg(_(e_cannot_lock_unlock_local_variable));
6739 return FAIL;
6740 }
6741 }
6742
6743 // Checking is done at runtime.
6744 *name_end = NUL;
6745 len = name_end - p + 20;
6746 buf = alloc(len);
6747 if (buf == NULL)
6748 ret = FAIL;
6749 else
6750 {
6751 vim_snprintf((char *)buf, len, "%s %s",
6752 eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
6753 p);
6754 ret = generate_EXEC(cctx, buf);
6755
6756 vim_free(buf);
6757 *name_end = cc;
6758 }
6759 return ret;
6760}
6761
6762/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006763 * compile "unlet var", "lock var" and "unlock var"
6764 * "arg" points to "var".
6765 */
6766 static char_u *
6767compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6768{
Bram Moolenaarb2cb6c82021-03-28 20:38:34 +02006769 ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6770 eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
6771 cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006772 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6773}
6774
6775/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006776 * Compile an :import command.
6777 */
6778 static char_u *
6779compile_import(char_u *arg, cctx_T *cctx)
6780{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006781 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782}
6783
6784/*
6785 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6786 */
6787 static int
6788compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6789{
6790 garray_T *instr = &cctx->ctx_instr;
6791 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6792
6793 if (endlabel == NULL)
6794 return FAIL;
6795 endlabel->el_next = *el;
6796 *el = endlabel;
6797 endlabel->el_end_label = instr->ga_len;
6798
6799 generate_JUMP(cctx, when, 0);
6800 return OK;
6801}
6802
6803 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006804compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805{
6806 garray_T *instr = &cctx->ctx_instr;
6807
6808 while (*el != NULL)
6809 {
6810 endlabel_T *cur = (*el);
6811 isn_T *isn;
6812
6813 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006814 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006815 *el = cur->el_next;
6816 vim_free(cur);
6817 }
6818}
6819
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006820 static void
6821compile_free_jump_to_end(endlabel_T **el)
6822{
6823 while (*el != NULL)
6824 {
6825 endlabel_T *cur = (*el);
6826
6827 *el = cur->el_next;
6828 vim_free(cur);
6829 }
6830}
6831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006832/*
6833 * Create a new scope and set up the generic items.
6834 */
6835 static scope_T *
6836new_scope(cctx_T *cctx, scopetype_T type)
6837{
6838 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6839
6840 if (scope == NULL)
6841 return NULL;
6842 scope->se_outer = cctx->ctx_scope;
6843 cctx->ctx_scope = scope;
6844 scope->se_type = type;
6845 scope->se_local_count = cctx->ctx_locals.ga_len;
6846 return scope;
6847}
6848
6849/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006850 * Free the current scope and go back to the outer scope.
6851 */
6852 static void
6853drop_scope(cctx_T *cctx)
6854{
6855 scope_T *scope = cctx->ctx_scope;
6856
6857 if (scope == NULL)
6858 {
6859 iemsg("calling drop_scope() without a scope");
6860 return;
6861 }
6862 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006863 switch (scope->se_type)
6864 {
6865 case IF_SCOPE:
6866 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6867 case FOR_SCOPE:
6868 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6869 case WHILE_SCOPE:
6870 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6871 case TRY_SCOPE:
6872 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6873 case NO_SCOPE:
6874 case BLOCK_SCOPE:
6875 break;
6876 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006877 vim_free(scope);
6878}
6879
6880/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006881 * compile "if expr"
6882 *
6883 * "if expr" Produces instructions:
6884 * EVAL expr Push result of "expr"
6885 * JUMP_IF_FALSE end
6886 * ... body ...
6887 * end:
6888 *
6889 * "if expr | else" Produces instructions:
6890 * EVAL expr Push result of "expr"
6891 * JUMP_IF_FALSE else
6892 * ... body ...
6893 * JUMP_ALWAYS end
6894 * else:
6895 * ... body ...
6896 * end:
6897 *
6898 * "if expr1 | elseif expr2 | else" Produces instructions:
6899 * EVAL expr Push result of "expr"
6900 * JUMP_IF_FALSE elseif
6901 * ... body ...
6902 * JUMP_ALWAYS end
6903 * elseif:
6904 * EVAL expr Push result of "expr"
6905 * JUMP_IF_FALSE else
6906 * ... body ...
6907 * JUMP_ALWAYS end
6908 * else:
6909 * ... body ...
6910 * end:
6911 */
6912 static char_u *
6913compile_if(char_u *arg, cctx_T *cctx)
6914{
6915 char_u *p = arg;
6916 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006917 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006918 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006919 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006920 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006921
Bram Moolenaara5565e42020-05-09 15:44:01 +02006922 CLEAR_FIELD(ppconst);
6923 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006924 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006925 clear_ppconst(&ppconst);
6926 return NULL;
6927 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006928 if (!ends_excmd2(arg, skipwhite(p)))
6929 {
6930 semsg(_(e_trailing_arg), p);
6931 return NULL;
6932 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006933 if (cctx->ctx_skip == SKIP_YES)
6934 clear_ppconst(&ppconst);
6935 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006936 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006937 int error = FALSE;
6938 int v;
6939
Bram Moolenaara5565e42020-05-09 15:44:01 +02006940 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006941 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006942 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006943 if (error)
6944 return NULL;
6945 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006946 }
6947 else
6948 {
6949 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006950 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006951 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006952 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006953 if (bool_on_stack(cctx) == FAIL)
6954 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956
Bram Moolenaara91a7132021-03-25 21:12:15 +01006957 // CMDMOD_REV must come before the jump
6958 generate_undo_cmdmods(cctx);
6959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 scope = new_scope(cctx, IF_SCOPE);
6961 if (scope == NULL)
6962 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006963 scope->se_skip_save = skip_save;
6964 // "is_had_return" will be reset if any block does not end in :return
6965 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006966
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006967 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006968 {
6969 // "where" is set when ":elseif", "else" or ":endif" is found
6970 scope->se_u.se_if.is_if_label = instr->ga_len;
6971 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6972 }
6973 else
6974 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975
Bram Moolenaarced68a02021-01-24 17:53:47 +01006976#ifdef FEAT_PROFILE
6977 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6978 && skip_save != SKIP_YES)
6979 {
6980 // generated a profile start, need to generate a profile end, since it
6981 // won't be done after returning
6982 cctx->ctx_skip = SKIP_NOT;
6983 generate_instr(cctx, ISN_PROF_END);
6984 cctx->ctx_skip = SKIP_YES;
6985 }
6986#endif
6987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988 return p;
6989}
6990
6991 static char_u *
6992compile_elseif(char_u *arg, cctx_T *cctx)
6993{
6994 char_u *p = arg;
6995 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006996 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 isn_T *isn;
6998 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006999 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02007000 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001
7002 if (scope == NULL || scope->se_type != IF_SCOPE)
7003 {
7004 emsg(_(e_elseif_without_if));
7005 return NULL;
7006 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007007 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007008 if (!cctx->ctx_had_return)
7009 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007010
Bram Moolenaarced68a02021-01-24 17:53:47 +01007011 if (cctx->ctx_skip == SKIP_NOT)
7012 {
7013 // previous block was executed, this one and following will not
7014 cctx->ctx_skip = SKIP_YES;
7015 scope->se_u.se_if.is_seen_skip_not = TRUE;
7016 }
7017 if (scope->se_u.se_if.is_seen_skip_not)
7018 {
7019 // A previous block was executed, skip over expression and bail out.
Bram Moolenaara91a7132021-03-25 21:12:15 +01007020 // Do not count the "elseif" for profiling and cmdmod
7021 instr->ga_len = current_instr_idx(cctx);
7022
Bram Moolenaarced68a02021-01-24 17:53:47 +01007023 skip_expr_cctx(&p, cctx);
7024 return p;
7025 }
7026
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007027 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007028 {
Bram Moolenaara91a7132021-03-25 21:12:15 +01007029 int moved_cmdmod = FALSE;
7030
7031 // Move any CMDMOD instruction to after the jump
7032 if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
7033 {
7034 if (ga_grow(instr, 1) == FAIL)
7035 return NULL;
7036 ((isn_T *)instr->ga_data)[instr->ga_len] =
7037 ((isn_T *)instr->ga_data)[instr->ga_len - 1];
7038 --instr->ga_len;
7039 moved_cmdmod = TRUE;
7040 }
7041
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007042 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007044 return NULL;
7045 // previous "if" or "elseif" jumps here
7046 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7047 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007048 if (moved_cmdmod)
7049 ++instr->ga_len;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007052 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02007053 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02007054 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01007055 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02007056 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01007057#ifdef FEAT_PROFILE
7058 if (cctx->ctx_profiling)
7059 {
7060 // the previous block was skipped, need to profile this line
7061 generate_instr(cctx, ISN_PROF_START);
7062 instr_count = instr->ga_len;
7063 }
7064#endif
7065 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02007066 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007067 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02007068 clear_ppconst(&ppconst);
7069 return NULL;
7070 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02007071 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007072 if (!ends_excmd2(arg, skipwhite(p)))
7073 {
7074 semsg(_(e_trailing_arg), p);
7075 return NULL;
7076 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02007077 if (scope->se_skip_save == SKIP_YES)
7078 clear_ppconst(&ppconst);
7079 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02007080 {
Bram Moolenaar13106602020-10-04 16:06:05 +02007081 int error = FALSE;
7082 int v;
7083
Bram Moolenaar7f141552020-05-09 17:35:53 +02007084 // The expression results in a constant.
7085 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02007086 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
7087 if (error)
7088 return NULL;
7089 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007090 clear_ppconst(&ppconst);
7091 scope->se_u.se_if.is_if_label = -1;
7092 }
7093 else
7094 {
7095 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007096 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02007097 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007098 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02007099 if (bool_on_stack(cctx) == FAIL)
7100 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007101
Bram Moolenaara91a7132021-03-25 21:12:15 +01007102 // CMDMOD_REV must come before the jump
7103 generate_undo_cmdmods(cctx);
7104
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007105 // "where" is set when ":elseif", "else" or ":endif" is found
7106 scope->se_u.se_if.is_if_label = instr->ga_len;
7107 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
7108 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109
7110 return p;
7111}
7112
7113 static char_u *
7114compile_else(char_u *arg, cctx_T *cctx)
7115{
7116 char_u *p = arg;
7117 garray_T *instr = &cctx->ctx_instr;
7118 isn_T *isn;
7119 scope_T *scope = cctx->ctx_scope;
7120
7121 if (scope == NULL || scope->se_type != IF_SCOPE)
7122 {
7123 emsg(_(e_else_without_if));
7124 return NULL;
7125 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01007126 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007127 if (!cctx->ctx_had_return)
7128 scope->se_u.se_if.is_had_return = FALSE;
7129 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007130
Bram Moolenaarced68a02021-01-24 17:53:47 +01007131#ifdef FEAT_PROFILE
7132 if (cctx->ctx_profiling)
7133 {
7134 if (cctx->ctx_skip == SKIP_NOT
7135 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7136 .isn_type == ISN_PROF_START)
7137 // the previous block was executed, do not count "else" for profiling
7138 --instr->ga_len;
7139 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
7140 {
7141 // the previous block was not executed, this one will, do count the
7142 // "else" for profiling
7143 cctx->ctx_skip = SKIP_NOT;
7144 generate_instr(cctx, ISN_PROF_END);
7145 generate_instr(cctx, ISN_PROF_START);
7146 cctx->ctx_skip = SKIP_YES;
7147 }
7148 }
7149#endif
7150
7151 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007152 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007153 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007154 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007155 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02007156 if (!cctx->ctx_had_return
7157 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
7158 JUMP_ALWAYS, cctx) == FAIL)
7159 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007160 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007161
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007162 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007163 {
7164 if (scope->se_u.se_if.is_if_label >= 0)
7165 {
7166 // previous "if" or "elseif" jumps here
7167 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7168 isn->isn_arg.jump.jump_where = instr->ga_len;
7169 scope->se_u.se_if.is_if_label = -1;
7170 }
7171 }
7172
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02007173 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02007174 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
7175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007176
7177 return p;
7178}
7179
7180 static char_u *
7181compile_endif(char_u *arg, cctx_T *cctx)
7182{
7183 scope_T *scope = cctx->ctx_scope;
7184 ifscope_T *ifscope;
7185 garray_T *instr = &cctx->ctx_instr;
7186 isn_T *isn;
7187
Bram Moolenaarfa984412021-03-25 22:15:28 +01007188 if (misplaced_cmdmod(cctx))
7189 return NULL;
7190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007191 if (scope == NULL || scope->se_type != IF_SCOPE)
7192 {
7193 emsg(_(e_endif_without_if));
7194 return NULL;
7195 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007196 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007197 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007198 if (!cctx->ctx_had_return)
7199 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007200
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007201 if (scope->se_u.se_if.is_if_label >= 0)
7202 {
7203 // previous "if" or "elseif" jumps here
7204 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
7205 isn->isn_arg.jump.jump_where = instr->ga_len;
7206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007207 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007208 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01007209
7210#ifdef FEAT_PROFILE
7211 // even when skipping we count the endif as executed, unless the block it's
7212 // in is skipped
7213 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7214 && scope->se_skip_save != SKIP_YES)
7215 {
7216 cctx->ctx_skip = SKIP_NOT;
7217 generate_instr(cctx, ISN_PROF_START);
7218 }
7219#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007220 cctx->ctx_skip = scope->se_skip_save;
7221
7222 // If all the blocks end in :return and there is an :else then the
7223 // had_return flag is set.
7224 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007225
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007226 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007227 return arg;
7228}
7229
7230/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007231 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232 *
7233 * Produces instructions:
7234 * PUSHNR -1
7235 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007236 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007237 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7238 * - if beyond end, jump to "end"
7239 * - otherwise get item from list and push it
7240 * STORE var Store item in "var"
7241 * ... body ...
7242 * JUMP top Jump back to repeat
7243 * end: DROP Drop the result of "expr"
7244 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007245 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7246 * UNPACK 2 Split item in 2
7247 * STORE var1 Store item in "var1"
7248 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 */
7250 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007251compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007252{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007253 char_u *arg;
7254 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007255 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007257 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007258 int var_count = 0;
7259 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 size_t varlen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007261 garray_T *stack = &cctx->ctx_type_stack;
7262 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007263 lvar_T *loop_lvar; // loop iteration variable
7264 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007265 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007266 type_T *item_type = &t_any;
7267 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268
Bram Moolenaar792f7862020-11-23 08:31:18 +01007269 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007270 if (p == NULL)
7271 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007272 if (var_count == 0)
7273 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274
7275 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007276 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007277 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7278 return NULL;
7279 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280 {
7281 emsg(_(e_missing_in));
7282 return NULL;
7283 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007284 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007285 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7286 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007288 scope = new_scope(cctx, FOR_SCOPE);
7289 if (scope == NULL)
7290 return NULL;
7291
Bram Moolenaar792f7862020-11-23 08:31:18 +01007292 // Reserve a variable to store the loop iteration counter and initialize it
7293 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007294 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7295 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007296 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007297 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007298 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007300 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007301 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007302
7303 // compile "expr", it remains on the stack until "endfor"
7304 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007305 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007306 {
7307 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007309 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007310 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007312 // If we know the type of "var" and it is a not a list or string we can
7313 // give an error now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007315 if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
7316 && vartype->tt_type != VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007317 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01007318 // TODO: support Blob
7319 semsg(_(e_for_loop_on_str_not_supported),
7320 vartype_name(vartype->tt_type));
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007321 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007322 return NULL;
7323 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007324
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007325 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007326 {
7327 if (var_count == 1)
7328 item_type = vartype->tt_member;
7329 else if (vartype->tt_member->tt_type == VAR_LIST
7330 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7331 item_type = vartype->tt_member->tt_member;
7332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333
Bram Moolenaara91a7132021-03-25 21:12:15 +01007334 // CMDMOD_REV must come before the FOR instruction
7335 generate_undo_cmdmods(cctx);
7336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 // "for_end" is set when ":endfor" is found
Bram Moolenaara91a7132021-03-25 21:12:15 +01007338 scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007339 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340
Bram Moolenaar792f7862020-11-23 08:31:18 +01007341 arg = arg_start;
7342 if (var_count > 1)
7343 {
7344 generate_UNPACK(cctx, var_count, semicolon);
7345 arg = skipwhite(arg + 1); // skip white after '['
7346
7347 // the list item is replaced by a number of items
7348 if (ga_grow(stack, var_count - 1) == FAIL)
7349 {
7350 drop_scope(cctx);
7351 return NULL;
7352 }
7353 --stack->ga_len;
7354 for (idx = 0; idx < var_count; ++idx)
7355 {
7356 ((type_T **)stack->ga_data)[stack->ga_len] =
7357 (semicolon && idx == 0) ? vartype : item_type;
7358 ++stack->ga_len;
7359 }
7360 }
7361
7362 for (idx = 0; idx < var_count; ++idx)
7363 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007364 assign_dest_T dest = dest_local;
7365 int opt_flags = 0;
7366 int vimvaridx = -1;
7367 type_T *type = &t_any;
7368
7369 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007370 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007371 name = vim_strnsave(arg, varlen);
7372 if (name == NULL)
7373 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007374
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007375 // TODO: script var not supported?
7376 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7377 &vimvaridx, &type, cctx) == FAIL)
7378 goto failed;
7379 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007380 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007381 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7382 0, 0, type, name) == FAIL)
7383 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007384 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007385 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007386 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007387 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007388 {
7389 semsg(_(e_variable_already_declared), arg);
7390 goto failed;
7391 }
7392
Bram Moolenaarea870692020-12-02 14:24:30 +01007393 if (STRNCMP(name, "s:", 2) == 0)
7394 {
7395 semsg(_(e_cannot_declare_script_variable_in_function), name);
7396 goto failed;
7397 }
7398
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007399 // Reserve a variable to store "var".
7400 // TODO: check for type
7401 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7402 if (var_lvar == NULL)
7403 // out of memory or used as an argument
7404 goto failed;
7405
7406 if (semicolon && idx == var_count - 1)
7407 var_lvar->lv_type = vartype;
7408 else
7409 var_lvar->lv_type = item_type;
7410 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7411 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007412
Bram Moolenaar036d0712021-01-17 20:23:38 +01007413 if (*p == ':')
7414 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007415 if (*p == ',' || *p == ';')
7416 ++p;
7417 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007418 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007419 }
7420
7421 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007422
7423failed:
7424 vim_free(name);
7425 drop_scope(cctx);
7426 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427}
7428
7429/*
7430 * compile "endfor"
7431 */
7432 static char_u *
7433compile_endfor(char_u *arg, cctx_T *cctx)
7434{
7435 garray_T *instr = &cctx->ctx_instr;
7436 scope_T *scope = cctx->ctx_scope;
7437 forscope_T *forscope;
7438 isn_T *isn;
7439
Bram Moolenaarfa984412021-03-25 22:15:28 +01007440 if (misplaced_cmdmod(cctx))
7441 return NULL;
Bram Moolenaara91a7132021-03-25 21:12:15 +01007442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 if (scope == NULL || scope->se_type != FOR_SCOPE)
7444 {
7445 emsg(_(e_for));
7446 return NULL;
7447 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007448 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007450 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451
7452 // At end of ":for" scope jump back to the FOR instruction.
7453 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7454
7455 // Fill in the "end" label in the FOR statement so it can jump here
7456 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7457 isn->isn_arg.forloop.for_end = instr->ga_len;
7458
7459 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007460 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461
7462 // Below the ":for" scope drop the "expr" list from the stack.
7463 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7464 return NULL;
7465
7466 vim_free(scope);
7467
7468 return arg;
7469}
7470
7471/*
7472 * compile "while expr"
7473 *
7474 * Produces instructions:
7475 * top: EVAL expr Push result of "expr"
7476 * JUMP_IF_FALSE end jump if false
7477 * ... body ...
7478 * JUMP top Jump back to repeat
7479 * end:
7480 *
7481 */
7482 static char_u *
7483compile_while(char_u *arg, cctx_T *cctx)
7484{
7485 char_u *p = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 scope_T *scope;
7487
7488 scope = new_scope(cctx, WHILE_SCOPE);
7489 if (scope == NULL)
7490 return NULL;
7491
Bram Moolenaara91a7132021-03-25 21:12:15 +01007492 // "endwhile" jumps back here, one before when profiling or using cmdmods
7493 scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494
7495 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007496 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007498 if (!ends_excmd2(arg, skipwhite(p)))
7499 {
7500 semsg(_(e_trailing_arg), p);
7501 return NULL;
7502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007503
Bram Moolenaar13106602020-10-04 16:06:05 +02007504 if (bool_on_stack(cctx) == FAIL)
7505 return FAIL;
7506
Bram Moolenaara91a7132021-03-25 21:12:15 +01007507 // CMDMOD_REV must come before the jump
7508 generate_undo_cmdmods(cctx);
7509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007511 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007512 JUMP_IF_FALSE, cctx) == FAIL)
7513 return FAIL;
7514
7515 return p;
7516}
7517
7518/*
7519 * compile "endwhile"
7520 */
7521 static char_u *
7522compile_endwhile(char_u *arg, cctx_T *cctx)
7523{
7524 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007525 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007526
Bram Moolenaarfa984412021-03-25 22:15:28 +01007527 if (misplaced_cmdmod(cctx))
7528 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7530 {
7531 emsg(_(e_while));
7532 return NULL;
7533 }
7534 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007535 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007536
Bram Moolenaarf002a412021-01-24 13:34:18 +01007537#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007538 // count the endwhile before jumping
7539 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007540#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007543 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007544
7545 // Fill in the "end" label in the WHILE statement so it can jump here.
7546 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007547 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7548 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549
7550 vim_free(scope);
7551
7552 return arg;
7553}
7554
7555/*
7556 * compile "continue"
7557 */
7558 static char_u *
7559compile_continue(char_u *arg, cctx_T *cctx)
7560{
7561 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007562 int try_scopes = 0;
7563 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564
7565 for (;;)
7566 {
7567 if (scope == NULL)
7568 {
7569 emsg(_(e_continue));
7570 return NULL;
7571 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007572 if (scope->se_type == FOR_SCOPE)
7573 {
7574 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007576 }
7577 if (scope->se_type == WHILE_SCOPE)
7578 {
7579 loop_label = scope->se_u.se_while.ws_top_label;
7580 break;
7581 }
7582 if (scope->se_type == TRY_SCOPE)
7583 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 scope = scope->se_outer;
7585 }
7586
Bram Moolenaarc150c092021-02-13 15:02:46 +01007587 if (try_scopes > 0)
7588 // Inside one or more try/catch blocks we first need to jump to the
7589 // "finally" or "endtry" to cleanup.
7590 generate_TRYCONT(cctx, try_scopes, loop_label);
7591 else
7592 // Jump back to the FOR or WHILE instruction.
7593 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 return arg;
7596}
7597
7598/*
7599 * compile "break"
7600 */
7601 static char_u *
7602compile_break(char_u *arg, cctx_T *cctx)
7603{
7604 scope_T *scope = cctx->ctx_scope;
7605 endlabel_T **el;
7606
7607 for (;;)
7608 {
7609 if (scope == NULL)
7610 {
7611 emsg(_(e_break));
7612 return NULL;
7613 }
7614 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7615 break;
7616 scope = scope->se_outer;
7617 }
7618
7619 // Jump to the end of the FOR or WHILE loop.
7620 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007621 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007622 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007623 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7625 return FAIL;
7626
7627 return arg;
7628}
7629
7630/*
7631 * compile "{" start of block
7632 */
7633 static char_u *
7634compile_block(char_u *arg, cctx_T *cctx)
7635{
7636 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7637 return NULL;
7638 return skipwhite(arg + 1);
7639}
7640
7641/*
7642 * compile end of block: drop one scope
7643 */
7644 static void
7645compile_endblock(cctx_T *cctx)
7646{
7647 scope_T *scope = cctx->ctx_scope;
7648
7649 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007650 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 vim_free(scope);
7652}
7653
7654/*
7655 * compile "try"
7656 * Creates a new scope for the try-endtry, pointing to the first catch and
7657 * finally.
7658 * Creates another scope for the "try" block itself.
7659 * TRY instruction sets up exception handling at runtime.
7660 *
7661 * "try"
7662 * TRY -> catch1, -> finally push trystack entry
7663 * ... try block
7664 * "throw {exception}"
7665 * EVAL {exception}
7666 * THROW create exception
7667 * ... try block
7668 * " catch {expr}"
7669 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007670 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 * EVAL {expr}
7672 * MATCH
7673 * JUMP nomatch -> catch2
7674 * CATCH remove exception
7675 * ... catch block
7676 * " catch"
7677 * JUMP -> finally
7678 * catch2: CATCH remove exception
7679 * ... catch block
7680 * " finally"
7681 * finally:
7682 * ... finally block
7683 * " endtry"
7684 * ENDTRY pop trystack entry, may rethrow
7685 */
7686 static char_u *
7687compile_try(char_u *arg, cctx_T *cctx)
7688{
7689 garray_T *instr = &cctx->ctx_instr;
7690 scope_T *try_scope;
7691 scope_T *scope;
7692
Bram Moolenaarfa984412021-03-25 22:15:28 +01007693 if (misplaced_cmdmod(cctx))
7694 return NULL;
7695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007696 // scope that holds the jumps that go to catch/finally/endtry
7697 try_scope = new_scope(cctx, TRY_SCOPE);
7698 if (try_scope == NULL)
7699 return NULL;
7700
Bram Moolenaar69f70502021-01-01 16:10:46 +01007701 if (cctx->ctx_skip != SKIP_YES)
7702 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007703 isn_T *isn;
7704
7705 // "try_catch" is set when the first ":catch" is found or when no catch
7706 // is found and ":finally" is found.
7707 // "try_finally" is set when ":finally" is found
7708 // "try_endtry" is set when ":endtry" is found
Bram Moolenaar69f70502021-01-01 16:10:46 +01007709 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007710 if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
7711 return NULL;
7712 isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
7713 if (isn->isn_arg.try.try_ref == NULL)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007714 return NULL;
7715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007716
7717 // scope for the try block itself
7718 scope = new_scope(cctx, BLOCK_SCOPE);
7719 if (scope == NULL)
7720 return NULL;
7721
7722 return arg;
7723}
7724
7725/*
7726 * compile "catch {expr}"
7727 */
7728 static char_u *
7729compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7730{
7731 scope_T *scope = cctx->ctx_scope;
7732 garray_T *instr = &cctx->ctx_instr;
7733 char_u *p;
7734 isn_T *isn;
7735
Bram Moolenaarfa984412021-03-25 22:15:28 +01007736 if (misplaced_cmdmod(cctx))
7737 return NULL;
7738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007739 // end block scope from :try or :catch
7740 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7741 compile_endblock(cctx);
7742 scope = cctx->ctx_scope;
7743
7744 // Error if not in a :try scope
7745 if (scope == NULL || scope->se_type != TRY_SCOPE)
7746 {
7747 emsg(_(e_catch));
7748 return NULL;
7749 }
7750
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007751 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007753 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007754 return NULL;
7755 }
7756
Bram Moolenaar69f70502021-01-01 16:10:46 +01007757 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007759#ifdef FEAT_PROFILE
7760 // the profile-start should be after the jump
7761 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7762 .isn_type == ISN_PROF_START)
7763 --instr->ga_len;
7764#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007765 // Jump from end of previous block to :finally or :endtry
7766 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7767 JUMP_ALWAYS, cctx) == FAIL)
7768 return NULL;
7769
7770 // End :try or :catch scope: set value in ISN_TRY instruction
7771 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007772 if (isn->isn_arg.try.try_ref->try_catch == 0)
7773 isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007774 if (scope->se_u.se_try.ts_catch_label != 0)
7775 {
7776 // Previous catch without match jumps here
7777 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7778 isn->isn_arg.jump.jump_where = instr->ga_len;
7779 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007780#ifdef FEAT_PROFILE
7781 if (cctx->ctx_profiling)
7782 {
7783 // a "throw" that jumps here needs to be counted
7784 generate_instr(cctx, ISN_PROF_END);
7785 // the "catch" is also counted
7786 generate_instr(cctx, ISN_PROF_START);
7787 }
7788#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007789 }
7790
7791 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007792 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007793 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007794 scope->se_u.se_try.ts_caught_all = TRUE;
7795 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 }
7797 else
7798 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007799 char_u *end;
7800 char_u *pat;
7801 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007802 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007803 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 // Push v:exception, push {expr} and MATCH
7806 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7807
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007808 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007809 if (*end != *p)
7810 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007811 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007812 vim_free(tofree);
7813 return FAIL;
7814 }
7815 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007816 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007817 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007818 len = (int)(end - tofree);
7819 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007820 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007821 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007822 if (pat == NULL)
7823 return FAIL;
7824 if (generate_PUSHS(cctx, pat) == FAIL)
7825 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007827 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7828 return NULL;
7829
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007830 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007831 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7832 return NULL;
7833 }
7834
Bram Moolenaar69f70502021-01-01 16:10:46 +01007835 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007836 return NULL;
7837
7838 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7839 return NULL;
7840 return p;
7841}
7842
7843 static char_u *
7844compile_finally(char_u *arg, cctx_T *cctx)
7845{
7846 scope_T *scope = cctx->ctx_scope;
7847 garray_T *instr = &cctx->ctx_instr;
7848 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007849 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007850
Bram Moolenaarfa984412021-03-25 22:15:28 +01007851 if (misplaced_cmdmod(cctx))
7852 return NULL;
7853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007854 // end block scope from :try or :catch
7855 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7856 compile_endblock(cctx);
7857 scope = cctx->ctx_scope;
7858
7859 // Error if not in a :try scope
7860 if (scope == NULL || scope->se_type != TRY_SCOPE)
7861 {
7862 emsg(_(e_finally));
7863 return NULL;
7864 }
7865
7866 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007867 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007868 if (isn->isn_arg.try.try_ref->try_finally != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869 {
7870 emsg(_(e_finally_dup));
7871 return NULL;
7872 }
7873
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007874 this_instr = instr->ga_len;
7875#ifdef FEAT_PROFILE
7876 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7877 .isn_type == ISN_PROF_START)
7878 // jump to the profile start of the "finally"
7879 --this_instr;
7880#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007881
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007882 // Fill in the "end" label in jumps at the end of the blocks.
7883 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7884 this_instr, cctx);
7885
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007886 // If there is no :catch then an exception jumps to :finally.
7887 if (isn->isn_arg.try.try_ref->try_catch == 0)
7888 isn->isn_arg.try.try_ref->try_catch = this_instr;
7889 isn->isn_arg.try.try_ref->try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007890 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007891 {
7892 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007893 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007894 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007895 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007896 }
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007897 if (generate_instr(cctx, ISN_FINALLY) == NULL)
7898 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007900 // TODO: set index in ts_finally_label jumps
7901
7902 return arg;
7903}
7904
7905 static char_u *
7906compile_endtry(char_u *arg, cctx_T *cctx)
7907{
7908 scope_T *scope = cctx->ctx_scope;
7909 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007910 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007911
Bram Moolenaarfa984412021-03-25 22:15:28 +01007912 if (misplaced_cmdmod(cctx))
7913 return NULL;
7914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007915 // end block scope from :catch or :finally
7916 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7917 compile_endblock(cctx);
7918 scope = cctx->ctx_scope;
7919
7920 // Error if not in a :try scope
7921 if (scope == NULL || scope->se_type != TRY_SCOPE)
7922 {
7923 if (scope == NULL)
7924 emsg(_(e_no_endtry));
7925 else if (scope->se_type == WHILE_SCOPE)
7926 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007927 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007928 emsg(_(e_endfor));
7929 else
7930 emsg(_(e_endif));
7931 return NULL;
7932 }
7933
Bram Moolenaarc150c092021-02-13 15:02:46 +01007934 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007935 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007937 if (try_isn->isn_arg.try.try_ref->try_catch == 0
7938 && try_isn->isn_arg.try.try_ref->try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007939 {
7940 emsg(_(e_missing_catch_or_finally));
7941 return NULL;
7942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007943
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007944#ifdef FEAT_PROFILE
7945 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7946 .isn_type == ISN_PROF_START)
7947 // move the profile start after "endtry" so that it's not counted when
7948 // the exception is rethrown.
7949 --instr->ga_len;
7950#endif
7951
Bram Moolenaar69f70502021-01-01 16:10:46 +01007952 // Fill in the "end" label in jumps at the end of the blocks, if not
7953 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007954 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7955 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007956
Bram Moolenaar69f70502021-01-01 16:10:46 +01007957 if (scope->se_u.se_try.ts_catch_label != 0)
7958 {
7959 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007960 isn_T *isn = ((isn_T *)instr->ga_data)
7961 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007962 isn->isn_arg.jump.jump_where = instr->ga_len;
7963 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007964 }
7965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007966 compile_endblock(cctx);
7967
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007968 if (cctx->ctx_skip != SKIP_YES)
7969 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01007970 // End :catch or :finally scope: set instruction index in ISN_TRY
7971 // instruction
7972 try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007973 if (cctx->ctx_skip != SKIP_YES
7974 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7975 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007976#ifdef FEAT_PROFILE
7977 if (cctx->ctx_profiling)
7978 generate_instr(cctx, ISN_PROF_START);
7979#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007981 return arg;
7982}
7983
7984/*
7985 * compile "throw {expr}"
7986 */
7987 static char_u *
7988compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7989{
7990 char_u *p = skipwhite(arg);
7991
Bram Moolenaara5565e42020-05-09 15:44:01 +02007992 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007994 if (cctx->ctx_skip == SKIP_YES)
7995 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007996 if (may_generate_2STRING(-1, cctx) == FAIL)
7997 return NULL;
7998 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7999 return NULL;
8000
8001 return p;
8002}
8003
8004/*
8005 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008006 * compile "echomsg expr"
8007 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01008008 * compile "execute expr"
8009 */
8010 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008011compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008012{
8013 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01008014 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008015 int count = 0;
8016
8017 for (;;)
8018 {
Bram Moolenaare4984292020-12-13 14:19:25 +01008019 if (ends_excmd2(prev, p))
8020 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008021 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01008022 return NULL;
8023 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02008024 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01008025 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008026 }
8027
Bram Moolenaare4984292020-12-13 14:19:25 +01008028 if (count > 0)
8029 {
8030 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
8031 generate_ECHO(cctx, cmdidx == CMD_echo, count);
8032 else if (cmdidx == CMD_execute)
8033 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
8034 else if (cmdidx == CMD_echomsg)
8035 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
8036 else
8037 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
8038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008039 return p;
8040}
8041
8042/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01008043 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01008044 * instruction to compute it and return OK.
8045 * Otherwise return FAIL, the caller must deal with any range.
8046 */
8047 static int
8048compile_variable_range(exarg_T *eap, cctx_T *cctx)
8049{
8050 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
8051 char_u *p = skipdigits(eap->cmd);
8052
8053 if (p == range_end)
8054 return FAIL;
8055 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
8056}
8057
8058/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008059 * :put r
8060 * :put ={expr}
8061 */
8062 static char_u *
8063compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
8064{
8065 char_u *line = arg;
8066 linenr_T lnum;
8067 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008068 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008069
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008070 eap->regname = *line;
8071
8072 if (eap->regname == '=')
8073 {
8074 char_u *p = line + 1;
8075
8076 if (compile_expr0(&p, cctx) == FAIL)
8077 return NULL;
8078 line = p;
8079 }
8080 else if (eap->regname != NUL)
8081 ++line;
8082
Bram Moolenaar08597872020-12-10 19:43:40 +01008083 if (compile_variable_range(eap, cctx) == OK)
8084 {
8085 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
8086 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008087 else
Bram Moolenaar08597872020-12-10 19:43:40 +01008088 {
8089 // Either no range or a number.
8090 // "errormsg" will not be set because the range is ADDR_LINES.
8091 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01008092 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01008093 return NULL;
8094 if (eap->addr_count == 0)
8095 lnum = -1;
8096 else
8097 lnum = eap->line2;
8098 if (above)
8099 --lnum;
8100 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008101
8102 generate_PUT(cctx, eap->regname, lnum);
8103 return line;
8104}
8105
8106/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008107 * A command that is not compiled, execute with legacy code.
8108 */
8109 static char_u *
8110compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
8111{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008112 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008113 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008114 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008115
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008116 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008117 goto theend;
8118
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02008119 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008120 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008121 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008122 int usefilter = FALSE;
8123
8124 has_expr = argt & (EX_XFILE | EX_EXPAND);
8125
8126 // If the command can be followed by a bar, find the bar and truncate
8127 // it, so that the following command can be compiled.
8128 // The '|' is overwritten with a NUL, it is put back below.
8129 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
8130 && *eap->arg == '!')
8131 // :w !filter or :r !filter or :r! filter
8132 usefilter = TRUE;
8133 if ((argt & EX_TRLBAR) && !usefilter)
8134 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02008135 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008136 separate_nextcmd(eap);
8137 if (eap->nextcmd != NULL)
8138 nextcmd = eap->nextcmd;
8139 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01008140 else if (eap->cmdidx == CMD_wincmd)
8141 {
8142 p = eap->arg;
8143 if (*p != NUL)
8144 ++p;
8145 if (*p == 'g' || *p == Ctrl_G)
8146 ++p;
8147 p = skipwhite(p);
8148 if (*p == '|')
8149 {
8150 *p = NUL;
8151 nextcmd = p + 1;
8152 }
8153 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008154 }
8155
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008156 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
8157 {
8158 // expand filename in "syntax include [@group] filename"
8159 has_expr = TRUE;
8160 eap->arg = skipwhite(eap->arg + 7);
8161 if (*eap->arg == '@')
8162 eap->arg = skiptowhite(eap->arg);
8163 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008164
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008165 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
8166 && STRLEN(eap->arg) > 4)
8167 {
8168 int delim = *eap->arg;
8169
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008170 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01008171 if (*p == delim)
8172 {
8173 eap->arg = p + 1;
8174 has_expr = TRUE;
8175 }
8176 }
8177
Bram Moolenaarecac5912021-01-05 19:23:28 +01008178 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
8179 {
8180 // TODO: should only expand when appropriate for the command
8181 eap->arg = skiptowhite(eap->arg);
8182 has_expr = TRUE;
8183 }
8184
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02008185 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008186 {
8187 int count = 0;
8188 char_u *start = skipwhite(line);
8189
8190 // :cmd xxx`=expr1`yyy`=expr2`zzz
8191 // PUSHS ":cmd xxx"
8192 // eval expr1
8193 // PUSHS "yyy"
8194 // eval expr2
8195 // PUSHS "zzz"
8196 // EXECCONCAT 5
8197 for (;;)
8198 {
8199 if (p > start)
8200 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02008201 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008202 ++count;
8203 }
8204 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02008205 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008206 return NULL;
8207 may_generate_2STRING(-1, cctx);
8208 ++count;
8209 p = skipwhite(p);
8210 if (*p != '`')
8211 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008212 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008213 return NULL;
8214 }
8215 start = p + 1;
8216
8217 p = (char_u *)strstr((char *)start, "`=");
8218 if (p == NULL)
8219 {
8220 if (*skipwhite(start) != NUL)
8221 {
8222 generate_PUSHS(cctx, vim_strsave(start));
8223 ++count;
8224 }
8225 break;
8226 }
8227 }
8228 generate_EXECCONCAT(cctx, count);
8229 }
8230 else
8231 generate_EXEC(cctx, line);
8232
8233theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008234 if (*nextcmd != NUL)
8235 {
8236 // the parser expects a pointer to the bar, put it back
8237 --nextcmd;
8238 *nextcmd = '|';
8239 }
8240
8241 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008242}
8243
8244/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008245 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008246 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008247 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008248 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008249add_def_function(ufunc_T *ufunc)
8250{
8251 dfunc_T *dfunc;
8252
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008253 if (def_functions.ga_len == 0)
8254 {
8255 // The first position is not used, so that a zero uf_dfunc_idx means it
8256 // wasn't set.
8257 if (ga_grow(&def_functions, 1) == FAIL)
8258 return FAIL;
8259 ++def_functions.ga_len;
8260 }
8261
Bram Moolenaar09689a02020-05-09 22:50:08 +02008262 // Add the function to "def_functions".
8263 if (ga_grow(&def_functions, 1) == FAIL)
8264 return FAIL;
8265 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8266 CLEAR_POINTER(dfunc);
8267 dfunc->df_idx = def_functions.ga_len;
8268 ufunc->uf_dfunc_idx = dfunc->df_idx;
8269 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008270 dfunc->df_name = vim_strsave(ufunc->uf_name);
8271 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008272 ++def_functions.ga_len;
8273 return OK;
8274}
8275
8276/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008277 * After ex_function() has collected all the function lines: parse and compile
8278 * the lines into instructions.
8279 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008280 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8281 * the return statement (used for lambda). When uf_ret_type is already set
8282 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008283 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008284 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008285 * This can be used recursively through compile_lambda(), which may reallocate
8286 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008287 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008288 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008289 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008290compile_def_function(
8291 ufunc_T *ufunc,
8292 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008293 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008294 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008296 char_u *line = NULL;
8297 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008298 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008299 cctx_T cctx;
8300 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008301 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 int ret = FAIL;
8303 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008304 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008305 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008306 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008307#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008308 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008309#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008310
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008311 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008312 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008313 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008314 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008315 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8316 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008317 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008319 else
8320 {
8321 if (add_def_function(ufunc) == FAIL)
8322 return FAIL;
8323 new_def_function = TRUE;
8324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008325
Bram Moolenaar985116a2020-07-12 17:31:09 +02008326 ufunc->uf_def_status = UF_COMPILING;
8327
Bram Moolenaara80faa82020-04-12 19:37:17 +02008328 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008329
Bram Moolenaarf002a412021-01-24 13:34:18 +01008330#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008331 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008332#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008333 cctx.ctx_ufunc = ufunc;
8334 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008335 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8337 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8338 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8339 cctx.ctx_type_list = &ufunc->uf_type_list;
8340 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8341 instr = &cctx.ctx_instr;
8342
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008343 // Set the context to the function, it may be compiled when called from
8344 // another script. Set the script version to the most modern one.
8345 // The line number will be set in next_line_from_context().
8346 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8348
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008349 // Make sure error messages are OK.
8350 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8351 if (do_estack_push)
8352 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008353 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008354
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008355 if (ufunc->uf_def_args.ga_len > 0)
8356 {
8357 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008358 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008359 int uf_args_len = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008360 int i;
8361 char_u *arg;
8362 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008363 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008364
8365 // Produce instructions for the default values of optional arguments.
8366 // Store the instruction index in uf_def_arg_idx[] so that we know
8367 // where to start when the function is called, depending on the number
8368 // of arguments.
8369 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8370 if (ufunc->uf_def_arg_idx == NULL)
8371 goto erret;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008372 SOURCING_LNUM = 0; // line number unknown
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008373 for (i = 0; i < count; ++i)
8374 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008375 garray_T *stack = &cctx.ctx_type_stack;
8376 type_T *val_type;
8377 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008378 where_T where;
Bram Moolenaar12bce952021-03-11 20:04:04 +01008379 int r;
8380
8381 // Make sure later arguments are not found.
8382 ufunc->uf_args.ga_len = i;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008383
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008384 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8385 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaar12bce952021-03-11 20:04:04 +01008386 r = compile_expr0(&arg, &cctx);
8387
8388 ufunc->uf_args.ga_len = uf_args_len;
8389 if (r == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008390 goto erret;
8391
8392 // If no type specified use the type of the default value.
8393 // Otherwise check that the default value type matches the
8394 // specified type.
8395 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008396 where.wt_index = arg_idx + 1;
8397 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008398 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008399 {
8400 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008401 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008402 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008403 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008404 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008405 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008406
8407 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008408 goto erret;
8409 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008410 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008411
8412 if (did_set_arg_type)
8413 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008414 }
8415
8416 /*
8417 * Loop over all the lines of the function and generate instructions.
8418 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008419 for (;;)
8420 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008421 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008422 int starts_with_colon = FALSE;
8423 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008424 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008425
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008426 // Bail out on the first error to avoid a flood of errors and report
8427 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008428 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008429 goto erret;
8430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008431 if (line != NULL && *line == '|')
8432 // the line continues after a '|'
8433 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008434 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008435 && !(*line == '#' && (line == cctx.ctx_line_start
8436 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008438 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 goto erret;
8440 }
Bram Moolenaar4b3e1962021-03-18 21:37:55 +01008441 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
8442 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008443 else
8444 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008445 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008446 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008447 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008448 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008449#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008450 if (cctx.ctx_skip != SKIP_YES)
8451 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008452#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008455 }
8456
Bram Moolenaara80faa82020-04-12 19:37:17 +02008457 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458 ea.cmdlinep = &line;
8459 ea.cmd = skipwhite(line);
8460
Bram Moolenaarb2049902021-01-24 12:53:53 +01008461 if (*ea.cmd == '#')
8462 {
8463 // "#" starts a comment
8464 line = (char_u *)"";
8465 continue;
8466 }
8467
Bram Moolenaarf002a412021-01-24 13:34:18 +01008468#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008469 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8470 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008471 {
8472 may_generate_prof_end(&cctx, prof_lnum);
8473
8474 prof_lnum = cctx.ctx_lnum;
8475 generate_instr(&cctx, ISN_PROF_START);
8476 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008477#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008478
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008479 // Some things can be recognized by the first character.
8480 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008482 case '}':
8483 {
8484 // "}" ends a block scope
8485 scopetype_T stype = cctx.ctx_scope == NULL
8486 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008487
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008488 if (stype == BLOCK_SCOPE)
8489 {
8490 compile_endblock(&cctx);
8491 line = ea.cmd;
8492 }
8493 else
8494 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008495 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008496 goto erret;
8497 }
8498 if (line != NULL)
8499 line = skipwhite(ea.cmd + 1);
8500 continue;
8501 }
8502
8503 case '{':
8504 // "{" starts a block scope
8505 // "{'a': 1}->func() is something else
8506 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8507 {
8508 line = compile_block(ea.cmd, &cctx);
8509 continue;
8510 }
8511 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512 }
8513
8514 /*
8515 * COMMAND MODIFIERS
8516 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008517 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008518 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8519 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 {
8521 if (errormsg != NULL)
8522 goto erret;
8523 // empty line or comment
8524 line = (char_u *)"";
8525 continue;
8526 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008527 generate_cmdmods(&cctx, &local_cmdmod);
8528 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008529
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008530 // Check if there was a colon after the last command modifier or before
8531 // the current position.
8532 for (p = ea.cmd; p >= line; --p)
8533 {
8534 if (*p == ':')
8535 starts_with_colon = TRUE;
8536 if (p < ea.cmd && !VIM_ISWHITE(*p))
8537 break;
8538 }
8539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008541 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008542 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008543 {
8544 if (*ea.cmd == '(')
8545 // not for "call()"
8546 ea.cmd = p;
8547 else
8548 ea.cmd = skipwhite(ea.cmd);
8549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008551 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008553 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008554
Bram Moolenaar17126b12021-01-07 22:03:02 +01008555 // Check for assignment after command modifiers.
8556 assign = may_compile_assignment(&ea, &line, &cctx);
8557 if (assign == OK)
8558 goto nextline;
8559 if (assign == FAIL)
8560 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008561 }
8562
8563 /*
8564 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008565 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008566 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008567 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008568 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008569 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008570 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008571 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008572 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008573 if (!starts_with_colon)
8574 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008575 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008576 goto erret;
8577 }
Bram Moolenaarada1d872021-02-20 08:16:51 +01008578 ea.addr_count = 1;
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008579 if (ends_excmd2(line, ea.cmd))
8580 {
8581 // A range without a command: jump to the line.
8582 // TODO: compile to a more efficient command, possibly
8583 // calling parse_cmd_address().
8584 ea.cmdidx = CMD_SIZE;
8585 line = compile_exec(line, &ea, &cctx);
8586 goto nextline;
8587 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008588 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008589 }
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01008590 p = find_ex_command(&ea, NULL, starts_with_colon
8591 ? NULL : item_exists, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008593 if (p == NULL)
8594 {
8595 if (cctx.ctx_skip != SKIP_YES)
8596 emsg(_(e_ambiguous_use_of_user_defined_command));
8597 goto erret;
8598 }
8599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008600 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8601 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008602 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008603 {
8604 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008605 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008606 }
8607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008609 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008610 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008611 // CMD_var cannot happen, compile_assignment() above would be
8612 // used. Most likely an assignment to a non-existing variable.
8613 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008614 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616 }
8617
Bram Moolenaar3988f642020-08-27 22:43:03 +02008618 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008619 && ea.cmdidx != CMD_elseif
8620 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008621 && ea.cmdidx != CMD_endif
8622 && ea.cmdidx != CMD_endfor
8623 && ea.cmdidx != CMD_endwhile
8624 && ea.cmdidx != CMD_catch
8625 && ea.cmdidx != CMD_finally
8626 && ea.cmdidx != CMD_endtry)
8627 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008628 emsg(_(e_unreachable_code_after_return));
8629 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008630 }
8631
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008632 p = skipwhite(p);
8633 if (ea.cmdidx != CMD_SIZE
8634 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8635 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008636 if (ea.cmdidx >= 0)
8637 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008638 if ((ea.argt & EX_BANG) && *p == '!')
8639 {
8640 ea.forceit = TRUE;
8641 p = skipwhite(p + 1);
8642 }
8643 }
8644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008645 switch (ea.cmdidx)
8646 {
8647 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008648 ea.arg = p;
8649 line = compile_nested_function(&ea, &cctx);
8650 break;
8651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008652 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008653 // TODO: should we allow this, e.g. to declare a global
8654 // function?
8655 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656 goto erret;
8657
8658 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008659 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008660 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008661 break;
8662
8663 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008664 emsg(_(e_cannot_use_let_in_vim9_script));
8665 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008666 case CMD_var:
8667 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 case CMD_const:
8669 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008670 if (line == p)
8671 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008672 break;
8673
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008674 case CMD_unlet:
8675 case CMD_unlockvar:
8676 case CMD_lockvar:
8677 line = compile_unletlock(p, &ea, &cctx);
8678 break;
8679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008680 case CMD_import:
8681 line = compile_import(p, &cctx);
8682 break;
8683
8684 case CMD_if:
8685 line = compile_if(p, &cctx);
8686 break;
8687 case CMD_elseif:
8688 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008689 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 break;
8691 case CMD_else:
8692 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008693 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008694 break;
8695 case CMD_endif:
8696 line = compile_endif(p, &cctx);
8697 break;
8698
8699 case CMD_while:
8700 line = compile_while(p, &cctx);
8701 break;
8702 case CMD_endwhile:
8703 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008704 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705 break;
8706
8707 case CMD_for:
8708 line = compile_for(p, &cctx);
8709 break;
8710 case CMD_endfor:
8711 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008712 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008713 break;
8714 case CMD_continue:
8715 line = compile_continue(p, &cctx);
8716 break;
8717 case CMD_break:
8718 line = compile_break(p, &cctx);
8719 break;
8720
8721 case CMD_try:
8722 line = compile_try(p, &cctx);
8723 break;
8724 case CMD_catch:
8725 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008726 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008727 break;
8728 case CMD_finally:
8729 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008730 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008731 break;
8732 case CMD_endtry:
8733 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008734 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 break;
8736 case CMD_throw:
8737 line = compile_throw(p, &cctx);
8738 break;
8739
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008740 case CMD_eval:
8741 if (compile_expr0(&p, &cctx) == FAIL)
8742 goto erret;
8743
Bram Moolenaar3988f642020-08-27 22:43:03 +02008744 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008745 generate_instr_drop(&cctx, ISN_DROP, 1);
8746
8747 line = skipwhite(p);
8748 break;
8749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008751 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008752 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008753 case CMD_echomsg:
8754 case CMD_echoerr:
8755 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008756 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008757
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008758 case CMD_put:
8759 ea.cmd = cmd;
8760 line = compile_put(p, &ea, &cctx);
8761 break;
8762
Bram Moolenaar3988f642020-08-27 22:43:03 +02008763 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008764
Bram Moolenaarae616492020-07-28 20:07:27 +02008765 case CMD_append:
8766 case CMD_change:
8767 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008768 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008769 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008770 case CMD_xit:
8771 not_in_vim9(&ea);
8772 goto erret;
8773
Bram Moolenaar002262f2020-07-08 17:47:57 +02008774 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008775 if (cctx.ctx_skip != SKIP_YES)
8776 {
8777 semsg(_(e_invalid_command_str), ea.cmd);
8778 goto erret;
8779 }
8780 // We don't check for a next command here.
8781 line = (char_u *)"";
8782 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008784 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008785 if (cctx.ctx_skip == SKIP_YES)
8786 {
8787 // We don't check for a next command here.
8788 line = (char_u *)"";
8789 }
8790 else
8791 {
8792 // Not recognized, execute with do_cmdline_cmd().
8793 ea.arg = p;
8794 line = compile_exec(line, &ea, &cctx);
8795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 break;
8797 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008798nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008799 if (line == NULL)
8800 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008801 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008802
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008803 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008804 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008806 if (cctx.ctx_type_stack.ga_len < 0)
8807 {
8808 iemsg("Type stack underflow");
8809 goto erret;
8810 }
8811 }
8812
8813 if (cctx.ctx_scope != NULL)
8814 {
8815 if (cctx.ctx_scope->se_type == IF_SCOPE)
8816 emsg(_(e_endif));
8817 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8818 emsg(_(e_endwhile));
8819 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8820 emsg(_(e_endfor));
8821 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008822 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823 goto erret;
8824 }
8825
Bram Moolenaarefd88552020-06-18 20:50:10 +02008826 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008827 {
8828 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8829 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008830 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008831 goto erret;
8832 }
8833
8834 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008835 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008836 }
8837
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008838 {
8839 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8840 + ufunc->uf_dfunc_idx;
8841 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008842 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008843#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008844 if (cctx.ctx_profiling)
8845 {
8846 dfunc->df_instr_prof = instr->ga_data;
8847 dfunc->df_instr_prof_count = instr->ga_len;
8848 }
8849 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008850#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008851 {
8852 dfunc->df_instr = instr->ga_data;
8853 dfunc->df_instr_count = instr->ga_len;
8854 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008855 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008856 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008857 if (cctx.ctx_outer_used)
8858 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008859 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008861
8862 ret = OK;
8863
8864erret:
8865 if (ret == FAIL)
8866 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008867 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008868 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8869 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008870
8871 for (idx = 0; idx < instr->ga_len; ++idx)
8872 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008873 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008874 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008875
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008876 // If using the last entry in the table and it was added above, we
8877 // might as well remove it.
8878 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008879 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008880 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008881 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008882 ufunc->uf_dfunc_idx = 0;
8883 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008884 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008885
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008886 while (cctx.ctx_scope != NULL)
8887 drop_scope(&cctx);
8888
Bram Moolenaar20431c92020-03-20 18:39:46 +01008889 // Don't execute this function body.
8890 ga_clear_strings(&ufunc->uf_lines);
8891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008892 if (errormsg != NULL)
8893 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008894 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008895 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008896 }
8897
8898 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008899 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008900 if (do_estack_push)
8901 estack_pop();
8902
Bram Moolenaar20431c92020-03-20 18:39:46 +01008903 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008904 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008906 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008907}
8908
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008909 void
8910set_function_type(ufunc_T *ufunc)
8911{
8912 int varargs = ufunc->uf_va_name != NULL;
8913 int argcount = ufunc->uf_args.ga_len;
8914
8915 // Create a type for the function, with the return type and any
8916 // argument types.
8917 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8918 // The type is included in "tt_args".
8919 if (argcount > 0 || varargs)
8920 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01008921 if (ufunc->uf_type_list.ga_itemsize == 0)
8922 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008923 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8924 argcount, &ufunc->uf_type_list);
8925 // Add argument types to the function type.
8926 if (func_type_add_arg_types(ufunc->uf_func_type,
8927 argcount + varargs,
8928 &ufunc->uf_type_list) == FAIL)
8929 return;
8930 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8931 ufunc->uf_func_type->tt_min_argcount =
8932 argcount - ufunc->uf_def_args.ga_len;
8933 if (ufunc->uf_arg_types == NULL)
8934 {
8935 int i;
8936
8937 // lambda does not have argument types.
8938 for (i = 0; i < argcount; ++i)
8939 ufunc->uf_func_type->tt_args[i] = &t_any;
8940 }
8941 else
8942 mch_memmove(ufunc->uf_func_type->tt_args,
8943 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8944 if (varargs)
8945 {
8946 ufunc->uf_func_type->tt_args[argcount] =
8947 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8948 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8949 }
8950 }
8951 else
8952 // No arguments, can use a predefined type.
8953 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8954 argcount, &ufunc->uf_type_list);
8955}
8956
8957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008958/*
8959 * Delete an instruction, free what it contains.
8960 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008961 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008962delete_instr(isn_T *isn)
8963{
8964 switch (isn->isn_type)
8965 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008966 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008968 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008969 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008970 case ISN_LOADENV:
8971 case ISN_LOADG:
8972 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008973 case ISN_LOADT:
8974 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008975 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008976 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008977 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008978 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008979 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008980 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008981 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008982 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008983 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008984 case ISN_STOREW:
8985 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008986 vim_free(isn->isn_arg.string);
8987 break;
8988
8989 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008990 case ISN_STORES:
8991 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008992 break;
8993
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008994 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008995 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008996 vim_free(isn->isn_arg.unlet.ul_name);
8997 break;
8998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008999 case ISN_STOREOPT:
9000 vim_free(isn->isn_arg.storeopt.so_name);
9001 break;
9002
9003 case ISN_PUSHBLOB: // push blob isn_arg.blob
9004 blob_unref(isn->isn_arg.blob);
9005 break;
9006
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009007 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009009 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009010#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009011 break;
9012
9013 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009014#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009015 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01009016#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01009017 break;
9018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009019 case ISN_UCALL:
9020 vim_free(isn->isn_arg.ufunc.cuf_name);
9021 break;
9022
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009023 case ISN_FUNCREF:
9024 {
9025 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9026 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01009027 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02009028
Bram Moolenaar077a4232020-12-22 18:33:27 +01009029 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
9030 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02009031 }
9032 break;
9033
9034 case ISN_DCALL:
9035 {
9036 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9037 + isn->isn_arg.dfunc.cdf_idx;
9038
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02009039 if (dfunc->df_ufunc != NULL
9040 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02009041 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02009042 }
9043 break;
9044
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009045 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02009046 {
9047 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
9048 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
9049
9050 if (ufunc != NULL)
9051 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009052 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02009053 func_ptr_unref(ufunc);
9054 }
9055
9056 vim_free(lambda);
9057 vim_free(isn->isn_arg.newfunc.nf_global);
9058 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02009059 break;
9060
Bram Moolenaar5e654232020-09-16 15:22:00 +02009061 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01009062 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02009063 free_type(isn->isn_arg.type.ct_type);
9064 break;
9065
Bram Moolenaar02194d22020-10-24 23:08:38 +02009066 case ISN_CMDMOD:
9067 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
9068 ->cmod_filter_regmatch.regprog);
9069 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
9070 break;
9071
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01009072 case ISN_LOADSCRIPT:
9073 case ISN_STORESCRIPT:
9074 vim_free(isn->isn_arg.script.scriptref);
9075 break;
9076
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009077 case ISN_TRY:
9078 vim_free(isn->isn_arg.try.try_ref);
9079 break;
9080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009081 case ISN_2BOOL:
9082 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02009083 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009084 case ISN_ADDBLOB:
9085 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009086 case ISN_ANYINDEX:
9087 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009088 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02009089 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009090 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009091 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009092 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02009093 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009094 case ISN_COMPAREANY:
9095 case ISN_COMPAREBLOB:
9096 case ISN_COMPAREBOOL:
9097 case ISN_COMPAREDICT:
9098 case ISN_COMPAREFLOAT:
9099 case ISN_COMPAREFUNC:
9100 case ISN_COMPARELIST:
9101 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009102 case ISN_COMPARESPECIAL:
9103 case ISN_COMPARESTRING:
9104 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02009105 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009106 case ISN_DROP:
9107 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02009108 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009109 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009110 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009111 case ISN_EXECCONCAT:
9112 case ISN_EXECUTE:
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01009113 case ISN_FINALLY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009114 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009115 case ISN_GETITEM:
9116 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02009117 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02009118 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02009119 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009120 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009121 case ISN_LOADBDICT:
9122 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02009123 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009124 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009125 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009126 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02009127 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02009128 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009129 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009130 case ISN_NEGATENR:
9131 case ISN_NEWDICT:
9132 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009133 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009134 case ISN_OPFLOAT:
9135 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009136 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02009137 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01009138 case ISN_PROF_END:
9139 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009140 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009141 case ISN_PUSHF:
9142 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009143 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02009144 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009145 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01009146 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009147 case ISN_SHUFFLE:
9148 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009149 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01009150 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009151 case ISN_STORENR:
9152 case ISN_STOREOUTER:
9153 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02009154 case ISN_STOREV:
9155 case ISN_STRINDEX:
9156 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009157 case ISN_THROW:
Bram Moolenaarc150c092021-02-13 15:02:46 +01009158 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01009159 case ISN_UNLETINDEX:
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01009160 case ISN_UNLETRANGE:
Bram Moolenaar792f7862020-11-23 08:31:18 +01009161 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009162 // nothing allocated
9163 break;
9164 }
9165}
9166
9167/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009168 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01009169 */
9170 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009171delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009172{
9173 int idx;
9174
9175 ga_clear(&dfunc->df_def_args_isn);
9176
9177 if (dfunc->df_instr != NULL)
9178 {
9179 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
9180 delete_instr(dfunc->df_instr + idx);
9181 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009182 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009183 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01009184#ifdef FEAT_PROFILE
9185 if (dfunc->df_instr_prof != NULL)
9186 {
9187 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
9188 delete_instr(dfunc->df_instr_prof + idx);
9189 VIM_CLEAR(dfunc->df_instr_prof);
9190 dfunc->df_instr_prof = NULL;
9191 }
9192#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01009193
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009194 if (mark_deleted)
9195 dfunc->df_deleted = TRUE;
9196 if (dfunc->df_ufunc != NULL)
9197 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01009198}
9199
9200/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009201 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009202 * function, unless another user function still uses it.
9203 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009204 */
9205 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009206unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009207{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009208 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009209 {
9210 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9211 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009212
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009213 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009214 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02009215 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009216 ufunc->uf_dfunc_idx = 0;
9217 if (dfunc->df_ufunc == ufunc)
9218 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009219 }
9220}
9221
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009222/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009223 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009224 */
9225 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009226link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009227{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009228 if (ufunc->uf_dfunc_idx > 0)
9229 {
9230 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
9231 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009232
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009233 ++dfunc->df_refcount;
9234 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02009235}
9236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009237#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01009238/*
9239 * Free all functions defined with ":def".
9240 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009241 void
9242free_def_functions(void)
9243{
Bram Moolenaar20431c92020-03-20 18:39:46 +01009244 int idx;
9245
9246 for (idx = 0; idx < def_functions.ga_len; ++idx)
9247 {
9248 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
9249
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01009250 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01009251 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009252 }
9253
9254 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009255}
9256#endif
9257
9258
9259#endif // FEAT_EVAL