blob: 4dfb42f352c4c05c1e9b699472302e8edb6e3b6d [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 Moolenaarab360522021-01-10 14:02:28 +0100264 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200265 return OK;
266 }
267 }
268
269 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270}
271
272/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200273 * Lookup a script-local variable in the current script, possibly defined in a
274 * block that contains the function "cctx->ctx_ufunc".
275 * "cctx" is NULL at the script level.
276 * if "len" is <= 0 "name" must be NUL terminated.
277 * Return NULL when not found.
278 */
279 static sallvar_T *
280find_script_var(char_u *name, size_t len, cctx_T *cctx)
281{
282 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
283 hashitem_T *hi;
284 int cc;
285 sallvar_T *sav;
286 ufunc_T *ufunc;
287
288 // Find the list of all script variables with the right name.
289 if (len > 0)
290 {
291 cc = name[len];
292 name[len] = NUL;
293 }
294 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
295 if (len > 0)
296 name[len] = cc;
297 if (HASHITEM_EMPTY(hi))
298 return NULL;
299
300 sav = HI2SAV(hi);
301 if (sav->sav_block_id == 0 || cctx == NULL)
302 // variable defined in the script scope or not in a function.
303 return sav;
304
305 // Go over the variables with this name and find one that was visible
306 // from the function.
307 ufunc = cctx->ctx_ufunc;
308 while (sav != NULL)
309 {
310 int idx;
311
312 // Go over the blocks that this function was defined in. If the
313 // variable block ID matches it was visible to the function.
314 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
315 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
316 return sav;
317 sav = sav->sav_next;
318 }
319
320 return NULL;
321}
322
323/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100324 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200325 */
326 static int
327script_is_vim9()
328{
329 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
330}
331
332/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200334 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
335 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200336 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 * Returns OK or FAIL.
338 */
339 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200340script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200342 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200344 if (current_sctx.sc_sid <= 0)
345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 is_vim9_script = script_is_vim9();
347 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200348 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200349 if (is_vim9_script)
350 {
351 // Check script variables that were visible where the function was
352 // defined.
353 if (find_script_var(name, len, cctx) != NULL)
354 return OK;
355 }
356 else
357 {
358 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
359 dictitem_T *di;
360 int cc;
361
362 // Check script variables that are currently visible
363 cc = name[len];
364 name[len] = NUL;
365 di = find_var_in_ht(ht, 0, name, TRUE);
366 name[len] = cc;
367 if (di != NULL)
368 return OK;
369 }
370
371 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372}
373
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100374/*
375 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200376 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200377 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100378 * Return FAIL and give an error if it defined.
379 */
380 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200381check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100382{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200383 int c = p[len];
384 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200385
386 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100388 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100389 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200391 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200392 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100393 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200394 // A local or script-local function can shadow a global function.
395 if (ufunc == NULL || !func_is_global(ufunc)
396 || (p[0] == 'g' && p[1] == ':'))
397 {
398 p[len] = c;
399 semsg(_(e_name_already_defined_str), p);
400 return FAIL;
401 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100402 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200403 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100404 return OK;
405}
406
Bram Moolenaar65b95452020-07-19 14:03:09 +0200407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100408/////////////////////////////////////////////////////////////////////
409// Following generate_ functions expect the caller to call ga_grow().
410
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200411#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
412#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414/*
415 * Generate an instruction without arguments.
416 * Returns a pointer to the new instruction, NULL if failed.
417 */
418 static isn_T *
419generate_instr(cctx_T *cctx, isntype_T isn_type)
420{
421 garray_T *instr = &cctx->ctx_instr;
422 isn_T *isn;
423
Bram Moolenaar080457c2020-03-03 21:53:32 +0100424 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 if (ga_grow(instr, 1) == FAIL)
426 return NULL;
427 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
428 isn->isn_type = isn_type;
429 isn->isn_lnum = cctx->ctx_lnum + 1;
430 ++instr->ga_len;
431
432 return isn;
433}
434
435/*
436 * Generate an instruction without arguments.
437 * "drop" will be removed from the stack.
438 * Returns a pointer to the new instruction, NULL if failed.
439 */
440 static isn_T *
441generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
442{
443 garray_T *stack = &cctx->ctx_type_stack;
444
Bram Moolenaar080457c2020-03-03 21:53:32 +0100445 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446 stack->ga_len -= drop;
447 return generate_instr(cctx, isn_type);
448}
449
450/*
451 * Generate instruction "isn_type" and put "type" on the type stack.
452 */
453 static isn_T *
454generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
455{
456 isn_T *isn;
457 garray_T *stack = &cctx->ctx_type_stack;
458
459 if ((isn = generate_instr(cctx, isn_type)) == NULL)
460 return NULL;
461
462 if (ga_grow(stack, 1) == FAIL)
463 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200464 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 ++stack->ga_len;
466
467 return isn;
468}
469
470/*
471 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200472 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473 */
474 static int
475may_generate_2STRING(int offset, cctx_T *cctx)
476{
477 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200478 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100480 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100482 RETURN_OK_IF_SKIP(cctx);
483 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 switch ((*type)->tt_type)
485 {
486 // nothing to be done
487 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200489 // conversion possible
490 case VAR_SPECIAL:
491 case VAR_BOOL:
492 case VAR_NUMBER:
493 case VAR_FLOAT:
494 break;
495
496 // conversion possible (with runtime check)
497 case VAR_ANY:
498 case VAR_UNKNOWN:
499 isntype = ISN_2STRING_ANY;
500 break;
501
502 // conversion not possible
503 case VAR_VOID:
504 case VAR_BLOB:
505 case VAR_FUNC:
506 case VAR_PARTIAL:
507 case VAR_LIST:
508 case VAR_DICT:
509 case VAR_JOB:
510 case VAR_CHANNEL:
511 to_string_error((*type)->tt_type);
512 return FAIL;
513 }
514
515 *type = &t_string;
516 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517 return FAIL;
518 isn->isn_arg.number = offset;
519
520 return OK;
521}
522
523 static int
524check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
525{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200526 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200528 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 {
530 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200531 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200533 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 return FAIL;
535 }
536 return OK;
537}
538
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200539 static int
540generate_add_instr(
541 cctx_T *cctx,
542 vartype_T vartype,
543 type_T *type1,
544 type_T *type2)
545{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100546 garray_T *stack = &cctx->ctx_type_stack;
547 isn_T *isn = generate_instr_drop(cctx,
548 vartype == VAR_NUMBER ? ISN_OPNR
549 : vartype == VAR_LIST ? ISN_ADDLIST
550 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200551#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100552 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200553#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100554 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200555
556 if (vartype != VAR_LIST && vartype != VAR_BLOB
557 && type1->tt_type != VAR_ANY
558 && type2->tt_type != VAR_ANY
559 && check_number_or_float(
560 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
561 return FAIL;
562
563 if (isn != NULL)
564 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100565
566 // When concatenating two lists with different member types the member type
567 // becomes "any".
568 if (vartype == VAR_LIST
569 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
570 && type1->tt_member != type2->tt_member)
571 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
572
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200573 return isn == NULL ? FAIL : OK;
574}
575
576/*
577 * Get the type to use for an instruction for an operation on "type1" and
578 * "type2". If they are matching use a type-specific instruction. Otherwise
579 * fall back to runtime type checking.
580 */
581 static vartype_T
582operator_type(type_T *type1, type_T *type2)
583{
584 if (type1->tt_type == type2->tt_type
585 && (type1->tt_type == VAR_NUMBER
586 || type1->tt_type == VAR_LIST
587#ifdef FEAT_FLOAT
588 || type1->tt_type == VAR_FLOAT
589#endif
590 || type1->tt_type == VAR_BLOB))
591 return type1->tt_type;
592 return VAR_ANY;
593}
594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595/*
596 * Generate an instruction with two arguments. The instruction depends on the
597 * type of the arguments.
598 */
599 static int
600generate_two_op(cctx_T *cctx, char_u *op)
601{
602 garray_T *stack = &cctx->ctx_type_stack;
603 type_T *type1;
604 type_T *type2;
605 vartype_T vartype;
606 isn_T *isn;
607
Bram Moolenaar080457c2020-03-03 21:53:32 +0100608 RETURN_OK_IF_SKIP(cctx);
609
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200610 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
612 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200613 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614
615 switch (*op)
616 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200617 case '+':
618 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 break;
621
622 case '-':
623 case '*':
624 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
625 op) == FAIL)
626 return FAIL;
627 if (vartype == VAR_NUMBER)
628 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
629#ifdef FEAT_FLOAT
630 else if (vartype == VAR_FLOAT)
631 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
632#endif
633 else
634 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
635 if (isn != NULL)
636 isn->isn_arg.op.op_type = *op == '*'
637 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
638 break;
639
Bram Moolenaar4c683752020-04-05 21:38:23 +0200640 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200642 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 && type2->tt_type != VAR_NUMBER))
644 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200645 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 return FAIL;
647 }
648 isn = generate_instr_drop(cctx,
649 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
650 if (isn != NULL)
651 isn->isn_arg.op.op_type = EXPR_REM;
652 break;
653 }
654
655 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200656 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 {
658 type_T *type = &t_any;
659
660#ifdef FEAT_FLOAT
661 // float+number and number+float results in float
662 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
663 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
664 type = &t_float;
665#endif
666 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
667 }
668
669 return OK;
670}
671
672/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200673 * Get the instruction to use for comparing "type1" with "type2"
674 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200676 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100677get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678{
679 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680
Bram Moolenaar4c683752020-04-05 21:38:23 +0200681 if (type1 == VAR_UNKNOWN)
682 type1 = VAR_ANY;
683 if (type2 == VAR_UNKNOWN)
684 type2 = VAR_ANY;
685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 if (type1 == type2)
687 {
688 switch (type1)
689 {
690 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
691 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
692 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
693 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
694 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
695 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
696 case VAR_LIST: isntype = ISN_COMPARELIST; break;
697 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
698 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 default: isntype = ISN_COMPAREANY; break;
700 }
701 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200702 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
704 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
705 isntype = ISN_COMPAREANY;
706
Bram Moolenaar657137c2021-01-09 15:45:23 +0100707 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 && (isntype == ISN_COMPAREBOOL
709 || isntype == ISN_COMPARESPECIAL
710 || isntype == ISN_COMPARENR
711 || isntype == ISN_COMPAREFLOAT))
712 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200713 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100714 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200715 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 }
717 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100718 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
720 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100721 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
722 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 && (type1 == VAR_BLOB || type2 == VAR_BLOB
724 || type1 == VAR_LIST || type2 == VAR_LIST))))
725 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200726 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200728 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200730 return isntype;
731}
732
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200733 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100734check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200735{
736 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
737 return FAIL;
738 return OK;
739}
740
Bram Moolenaara5565e42020-05-09 15:44:01 +0200741/*
742 * Generate an ISN_COMPARE* instruction with a boolean result.
743 */
744 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100745generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200746{
747 isntype_T isntype;
748 isn_T *isn;
749 garray_T *stack = &cctx->ctx_type_stack;
750 vartype_T type1;
751 vartype_T type2;
752
753 RETURN_OK_IF_SKIP(cctx);
754
755 // Get the known type of the two items on the stack. If they are matching
756 // use a type-specific instruction. Otherwise fall back to runtime type
757 // checking.
758 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
759 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100760 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200761 if (isntype == ISN_DROP)
762 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
764 if ((isn = generate_instr(cctx, isntype)) == NULL)
765 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100766 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 isn->isn_arg.op.op_ic = ic;
768
769 // takes two arguments, puts one bool back
770 if (stack->ga_len >= 2)
771 {
772 --stack->ga_len;
773 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
774 }
775
776 return OK;
777}
778
779/*
780 * Generate an ISN_2BOOL instruction.
781 */
782 static int
783generate_2BOOL(cctx_T *cctx, int invert)
784{
785 isn_T *isn;
786 garray_T *stack = &cctx->ctx_type_stack;
787
Bram Moolenaar080457c2020-03-03 21:53:32 +0100788 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
790 return FAIL;
791 isn->isn_arg.number = invert;
792
793 // type becomes bool
794 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
795
796 return OK;
797}
798
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200799/*
800 * Generate an ISN_COND2BOOL instruction.
801 */
802 static int
803generate_COND2BOOL(cctx_T *cctx)
804{
805 isn_T *isn;
806 garray_T *stack = &cctx->ctx_type_stack;
807
808 RETURN_OK_IF_SKIP(cctx);
809 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
810 return FAIL;
811
812 // type becomes bool
813 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
814
815 return OK;
816}
817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200819generate_TYPECHECK(
820 cctx_T *cctx,
821 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100822 int offset,
823 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824{
825 isn_T *isn;
826 garray_T *stack = &cctx->ctx_type_stack;
827
Bram Moolenaar080457c2020-03-03 21:53:32 +0100828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
830 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200831 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100832 isn->isn_arg.type.ct_off = (int8_T)offset;
833 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar5e654232020-09-16 15:22:00 +0200835 // type becomes expected
836 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
838 return OK;
839}
840
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100841 static int
842generate_SETTYPE(
843 cctx_T *cctx,
844 type_T *expected)
845{
846 isn_T *isn;
847
848 RETURN_OK_IF_SKIP(cctx);
849 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
850 return FAIL;
851 isn->isn_arg.type.ct_type = alloc_type(expected);
852 return OK;
853}
854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200856 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
857 * used. Return FALSE if the types will never match.
858 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100859 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200860use_typecheck(type_T *actual, type_T *expected)
861{
862 if (actual->tt_type == VAR_ANY
863 || actual->tt_type == VAR_UNKNOWN
864 || (actual->tt_type == VAR_FUNC
865 && (expected->tt_type == VAR_FUNC
866 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100867 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
868 && ((actual->tt_member == &t_void)
869 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 return TRUE;
871 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
872 && actual->tt_type == expected->tt_type)
873 // This takes care of a nested list or dict.
874 return use_typecheck(actual->tt_member, expected->tt_member);
875 return FALSE;
876}
877
878/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200879 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200880 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200881 * - "actual" is a type that can be "expected" type: add a runtime check; or
882 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200883 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
884 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200885 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100886 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200887need_type(
888 type_T *actual,
889 type_T *expected,
890 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100891 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200892 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200893 int silent,
894 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200895{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100896 where_T where;
897
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200898 if (expected == &t_bool && actual != &t_bool
899 && (actual->tt_flags & TTFLAG_BOOL_OK))
900 {
901 // Using "0", "1" or the result of an expression with "&&" or "||" as a
902 // boolean is OK but requires a conversion.
903 generate_2BOOL(cctx, FALSE);
904 return OK;
905 }
906
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100907 where.wt_index = arg_idx;
908 where.wt_variable = FALSE;
909 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200910 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200911
912 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200913 // If it's a constant a runtime check makes no sense.
914 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200915 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100916 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200917 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200918 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200919
920 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100921 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200922 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200923}
924
925/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100926 * Check that the top of the type stack has a type that can be used as a
927 * condition. Give an error and return FAIL if not.
928 */
929 static int
930bool_on_stack(cctx_T *cctx)
931{
932 garray_T *stack = &cctx->ctx_type_stack;
933 type_T *type;
934
935 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
936 if (type == &t_bool)
937 return OK;
938
939 if (type == &t_any || type == &t_number)
940 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
941 // This requires a runtime type check.
942 return generate_COND2BOOL(cctx);
943
Bram Moolenaar351ead02021-01-16 16:07:01 +0100944 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100945}
946
947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 * Generate an ISN_PUSHNR instruction.
949 */
950 static int
951generate_PUSHNR(cctx_T *cctx, varnumber_T number)
952{
953 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200954 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
Bram Moolenaar080457c2020-03-03 21:53:32 +0100956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
958 return FAIL;
959 isn->isn_arg.number = number;
960
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200961 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200962 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100963 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 return OK;
965}
966
967/*
968 * Generate an ISN_PUSHBOOL instruction.
969 */
970 static int
971generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
972{
973 isn_T *isn;
974
Bram Moolenaar080457c2020-03-03 21:53:32 +0100975 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
977 return FAIL;
978 isn->isn_arg.number = number;
979
980 return OK;
981}
982
983/*
984 * Generate an ISN_PUSHSPEC instruction.
985 */
986 static int
987generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
988{
989 isn_T *isn;
990
Bram Moolenaar080457c2020-03-03 21:53:32 +0100991 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
993 return FAIL;
994 isn->isn_arg.number = number;
995
996 return OK;
997}
998
999#ifdef FEAT_FLOAT
1000/*
1001 * Generate an ISN_PUSHF instruction.
1002 */
1003 static int
1004generate_PUSHF(cctx_T *cctx, float_T fnumber)
1005{
1006 isn_T *isn;
1007
Bram Moolenaar080457c2020-03-03 21:53:32 +01001008 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1010 return FAIL;
1011 isn->isn_arg.fnumber = fnumber;
1012
1013 return OK;
1014}
1015#endif
1016
1017/*
1018 * Generate an ISN_PUSHS instruction.
1019 * Consumes "str".
1020 */
1021 static int
1022generate_PUSHS(cctx_T *cctx, char_u *str)
1023{
1024 isn_T *isn;
1025
Bram Moolenaar508b5612021-01-02 18:17:26 +01001026 if (cctx->ctx_skip == SKIP_YES)
1027 {
1028 vim_free(str);
1029 return OK;
1030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1032 return FAIL;
1033 isn->isn_arg.string = str;
1034
1035 return OK;
1036}
1037
1038/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001039 * Generate an ISN_PUSHCHANNEL instruction.
1040 * Consumes "channel".
1041 */
1042 static int
1043generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1044{
1045 isn_T *isn;
1046
Bram Moolenaar080457c2020-03-03 21:53:32 +01001047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001048 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.channel = channel;
1051
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_PUSHJOB instruction.
1057 * Consumes "job".
1058 */
1059 static int
1060generate_PUSHJOB(cctx_T *cctx, job_T *job)
1061{
1062 isn_T *isn;
1063
Bram Moolenaar080457c2020-03-03 21:53:32 +01001064 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001065 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001066 return FAIL;
1067 isn->isn_arg.job = job;
1068
1069 return OK;
1070}
1071
1072/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 * Generate an ISN_PUSHBLOB instruction.
1074 * Consumes "blob".
1075 */
1076 static int
1077generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1078{
1079 isn_T *isn;
1080
Bram Moolenaar080457c2020-03-03 21:53:32 +01001081 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1083 return FAIL;
1084 isn->isn_arg.blob = blob;
1085
1086 return OK;
1087}
1088
1089/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090 * Generate an ISN_PUSHFUNC instruction with name "name".
1091 * Consumes "name".
1092 */
1093 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001094generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001095{
1096 isn_T *isn;
1097
Bram Moolenaar080457c2020-03-03 21:53:32 +01001098 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001099 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001100 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001101 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001102
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001107 * Generate an ISN_GETITEM instruction with "index".
1108 */
1109 static int
1110generate_GETITEM(cctx_T *cctx, int index)
1111{
1112 isn_T *isn;
1113 garray_T *stack = &cctx->ctx_type_stack;
1114 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1115 type_T *item_type = &t_any;
1116
1117 RETURN_OK_IF_SKIP(cctx);
1118
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001119 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001120 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001121 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001122 emsg(_(e_listreq));
1123 return FAIL;
1124 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001125 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001126 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1127 return FAIL;
1128 isn->isn_arg.number = index;
1129
1130 // add the item type to the type stack
1131 if (ga_grow(stack, 1) == FAIL)
1132 return FAIL;
1133 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1134 ++stack->ga_len;
1135 return OK;
1136}
1137
1138/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001139 * Generate an ISN_SLICE instruction with "count".
1140 */
1141 static int
1142generate_SLICE(cctx_T *cctx, int count)
1143{
1144 isn_T *isn;
1145
1146 RETURN_OK_IF_SKIP(cctx);
1147 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1148 return FAIL;
1149 isn->isn_arg.number = count;
1150 return OK;
1151}
1152
1153/*
1154 * Generate an ISN_CHECKLEN instruction with "min_len".
1155 */
1156 static int
1157generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1158{
1159 isn_T *isn;
1160
1161 RETURN_OK_IF_SKIP(cctx);
1162
1163 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1164 return FAIL;
1165 isn->isn_arg.checklen.cl_min_len = min_len;
1166 isn->isn_arg.checklen.cl_more_OK = more_OK;
1167
1168 return OK;
1169}
1170
1171/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 * Generate an ISN_STORE instruction.
1173 */
1174 static int
1175generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1176{
1177 isn_T *isn;
1178
Bram Moolenaar080457c2020-03-03 21:53:32 +01001179 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1181 return FAIL;
1182 if (name != NULL)
1183 isn->isn_arg.string = vim_strsave(name);
1184 else
1185 isn->isn_arg.number = idx;
1186
1187 return OK;
1188}
1189
1190/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001191 * Generate an ISN_STOREOUTER instruction.
1192 */
1193 static int
1194generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1195{
1196 isn_T *isn;
1197
1198 RETURN_OK_IF_SKIP(cctx);
1199 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1200 return FAIL;
1201 isn->isn_arg.outer.outer_idx = idx;
1202 isn->isn_arg.outer.outer_depth = level;
1203
1204 return OK;
1205}
1206
1207/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1209 */
1210 static int
1211generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1212{
1213 isn_T *isn;
1214
Bram Moolenaar080457c2020-03-03 21:53:32 +01001215 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1217 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001218 isn->isn_arg.storenr.stnr_idx = idx;
1219 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220
1221 return OK;
1222}
1223
1224/*
1225 * Generate an ISN_STOREOPT instruction
1226 */
1227 static int
1228generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1229{
1230 isn_T *isn;
1231
Bram Moolenaar080457c2020-03-03 21:53:32 +01001232 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001233 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 return FAIL;
1235 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1236 isn->isn_arg.storeopt.so_flags = opt_flags;
1237
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_LOAD or similar instruction.
1243 */
1244 static int
1245generate_LOAD(
1246 cctx_T *cctx,
1247 isntype_T isn_type,
1248 int idx,
1249 char_u *name,
1250 type_T *type)
1251{
1252 isn_T *isn;
1253
Bram Moolenaar080457c2020-03-03 21:53:32 +01001254 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1256 return FAIL;
1257 if (name != NULL)
1258 isn->isn_arg.string = vim_strsave(name);
1259 else
1260 isn->isn_arg.number = idx;
1261
1262 return OK;
1263}
1264
1265/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001266 * Generate an ISN_LOADOUTER instruction
1267 */
1268 static int
1269generate_LOADOUTER(
1270 cctx_T *cctx,
1271 int idx,
1272 int nesting,
1273 type_T *type)
1274{
1275 isn_T *isn;
1276
1277 RETURN_OK_IF_SKIP(cctx);
1278 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1279 return FAIL;
1280 isn->isn_arg.outer.outer_idx = idx;
1281 isn->isn_arg.outer.outer_depth = nesting;
1282
1283 return OK;
1284}
1285
1286/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001287 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288 */
1289 static int
1290generate_LOADV(
1291 cctx_T *cctx,
1292 char_u *name,
1293 int error)
1294{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001295 int di_flags;
1296 int vidx = find_vim_var(name, &di_flags);
1297 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001298
Bram Moolenaar080457c2020-03-03 21:53:32 +01001299 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300 if (vidx < 0)
1301 {
1302 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001303 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001304 return FAIL;
1305 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001306 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001307
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001308 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001309}
1310
1311/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001312 * Generate an ISN_UNLET instruction.
1313 */
1314 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001315generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001316{
1317 isn_T *isn;
1318
1319 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001320 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001321 return FAIL;
1322 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1323 isn->isn_arg.unlet.ul_forceit = forceit;
1324
1325 return OK;
1326}
1327
1328/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001329 * Generate an ISN_LOCKCONST instruction.
1330 */
1331 static int
1332generate_LOCKCONST(cctx_T *cctx)
1333{
1334 isn_T *isn;
1335
1336 RETURN_OK_IF_SKIP(cctx);
1337 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1338 return FAIL;
1339 return OK;
1340}
1341
1342/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 * Generate an ISN_LOADS instruction.
1344 */
1345 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001346generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 int sid,
1351 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352{
1353 isn_T *isn;
1354
Bram Moolenaar080457c2020-03-03 21:53:32 +01001355 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001356 if (isn_type == ISN_LOADS)
1357 isn = generate_instr_type(cctx, isn_type, type);
1358 else
1359 isn = generate_instr_drop(cctx, isn_type, 1);
1360 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1363 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1370 */
1371 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001372generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 cctx_T *cctx,
1374 isntype_T isn_type,
1375 int sid,
1376 int idx,
1377 type_T *type)
1378{
1379 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001380 scriptref_T *sref;
1381 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
Bram Moolenaar080457c2020-03-03 21:53:32 +01001383 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 if (isn_type == ISN_LOADSCRIPT)
1385 isn = generate_instr_type(cctx, isn_type, type);
1386 else
1387 isn = generate_instr_drop(cctx, isn_type, 1);
1388 if (isn == NULL)
1389 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001390
1391 // This requires three arguments, which doesn't fit in an instruction, thus
1392 // we need to allocate a struct for this.
1393 sref = ALLOC_ONE(scriptref_T);
1394 if (sref == NULL)
1395 return FAIL;
1396 isn->isn_arg.script.scriptref = sref;
1397 sref->sref_sid = sid;
1398 sref->sref_idx = idx;
1399 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001400 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 return OK;
1402}
1403
1404/*
1405 * Generate an ISN_NEWLIST instruction.
1406 */
1407 static int
1408generate_NEWLIST(cctx_T *cctx, int count)
1409{
1410 isn_T *isn;
1411 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 type_T *type;
1413 type_T *member;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1417 return FAIL;
1418 isn->isn_arg.number = count;
1419
Bram Moolenaar127542b2020-08-09 17:22:04 +02001420 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001421 if (count == 0)
1422 member = &t_void;
1423 else
1424 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001425 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1426 cctx->ctx_type_list);
1427 type = get_list_type(member, cctx->ctx_type_list);
1428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 // drop the value types
1430 stack->ga_len -= count;
1431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 // add the list type to the type stack
1433 if (ga_grow(stack, 1) == FAIL)
1434 return FAIL;
1435 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1436 ++stack->ga_len;
1437
1438 return OK;
1439}
1440
1441/*
1442 * Generate an ISN_NEWDICT instruction.
1443 */
1444 static int
1445generate_NEWDICT(cctx_T *cctx, int count)
1446{
1447 isn_T *isn;
1448 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 type_T *type;
1450 type_T *member;
1451
Bram Moolenaar080457c2020-03-03 21:53:32 +01001452 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1454 return FAIL;
1455 isn->isn_arg.number = count;
1456
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001457 if (count == 0)
1458 member = &t_void;
1459 else
1460 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001461 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1462 cctx->ctx_type_list);
1463 type = get_dict_type(member, cctx->ctx_type_list);
1464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 // drop the key and value types
1466 stack->ga_len -= 2 * count;
1467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 // add the dict type to the type stack
1469 if (ga_grow(stack, 1) == FAIL)
1470 return FAIL;
1471 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1472 ++stack->ga_len;
1473
1474 return OK;
1475}
1476
1477/*
1478 * Generate an ISN_FUNCREF instruction.
1479 */
1480 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001481generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482{
1483 isn_T *isn;
1484 garray_T *stack = &cctx->ctx_type_stack;
1485
Bram Moolenaar080457c2020-03-03 21:53:32 +01001486 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1488 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001489 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001490 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
Bram Moolenaarab360522021-01-10 14:02:28 +01001492 // if the referenced function is a closure, it may use items further up in
1493 // the nested context, including this one.
1494 if (ufunc->uf_flags & FC_CLOSURE)
1495 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 if (ga_grow(stack, 1) == FAIL)
1498 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001499 ((type_T **)stack->ga_data)[stack->ga_len] =
1500 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 ++stack->ga_len;
1502
1503 return OK;
1504}
1505
1506/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001507 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001508 * "lambda_name" and "func_name" must be in allocated memory and will be
1509 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001510 */
1511 static int
1512generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1513{
1514 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001515
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001516 if (cctx->ctx_skip == SKIP_YES)
1517 {
1518 vim_free(lambda_name);
1519 vim_free(func_name);
1520 return OK;
1521 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001522 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001523 {
1524 vim_free(lambda_name);
1525 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001526 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001527 }
1528 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001529 isn->isn_arg.newfunc.nf_global = func_name;
1530
1531 return OK;
1532}
1533
1534/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001535 * Generate an ISN_DEF instruction: list functions
1536 */
1537 static int
1538generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1539{
1540 isn_T *isn;
1541
1542 RETURN_OK_IF_SKIP(cctx);
1543 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1544 return FAIL;
1545 if (len > 0)
1546 {
1547 isn->isn_arg.string = vim_strnsave(name, len);
1548 if (isn->isn_arg.string == NULL)
1549 return FAIL;
1550 }
1551 return OK;
1552}
1553
1554/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 * Generate an ISN_JUMP instruction.
1556 */
1557 static int
1558generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1559{
1560 isn_T *isn;
1561 garray_T *stack = &cctx->ctx_type_stack;
1562
Bram Moolenaar080457c2020-03-03 21:53:32 +01001563 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1565 return FAIL;
1566 isn->isn_arg.jump.jump_when = when;
1567 isn->isn_arg.jump.jump_where = where;
1568
1569 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1570 --stack->ga_len;
1571
1572 return OK;
1573}
1574
1575 static int
1576generate_FOR(cctx_T *cctx, int loop_idx)
1577{
1578 isn_T *isn;
1579 garray_T *stack = &cctx->ctx_type_stack;
1580
Bram Moolenaar080457c2020-03-03 21:53:32 +01001581 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1583 return FAIL;
1584 isn->isn_arg.forloop.for_idx = loop_idx;
1585
1586 if (ga_grow(stack, 1) == FAIL)
1587 return FAIL;
1588 // type doesn't matter, will be stored next
1589 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1590 ++stack->ga_len;
1591
1592 return OK;
1593}
1594
1595/*
1596 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001597 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 * Return FAIL if the number of arguments is wrong.
1599 */
1600 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001601generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602{
1603 isn_T *isn;
1604 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001605 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001606 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001607 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608
Bram Moolenaar080457c2020-03-03 21:53:32 +01001609 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001610 argoff = check_internal_func(func_idx, argcount);
1611 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 return FAIL;
1613
Bram Moolenaar389df252020-07-09 21:20:47 +02001614 if (method_call && argoff > 1)
1615 {
1616 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1617 return FAIL;
1618 isn->isn_arg.shuffle.shfl_item = argcount;
1619 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1620 }
1621
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001622 if (argcount > 0)
1623 {
1624 // Check the types of the arguments.
1625 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001626 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1627 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001628 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001629 if (internal_func_is_map(func_idx))
1630 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001631 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1634 return FAIL;
1635 isn->isn_arg.bfunc.cbf_idx = func_idx;
1636 isn->isn_arg.bfunc.cbf_argcount = argcount;
1637
Bram Moolenaar94738d82020-10-21 14:25:07 +02001638 // Drop the argument types and push the return type.
1639 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 if (ga_grow(stack, 1) == FAIL)
1641 return FAIL;
1642 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001643 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001644 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001646 if (maptype != NULL && maptype->tt_member != NULL
1647 && maptype->tt_member != &t_any)
1648 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001649 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 return OK;
1652}
1653
1654/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001655 * Generate an ISN_LISTAPPEND instruction. Works like add().
1656 * Argument count is already checked.
1657 */
1658 static int
1659generate_LISTAPPEND(cctx_T *cctx)
1660{
1661 garray_T *stack = &cctx->ctx_type_stack;
1662 type_T *list_type;
1663 type_T *item_type;
1664 type_T *expected;
1665
1666 // Caller already checked that list_type is a list.
1667 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1668 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1669 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001670 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001671 return FAIL;
1672
1673 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1674 return FAIL;
1675
1676 --stack->ga_len; // drop the argument
1677 return OK;
1678}
1679
1680/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001681 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1682 * Argument count is already checked.
1683 */
1684 static int
1685generate_BLOBAPPEND(cctx_T *cctx)
1686{
1687 garray_T *stack = &cctx->ctx_type_stack;
1688 type_T *item_type;
1689
1690 // Caller already checked that blob_type is a blob.
1691 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001692 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001693 return FAIL;
1694
1695 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1696 return FAIL;
1697
1698 --stack->ga_len; // drop the argument
1699 return OK;
1700}
1701
1702/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001703 * Return TRUE if "ufunc" should be compiled, taking into account whether
1704 * "profile" indicates profiling is to be done.
1705 */
1706 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001707func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001708{
1709 switch (ufunc->uf_def_status)
1710 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001711 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001712 case UF_TO_BE_COMPILED: return TRUE;
1713 case UF_COMPILED:
1714 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001715#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1717 + ufunc->uf_dfunc_idx;
1718
1719 return profile ? dfunc->df_instr_prof == NULL
1720 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001721#else
1722 break;
1723#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001724 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001725 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001726 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001727 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001728}
1729
1730/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 * Generate an ISN_DCALL or ISN_UCALL instruction.
1732 * Return FAIL if the number of arguments is wrong.
1733 */
1734 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001735generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736{
1737 isn_T *isn;
1738 garray_T *stack = &cctx->ctx_type_stack;
1739 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001740 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
Bram Moolenaar080457c2020-03-03 21:53:32 +01001742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if (argcount > regular_args && !has_varargs(ufunc))
1744 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001745 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 return FAIL;
1747 }
1748 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1749 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001750 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 return FAIL;
1752 }
1753
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001754 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001755 {
1756 int i;
1757
1758 for (i = 0; i < argcount; ++i)
1759 {
1760 type_T *expected;
1761 type_T *actual;
1762
1763 if (i < regular_args)
1764 {
1765 if (ufunc->uf_arg_types == NULL)
1766 continue;
1767 expected = ufunc->uf_arg_types[i];
1768 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001769 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1770 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001771 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001772 else
1773 expected = ufunc->uf_va_type->tt_member;
1774 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001775 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001776 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001777 {
1778 arg_type_mismatch(expected, actual, i + 1);
1779 return FAIL;
1780 }
1781 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001782 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001783 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001784 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001785 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001786 }
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001789 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001790 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001792 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 {
1794 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1795 isn->isn_arg.dfunc.cdf_argcount = argcount;
1796 }
1797 else
1798 {
1799 // A user function may be deleted and redefined later, can't use the
1800 // ufunc pointer, need to look it up again at runtime.
1801 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1802 isn->isn_arg.ufunc.cuf_argcount = argcount;
1803 }
1804
1805 stack->ga_len -= argcount; // drop the arguments
1806 if (ga_grow(stack, 1) == FAIL)
1807 return FAIL;
1808 // add return value
1809 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1810 ++stack->ga_len;
1811
1812 return OK;
1813}
1814
1815/*
1816 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1817 */
1818 static int
1819generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1820{
1821 isn_T *isn;
1822 garray_T *stack = &cctx->ctx_type_stack;
1823
Bram Moolenaar080457c2020-03-03 21:53:32 +01001824 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1826 return FAIL;
1827 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1828 isn->isn_arg.ufunc.cuf_argcount = argcount;
1829
1830 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001831 if (ga_grow(stack, 1) == FAIL)
1832 return FAIL;
1833 // add return value
1834 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1835 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836
1837 return OK;
1838}
1839
1840/*
1841 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001842 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 */
1844 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001845generate_PCALL(
1846 cctx_T *cctx,
1847 int argcount,
1848 char_u *name,
1849 type_T *type,
1850 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851{
1852 isn_T *isn;
1853 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001854 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855
Bram Moolenaar080457c2020-03-03 21:53:32 +01001856 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001857
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001858 if (type->tt_type == VAR_ANY)
1859 ret_type = &t_any;
1860 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001861 {
1862 if (type->tt_argcount != -1)
1863 {
1864 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1865
1866 if (argcount < type->tt_min_argcount - varargs)
1867 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001868 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001869 return FAIL;
1870 }
1871 if (!varargs && argcount > type->tt_argcount)
1872 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001873 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001874 return FAIL;
1875 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001876 if (type->tt_args != NULL)
1877 {
1878 int i;
1879
1880 for (i = 0; i < argcount; ++i)
1881 {
1882 int offset = -argcount + i - 1;
1883 type_T *actual = ((type_T **)stack->ga_data)[
1884 stack->ga_len + offset];
1885 type_T *expected;
1886
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001887 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001888 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001889 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001890 else
1891 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001892 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001893 cctx, TRUE, FALSE) == FAIL)
1894 {
1895 arg_type_mismatch(expected, actual, i + 1);
1896 return FAIL;
1897 }
1898 }
1899 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001900 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001901 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001902 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001903 else
1904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001905 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001906 return FAIL;
1907 }
1908
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1910 return FAIL;
1911 isn->isn_arg.pfunc.cpf_top = at_top;
1912 isn->isn_arg.pfunc.cpf_argcount = argcount;
1913
1914 stack->ga_len -= argcount; // drop the arguments
1915
1916 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001917 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001919 // If partial is above the arguments it must be cleared and replaced with
1920 // the return value.
1921 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1922 return FAIL;
1923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 return OK;
1925}
1926
1927/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001928 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 */
1930 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001931generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932{
1933 isn_T *isn;
1934 garray_T *stack = &cctx->ctx_type_stack;
1935 type_T *type;
1936
Bram Moolenaar080457c2020-03-03 21:53:32 +01001937 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001938 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001940 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001942 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001944 if (type->tt_type != VAR_DICT && type != &t_any)
1945 {
1946 emsg(_(e_dictreq));
1947 return FAIL;
1948 }
1949 // change dict type to dict member type
1950 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001951 {
1952 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1953 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
1956 return OK;
1957}
1958
1959/*
1960 * Generate an ISN_ECHO instruction.
1961 */
1962 static int
1963generate_ECHO(cctx_T *cctx, int with_white, int count)
1964{
1965 isn_T *isn;
1966
Bram Moolenaar080457c2020-03-03 21:53:32 +01001967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1969 return FAIL;
1970 isn->isn_arg.echo.echo_with_white = with_white;
1971 isn->isn_arg.echo.echo_count = count;
1972
1973 return OK;
1974}
1975
Bram Moolenaarad39c092020-02-26 18:23:43 +01001976/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001977 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001978 */
1979 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001980generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001981{
1982 isn_T *isn;
1983
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001984 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001985 return FAIL;
1986 isn->isn_arg.number = count;
1987
1988 return OK;
1989}
1990
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001991/*
1992 * Generate an ISN_PUT instruction.
1993 */
1994 static int
1995generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1996{
1997 isn_T *isn;
1998
1999 RETURN_OK_IF_SKIP(cctx);
2000 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2001 return FAIL;
2002 isn->isn_arg.put.put_regname = regname;
2003 isn->isn_arg.put.put_lnum = lnum;
2004 return OK;
2005}
2006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 static int
2008generate_EXEC(cctx_T *cctx, char_u *line)
2009{
2010 isn_T *isn;
2011
Bram Moolenaar080457c2020-03-03 21:53:32 +01002012 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2014 return FAIL;
2015 isn->isn_arg.string = vim_strsave(line);
2016 return OK;
2017}
2018
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002019 static int
2020generate_EXECCONCAT(cctx_T *cctx, int count)
2021{
2022 isn_T *isn;
2023
2024 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2025 return FAIL;
2026 isn->isn_arg.number = count;
2027 return OK;
2028}
2029
Bram Moolenaar08597872020-12-10 19:43:40 +01002030/*
2031 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2032 */
2033 static int
2034generate_RANGE(cctx_T *cctx, char_u *range)
2035{
2036 isn_T *isn;
2037 garray_T *stack = &cctx->ctx_type_stack;
2038
2039 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2040 return FAIL;
2041 isn->isn_arg.string = range;
2042
2043 if (ga_grow(stack, 1) == FAIL)
2044 return FAIL;
2045 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2046 ++stack->ga_len;
2047 return OK;
2048}
2049
Bram Moolenaar792f7862020-11-23 08:31:18 +01002050 static int
2051generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2052{
2053 isn_T *isn;
2054
2055 RETURN_OK_IF_SKIP(cctx);
2056 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2057 return FAIL;
2058 isn->isn_arg.unpack.unp_count = var_count;
2059 isn->isn_arg.unpack.unp_semicolon = semicolon;
2060 return OK;
2061}
2062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002064 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002065 */
2066 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002067generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002068{
2069 isn_T *isn;
2070
Bram Moolenaar02194d22020-10-24 23:08:38 +02002071 if (cmod->cmod_flags != 0
2072 || cmod->cmod_split != 0
2073 || cmod->cmod_verbose != 0
2074 || cmod->cmod_tab != 0
2075 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002076 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002077 cctx->ctx_has_cmdmod = TRUE;
2078
2079 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002080 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002081 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2082 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2083 return FAIL;
2084 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002085 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002086 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002087 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002088
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002089 return OK;
2090}
2091
2092 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002093generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002094{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002095 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2096 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002097 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002098 return OK;
2099}
2100
Bram Moolenaarf002a412021-01-24 13:34:18 +01002101#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002102 static void
2103may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2104{
2105 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002106 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002107}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002108#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002109
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002112 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002114 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002115reserve_local(
2116 cctx_T *cctx,
2117 char_u *name,
2118 size_t len,
2119 int isConst,
2120 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 lvar_T *lvar;
2123
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002124 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002126 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002127 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 }
2129
2130 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002131 return NULL;
2132 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002133 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002135 // Every local variable uses the next entry on the stack. We could re-use
2136 // the last ones when leaving a scope, but then variables used in a closure
2137 // might get overwritten. To keep things simple do not re-use stack
2138 // entries. This is less efficient, but memory is cheap these days.
2139 lvar->lv_idx = cctx->ctx_locals_count++;
2140
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002141 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 lvar->lv_const = isConst;
2143 lvar->lv_type = type;
2144
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002145 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146}
2147
2148/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002149 * Remove local variables above "new_top".
2150 */
2151 static void
2152unwind_locals(cctx_T *cctx, int new_top)
2153{
2154 if (cctx->ctx_locals.ga_len > new_top)
2155 {
2156 int idx;
2157 lvar_T *lvar;
2158
2159 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2160 {
2161 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2162 vim_free(lvar->lv_name);
2163 }
2164 }
2165 cctx->ctx_locals.ga_len = new_top;
2166}
2167
2168/*
2169 * Free all local variables.
2170 */
2171 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002172free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002173{
2174 unwind_locals(cctx, 0);
2175 ga_clear(&cctx->ctx_locals);
2176}
2177
2178/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002179 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2180 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2181 * error if the variable was defined with :const.
2182 */
2183 static int
2184check_item_writable(svar_T *sv, int check_writable, char_u *name)
2185{
2186 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2187 || (check_writable == ASSIGN_FINAL
2188 && sv->sv_const == ASSIGN_CONST))
2189 {
2190 semsg(_(e_readonlyvar), name);
2191 return FAIL;
2192 }
2193 return OK;
2194}
2195
2196/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002198 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 * Returns the index in "sn_var_vals" if found.
2200 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002201 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 */
2203 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002204get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205{
2206 hashtab_T *ht;
2207 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002208 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002209 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 int idx;
2211
Bram Moolenaare3d46852020-08-29 13:39:17 +02002212 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002214 if (sid == current_sctx.sc_sid)
2215 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002216 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002217
2218 if (sav == NULL)
2219 return -2;
2220 idx = sav->sav_var_vals_idx;
2221 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002222 if (check_item_writable(sv, check_writable, name) == FAIL)
2223 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002224 return idx;
2225 }
2226
2227 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 ht = &SCRIPT_VARS(sid);
2229 di = find_var_in_ht(ht, 0, name, TRUE);
2230 if (di == NULL)
2231 return -2;
2232
2233 // Now find the svar_T index in sn_var_vals.
2234 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2235 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002236 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 if (sv->sv_tv == &di->di_tv)
2238 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002239 if (check_item_writable(sv, check_writable, name) == FAIL)
2240 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 return idx;
2242 }
2243 }
2244 return -1;
2245}
2246
2247/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002248 * Find "name" in imported items of the current script or in "cctx" if not
2249 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 */
2251 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002252find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 int idx;
2255
Bram Moolenaare3d46852020-08-29 13:39:17 +02002256 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002257 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 if (cctx != NULL)
2259 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2260 {
2261 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2262 + idx;
2263
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002264 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2265 : STRLEN(import->imp_name) == len
2266 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 return import;
2268 }
2269
Bram Moolenaarefa94442020-08-08 22:16:00 +02002270 return find_imported_in_script(name, len, current_sctx.sc_sid);
2271}
2272
2273 imported_T *
2274find_imported_in_script(char_u *name, size_t len, int sid)
2275{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002276 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002277 int idx;
2278
Bram Moolenaare3d46852020-08-29 13:39:17 +02002279 if (!SCRIPT_ID_VALID(sid))
2280 return NULL;
2281 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2283 {
2284 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2285
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002286 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2287 : STRLEN(import->imp_name) == len
2288 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 return import;
2290 }
2291 return NULL;
2292}
2293
2294/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002295 * Free all imported variables.
2296 */
2297 static void
2298free_imported(cctx_T *cctx)
2299{
2300 int idx;
2301
2302 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2303 {
2304 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2305
2306 vim_free(import->imp_name);
2307 }
2308 ga_clear(&cctx->ctx_imports);
2309}
2310
2311/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002312 * Return a pointer to the next line that isn't empty or only contains a
2313 * comment. Skips over white space.
2314 * Returns NULL if there is none.
2315 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002316 char_u *
2317peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002318{
2319 int lnum = cctx->ctx_lnum;
2320
2321 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2322 {
2323 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002324 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002325
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002326 // ignore NULLs inserted for continuation lines
2327 if (line != NULL)
2328 {
2329 p = skipwhite(line);
2330 if (*p != NUL && !vim9_comment_start(p))
2331 return p;
2332 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002333 }
2334 return NULL;
2335}
2336
2337/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002338 * Called when checking for a following operator at "arg". When the rest of
2339 * the line is empty or only a comment, peek the next line. If there is a next
2340 * line return a pointer to it and set "nextp".
2341 * Otherwise skip over white space.
2342 */
2343 static char_u *
2344may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2345{
2346 char_u *p = skipwhite(arg);
2347
2348 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002349 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002350 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002351 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002352 if (*nextp != NULL)
2353 return *nextp;
2354 }
2355 return p;
2356}
2357
2358/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002359 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002360 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002361 * Returns NULL when at the end.
2362 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002363 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002364next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002365{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002366 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002367
2368 do
2369 {
2370 ++cctx->ctx_lnum;
2371 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002372 {
2373 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002374 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002375 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002376 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002377 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002378 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002379 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002380 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002381 return line;
2382}
2383
2384/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002385 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002386 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002387 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002388 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2389 */
2390 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002391may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002392{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002393 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002394 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002395 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002396 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002397
2398 if (next == NULL)
2399 return FAIL;
2400 *arg = skipwhite(next);
2401 }
2402 return OK;
2403}
2404
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002405/*
2406 * Idem, and give an error when failed.
2407 */
2408 static int
2409may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2410{
2411 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2412 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002413 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002414 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002415 return FAIL;
2416 }
2417 return OK;
2418}
2419
2420
Bram Moolenaara5565e42020-05-09 15:44:01 +02002421// Structure passed between the compile_expr* functions to keep track of
2422// constants that have been parsed but for which no code was produced yet. If
2423// possible expressions on these constants are applied at compile time. If
2424// that is not possible, the code to push the constants needs to be generated
2425// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002426// Using 50 should be more than enough of 5 levels of ().
2427#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002428typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002429 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002430 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002431 int pp_is_const; // all generated code was constants, used for a
2432 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002433} ppconst_T;
2434
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002435static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002436static int compile_expr0(char_u **arg, cctx_T *cctx);
2437static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2438
Bram Moolenaara5565e42020-05-09 15:44:01 +02002439/*
2440 * Generate a PUSH instruction for "tv".
2441 * "tv" will be consumed or cleared.
2442 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2443 */
2444 static int
2445generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2446{
2447 if (tv != NULL)
2448 {
2449 switch (tv->v_type)
2450 {
2451 case VAR_UNKNOWN:
2452 break;
2453 case VAR_BOOL:
2454 generate_PUSHBOOL(cctx, tv->vval.v_number);
2455 break;
2456 case VAR_SPECIAL:
2457 generate_PUSHSPEC(cctx, tv->vval.v_number);
2458 break;
2459 case VAR_NUMBER:
2460 generate_PUSHNR(cctx, tv->vval.v_number);
2461 break;
2462#ifdef FEAT_FLOAT
2463 case VAR_FLOAT:
2464 generate_PUSHF(cctx, tv->vval.v_float);
2465 break;
2466#endif
2467 case VAR_BLOB:
2468 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2469 tv->vval.v_blob = NULL;
2470 break;
2471 case VAR_STRING:
2472 generate_PUSHS(cctx, tv->vval.v_string);
2473 tv->vval.v_string = NULL;
2474 break;
2475 default:
2476 iemsg("constant type not supported");
2477 clear_tv(tv);
2478 return FAIL;
2479 }
2480 tv->v_type = VAR_UNKNOWN;
2481 }
2482 return OK;
2483}
2484
2485/*
2486 * Generate code for any ppconst entries.
2487 */
2488 static int
2489generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2490{
2491 int i;
2492 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002493 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002494
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002495 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002496 for (i = 0; i < ppconst->pp_used; ++i)
2497 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2498 ret = FAIL;
2499 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002500 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002501 return ret;
2502}
2503
2504/*
2505 * Clear ppconst constants. Used when failing.
2506 */
2507 static void
2508clear_ppconst(ppconst_T *ppconst)
2509{
2510 int i;
2511
2512 for (i = 0; i < ppconst->pp_used; ++i)
2513 clear_tv(&ppconst->pp_tv[i]);
2514 ppconst->pp_used = 0;
2515}
2516
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002517/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002518 * Generate an instruction to load script-local variable "name", without the
2519 * leading "s:".
2520 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 */
2522 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002523compile_load_scriptvar(
2524 cctx_T *cctx,
2525 char_u *name, // variable NUL terminated
2526 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002527 char_u **end, // end of variable
2528 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002530 scriptitem_T *si;
2531 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 imported_T *import;
2533
Bram Moolenaare3d46852020-08-29 13:39:17 +02002534 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2535 return FAIL;
2536 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002537 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002538 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002540 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002541 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2542 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
2544 if (idx >= 0)
2545 {
2546 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2547
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002548 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 current_sctx.sc_sid, idx, sv->sv_type);
2550 return OK;
2551 }
2552
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002553 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 if (import != NULL)
2555 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002556 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002557 {
2558 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002559 char_u *exp_name;
2560 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002561 ufunc_T *ufunc;
2562 type_T *type;
2563
2564 // Used "import * as Name", need to lookup the member.
2565 if (*p != '.')
2566 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002567 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002568 return FAIL;
2569 }
2570 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002571 if (VIM_ISWHITE(*p))
2572 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002573 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002574 return FAIL;
2575 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002576
Bram Moolenaar1c991142020-07-04 13:15:31 +02002577 // isolate one name
2578 exp_name = p;
2579 while (eval_isnamec(*p))
2580 ++p;
2581 cc = *p;
2582 *p = NUL;
2583
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002584 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002585 *p = cc;
2586 p = skipwhite(p);
2587
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002588 // TODO: what if it is a function?
2589 if (idx < 0)
2590 return FAIL;
2591 *end = p;
2592
2593 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2594 import->imp_sid,
2595 idx,
2596 type);
2597 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002598 else if (import->imp_funcname != NULL)
2599 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002600 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002601 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2602 import->imp_sid,
2603 import->imp_var_vals_idx,
2604 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 return OK;
2606 }
2607
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002608 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002609 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 return FAIL;
2611}
2612
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613 static int
2614generate_funcref(cctx_T *cctx, char_u *name)
2615{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002616 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002617
2618 if (ufunc == NULL)
2619 return FAIL;
2620
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002621 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002622 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2623 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002624 == FAIL)
2625 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002626 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002627}
2628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629/*
2630 * Compile a variable name into a load instruction.
2631 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002632 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 * When "error" is FALSE do not give an error when not found.
2634 */
2635 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002636compile_load(
2637 char_u **arg,
2638 char_u *end_arg,
2639 cctx_T *cctx,
2640 int is_expr,
2641 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642{
2643 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002644 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002645 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002647 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648
2649 if (*(*arg + 1) == ':')
2650 {
2651 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002652 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002653 {
2654 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002656 switch (**arg)
2657 {
2658 case 'g': isn_type = ISN_LOADGDICT; break;
2659 case 'w': isn_type = ISN_LOADWDICT; break;
2660 case 't': isn_type = ISN_LOADTDICT; break;
2661 case 'b': isn_type = ISN_LOADBDICT; break;
2662 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002663 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002664 goto theend;
2665 }
2666 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2667 goto theend;
2668 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002669 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 else
2671 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002672 isntype_T isn_type = ISN_DROP;
2673
2674 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2675 if (name == NULL)
2676 return FAIL;
2677
2678 switch (**arg)
2679 {
2680 case 'v': res = generate_LOADV(cctx, name, error);
2681 break;
2682 case 's': res = compile_load_scriptvar(cctx, name,
2683 NULL, NULL, error);
2684 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002685 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2686 isn_type = ISN_LOADG;
2687 else
2688 {
2689 isn_type = ISN_LOADAUTO;
2690 vim_free(name);
2691 name = vim_strnsave(*arg, end - *arg);
2692 if (name == NULL)
2693 return FAIL;
2694 }
2695 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002696 case 'w': isn_type = ISN_LOADW; break;
2697 case 't': isn_type = ISN_LOADT; break;
2698 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002699 default: // cannot happen, just in case
2700 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002701 goto theend;
2702 }
2703 if (isn_type != ISN_DROP)
2704 {
2705 // Global, Buffer-local, Window-local and Tabpage-local
2706 // variables can be defined later, thus we don't check if it
2707 // exists, give error at runtime.
2708 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 }
2711 }
2712 else
2713 {
2714 size_t len = end - *arg;
2715 int idx;
2716 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002717 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718
2719 name = vim_strnsave(*arg, end - *arg);
2720 if (name == NULL)
2721 return FAIL;
2722
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002723 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002725 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002726 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
2728 else
2729 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002730 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002731
Bram Moolenaar709664c2020-12-12 14:33:41 +01002732 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002734 type = lvar.lv_type;
2735 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002736 if (lvar.lv_from_outer != 0)
2737 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002738 else
2739 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 }
2741 else
2742 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002743 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002744 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002745 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002746 || find_imported(name, 0, cctx) != NULL)
2747 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002748
Bram Moolenaar0f769812020-09-12 18:32:34 +02002749 // When evaluating an expression and the name starts with an
2750 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002751 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002752 if (res == FAIL && is_expr
2753 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002754 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 }
2756 }
2757 if (gen_load)
2758 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002759 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002760 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002761 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002762 cctx->ctx_outer_used = TRUE;
2763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 }
2765
2766 *arg = end;
2767
2768theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002769 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002770 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 vim_free(name);
2772 return res;
2773}
2774
2775/*
2776 * Compile the argument expressions.
2777 * "arg" points to just after the "(" and is advanced to after the ")"
2778 */
2779 static int
2780compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2781{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002782 char_u *p = *arg;
2783 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002784 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
Bram Moolenaare6085c52020-04-12 20:19:16 +02002786 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002788 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2789 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002790 if (*p == ')')
2791 {
2792 *arg = p + 1;
2793 return OK;
2794 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002795 if (must_end)
2796 {
2797 semsg(_(e_missing_comma_before_argument_str), p);
2798 return FAIL;
2799 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002800
Bram Moolenaara5565e42020-05-09 15:44:01 +02002801 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 return FAIL;
2803 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002804
2805 if (*p != ',' && *skipwhite(p) == ',')
2806 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002807 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002808 p = skipwhite(p);
2809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002811 {
2812 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002813 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002814 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002815 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002816 else
2817 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002818 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002819 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002821failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002822 emsg(_(e_missing_close));
2823 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824}
2825
2826/*
2827 * Compile a function call: name(arg1, arg2)
2828 * "arg" points to "name", "arg + varlen" to the "(".
2829 * "argcount_init" is 1 for "value->method()"
2830 * Instructions:
2831 * EVAL arg1
2832 * EVAL arg2
2833 * BCALL / DCALL / UCALL
2834 */
2835 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002836compile_call(
2837 char_u **arg,
2838 size_t varlen,
2839 cctx_T *cctx,
2840 ppconst_T *ppconst,
2841 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842{
2843 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002844 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 int argcount = argcount_init;
2846 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002847 char_u fname_buf[FLEN_FIXED + 1];
2848 char_u *tofree = NULL;
2849 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002850 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002851 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002852 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853
Bram Moolenaara5565e42020-05-09 15:44:01 +02002854 // we can evaluate "has('name')" at compile time
2855 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2856 {
2857 char_u *s = skipwhite(*arg + varlen + 1);
2858 typval_T argvars[2];
2859
2860 argvars[0].v_type = VAR_UNKNOWN;
2861 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002862 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002863 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002864 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002865 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002866 if (*s == ')' && argvars[0].v_type == VAR_STRING
2867 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002868 {
2869 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2870
2871 *arg = s + 1;
2872 argvars[1].v_type = VAR_UNKNOWN;
2873 tv->v_type = VAR_NUMBER;
2874 tv->vval.v_number = 0;
2875 f_has(argvars, tv);
2876 clear_tv(&argvars[0]);
2877 ++ppconst->pp_used;
2878 return OK;
2879 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002880 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002881 }
2882
2883 if (generate_ppconst(cctx, ppconst) == FAIL)
2884 return FAIL;
2885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 if (varlen >= sizeof(namebuf))
2887 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002888 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 return FAIL;
2890 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002891 vim_strncpy(namebuf, *arg, varlen);
2892 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
2894 *arg = skipwhite(*arg + varlen + 1);
2895 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002896 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897
Bram Moolenaar03290b82020-12-19 16:30:44 +01002898 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002899 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 {
2901 int idx;
2902
2903 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002904 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002906 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002907 if (STRCMP(name, "flatten") == 0)
2908 {
2909 emsg(_(e_cannot_use_flatten_in_vim9_script));
2910 goto theend;
2911 }
2912
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002913 if (STRCMP(name, "add") == 0 && argcount == 2)
2914 {
2915 garray_T *stack = &cctx->ctx_type_stack;
2916 type_T *type = ((type_T **)stack->ga_data)[
2917 stack->ga_len - 2];
2918
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002919 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002920 if (type->tt_type == VAR_LIST)
2921 {
2922 // inline "add(list, item)" so that the type can be checked
2923 res = generate_LISTAPPEND(cctx);
2924 idx = -1;
2925 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002926 else if (type->tt_type == VAR_BLOB)
2927 {
2928 // inline "add(blob, nr)" so that the type can be checked
2929 res = generate_BLOBAPPEND(cctx);
2930 idx = -1;
2931 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002932 }
2933
2934 if (idx >= 0)
2935 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2936 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002937 else
2938 semsg(_(e_unknownfunc), namebuf);
2939 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
2941
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002942 // An argument or local variable can be a function reference, this
2943 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002944 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002945 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002946 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002947 // If we can find the function by name generate the right call.
2948 // Skip global functions here, a local funcref takes precedence.
2949 ufunc = find_func(name, FALSE, cctx);
2950 if (ufunc != NULL && !func_is_global(ufunc))
2951 {
2952 res = generate_CALL(cctx, ufunc, argcount);
2953 goto theend;
2954 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956
2957 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002958 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002959 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002961 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002962 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002963 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002964 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002965 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002966
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002967 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002968 goto theend;
2969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
Bram Moolenaar0f769812020-09-12 18:32:34 +02002971 // If we can find a global function by name generate the right call.
2972 if (ufunc != NULL)
2973 {
2974 res = generate_CALL(cctx, ufunc, argcount);
2975 goto theend;
2976 }
2977
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002978 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002979 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002980 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002981 res = generate_UCALL(cctx, name, argcount);
2982 else
2983 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002984
2985theend:
2986 vim_free(tofree);
2987 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988}
2989
2990// like NAMESPACE_CHAR but with 'a' and 'l'.
2991#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2992
2993/*
2994 * Find the end of a variable or function name. Unlike find_name_end() this
2995 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002996 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 * Return a pointer to just after the name. Equal to "arg" if there is no
2998 * valid name.
2999 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003000 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003001to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002{
3003 char_u *p;
3004
3005 // Quick check for valid starting character.
3006 if (!eval_isnamec1(*arg))
3007 return arg;
3008
3009 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3010 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3011 // and can be used in slice "[n:]".
3012 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003013 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3015 break;
3016 return p;
3017}
3018
3019/*
3020 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003021 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 */
3023 char_u *
3024to_name_const_end(char_u *arg)
3025{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003026 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 typval_T rettv;
3028
3029 if (p == arg && *arg == '[')
3030 {
3031
3032 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003033 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 p = arg;
3035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 return p;
3037}
3038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039/*
3040 * parse a list: [expr, expr]
3041 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003042 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 */
3044 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003045compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046{
3047 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003048 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003050 int is_const;
3051 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003053 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003055 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003056 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003057 semsg(_(e_list_end), *arg);
3058 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003059 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003060 if (*p == ',')
3061 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003062 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003063 return FAIL;
3064 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003065 if (*p == ']')
3066 {
3067 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003068 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003069 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003070 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003071 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003072 if (!is_const)
3073 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 ++count;
3075 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003076 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003078 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3079 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003080 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003081 return FAIL;
3082 }
3083 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003084 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 p = skipwhite(p);
3086 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003087 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003089 ppconst->pp_is_const = is_all_const;
3090 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091}
3092
3093/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003094 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003096 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 */
3098 static int
3099compile_lambda(char_u **arg, cctx_T *cctx)
3100{
Bram Moolenaare462f522020-12-27 14:43:30 +01003101 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 typval_T rettv;
3103 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003104 evalarg_T evalarg;
3105
3106 CLEAR_FIELD(evalarg);
3107 evalarg.eval_flags = EVAL_EVALUATE;
3108 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109
3110 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003111 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3112 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003113 {
3114 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003115 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003116 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003117
Bram Moolenaar65c44152020-12-24 15:14:01 +01003118 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003120 ++ufunc->uf_refcount;
3121 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122
Bram Moolenaar65c44152020-12-24 15:14:01 +01003123 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003124 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003126 clear_evalarg(&evalarg, NULL);
3127
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003128 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003129 {
3130 // The return type will now be known.
3131 set_function_type(ufunc);
3132
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003133 // The function reference count will be 1. When the ISN_FUNCREF
3134 // instruction is deleted the reference count is decremented and the
3135 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003136 return generate_FUNCREF(cctx, ufunc);
3137 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003138
3139 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 return FAIL;
3141}
3142
3143/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003144 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003146 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 */
3148 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003149compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150{
3151 garray_T *instr = &cctx->ctx_instr;
3152 int count = 0;
3153 dict_T *d = dict_alloc();
3154 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003155 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003156 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003157 int is_const;
3158 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
3160 if (d == NULL)
3161 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003162 if (generate_ppconst(cctx, ppconst) == FAIL)
3163 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003164 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003166 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167
Bram Moolenaar23c55272020-06-21 16:58:13 +02003168 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003169 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003170 *arg = NULL;
3171 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003172 }
3173
3174 if (**arg == '}')
3175 break;
3176
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003177 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003179 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003181 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003182 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003183 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003186 if (isn->isn_type == ISN_PUSHNR)
3187 {
3188 char buf[NUMBUFLEN];
3189
3190 // Convert to string at compile time.
3191 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3192 isn->isn_type = ISN_PUSHS;
3193 isn->isn_arg.string = vim_strsave((char_u *)buf);
3194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 if (isn->isn_type == ISN_PUSHS)
3196 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003197 else if (may_generate_2STRING(-1, cctx) == FAIL)
3198 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003199 *arg = skipwhite(*arg);
3200 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003201 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003202 emsg(_(e_missing_matching_bracket_after_dict_key));
3203 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003204 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003205 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003207 else
3208 {
3209 // {"name": value},
3210 // {'name': value},
3211 // {name: value} use "name" as a literal key
3212 key = get_literal_key(arg);
3213 if (key == NULL)
3214 return FAIL;
3215 if (generate_PUSHS(cctx, key) == FAIL)
3216 return FAIL;
3217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
3219 // Check for duplicate keys, if using string keys.
3220 if (key != NULL)
3221 {
3222 item = dict_find(d, key, -1);
3223 if (item != NULL)
3224 {
3225 semsg(_(e_duplicate_key), key);
3226 goto failret;
3227 }
3228 item = dictitem_alloc(key);
3229 if (item != NULL)
3230 {
3231 item->di_tv.v_type = VAR_UNKNOWN;
3232 item->di_tv.v_lock = 0;
3233 if (dict_add(d, item) == FAIL)
3234 dictitem_free(item);
3235 }
3236 }
3237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 if (**arg != ':')
3239 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003240 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003241 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003242 else
3243 semsg(_(e_missing_dict_colon), *arg);
3244 return FAIL;
3245 }
3246 whitep = *arg + 1;
3247 if (!IS_WHITE_OR_NUL(*whitep))
3248 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003249 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 return FAIL;
3251 }
3252
Bram Moolenaar23c55272020-06-21 16:58:13 +02003253 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003254 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003255 *arg = NULL;
3256 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003257 }
3258
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003259 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003261 if (!is_const)
3262 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 ++count;
3264
Bram Moolenaar2c330432020-04-13 14:41:35 +02003265 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003266 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003267 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003268 *arg = NULL;
3269 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (**arg == '}')
3272 break;
3273 if (**arg != ',')
3274 {
3275 semsg(_(e_missing_dict_comma), *arg);
3276 goto failret;
3277 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003278 if (IS_WHITE_OR_NUL(*whitep))
3279 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003280 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003281 return FAIL;
3282 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003283 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003284 if (!IS_WHITE_OR_NUL(*whitep))
3285 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003286 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003287 return FAIL;
3288 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003289 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 }
3291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 *arg = *arg + 1;
3293
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003294 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003295 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003296 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003297 *arg += STRLEN(*arg);
3298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003300 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 return generate_NEWDICT(cctx, count);
3302
3303failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003304 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003305 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003306 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003307 *arg = (char_u *)"";
3308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 dict_unref(d);
3310 return FAIL;
3311}
3312
3313/*
3314 * Compile "&option".
3315 */
3316 static int
3317compile_get_option(char_u **arg, cctx_T *cctx)
3318{
3319 typval_T rettv;
3320 char_u *start = *arg;
3321 int ret;
3322
3323 // parse the option and get the current value to get the type.
3324 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003325 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 if (ret == OK)
3327 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003328 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003329 char_u *name = vim_strnsave(start, *arg - start);
3330 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3331 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332
3333 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3334 vim_free(name);
3335 }
3336 clear_tv(&rettv);
3337
3338 return ret;
3339}
3340
3341/*
3342 * Compile "$VAR".
3343 */
3344 static int
3345compile_get_env(char_u **arg, cctx_T *cctx)
3346{
3347 char_u *start = *arg;
3348 int len;
3349 int ret;
3350 char_u *name;
3351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 ++*arg;
3353 len = get_env_len(arg);
3354 if (len == 0)
3355 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003356 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 return FAIL;
3358 }
3359
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003360 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 name = vim_strnsave(start, len + 1);
3362 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3363 vim_free(name);
3364 return ret;
3365}
3366
3367/*
3368 * Compile "@r".
3369 */
3370 static int
3371compile_get_register(char_u **arg, cctx_T *cctx)
3372{
3373 int ret;
3374
3375 ++*arg;
3376 if (**arg == NUL)
3377 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003378 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 return FAIL;
3380 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003381 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 {
3383 emsg_invreg(**arg);
3384 return FAIL;
3385 }
3386 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3387 ++*arg;
3388 return ret;
3389}
3390
3391/*
3392 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003393 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 */
3395 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003396apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003398 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399
3400 // this works from end to start
3401 while (p > start)
3402 {
3403 --p;
3404 if (*p == '-' || *p == '+')
3405 {
3406 // only '-' has an effect, for '+' we only check the type
3407#ifdef FEAT_FLOAT
3408 if (rettv->v_type == VAR_FLOAT)
3409 {
3410 if (*p == '-')
3411 rettv->vval.v_float = -rettv->vval.v_float;
3412 }
3413 else
3414#endif
3415 {
3416 varnumber_T val;
3417 int error = FALSE;
3418
3419 // tv_get_number_chk() accepts a string, but we don't want that
3420 // here
3421 if (check_not_string(rettv) == FAIL)
3422 return FAIL;
3423 val = tv_get_number_chk(rettv, &error);
3424 clear_tv(rettv);
3425 if (error)
3426 return FAIL;
3427 if (*p == '-')
3428 val = -val;
3429 rettv->v_type = VAR_NUMBER;
3430 rettv->vval.v_number = val;
3431 }
3432 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003433 else if (numeric_only)
3434 {
3435 ++p;
3436 break;
3437 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003438 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 {
3440 int v = tv2bool(rettv);
3441
3442 // '!' is permissive in the type.
3443 clear_tv(rettv);
3444 rettv->v_type = VAR_BOOL;
3445 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3446 }
3447 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003448 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 return OK;
3450}
3451
3452/*
3453 * Recognize v: variables that are constants and set "rettv".
3454 */
3455 static void
3456get_vim_constant(char_u **arg, typval_T *rettv)
3457{
3458 if (STRNCMP(*arg, "v:true", 6) == 0)
3459 {
3460 rettv->v_type = VAR_BOOL;
3461 rettv->vval.v_number = VVAL_TRUE;
3462 *arg += 6;
3463 }
3464 else if (STRNCMP(*arg, "v:false", 7) == 0)
3465 {
3466 rettv->v_type = VAR_BOOL;
3467 rettv->vval.v_number = VVAL_FALSE;
3468 *arg += 7;
3469 }
3470 else if (STRNCMP(*arg, "v:null", 6) == 0)
3471 {
3472 rettv->v_type = VAR_SPECIAL;
3473 rettv->vval.v_number = VVAL_NULL;
3474 *arg += 6;
3475 }
3476 else if (STRNCMP(*arg, "v:none", 6) == 0)
3477 {
3478 rettv->v_type = VAR_SPECIAL;
3479 rettv->vval.v_number = VVAL_NONE;
3480 *arg += 6;
3481 }
3482}
3483
Bram Moolenaar657137c2021-01-09 15:45:23 +01003484 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003485get_compare_type(char_u *p, int *len, int *type_is)
3486{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003487 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003488 int i;
3489
3490 switch (p[0])
3491 {
3492 case '=': if (p[1] == '=')
3493 type = EXPR_EQUAL;
3494 else if (p[1] == '~')
3495 type = EXPR_MATCH;
3496 break;
3497 case '!': if (p[1] == '=')
3498 type = EXPR_NEQUAL;
3499 else if (p[1] == '~')
3500 type = EXPR_NOMATCH;
3501 break;
3502 case '>': if (p[1] != '=')
3503 {
3504 type = EXPR_GREATER;
3505 *len = 1;
3506 }
3507 else
3508 type = EXPR_GEQUAL;
3509 break;
3510 case '<': if (p[1] != '=')
3511 {
3512 type = EXPR_SMALLER;
3513 *len = 1;
3514 }
3515 else
3516 type = EXPR_SEQUAL;
3517 break;
3518 case 'i': if (p[1] == 's')
3519 {
3520 // "is" and "isnot"; but not a prefix of a name
3521 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3522 *len = 5;
3523 i = p[*len];
3524 if (!isalnum(i) && i != '_')
3525 {
3526 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3527 *type_is = TRUE;
3528 }
3529 }
3530 break;
3531 }
3532 return type;
3533}
3534
3535/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003536 * Skip over an expression, ignoring most errors.
3537 */
3538 static void
3539skip_expr_cctx(char_u **arg, cctx_T *cctx)
3540{
3541 evalarg_T evalarg;
3542
3543 CLEAR_FIELD(evalarg);
3544 evalarg.eval_cctx = cctx;
3545 skip_expr(arg, &evalarg);
3546}
3547
3548/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003550 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 */
3552 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003553compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003555 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556
3557 // this works from end to start
3558 while (p > start)
3559 {
3560 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003561 while (VIM_ISWHITE(*p))
3562 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 if (*p == '-' || *p == '+')
3564 {
3565 int negate = *p == '-';
3566 isn_T *isn;
3567
3568 // TODO: check type
3569 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3570 {
3571 --p;
3572 if (*p == '-')
3573 negate = !negate;
3574 }
3575 // only '-' has an effect, for '+' we only check the type
3576 if (negate)
3577 isn = generate_instr(cctx, ISN_NEGATENR);
3578 else
3579 isn = generate_instr(cctx, ISN_CHECKNR);
3580 if (isn == NULL)
3581 return FAIL;
3582 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003583 else if (numeric_only)
3584 {
3585 ++p;
3586 break;
3587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 else
3589 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003590 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003592 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003594 if (p[-1] == '!')
3595 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 }
3598 if (generate_2BOOL(cctx, invert) == FAIL)
3599 return FAIL;
3600 }
3601 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003602 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 return OK;
3604}
3605
3606/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003607 * Compile "(expression)": recursive!
3608 * Return FAIL/OK.
3609 */
3610 static int
3611compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3612{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003613 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003614 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003615
Bram Moolenaar24156692021-01-14 20:35:49 +01003616 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3617 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003618 if (ppconst->pp_used <= PPSIZE - 10)
3619 {
3620 ret = compile_expr1(arg, cctx, ppconst);
3621 }
3622 else
3623 {
3624 // Not enough space in ppconst, flush constants.
3625 if (generate_ppconst(cctx, ppconst) == FAIL)
3626 return FAIL;
3627 ret = compile_expr0(arg, cctx);
3628 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003629 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3630 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003631 if (**arg == ')')
3632 ++*arg;
3633 else if (ret == OK)
3634 {
3635 emsg(_(e_missing_close));
3636 ret = FAIL;
3637 }
3638 return ret;
3639}
3640
3641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003643 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 */
3645 static int
3646compile_subscript(
3647 char_u **arg,
3648 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003649 char_u *start_leader,
3650 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003651 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003653 char_u *name_start = *end_leader;
3654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 for (;;)
3656 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003657 char_u *p = skipwhite(*arg);
3658
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003659 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003660 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003661 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003662
3663 // If a following line starts with "->{" or "->X" advance to that
3664 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003665 // Also if a following line starts with ".x".
3666 if (next != NULL &&
3667 ((next[0] == '-' && next[1] == '>'
3668 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003669 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003670 {
3671 next = next_line_from_context(cctx, TRUE);
3672 if (next == NULL)
3673 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003674 *arg = next;
3675 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003676 }
3677 }
3678
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003679 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003680 // not a function call.
3681 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003683 garray_T *stack = &cctx->ctx_type_stack;
3684 type_T *type;
3685 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003687 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003688 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003689 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003692 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3693
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003694 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3696 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003697 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 return FAIL;
3699 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003700 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003702 char_u *pstart = p;
3703
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003704 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003705 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003706 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 // something->method()
3709 // Apply the '!', '-' and '+' first:
3710 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003711 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003714 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003715 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003716 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003717 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003718 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003719 int argcount = 1;
3720 char_u *expr;
3721 garray_T *stack;
3722 type_T *type;
3723
3724 // Funcref call: list->(Refs[2])(arg)
3725 // or lambda: list->((arg) => expr)(arg)
3726 // Fist compile the arguments.
3727 expr = *arg;
3728 *arg = skipwhite(*arg + 1);
3729 skip_expr_cctx(arg, cctx);
3730 *arg = skipwhite(*arg);
3731 if (**arg != ')')
3732 {
3733 semsg(_(e_missing_paren), *arg);
3734 return FAIL;
3735 }
3736 ++*arg;
3737 if (**arg != '(')
3738 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003739 if (*skipwhite(*arg) == '(')
3740 emsg(_(e_nowhitespace));
3741 else
3742 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003743 return FAIL;
3744 }
3745
3746 *arg = skipwhite(*arg + 1);
3747 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3748 return FAIL;
3749
3750 // Compile the function expression.
3751 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3752 return FAIL;
3753
3754 stack = &cctx->ctx_type_stack;
3755 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3756 if (generate_PCALL(cctx, argcount,
3757 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 return FAIL;
3759 }
3760 else
3761 {
3762 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003763 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003764 if (!eval_isnamec1(*p))
3765 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003766 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003767 return FAIL;
3768 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003769 if (ASCII_ISALPHA(*p) && p[1] == ':')
3770 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003771 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 ;
3773 if (*p != '(')
3774 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003775 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 }
3778 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003779 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 return FAIL;
3781 }
3782 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003783 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003785 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003786 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003787 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003788 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003789 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003790
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003791 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003792 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003793 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003794 // TODO: blob index
3795 // TODO: more arguments
3796 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003797 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003798 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003799 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003800
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003801 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003802 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003803 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003804 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003805 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003806 // missing first index is equal to zero
3807 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003808 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003809 else
3810 {
3811 if (compile_expr0(arg, cctx) == FAIL)
3812 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003813 if (**arg == ':')
3814 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003815 semsg(_(e_white_space_required_before_and_after_str_at_str),
3816 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003817 return FAIL;
3818 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003819 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003820 return FAIL;
3821 *arg = skipwhite(*arg);
3822 }
3823 if (**arg == ':')
3824 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003825 is_slice = TRUE;
3826 ++*arg;
3827 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3828 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003829 semsg(_(e_white_space_required_before_and_after_str_at_str),
3830 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003831 return FAIL;
3832 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003833 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003834 return FAIL;
3835 if (**arg == ']')
3836 // missing second index is equal to end of string
3837 generate_PUSHNR(cctx, -1);
3838 else
3839 {
3840 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003841 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003842 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003843 return FAIL;
3844 *arg = skipwhite(*arg);
3845 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847
3848 if (**arg != ']')
3849 {
3850 emsg(_(e_missbrac));
3851 return FAIL;
3852 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003853 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003855 // We can index a list and a dict. If we don't know the type
3856 // we can use the index value type.
3857 // TODO: If we don't know use an instruction to figure it out at
3858 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003859 typep = ((type_T **)stack->ga_data) + stack->ga_len
3860 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003861 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003862 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3863 // If the index is a string, the variable must be a Dict.
3864 if (*typep == &t_any && valtype == &t_string)
3865 vtype = VAR_DICT;
3866 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003867 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003868 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003869 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003870 return FAIL;
3871 if (is_slice)
3872 {
3873 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003874 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003875 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003876 return FAIL;
3877 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003878 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003879
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003880 if (vtype == VAR_DICT)
3881 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003882 if (is_slice)
3883 {
3884 emsg(_(e_cannot_slice_dictionary));
3885 return FAIL;
3886 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003887 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003888 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003889 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003890 if (*typep == &t_unknown)
3891 // empty dict was used
3892 *typep = &t_any;
3893 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003894 else
3895 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003896 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003897 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003898 return FAIL;
3899 *typep = &t_any;
3900 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003901 if (may_generate_2STRING(-1, cctx) == FAIL)
3902 return FAIL;
3903 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3904 return FAIL;
3905 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003906 else if (vtype == VAR_STRING)
3907 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003908 *typep = &t_string;
3909 if ((is_slice
3910 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3911 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003912 return FAIL;
3913 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003914 else if (vtype == VAR_BLOB)
3915 {
3916 emsg("Sorry, blob index and slice not implemented yet");
3917 return FAIL;
3918 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003919 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003920 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003921 if (is_slice)
3922 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003923 if (generate_instr_drop(cctx,
3924 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3925 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003926 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003927 }
Bram Moolenaared591872020-08-15 22:14:53 +02003928 else
3929 {
3930 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003931 {
Bram Moolenaared591872020-08-15 22:14:53 +02003932 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003933 if (*typep == &t_unknown)
3934 // empty list was used
3935 *typep = &t_any;
3936 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003937 if (generate_instr_drop(cctx,
3938 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3939 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003940 return FAIL;
3941 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003942 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003943 else
3944 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003945 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003946 return FAIL;
3947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003949 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003951 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003952 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003953 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003954 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003955
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003956 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003957 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003958 {
3959 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003960 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003961 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003962 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003963 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 while (eval_isnamec(*p))
3965 MB_PTR_ADV(p);
3966 if (p == *arg)
3967 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003968 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 return FAIL;
3970 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003971 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 return FAIL;
3973 *arg = p;
3974 }
3975 else
3976 break;
3977 }
3978
3979 // TODO - see handle_subscript():
3980 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3981 // Don't do this when "Func" is already a partial that was bound
3982 // explicitly (pt_auto is FALSE).
3983
3984 return OK;
3985}
3986
3987/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003988 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3989 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003991 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003992 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003993 *
3994 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 */
3996
3997/*
3998 * number number constant
3999 * 0zFFFFFFFF Blob constant
4000 * "string" string constant
4001 * 'string' literal string constant
4002 * &option-name option value
4003 * @r register contents
4004 * identifier variable value
4005 * function() function call
4006 * $VAR environment variable
4007 * (expression) nested expression
4008 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004009 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 *
4011 * Also handle:
4012 * ! in front logical NOT
4013 * - in front unary minus
4014 * + in front unary plus (ignored)
4015 * trailing (arg) funcref/partial call
4016 * trailing [] subscript in String or List
4017 * trailing .name entry in Dictionary
4018 * trailing ->name() method call
4019 */
4020 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004021compile_expr7(
4022 char_u **arg,
4023 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004024 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 char_u *start_leader, *end_leader;
4027 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004028 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004029 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004031 ppconst->pp_is_const = FALSE;
4032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 /*
4034 * Skip '!', '-' and '+' characters. They are handled later.
4035 */
4036 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004037 if (eval_leader(arg, TRUE) == FAIL)
4038 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 end_leader = *arg;
4040
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004041 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 switch (**arg)
4043 {
4044 /*
4045 * Number constant.
4046 */
4047 case '0': // also for blob starting with 0z
4048 case '1':
4049 case '2':
4050 case '3':
4051 case '4':
4052 case '5':
4053 case '6':
4054 case '7':
4055 case '8':
4056 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004057 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004059 // Apply "-" and "+" just before the number now, right to
4060 // left. Matters especially when "->" follows. Stops at
4061 // '!'.
4062 if (apply_leader(rettv, TRUE,
4063 start_leader, &end_leader) == FAIL)
4064 {
4065 clear_tv(rettv);
4066 return FAIL;
4067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 break;
4069
4070 /*
4071 * String constant: "string".
4072 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004073 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 return FAIL;
4075 break;
4076
4077 /*
4078 * Literal string constant: 'str''ing'.
4079 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004080 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 return FAIL;
4082 break;
4083
4084 /*
4085 * Constant Vim variable.
4086 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004087 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 ret = NOTDONE;
4089 break;
4090
4091 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004092 * "true" constant
4093 */
4094 case 't': if (STRNCMP(*arg, "true", 4) == 0
4095 && !eval_isnamec((*arg)[4]))
4096 {
4097 *arg += 4;
4098 rettv->v_type = VAR_BOOL;
4099 rettv->vval.v_number = VVAL_TRUE;
4100 }
4101 else
4102 ret = NOTDONE;
4103 break;
4104
4105 /*
4106 * "false" constant
4107 */
4108 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4109 && !eval_isnamec((*arg)[5]))
4110 {
4111 *arg += 5;
4112 rettv->v_type = VAR_BOOL;
4113 rettv->vval.v_number = VVAL_FALSE;
4114 }
4115 else
4116 ret = NOTDONE;
4117 break;
4118
4119 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004120 * "null" constant
4121 */
4122 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4123 && !eval_isnamec((*arg)[5]))
4124 {
4125 *arg += 4;
4126 rettv->v_type = VAR_SPECIAL;
4127 rettv->vval.v_number = VVAL_NULL;
4128 }
4129 else
4130 ret = NOTDONE;
4131 break;
4132
4133 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 * List: [expr, expr]
4135 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004136 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4137 return FAIL;
4138 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 break;
4140
4141 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 * Dictionary: {'key': val, 'key': val}
4143 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004144 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4145 return FAIL;
4146 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 break;
4148
4149 /*
4150 * Option value: &name
4151 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004152 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4153 return FAIL;
4154 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004155 break;
4156
4157 /*
4158 * Environment variable: $VAR.
4159 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004160 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4161 return FAIL;
4162 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 break;
4164
4165 /*
4166 * Register contents: @r.
4167 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004168 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4169 return FAIL;
4170 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 break;
4172 /*
4173 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004174 * lambda: (arg, arg) => expr
4175 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004177 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4178 ret = compile_lambda(arg, cctx);
4179 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004180 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 break;
4182
4183 default: ret = NOTDONE;
4184 break;
4185 }
4186 if (ret == FAIL)
4187 return FAIL;
4188
Bram Moolenaar1c747212020-05-09 18:28:34 +02004189 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004191 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004192 clear_tv(rettv);
4193 else
4194 // A constant expression can possibly be handled compile time,
4195 // return the value instead of generating code.
4196 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 }
4198 else if (ret == NOTDONE)
4199 {
4200 char_u *p;
4201 int r;
4202
4203 if (!eval_isnamec1(**arg))
4204 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004205 if (ends_excmd(*skipwhite(*arg)))
4206 semsg(_(e_empty_expression_str), *arg);
4207 else
4208 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 return FAIL;
4210 }
4211
4212 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004213 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004215 {
4216 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4217 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 {
4220 if (generate_ppconst(cctx, ppconst) == FAIL)
4221 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004222 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 if (r == FAIL)
4225 return FAIL;
4226 }
4227
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004228 // Handle following "[]", ".member", etc.
4229 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004230 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004231 ppconst) == FAIL)
4232 return FAIL;
4233 if (ppconst->pp_used > 0)
4234 {
4235 // apply the '!', '-' and '+' before the constant
4236 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004237 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004238 return FAIL;
4239 return OK;
4240 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004241 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004243 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244}
4245
4246/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004247 * Give the "white on both sides" error, taking the operator from "p[len]".
4248 */
4249 void
4250error_white_both(char_u *op, int len)
4251{
4252 char_u buf[10];
4253
4254 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004255 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004256}
4257
4258/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004259 * <type>expr7: runtime type check / conversion
4260 */
4261 static int
4262compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4263{
4264 type_T *want_type = NULL;
4265
4266 // Recognize <type>
4267 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4268 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004269 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004270 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4271 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004272 return FAIL;
4273
4274 if (**arg != '>')
4275 {
4276 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004277 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004278 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004279 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004280 return FAIL;
4281 }
4282 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004283 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004284 return FAIL;
4285 }
4286
4287 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4288 return FAIL;
4289
4290 if (want_type != NULL)
4291 {
4292 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004293 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004294 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004295
Bram Moolenaard1103582020-08-14 22:44:25 +02004296 generate_ppconst(cctx, ppconst);
4297 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004298 where.wt_index = 0;
4299 where.wt_variable = FALSE;
4300 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004301 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004302 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004303 return FAIL;
4304 }
4305 }
4306
4307 return OK;
4308}
4309
4310/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 * * number multiplication
4312 * / number division
4313 * % number modulo
4314 */
4315 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004316compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317{
4318 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004319 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004320 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004322 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004323 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 return FAIL;
4325
4326 /*
4327 * Repeat computing, until no "*", "/" or "%" is following.
4328 */
4329 for (;;)
4330 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004331 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 if (*op != '*' && *op != '/' && *op != '%')
4333 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004334 if (next != NULL)
4335 {
4336 *arg = next_line_from_context(cctx, TRUE);
4337 op = skipwhite(*arg);
4338 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004339
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004340 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004342 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004345 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004346 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004348 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004349 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004351
4352 if (ppconst->pp_used == ppconst_used + 2
4353 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4354 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004355 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004356 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4357 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4358 varnumber_T res = 0;
4359 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361 // both are numbers: compute the result
4362 switch (*op)
4363 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004364 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004365 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004366 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004367 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004368 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004369 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004370 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004371 break;
4372 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004373 if (failed)
4374 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004375 tv1->vval.v_number = res;
4376 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004377 }
4378 else
4379 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004380 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004381 generate_two_op(cctx, op);
4382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 }
4384
4385 return OK;
4386}
4387
4388/*
4389 * + number addition
4390 * - number subtraction
4391 * .. string concatenation
4392 */
4393 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004394compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395{
4396 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004397 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
4401 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004402 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 return FAIL;
4404
4405 /*
4406 * Repeat computing, until no "+", "-" or ".." is following.
4407 */
4408 for (;;)
4409 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004410 op = may_peek_next_line(cctx, *arg, &next);
4411 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 break;
4413 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004414 if (next != NULL)
4415 {
4416 *arg = next_line_from_context(cctx, TRUE);
4417 op = skipwhite(*arg);
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004422 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004423 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 }
4425
Bram Moolenaare0de1712020-12-02 17:36:54 +01004426 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004427 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004429 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 return FAIL;
4432
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004434 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004435 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4436 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4437 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4438 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004440 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4441 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004443 // concat/subtract/add constant numbers
4444 if (*op == '+')
4445 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4446 else if (*op == '-')
4447 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4448 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004449 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004450 // concatenate constant strings
4451 char_u *s1 = tv1->vval.v_string;
4452 char_u *s2 = tv2->vval.v_string;
4453 size_t len1 = STRLEN(s1);
4454
4455 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4456 if (tv1->vval.v_string == NULL)
4457 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004459 return FAIL;
4460 }
4461 mch_memmove(tv1->vval.v_string, s1, len1);
4462 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004463 vim_free(s1);
4464 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004465 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 }
4468 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004469 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004470 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004471 if (*op == '.')
4472 {
4473 if (may_generate_2STRING(-2, cctx) == FAIL
4474 || may_generate_2STRING(-1, cctx) == FAIL)
4475 return FAIL;
4476 generate_instr_drop(cctx, ISN_CONCAT, 1);
4477 }
4478 else
4479 generate_two_op(cctx, op);
4480 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 }
4482
4483 return OK;
4484}
4485
4486/*
4487 * expr5a == expr5b
4488 * expr5a =~ expr5b
4489 * expr5a != expr5b
4490 * expr5a !~ expr5b
4491 * expr5a > expr5b
4492 * expr5a >= expr5b
4493 * expr5a < expr5b
4494 * expr5a <= expr5b
4495 * expr5a is expr5b
4496 * expr5a isnot expr5b
4497 *
4498 * Produces instructions:
4499 * EVAL expr5a Push result of "expr5a"
4500 * EVAL expr5b Push result of "expr5b"
4501 * COMPARE one of the compare instructions
4502 */
4503 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004506 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004508 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004511 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
4513 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004514 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 return FAIL;
4516
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004517 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004518 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519
4520 /*
4521 * If there is a comparative operator, use it.
4522 */
4523 if (type != EXPR_UNKNOWN)
4524 {
4525 int ic = FALSE; // Default: do not ignore case
4526
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004527 if (next != NULL)
4528 {
4529 *arg = next_line_from_context(cctx, TRUE);
4530 p = skipwhite(*arg);
4531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 if (type_is && (p[len] == '?' || p[len] == '#'))
4533 {
4534 semsg(_(e_invexpr2), *arg);
4535 return FAIL;
4536 }
4537 // extra question mark appended: ignore case
4538 if (p[len] == '?')
4539 {
4540 ic = TRUE;
4541 ++len;
4542 }
4543 // extra '#' appended: match case (ignored)
4544 else if (p[len] == '#')
4545 ++len;
4546 // nothing appended: match case
4547
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004548 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004550 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 }
4553
4554 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004555 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004556 return FAIL;
4557
Bram Moolenaara5565e42020-05-09 15:44:01 +02004558 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 return FAIL;
4560
Bram Moolenaara5565e42020-05-09 15:44:01 +02004561 if (ppconst->pp_used == ppconst_used + 2)
4562 {
4563 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4564 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4565 int ret;
4566
4567 // Both sides are a constant, compute the result now.
4568 // First check for a valid combination of types, this is more
4569 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004570 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004571 ret = FAIL;
4572 else
4573 {
4574 ret = typval_compare(tv1, tv2, type, ic);
4575 tv1->v_type = VAR_BOOL;
4576 tv1->vval.v_number = tv1->vval.v_number
4577 ? VVAL_TRUE : VVAL_FALSE;
4578 clear_tv(tv2);
4579 --ppconst->pp_used;
4580 }
4581 return ret;
4582 }
4583
4584 generate_ppconst(cctx, ppconst);
4585 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 }
4587
4588 return OK;
4589}
4590
Bram Moolenaar7f141552020-05-09 17:35:53 +02004591static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593/*
4594 * Compile || or &&.
4595 */
4596 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004597compile_and_or(
4598 char_u **arg,
4599 cctx_T *cctx,
4600 char *op,
4601 ppconst_T *ppconst,
4602 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004604 char_u *next;
4605 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 int opchar = *op;
4607
4608 if (p[0] == opchar && p[1] == opchar)
4609 {
4610 garray_T *instr = &cctx->ctx_instr;
4611 garray_T end_ga;
4612
4613 /*
4614 * Repeat until there is no following "||" or "&&"
4615 */
4616 ga_init2(&end_ga, sizeof(int), 10);
4617 while (p[0] == opchar && p[1] == opchar)
4618 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004619 if (next != NULL)
4620 {
4621 *arg = next_line_from_context(cctx, TRUE);
4622 p = skipwhite(*arg);
4623 }
4624
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004625 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4626 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004627 semsg(_(e_white_space_required_before_and_after_str_at_str),
4628 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004629 return FAIL;
4630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004632 // TODO: use ppconst if the value is a constant and check
4633 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004634 generate_ppconst(cctx, ppconst);
4635
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004636 // Every part must evaluate to a bool.
4637 if (bool_on_stack(cctx) == FAIL)
4638 {
4639 ga_clear(&end_ga);
4640 return FAIL;
4641 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 if (ga_grow(&end_ga, 1) == FAIL)
4644 {
4645 ga_clear(&end_ga);
4646 return FAIL;
4647 }
4648 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4649 ++end_ga.ga_len;
4650 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004651 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652
4653 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004654 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004655 {
4656 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004657 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004658 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004659
Bram Moolenaara5565e42020-05-09 15:44:01 +02004660 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4661 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 {
4663 ga_clear(&end_ga);
4664 return FAIL;
4665 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004666
4667 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004669 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004671 // Every part must evaluate to a bool.
4672 if (bool_on_stack(cctx) == FAIL)
4673 {
4674 ga_clear(&end_ga);
4675 return FAIL;
4676 }
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 // Fill in the end label in all jumps.
4679 while (end_ga.ga_len > 0)
4680 {
4681 isn_T *isn;
4682
4683 --end_ga.ga_len;
4684 isn = ((isn_T *)instr->ga_data)
4685 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4686 isn->isn_arg.jump.jump_where = instr->ga_len;
4687 }
4688 ga_clear(&end_ga);
4689 }
4690
4691 return OK;
4692}
4693
4694/*
4695 * expr4a && expr4a && expr4a logical AND
4696 *
4697 * Produces instructions:
4698 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004699 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004700 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004702 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 * EVAL expr4c Push result of "expr4c"
4704 * end:
4705 */
4706 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004709 int ppconst_used = ppconst->pp_used;
4710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004712 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 return FAIL;
4714
4715 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004716 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717}
4718
4719/*
4720 * expr3a || expr3b || expr3c logical OR
4721 *
4722 * Produces instructions:
4723 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004724 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004725 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004727 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728 * EVAL expr3c Push result of "expr3c"
4729 * end:
4730 */
4731 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004732compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 int ppconst_used = ppconst->pp_used;
4735
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004737 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 return FAIL;
4739
4740 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004741 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742}
4743
4744/*
4745 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004747 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 * JUMP_IF_FALSE alt jump if false
4749 * EVAL expr1a
4750 * JUMP_ALWAYS end
4751 * alt: EVAL expr1b
4752 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004753 *
4754 * Toplevel expression: expr2 ?? expr1
4755 * Produces instructions:
4756 * EVAL expr2 Push result of "expr2"
4757 * JUMP_AND_KEEP_IF_TRUE end jump if true
4758 * EVAL expr1
4759 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 */
4761 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004762compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763{
4764 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004765 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004766 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaar3988f642020-08-27 22:43:03 +02004768 // Ignore all kinds of errors when not producing code.
4769 if (cctx->ctx_skip == SKIP_YES)
4770 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004771 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004772 return OK;
4773 }
4774
Bram Moolenaar61a89812020-05-07 16:58:17 +02004775 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004776 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 return FAIL;
4778
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004779 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 if (*p == '?')
4781 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004782 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 garray_T *instr = &cctx->ctx_instr;
4784 garray_T *stack = &cctx->ctx_type_stack;
4785 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004786 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004787 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004788 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004789 int has_const_expr = FALSE;
4790 int const_value = FALSE;
4791 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004793 if (next != NULL)
4794 {
4795 *arg = next_line_from_context(cctx, TRUE);
4796 p = skipwhite(*arg);
4797 }
4798
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004799 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004800 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004801 semsg(_(e_white_space_required_before_and_after_str_at_str),
4802 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004803 return FAIL;
4804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805
Bram Moolenaara5565e42020-05-09 15:44:01 +02004806 if (ppconst->pp_used == ppconst_used + 1)
4807 {
4808 // the condition is a constant, we know whether the ? or the :
4809 // expression is to be evaluated.
4810 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004811 if (op_falsy)
4812 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4813 else
4814 {
4815 int error = FALSE;
4816
4817 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4818 &error);
4819 if (error)
4820 return FAIL;
4821 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004822 cctx->ctx_skip = save_skip == SKIP_YES ||
4823 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4824
4825 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4826 // "left ?? right" and "left" is truthy: produce "left"
4827 generate_ppconst(cctx, ppconst);
4828 else
4829 {
4830 clear_tv(&ppconst->pp_tv[ppconst_used]);
4831 --ppconst->pp_used;
4832 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004833 }
4834 else
4835 {
4836 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004837 if (op_falsy)
4838 end_idx = instr->ga_len;
4839 generate_JUMP(cctx, op_falsy
4840 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4841 if (op_falsy)
4842 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004843 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844
4845 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004846 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004847 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004848 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004849 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850
Bram Moolenaara5565e42020-05-09 15:44:01 +02004851 if (!has_const_expr)
4852 {
4853 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004855 if (!op_falsy)
4856 {
4857 // remember the type and drop it
4858 --stack->ga_len;
4859 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004861 end_idx = instr->ga_len;
4862 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004863
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004864 // jump here from JUMP_IF_FALSE
4865 isn = ((isn_T *)instr->ga_data) + alt_idx;
4866 isn->isn_arg.jump.jump_where = instr->ga_len;
4867 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004870 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004872 // Check for the ":".
4873 p = may_peek_next_line(cctx, *arg, &next);
4874 if (*p != ':')
4875 {
4876 emsg(_(e_missing_colon));
4877 return FAIL;
4878 }
4879 if (next != NULL)
4880 {
4881 *arg = next_line_from_context(cctx, TRUE);
4882 p = skipwhite(*arg);
4883 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004884
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004885 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4886 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004887 semsg(_(e_white_space_required_before_and_after_str_at_str),
4888 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004889 return FAIL;
4890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004892 // evaluate the third expression
4893 if (has_const_expr)
4894 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004895 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004896 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004897 return FAIL;
4898 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4899 return FAIL;
4900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 if (!has_const_expr)
4903 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004904 type_T **typep;
4905
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004909 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4910 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004911
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004912 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004913 isn = ((isn_T *)instr->ga_data) + end_idx;
4914 isn->isn_arg.jump.jump_where = instr->ga_len;
4915 }
4916
4917 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 }
4919 return OK;
4920}
4921
4922/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004923 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004924 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4925 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004926 */
4927 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004928compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929{
4930 ppconst_T ppconst;
4931
4932 CLEAR_FIELD(ppconst);
4933 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4934 {
4935 clear_ppconst(&ppconst);
4936 return FAIL;
4937 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004938 if (is_const != NULL)
4939 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004940 if (generate_ppconst(cctx, &ppconst) == FAIL)
4941 return FAIL;
4942 return OK;
4943}
4944
4945/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004946 * Toplevel expression.
4947 */
4948 static int
4949compile_expr0(char_u **arg, cctx_T *cctx)
4950{
4951 return compile_expr0_ext(arg, cctx, NULL);
4952}
4953
4954/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 * compile "return [expr]"
4956 */
4957 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004958compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959{
4960 char_u *p = arg;
4961 garray_T *stack = &cctx->ctx_type_stack;
4962 type_T *stack_type;
4963
4964 if (*p != NUL && *p != '|' && *p != '\n')
4965 {
4966 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004967 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 return NULL;
4969
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004970 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004971 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004972 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004973 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004974 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4975 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004976 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004977 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004978 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004979 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004980 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004981 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4982 && stack_type->tt_type != VAR_VOID
4983 && stack_type->tt_type != VAR_UNKNOWN)
4984 {
4985 emsg(_(e_returning_value_in_function_without_return_type));
4986 return NULL;
4987 }
4988 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004989 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004990 return NULL;
4991 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004992 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 }
4994 else
4995 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004996 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004997 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004998 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4999 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005001 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 return NULL;
5003 }
5004
5005 // No argument, return zero.
5006 generate_PUSHNR(cctx, 0);
5007 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005008
5009 // Undo any command modifiers.
5010 generate_undo_cmdmods(cctx);
5011
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005012 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 return NULL;
5014
5015 // "return val | endif" is possible
5016 return skipwhite(p);
5017}
5018
5019/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005020 * Get a line from the compilation context, compatible with exarg_T getline().
5021 * Return a pointer to the line in allocated memory.
5022 * Return NULL for end-of-file or some error.
5023 */
5024 static char_u *
5025exarg_getline(
5026 int c UNUSED,
5027 void *cookie,
5028 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005029 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005030{
5031 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005032 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005033
Bram Moolenaar66250c92020-08-20 15:02:42 +02005034 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005035 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005036 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005037 return NULL;
5038 ++cctx->ctx_lnum;
5039 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5040 // Comment lines result in NULL pointers, skip them.
5041 if (p != NULL)
5042 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005043 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005044}
5045
5046/*
5047 * Compile a nested :def command.
5048 */
5049 static char_u *
5050compile_nested_function(exarg_T *eap, cctx_T *cctx)
5051{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005052 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005053 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005054 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005055 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005056 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005057 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005058
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005059 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005060 {
5061 emsg(_(e_cannot_use_bang_with_nested_def));
5062 return NULL;
5063 }
5064
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005065 if (*name_start == '/')
5066 {
5067 name_end = skip_regexp(name_start + 1, '/', TRUE);
5068 if (*name_end == '/')
5069 ++name_end;
5070 eap->nextcmd = check_nextcmd(name_end);
5071 }
5072 if (name_end == name_start || *skipwhite(name_end) != '(')
5073 {
5074 if (!ends_excmd2(name_start, name_end))
5075 {
5076 semsg(_(e_invalid_command_str), eap->cmd);
5077 return NULL;
5078 }
5079
5080 // "def" or "def Name": list functions
5081 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5082 return NULL;
5083 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5084 }
5085
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005086 // Only g:Func() can use a namespace.
5087 if (name_start[1] == ':' && !is_global)
5088 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005089 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005090 return NULL;
5091 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005092 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5093 return NULL;
5094
Bram Moolenaar04b12692020-05-04 23:24:44 +02005095 eap->arg = name_end;
5096 eap->getline = exarg_getline;
5097 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005098 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005099 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005100 lambda_name = vim_strsave(get_lambda_name());
5101 if (lambda_name == NULL)
5102 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005103 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005104
Bram Moolenaar822ba242020-05-24 23:00:18 +02005105 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005106 {
5107 r = eap->skip ? OK : FAIL;
5108 goto theend;
5109 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005110 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5111 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005112 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005113 {
5114 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005115 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005116 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005117
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005118 if (is_global)
5119 {
5120 char_u *func_name = vim_strnsave(name_start + 2,
5121 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005122
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005123 if (func_name == NULL)
5124 r = FAIL;
5125 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005126 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005127 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005128 lambda_name = NULL;
5129 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005130 }
5131 else
5132 {
5133 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005134 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005135 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005136 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005137
Bram Moolenaareef21022020-08-01 22:16:43 +02005138 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005139 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005140 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005141 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005142 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005143
5144 // copy over the block scope IDs
5145 if (block_depth > 0)
5146 {
5147 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5148 if (ufunc->uf_block_ids != NULL)
5149 {
5150 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5151 sizeof(int) * block_depth);
5152 ufunc->uf_block_depth = block_depth;
5153 }
5154 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005155 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005156 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005157
5158theend:
5159 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005160 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005161}
5162
5163/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 * Return the length of an assignment operator, or zero if there isn't one.
5165 */
5166 int
5167assignment_len(char_u *p, int *heredoc)
5168{
5169 if (*p == '=')
5170 {
5171 if (p[1] == '<' && p[2] == '<')
5172 {
5173 *heredoc = TRUE;
5174 return 3;
5175 }
5176 return 1;
5177 }
5178 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5179 return 2;
5180 if (STRNCMP(p, "..=", 3) == 0)
5181 return 3;
5182 return 0;
5183}
5184
5185// words that cannot be used as a variable
5186static char *reserved[] = {
5187 "true",
5188 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005189 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 NULL
5191};
5192
Bram Moolenaar752fc692021-01-04 21:57:11 +01005193// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005194typedef enum {
5195 dest_local,
5196 dest_option,
5197 dest_env,
5198 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005199 dest_buffer,
5200 dest_window,
5201 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005202 dest_vimvar,
5203 dest_script,
5204 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005205 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005206} assign_dest_T;
5207
Bram Moolenaar752fc692021-01-04 21:57:11 +01005208// Used by compile_lhs() to store information about the LHS of an assignment
5209// and one argument of ":unlet" with an index.
5210typedef struct {
5211 assign_dest_T lhs_dest; // type of destination
5212
5213 char_u *lhs_name; // allocated name including
5214 // "[expr]" or ".name".
5215 size_t lhs_varlen; // length of the variable without
5216 // "[expr]" or ".name"
5217 char_u *lhs_dest_end; // end of the destination, including
5218 // "[expr]" or ".name".
5219
5220 int lhs_has_index; // has "[expr]" or ".name"
5221
5222 int lhs_new_local; // create new local variable
5223 int lhs_opt_flags; // for when destination is an option
5224 int lhs_vimvaridx; // for when destination is a v:var
5225
5226 lvar_T lhs_local_lvar; // used for existing local destination
5227 lvar_T lhs_arg_lvar; // used for argument destination
5228 lvar_T *lhs_lvar; // points to destination lvar
5229 int lhs_scriptvar_sid;
5230 int lhs_scriptvar_idx;
5231
5232 int lhs_has_type; // type was specified
5233 type_T *lhs_type;
5234 type_T *lhs_member_type;
5235} lhs_T;
5236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005238 * Generate the load instruction for "name".
5239 */
5240 static void
5241generate_loadvar(
5242 cctx_T *cctx,
5243 assign_dest_T dest,
5244 char_u *name,
5245 lvar_T *lvar,
5246 type_T *type)
5247{
5248 switch (dest)
5249 {
5250 case dest_option:
5251 // TODO: check the option exists
5252 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5253 break;
5254 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005255 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5256 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5257 else
5258 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005259 break;
5260 case dest_buffer:
5261 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5262 break;
5263 case dest_window:
5264 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5265 break;
5266 case dest_tab:
5267 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5268 break;
5269 case dest_script:
5270 compile_load_scriptvar(cctx,
5271 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5272 break;
5273 case dest_env:
5274 // Include $ in the name here
5275 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5276 break;
5277 case dest_reg:
5278 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5279 break;
5280 case dest_vimvar:
5281 generate_LOADV(cctx, name + 2, TRUE);
5282 break;
5283 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005284 if (lvar->lv_from_outer > 0)
5285 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5286 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005287 else
5288 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5289 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005290 case dest_expr:
5291 // list or dict value should already be on the stack.
5292 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005293 }
5294}
5295
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005296/*
5297 * Skip over "[expr]" or ".member".
5298 * Does not check for any errors.
5299 */
5300 static char_u *
5301skip_index(char_u *start)
5302{
5303 char_u *p = start;
5304
5305 if (*p == '[')
5306 {
5307 p = skipwhite(p + 1);
5308 (void)skip_expr(&p, NULL);
5309 p = skipwhite(p);
5310 if (*p == ']')
5311 return p + 1;
5312 return p;
5313 }
5314 // if (*p == '.')
5315 return to_name_end(p + 1, TRUE);
5316}
5317
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005318 void
5319vim9_declare_error(char_u *name)
5320{
5321 char *scope = "";
5322
5323 switch (*name)
5324 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005325 case 'g': scope = _("global"); break;
5326 case 'b': scope = _("buffer"); break;
5327 case 'w': scope = _("window"); break;
5328 case 't': scope = _("tab"); break;
5329 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005330 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005331 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005332 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005333 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005334 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005335 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005336 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005337 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005338 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005339}
5340
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005342 * For one assignment figure out the type of destination. Return it in "dest".
5343 * When not recognized "dest" is not set.
5344 * For an option "opt_flags" is set.
5345 * For a v:var "vimvaridx" is set.
5346 * "type" is set to the destination type if known, unchanted otherwise.
5347 * Return FAIL if an error message was given.
5348 */
5349 static int
5350get_var_dest(
5351 char_u *name,
5352 assign_dest_T *dest,
5353 int cmdidx,
5354 int *opt_flags,
5355 int *vimvaridx,
5356 type_T **type,
5357 cctx_T *cctx)
5358{
5359 char_u *p;
5360
5361 if (*name == '&')
5362 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005363 int cc;
5364 long numval;
5365 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005366
5367 *dest = dest_option;
5368 if (cmdidx == CMD_final || cmdidx == CMD_const)
5369 {
5370 emsg(_(e_const_option));
5371 return FAIL;
5372 }
5373 p = name;
5374 p = find_option_end(&p, opt_flags);
5375 if (p == NULL)
5376 {
5377 // cannot happen?
5378 emsg(_(e_letunexp));
5379 return FAIL;
5380 }
5381 cc = *p;
5382 *p = NUL;
5383 opt_type = get_option_value(skip_option_env_lead(name),
5384 &numval, NULL, *opt_flags);
5385 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005386 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005387 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005388 case gov_unknown:
5389 semsg(_(e_unknown_option), name);
5390 return FAIL;
5391 case gov_string:
5392 case gov_hidden_string:
5393 *type = &t_string;
5394 break;
5395 case gov_bool:
5396 case gov_hidden_bool:
5397 *type = &t_bool;
5398 break;
5399 case gov_number:
5400 case gov_hidden_number:
5401 *type = &t_number;
5402 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005403 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005404 }
5405 else if (*name == '$')
5406 {
5407 *dest = dest_env;
5408 *type = &t_string;
5409 }
5410 else if (*name == '@')
5411 {
5412 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5413 {
5414 emsg_invreg(name[1]);
5415 return FAIL;
5416 }
5417 *dest = dest_reg;
5418 *type = &t_string;
5419 }
5420 else if (STRNCMP(name, "g:", 2) == 0)
5421 {
5422 *dest = dest_global;
5423 }
5424 else if (STRNCMP(name, "b:", 2) == 0)
5425 {
5426 *dest = dest_buffer;
5427 }
5428 else if (STRNCMP(name, "w:", 2) == 0)
5429 {
5430 *dest = dest_window;
5431 }
5432 else if (STRNCMP(name, "t:", 2) == 0)
5433 {
5434 *dest = dest_tab;
5435 }
5436 else if (STRNCMP(name, "v:", 2) == 0)
5437 {
5438 typval_T *vtv;
5439 int di_flags;
5440
5441 *vimvaridx = find_vim_var(name + 2, &di_flags);
5442 if (*vimvaridx < 0)
5443 {
5444 semsg(_(e_variable_not_found_str), name);
5445 return FAIL;
5446 }
5447 // We use the current value of "sandbox" here, is that OK?
5448 if (var_check_ro(di_flags, name, FALSE))
5449 return FAIL;
5450 *dest = dest_vimvar;
5451 vtv = get_vim_var_tv(*vimvaridx);
5452 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5453 }
5454 return OK;
5455}
5456
5457/*
5458 * Generate a STORE instruction for "dest", not being "dest_local".
5459 * Return FAIL when out of memory.
5460 */
5461 static int
5462generate_store_var(
5463 cctx_T *cctx,
5464 assign_dest_T dest,
5465 int opt_flags,
5466 int vimvaridx,
5467 int scriptvar_idx,
5468 int scriptvar_sid,
5469 type_T *type,
5470 char_u *name)
5471{
5472 switch (dest)
5473 {
5474 case dest_option:
5475 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5476 opt_flags);
5477 case dest_global:
5478 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005479 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5480 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005481 case dest_buffer:
5482 // include b: with the name, easier to execute that way
5483 return generate_STORE(cctx, ISN_STOREB, 0, name);
5484 case dest_window:
5485 // include w: with the name, easier to execute that way
5486 return generate_STORE(cctx, ISN_STOREW, 0, name);
5487 case dest_tab:
5488 // include t: with the name, easier to execute that way
5489 return generate_STORE(cctx, ISN_STORET, 0, name);
5490 case dest_env:
5491 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5492 case dest_reg:
5493 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5494 case dest_vimvar:
5495 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5496 case dest_script:
5497 if (scriptvar_idx < 0)
5498 {
5499 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005500 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005501
Bram Moolenaar41d61962020-12-06 20:12:43 +01005502 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005503 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5504 scriptvar_sid, type);
5505 if (name_s != name)
5506 vim_free(name_s);
5507 return r;
5508 }
5509 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5510 scriptvar_sid, scriptvar_idx, type);
5511 case dest_local:
5512 case dest_expr:
5513 // cannot happen
5514 break;
5515 }
5516 return FAIL;
5517}
5518
Bram Moolenaar752fc692021-01-04 21:57:11 +01005519 static int
5520is_decl_command(int cmdidx)
5521{
5522 return cmdidx == CMD_let || cmdidx == CMD_var
5523 || cmdidx == CMD_final || cmdidx == CMD_const;
5524}
5525
5526/*
5527 * Figure out the LHS type and other properties for an assignment or one item
5528 * of ":unlet" with an index.
5529 * Returns OK or FAIL.
5530 */
5531 static int
5532compile_lhs(
5533 char_u *var_start,
5534 lhs_T *lhs,
5535 int cmdidx,
5536 int heredoc,
5537 int oplen,
5538 cctx_T *cctx)
5539{
5540 char_u *var_end;
5541 int is_decl = is_decl_command(cmdidx);
5542
5543 CLEAR_POINTER(lhs);
5544 lhs->lhs_dest = dest_local;
5545 lhs->lhs_vimvaridx = -1;
5546 lhs->lhs_scriptvar_idx = -1;
5547
5548 // "dest_end" is the end of the destination, including "[expr]" or
5549 // ".name".
5550 // "var_end" is the end of the variable/option/etc. name.
5551 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5552 if (*var_start == '@')
5553 var_end = var_start + 2;
5554 else
5555 {
5556 // skip over the leading "&", "&l:", "&g:" and "$"
5557 var_end = skip_option_env_lead(var_start);
5558 var_end = to_name_end(var_end, TRUE);
5559 }
5560
5561 // "a: type" is declaring variable "a" with a type, not dict "a:".
5562 if (is_decl && lhs->lhs_dest_end == var_start + 2
5563 && lhs->lhs_dest_end[-1] == ':')
5564 --lhs->lhs_dest_end;
5565 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5566 --var_end;
5567
5568 // compute the length of the destination without "[expr]" or ".name"
5569 lhs->lhs_varlen = var_end - var_start;
5570 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5571 if (lhs->lhs_name == NULL)
5572 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005573
5574 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5575 // Something follows after the variable: "var[idx]" or "var.key".
5576 lhs->lhs_has_index = TRUE;
5577
Bram Moolenaar752fc692021-01-04 21:57:11 +01005578 if (heredoc)
5579 lhs->lhs_type = &t_list_string;
5580 else
5581 lhs->lhs_type = &t_any;
5582
5583 if (cctx->ctx_skip != SKIP_YES)
5584 {
5585 int declare_error = FALSE;
5586
5587 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5588 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5589 &lhs->lhs_type, cctx) == FAIL)
5590 return FAIL;
5591 if (lhs->lhs_dest != dest_local)
5592 {
5593 // Specific kind of variable recognized.
5594 declare_error = is_decl;
5595 }
5596 else
5597 {
5598 int idx;
5599
5600 // No specific kind of variable recognized, just a name.
5601 for (idx = 0; reserved[idx] != NULL; ++idx)
5602 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5603 {
5604 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5605 return FAIL;
5606 }
5607
5608
5609 if (lookup_local(var_start, lhs->lhs_varlen,
5610 &lhs->lhs_local_lvar, cctx) == OK)
5611 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5612 else
5613 {
5614 CLEAR_FIELD(lhs->lhs_arg_lvar);
5615 if (arg_exists(var_start, lhs->lhs_varlen,
5616 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5617 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5618 {
5619 if (is_decl)
5620 {
5621 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5622 return FAIL;
5623 }
5624 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5625 }
5626 }
5627 if (lhs->lhs_lvar != NULL)
5628 {
5629 if (is_decl)
5630 {
5631 semsg(_(e_variable_already_declared), lhs->lhs_name);
5632 return FAIL;
5633 }
5634 }
5635 else
5636 {
5637 int script_namespace = lhs->lhs_varlen > 1
5638 && STRNCMP(var_start, "s:", 2) == 0;
5639 int script_var = (script_namespace
5640 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5641 FALSE, cctx)
5642 : script_var_exists(var_start, lhs->lhs_varlen,
5643 TRUE, cctx)) == OK;
5644 imported_T *import =
5645 find_imported(var_start, lhs->lhs_varlen, cctx);
5646
5647 if (script_namespace || script_var || import != NULL)
5648 {
5649 char_u *rawname = lhs->lhs_name
5650 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5651
5652 if (is_decl)
5653 {
5654 if (script_namespace)
5655 semsg(_(e_cannot_declare_script_variable_in_function),
5656 lhs->lhs_name);
5657 else
5658 semsg(_(e_variable_already_declared_in_script),
5659 lhs->lhs_name);
5660 return FAIL;
5661 }
5662 else if (cctx->ctx_ufunc->uf_script_ctx_version
5663 == SCRIPT_VERSION_VIM9
5664 && script_namespace
5665 && !script_var && import == NULL)
5666 {
5667 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5668 return FAIL;
5669 }
5670
5671 lhs->lhs_dest = dest_script;
5672
5673 // existing script-local variables should have a type
5674 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5675 if (import != NULL)
5676 lhs->lhs_scriptvar_sid = import->imp_sid;
5677 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5678 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005679 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005680 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005681 lhs->lhs_scriptvar_sid, rawname,
5682 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5683 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005684 if (lhs->lhs_scriptvar_idx >= 0)
5685 {
5686 scriptitem_T *si = SCRIPT_ITEM(
5687 lhs->lhs_scriptvar_sid);
5688 svar_T *sv =
5689 ((svar_T *)si->sn_var_vals.ga_data)
5690 + lhs->lhs_scriptvar_idx;
5691 lhs->lhs_type = sv->sv_type;
5692 }
5693 }
5694 }
5695 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5696 == FAIL)
5697 return FAIL;
5698 }
5699 }
5700
5701 if (declare_error)
5702 {
5703 vim9_declare_error(lhs->lhs_name);
5704 return FAIL;
5705 }
5706 }
5707
5708 // handle "a:name" as a name, not index "name" on "a"
5709 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5710 var_end = lhs->lhs_dest_end;
5711
5712 if (lhs->lhs_dest != dest_option)
5713 {
5714 if (is_decl && *var_end == ':')
5715 {
5716 char_u *p;
5717
5718 // parse optional type: "let var: type = expr"
5719 if (!VIM_ISWHITE(var_end[1]))
5720 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005721 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005722 return FAIL;
5723 }
5724 p = skipwhite(var_end + 1);
5725 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5726 if (lhs->lhs_type == NULL)
5727 return FAIL;
5728 lhs->lhs_has_type = TRUE;
5729 }
5730 else if (lhs->lhs_lvar != NULL)
5731 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5732 }
5733
5734 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5735 && lhs->lhs_type->tt_type != VAR_STRING
5736 && lhs->lhs_type->tt_type != VAR_ANY)
5737 {
5738 emsg(_(e_can_only_concatenate_to_string));
5739 return FAIL;
5740 }
5741
5742 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5743 && cctx->ctx_skip != SKIP_YES)
5744 {
5745 if (oplen > 1 && !heredoc)
5746 {
5747 // +=, /=, etc. require an existing variable
5748 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5749 return FAIL;
5750 }
5751 if (!is_decl)
5752 {
5753 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5754 return FAIL;
5755 }
5756
5757 // new local variable
5758 if ((lhs->lhs_type->tt_type == VAR_FUNC
5759 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5760 && var_wrong_func_name(lhs->lhs_name, TRUE))
5761 return FAIL;
5762 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5763 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5764 if (lhs->lhs_lvar == NULL)
5765 return FAIL;
5766 lhs->lhs_new_local = TRUE;
5767 }
5768
5769 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005770 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005771 {
5772 // Something follows after the variable: "var[idx]" or "var.key".
5773 // TODO: should we also handle "->func()" here?
5774 if (is_decl)
5775 {
5776 emsg(_(e_cannot_use_index_when_declaring_variable));
5777 return FAIL;
5778 }
5779
5780 if (var_start[lhs->lhs_varlen] == '['
5781 || var_start[lhs->lhs_varlen] == '.')
5782 {
5783 char_u *after = var_start + lhs->lhs_varlen;
5784 char_u *p;
5785
5786 // Only the last index is used below, if there are others
5787 // before it generate code for the expression. Thus for
5788 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5789 for (;;)
5790 {
5791 p = skip_index(after);
5792 if (*p != '[' && *p != '.')
5793 break;
5794 after = p;
5795 }
5796 if (after > var_start + lhs->lhs_varlen)
5797 {
5798 lhs->lhs_varlen = after - var_start;
5799 lhs->lhs_dest = dest_expr;
5800 // We don't know the type before evaluating the expression,
5801 // use "any" until then.
5802 lhs->lhs_type = &t_any;
5803 }
5804
Bram Moolenaar752fc692021-01-04 21:57:11 +01005805 if (lhs->lhs_type->tt_member == NULL)
5806 lhs->lhs_member_type = &t_any;
5807 else
5808 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5809 }
5810 else
5811 {
5812 semsg("Not supported yet: %s", var_start);
5813 return FAIL;
5814 }
5815 }
5816 return OK;
5817}
5818
5819/*
5820 * Assignment to a list or dict member, or ":unlet" for the item, using the
5821 * information in "lhs".
5822 * Returns OK or FAIL.
5823 */
5824 static int
5825compile_assign_unlet(
5826 char_u *var_start,
5827 lhs_T *lhs,
5828 int is_assign,
5829 type_T *rhs_type,
5830 cctx_T *cctx)
5831{
5832 char_u *p;
5833 int r;
5834 vartype_T dest_type;
5835 size_t varlen = lhs->lhs_varlen;
5836 garray_T *stack = &cctx->ctx_type_stack;
5837
5838 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5839 p = var_start + varlen;
5840 if (*p == '[')
5841 {
5842 p = skipwhite(p + 1);
5843 r = compile_expr0(&p, cctx);
5844 if (r == OK && *skipwhite(p) != ']')
5845 {
5846 // this should not happen
5847 emsg(_(e_missbrac));
5848 r = FAIL;
5849 }
5850 }
5851 else // if (*p == '.')
5852 {
5853 char_u *key_end = to_name_end(p + 1, TRUE);
5854 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5855
5856 r = generate_PUSHS(cctx, key);
5857 }
5858 if (r == FAIL)
5859 return FAIL;
5860
5861 if (lhs->lhs_type == &t_any)
5862 {
5863 // Index on variable of unknown type: check at runtime.
5864 dest_type = VAR_ANY;
5865 }
5866 else
5867 {
5868 dest_type = lhs->lhs_type->tt_type;
5869 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5870 return FAIL;
5871 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005872 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5873 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005874 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005875 }
5876
5877 // Load the dict or list. On the stack we then have:
5878 // - value (for assignment, not for :unlet)
5879 // - index
5880 // - variable
5881 if (lhs->lhs_dest == dest_expr)
5882 {
5883 int c = var_start[varlen];
5884
5885 // Evaluate "ll[expr]" of "ll[expr][idx]"
5886 p = var_start;
5887 var_start[varlen] = NUL;
5888 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5889 {
5890 // this should not happen
5891 emsg(_(e_missbrac));
5892 return FAIL;
5893 }
5894 var_start[varlen] = c;
5895
5896 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5897 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5898 // now we can properly check the type
5899 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005900 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005901 FALSE, FALSE) == FAIL)
5902 return FAIL;
5903 }
5904 else
5905 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5906 lhs->lhs_lvar, lhs->lhs_type);
5907
5908 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5909 {
5910 if (is_assign)
5911 {
5912 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5913
5914 if (isn == NULL)
5915 return FAIL;
5916 isn->isn_arg.vartype = dest_type;
5917 }
5918 else
5919 {
5920 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5921 return FAIL;
5922 }
5923 }
5924 else
5925 {
5926 emsg(_(e_indexable_type_required));
5927 return FAIL;
5928 }
5929
5930 return OK;
5931}
5932
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005933/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005934 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005935 * "let name"
5936 * "var name = expr"
5937 * "final name = expr"
5938 * "const name = expr"
5939 * "name = expr"
5940 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005941 * Return NULL for an error.
5942 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 */
5944 static char_u *
5945compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5946{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005947 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005949 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 char_u *ret = NULL;
5951 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005952 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005955 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 int oplen = 0;
5958 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005959 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005960 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005961 int is_decl = is_decl_command(cmdidx);
5962 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005964 // Skip over the "var" or "[var, var]" to get to any "=".
5965 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5966 if (p == NULL)
5967 return *arg == '[' ? arg : NULL;
5968
5969 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005971 // TODO: should we allow this, and figure out type inference from list
5972 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005973 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 return NULL;
5975 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005976 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978 sp = p;
5979 p = skipwhite(p);
5980 op = p;
5981 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005982
5983 if (var_count > 0 && oplen == 0)
5984 // can be something like "[1, 2]->func()"
5985 return arg;
5986
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005987 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005989 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005990 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005991 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 if (heredoc)
5994 {
5995 list_T *l;
5996 listitem_T *li;
5997
5998 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005999 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006001 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006002 if (l == NULL)
6003 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006004
Bram Moolenaar078269b2020-09-21 20:35:55 +02006005 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006007 // Push each line and the create the list.
6008 FOR_ALL_LIST_ITEMS(l, li)
6009 {
6010 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6011 li->li_tv.vval.v_string = NULL;
6012 }
6013 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 list_free(l);
6016 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006017 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006019 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006021 char_u *wp;
6022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006023 // for "[var, var] = expr" evaluate the expression here, loop over the
6024 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006025 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006026
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006027 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006028 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006029 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006030 if (compile_expr0(&p, cctx) == FAIL)
6031 return NULL;
6032 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006034 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006036 type_T *stacktype;
6037
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006038 stacktype = stack->ga_len == 0 ? &t_void
6039 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006040 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006042 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006043 goto theend;
6044 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006045 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006046 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006047 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006048 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006049 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6050 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006051 if (stacktype->tt_member != NULL)
6052 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006053 }
6054 }
6055
6056 /*
6057 * Loop over variables in "[var, var] = expr".
6058 * For "var = expr" and "let var: type" this is done only once.
6059 */
6060 if (var_count > 0)
6061 var_start = skipwhite(arg + 1); // skip over the "["
6062 else
6063 var_start = arg;
6064 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6065 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006066 int instr_count = -1;
6067
Bram Moolenaar752fc692021-01-04 21:57:11 +01006068 vim_free(lhs.lhs_name);
6069
6070 /*
6071 * Figure out the LHS type and other properties.
6072 */
6073 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6074 goto theend;
6075
6076 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006077 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006078 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006079 goto theend;
6080 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006081 if (!is_decl && lhs.lhs_lvar != NULL
6082 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006083 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006084 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006085 goto theend;
6086 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006087
6088 if (!heredoc)
6089 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006090 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006092 if (oplen > 0 && var_count == 0)
6093 {
6094 // skip over the "=" and the expression
6095 p = skipwhite(op + oplen);
6096 compile_expr0(&p, cctx);
6097 }
6098 }
6099 else if (oplen > 0)
6100 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006101 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006102 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006103
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006104 // For "var = expr" evaluate the expression.
6105 if (var_count == 0)
6106 {
6107 int r;
6108
6109 // for "+=", "*=", "..=" etc. first load the current value
6110 if (*op != '=')
6111 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006112 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6113 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006114
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006116 {
6117 // TODO: get member from list or dict
6118 emsg("Index with operation not supported yet");
6119 goto theend;
6120 }
6121 }
6122
6123 // Compile the expression. Temporarily hide the new local
6124 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006125 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006126 --cctx->ctx_locals.ga_len;
6127 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006128 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006129 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006130 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006131 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006132 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006133 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006134 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006135 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006136 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006137 ++cctx->ctx_locals.ga_len;
6138 if (r == FAIL)
6139 goto theend;
6140 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006141 else if (semicolon && var_idx == var_count - 1)
6142 {
6143 // For "[var; var] = expr" get the rest of the list
6144 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6145 goto theend;
6146 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006147 else
6148 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006149 // For "[var, var] = expr" get the "var_idx" item from the
6150 // list.
6151 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006152 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006153 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006154
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006155 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006156 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006158 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006159 if ((rhs_type->tt_type == VAR_FUNC
6160 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006161 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006162 goto theend;
6163
Bram Moolenaar752fc692021-01-04 21:57:11 +01006164 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006165 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006166 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006167 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006168 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006169 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006170 }
6171 else
6172 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006173 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006174 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006175 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006176 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006177 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006178 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006179 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006181 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006182 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006183 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006184 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006185 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006186 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006187 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006188
Bram Moolenaar71abe482020-09-14 22:28:30 +02006189 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006190 if (lhs.lhs_has_index)
6191 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006192 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006193 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006194 goto theend;
6195 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006196 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006197 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006198 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006199 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006201 else if (cmdidx == CMD_final)
6202 {
6203 emsg(_(e_final_requires_a_value));
6204 goto theend;
6205 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006206 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006208 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006209 goto theend;
6210 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006212 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006213 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006214 goto theend;
6215 }
6216 else
6217 {
6218 // variables are always initialized
6219 if (ga_grow(instr, 1) == FAIL)
6220 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006222 {
6223 case VAR_BOOL:
6224 generate_PUSHBOOL(cctx, VVAL_FALSE);
6225 break;
6226 case VAR_FLOAT:
6227#ifdef FEAT_FLOAT
6228 generate_PUSHF(cctx, 0.0);
6229#endif
6230 break;
6231 case VAR_STRING:
6232 generate_PUSHS(cctx, NULL);
6233 break;
6234 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006235 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006236 break;
6237 case VAR_FUNC:
6238 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6239 break;
6240 case VAR_LIST:
6241 generate_NEWLIST(cctx, 0);
6242 break;
6243 case VAR_DICT:
6244 generate_NEWDICT(cctx, 0);
6245 break;
6246 case VAR_JOB:
6247 generate_PUSHJOB(cctx, NULL);
6248 break;
6249 case VAR_CHANNEL:
6250 generate_PUSHCHANNEL(cctx, NULL);
6251 break;
6252 case VAR_NUMBER:
6253 case VAR_UNKNOWN:
6254 case VAR_ANY:
6255 case VAR_PARTIAL:
6256 case VAR_VOID:
6257 case VAR_SPECIAL: // cannot happen
6258 generate_PUSHNR(cctx, 0);
6259 break;
6260 }
6261 }
6262 if (var_count == 0)
6263 end = p;
6264 }
6265
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006266 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006267 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006268 break;
6269
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006270 if (oplen > 0 && *op != '=')
6271 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006272 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006273 type_T *stacktype;
6274
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006275 if (*op == '.')
6276 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006277 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006278 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006279 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006280 if (
6281#ifdef FEAT_FLOAT
6282 // If variable is float operation with number is OK.
6283 !(expected == &t_float && stacktype == &t_number) &&
6284#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006285 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006286 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006287 goto theend;
6288
6289 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006290 {
6291 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6292 goto theend;
6293 }
6294 else if (*op == '+')
6295 {
6296 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006297 operator_type(lhs.lhs_member_type, stacktype),
6298 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006299 goto theend;
6300 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006301 else if (generate_two_op(cctx, op) == FAIL)
6302 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304
Bram Moolenaar752fc692021-01-04 21:57:11 +01006305 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006306 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006307 // Use the info in "lhs" to store the value at the index in the
6308 // list or dict.
6309 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6310 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006312 }
6313 else
6314 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6316 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006317 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006318 generate_LOCKCONST(cctx);
6319
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006320 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 && (lhs.lhs_type->tt_type == VAR_DICT
6322 || lhs.lhs_type->tt_type == VAR_LIST)
6323 && lhs.lhs_type->tt_member != NULL
6324 && lhs.lhs_type->tt_member != &t_any
6325 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006326 // Set the type in the list or dict, so that it can be checked,
6327 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006328 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006329
Bram Moolenaar752fc692021-01-04 21:57:11 +01006330 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006331 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006332 if (generate_store_var(cctx, lhs.lhs_dest,
6333 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6334 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6335 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006336 goto theend;
6337 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006339 {
6340 isn_T *isn = ((isn_T *)instr->ga_data)
6341 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006342
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006343 // optimization: turn "var = 123" from ISN_PUSHNR +
6344 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006345 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006346 && instr->ga_len == instr_count + 1
6347 && isn->isn_type == ISN_PUSHNR)
6348 {
6349 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006350
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006351 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006352 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006353 isn->isn_arg.storenr.stnr_val = val;
6354 if (stack->ga_len > 0)
6355 --stack->ga_len;
6356 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006357 else if (lhs.lhs_lvar->lv_from_outer > 0)
6358 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6359 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006360 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006362 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006363 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006364
6365 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006368
6369 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006370 if (var_count > 0 && !semicolon)
6371 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006372 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006373 goto theend;
6374 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006375
Bram Moolenaarb2097502020-07-19 17:17:02 +02006376 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006377
6378theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 return ret;
6381}
6382
6383/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006384 * Check for an assignment at "eap->cmd", compile it if found.
6385 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6386 */
6387 static int
6388may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6389{
6390 char_u *pskip;
6391 char_u *p;
6392
6393 // Assuming the command starts with a variable or function name,
6394 // find what follows.
6395 // Skip over "var.member", "var[idx]" and the like.
6396 // Also "&opt = val", "$ENV = val" and "@r = val".
6397 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6398 ? eap->cmd + 1 : eap->cmd;
6399 p = to_name_end(pskip, TRUE);
6400 if (p > eap->cmd && *p != NUL)
6401 {
6402 char_u *var_end;
6403 int oplen;
6404 int heredoc;
6405
6406 if (eap->cmd[0] == '@')
6407 var_end = eap->cmd + 2;
6408 else
6409 var_end = find_name_end(pskip, NULL, NULL,
6410 FNE_CHECK_START | FNE_INCL_BR);
6411 oplen = assignment_len(skipwhite(var_end), &heredoc);
6412 if (oplen > 0)
6413 {
6414 size_t len = p - eap->cmd;
6415
6416 // Recognize an assignment if we recognize the variable
6417 // name:
6418 // "g:var = expr"
6419 // "local = expr" where "local" is a local var.
6420 // "script = expr" where "script" is a script-local var.
6421 // "import = expr" where "import" is an imported var
6422 // "&opt = expr"
6423 // "$ENV = expr"
6424 // "@r = expr"
6425 if (*eap->cmd == '&'
6426 || *eap->cmd == '$'
6427 || *eap->cmd == '@'
6428 || ((len) > 2 && eap->cmd[1] == ':')
6429 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6430 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6431 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6432 || find_imported(eap->cmd, len, cctx) != NULL)
6433 {
6434 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6435 if (*line == NULL || *line == eap->cmd)
6436 return FAIL;
6437 return OK;
6438 }
6439 }
6440 }
6441
6442 if (*eap->cmd == '[')
6443 {
6444 // [var, var] = expr
6445 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6446 if (*line == NULL)
6447 return FAIL;
6448 if (*line != eap->cmd)
6449 return OK;
6450 }
6451 return NOTDONE;
6452}
6453
6454/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006455 * Check if "name" can be "unlet".
6456 */
6457 int
6458check_vim9_unlet(char_u *name)
6459{
6460 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6461 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006462 // "unlet s:var" is allowed in legacy script.
6463 if (*name == 's' && !script_is_vim9())
6464 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006465 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006466 return FAIL;
6467 }
6468 return OK;
6469}
6470
6471/*
6472 * Callback passed to ex_unletlock().
6473 */
6474 static int
6475compile_unlet(
6476 lval_T *lvp,
6477 char_u *name_end,
6478 exarg_T *eap,
6479 int deep UNUSED,
6480 void *coookie)
6481{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006482 cctx_T *cctx = coookie;
6483 char_u *p = lvp->ll_name;
6484 int cc = *name_end;
6485 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006486
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 if (cctx->ctx_skip == SKIP_YES)
6488 return OK;
6489
6490 *name_end = NUL;
6491 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006492 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 // :unlet $ENV_VAR
6494 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6495 }
6496 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6497 {
6498 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006499
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 // This is similar to assigning: lookup the list/dict, compile the
6501 // idx/key. Then instead of storing the value unlet the item.
6502 // unlet {list}[idx]
6503 // unlet {dict}[key] dict.key
6504 //
6505 // Figure out the LHS type and other properties.
6506 //
6507 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6508
6509 // : unlet an indexed item
6510 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006511 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006512 iemsg("called compile_lhs() without an index");
6513 ret = FAIL;
6514 }
6515 else
6516 {
6517 // Use the info in "lhs" to unlet the item at the index in the
6518 // list or dict.
6519 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006520 }
6521
Bram Moolenaar752fc692021-01-04 21:57:11 +01006522 vim_free(lhs.lhs_name);
6523 }
6524 else if (check_vim9_unlet(p) == FAIL)
6525 {
6526 ret = FAIL;
6527 }
6528 else
6529 {
6530 // Normal name. Only supports g:, w:, t: and b: namespaces.
6531 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006532 }
6533
Bram Moolenaar752fc692021-01-04 21:57:11 +01006534 *name_end = cc;
6535 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006536}
6537
6538/*
6539 * compile "unlet var", "lock var" and "unlock var"
6540 * "arg" points to "var".
6541 */
6542 static char_u *
6543compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6544{
6545 char_u *p = arg;
6546
6547 if (eap->cmdidx != CMD_unlet)
6548 {
6549 emsg("Sorry, :lock and unlock not implemented yet");
6550 return NULL;
6551 }
6552
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006553 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6554 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006555 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6556}
6557
6558/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006559 * Compile an :import command.
6560 */
6561 static char_u *
6562compile_import(char_u *arg, cctx_T *cctx)
6563{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006564 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006565}
6566
6567/*
6568 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6569 */
6570 static int
6571compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6572{
6573 garray_T *instr = &cctx->ctx_instr;
6574 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6575
6576 if (endlabel == NULL)
6577 return FAIL;
6578 endlabel->el_next = *el;
6579 *el = endlabel;
6580 endlabel->el_end_label = instr->ga_len;
6581
6582 generate_JUMP(cctx, when, 0);
6583 return OK;
6584}
6585
6586 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006587compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006588{
6589 garray_T *instr = &cctx->ctx_instr;
6590
6591 while (*el != NULL)
6592 {
6593 endlabel_T *cur = (*el);
6594 isn_T *isn;
6595
6596 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006597 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006598 *el = cur->el_next;
6599 vim_free(cur);
6600 }
6601}
6602
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006603 static void
6604compile_free_jump_to_end(endlabel_T **el)
6605{
6606 while (*el != NULL)
6607 {
6608 endlabel_T *cur = (*el);
6609
6610 *el = cur->el_next;
6611 vim_free(cur);
6612 }
6613}
6614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006615/*
6616 * Create a new scope and set up the generic items.
6617 */
6618 static scope_T *
6619new_scope(cctx_T *cctx, scopetype_T type)
6620{
6621 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6622
6623 if (scope == NULL)
6624 return NULL;
6625 scope->se_outer = cctx->ctx_scope;
6626 cctx->ctx_scope = scope;
6627 scope->se_type = type;
6628 scope->se_local_count = cctx->ctx_locals.ga_len;
6629 return scope;
6630}
6631
6632/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006633 * Free the current scope and go back to the outer scope.
6634 */
6635 static void
6636drop_scope(cctx_T *cctx)
6637{
6638 scope_T *scope = cctx->ctx_scope;
6639
6640 if (scope == NULL)
6641 {
6642 iemsg("calling drop_scope() without a scope");
6643 return;
6644 }
6645 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006646 switch (scope->se_type)
6647 {
6648 case IF_SCOPE:
6649 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6650 case FOR_SCOPE:
6651 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6652 case WHILE_SCOPE:
6653 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6654 case TRY_SCOPE:
6655 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6656 case NO_SCOPE:
6657 case BLOCK_SCOPE:
6658 break;
6659 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006660 vim_free(scope);
6661}
6662
6663/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006664 * compile "if expr"
6665 *
6666 * "if expr" Produces instructions:
6667 * EVAL expr Push result of "expr"
6668 * JUMP_IF_FALSE end
6669 * ... body ...
6670 * end:
6671 *
6672 * "if expr | else" Produces instructions:
6673 * EVAL expr Push result of "expr"
6674 * JUMP_IF_FALSE else
6675 * ... body ...
6676 * JUMP_ALWAYS end
6677 * else:
6678 * ... body ...
6679 * end:
6680 *
6681 * "if expr1 | elseif expr2 | else" Produces instructions:
6682 * EVAL expr Push result of "expr"
6683 * JUMP_IF_FALSE elseif
6684 * ... body ...
6685 * JUMP_ALWAYS end
6686 * elseif:
6687 * EVAL expr Push result of "expr"
6688 * JUMP_IF_FALSE else
6689 * ... body ...
6690 * JUMP_ALWAYS end
6691 * else:
6692 * ... body ...
6693 * end:
6694 */
6695 static char_u *
6696compile_if(char_u *arg, cctx_T *cctx)
6697{
6698 char_u *p = arg;
6699 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006700 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006701 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006702 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006703 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006704
Bram Moolenaara5565e42020-05-09 15:44:01 +02006705 CLEAR_FIELD(ppconst);
6706 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006707 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006708 clear_ppconst(&ppconst);
6709 return NULL;
6710 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006711 if (!ends_excmd2(arg, skipwhite(p)))
6712 {
6713 semsg(_(e_trailing_arg), p);
6714 return NULL;
6715 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006716 if (cctx->ctx_skip == SKIP_YES)
6717 clear_ppconst(&ppconst);
6718 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006719 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006720 int error = FALSE;
6721 int v;
6722
Bram Moolenaara5565e42020-05-09 15:44:01 +02006723 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006724 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006725 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006726 if (error)
6727 return NULL;
6728 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006729 }
6730 else
6731 {
6732 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006733 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006734 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006735 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006736 if (bool_on_stack(cctx) == FAIL)
6737 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006738 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006739
6740 scope = new_scope(cctx, IF_SCOPE);
6741 if (scope == NULL)
6742 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006743 scope->se_skip_save = skip_save;
6744 // "is_had_return" will be reset if any block does not end in :return
6745 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006746
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006747 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006748 {
6749 // "where" is set when ":elseif", "else" or ":endif" is found
6750 scope->se_u.se_if.is_if_label = instr->ga_len;
6751 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6752 }
6753 else
6754 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006755
Bram Moolenaarced68a02021-01-24 17:53:47 +01006756#ifdef FEAT_PROFILE
6757 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6758 && skip_save != SKIP_YES)
6759 {
6760 // generated a profile start, need to generate a profile end, since it
6761 // won't be done after returning
6762 cctx->ctx_skip = SKIP_NOT;
6763 generate_instr(cctx, ISN_PROF_END);
6764 cctx->ctx_skip = SKIP_YES;
6765 }
6766#endif
6767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006768 return p;
6769}
6770
6771 static char_u *
6772compile_elseif(char_u *arg, cctx_T *cctx)
6773{
6774 char_u *p = arg;
6775 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006776 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006777 isn_T *isn;
6778 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006779 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006780 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006781
6782 if (scope == NULL || scope->se_type != IF_SCOPE)
6783 {
6784 emsg(_(e_elseif_without_if));
6785 return NULL;
6786 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006787 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006788 if (!cctx->ctx_had_return)
6789 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006790
Bram Moolenaarced68a02021-01-24 17:53:47 +01006791 if (cctx->ctx_skip == SKIP_NOT)
6792 {
6793 // previous block was executed, this one and following will not
6794 cctx->ctx_skip = SKIP_YES;
6795 scope->se_u.se_if.is_seen_skip_not = TRUE;
6796 }
6797 if (scope->se_u.se_if.is_seen_skip_not)
6798 {
6799 // A previous block was executed, skip over expression and bail out.
6800 // Do not count the "elseif" for profiling.
6801#ifdef FEAT_PROFILE
6802 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6803 .isn_type == ISN_PROF_START)
6804 --instr->ga_len;
6805#endif
6806 skip_expr_cctx(&p, cctx);
6807 return p;
6808 }
6809
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006810 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006811 {
6812 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006813 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006814 return NULL;
6815 // previous "if" or "elseif" jumps here
6816 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6817 isn->isn_arg.jump.jump_where = instr->ga_len;
6818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006819
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006820 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006821 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006822 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006823 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006824 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006825#ifdef FEAT_PROFILE
6826 if (cctx->ctx_profiling)
6827 {
6828 // the previous block was skipped, need to profile this line
6829 generate_instr(cctx, ISN_PROF_START);
6830 instr_count = instr->ga_len;
6831 }
6832#endif
6833 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006834 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006835 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006836 clear_ppconst(&ppconst);
6837 return NULL;
6838 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006839 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006840 if (!ends_excmd2(arg, skipwhite(p)))
6841 {
6842 semsg(_(e_trailing_arg), p);
6843 return NULL;
6844 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006845 if (scope->se_skip_save == SKIP_YES)
6846 clear_ppconst(&ppconst);
6847 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006848 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006849 int error = FALSE;
6850 int v;
6851
Bram Moolenaar7f141552020-05-09 17:35:53 +02006852 // The expression results in a constant.
6853 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006854 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6855 if (error)
6856 return NULL;
6857 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006858 clear_ppconst(&ppconst);
6859 scope->se_u.se_if.is_if_label = -1;
6860 }
6861 else
6862 {
6863 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006864 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006865 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006866 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006867 if (bool_on_stack(cctx) == FAIL)
6868 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006869
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006870 // "where" is set when ":elseif", "else" or ":endif" is found
6871 scope->se_u.se_if.is_if_label = instr->ga_len;
6872 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006874
6875 return p;
6876}
6877
6878 static char_u *
6879compile_else(char_u *arg, cctx_T *cctx)
6880{
6881 char_u *p = arg;
6882 garray_T *instr = &cctx->ctx_instr;
6883 isn_T *isn;
6884 scope_T *scope = cctx->ctx_scope;
6885
6886 if (scope == NULL || scope->se_type != IF_SCOPE)
6887 {
6888 emsg(_(e_else_without_if));
6889 return NULL;
6890 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006891 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006892 if (!cctx->ctx_had_return)
6893 scope->se_u.se_if.is_had_return = FALSE;
6894 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006895
Bram Moolenaarced68a02021-01-24 17:53:47 +01006896#ifdef FEAT_PROFILE
6897 if (cctx->ctx_profiling)
6898 {
6899 if (cctx->ctx_skip == SKIP_NOT
6900 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6901 .isn_type == ISN_PROF_START)
6902 // the previous block was executed, do not count "else" for profiling
6903 --instr->ga_len;
6904 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
6905 {
6906 // the previous block was not executed, this one will, do count the
6907 // "else" for profiling
6908 cctx->ctx_skip = SKIP_NOT;
6909 generate_instr(cctx, ISN_PROF_END);
6910 generate_instr(cctx, ISN_PROF_START);
6911 cctx->ctx_skip = SKIP_YES;
6912 }
6913 }
6914#endif
6915
6916 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006917 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006918 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006919 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006920 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006921 if (!cctx->ctx_had_return
6922 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6923 JUMP_ALWAYS, cctx) == FAIL)
6924 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006925 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006926
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006927 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006928 {
6929 if (scope->se_u.se_if.is_if_label >= 0)
6930 {
6931 // previous "if" or "elseif" jumps here
6932 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6933 isn->isn_arg.jump.jump_where = instr->ga_len;
6934 scope->se_u.se_if.is_if_label = -1;
6935 }
6936 }
6937
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006938 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006939 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941
6942 return p;
6943}
6944
6945 static char_u *
6946compile_endif(char_u *arg, cctx_T *cctx)
6947{
6948 scope_T *scope = cctx->ctx_scope;
6949 ifscope_T *ifscope;
6950 garray_T *instr = &cctx->ctx_instr;
6951 isn_T *isn;
6952
6953 if (scope == NULL || scope->se_type != IF_SCOPE)
6954 {
6955 emsg(_(e_endif_without_if));
6956 return NULL;
6957 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006958 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006959 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006960 if (!cctx->ctx_had_return)
6961 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006962
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006963 if (scope->se_u.se_if.is_if_label >= 0)
6964 {
6965 // previous "if" or "elseif" jumps here
6966 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6967 isn->isn_arg.jump.jump_where = instr->ga_len;
6968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006970 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01006971
6972#ifdef FEAT_PROFILE
6973 // even when skipping we count the endif as executed, unless the block it's
6974 // in is skipped
6975 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6976 && scope->se_skip_save != SKIP_YES)
6977 {
6978 cctx->ctx_skip = SKIP_NOT;
6979 generate_instr(cctx, ISN_PROF_START);
6980 }
6981#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02006982 cctx->ctx_skip = scope->se_skip_save;
6983
6984 // If all the blocks end in :return and there is an :else then the
6985 // had_return flag is set.
6986 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006988 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006989 return arg;
6990}
6991
6992/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006993 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 *
6995 * Produces instructions:
6996 * PUSHNR -1
6997 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006998 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006999 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7000 * - if beyond end, jump to "end"
7001 * - otherwise get item from list and push it
7002 * STORE var Store item in "var"
7003 * ... body ...
7004 * JUMP top Jump back to repeat
7005 * end: DROP Drop the result of "expr"
7006 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007007 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7008 * UNPACK 2 Split item in 2
7009 * STORE var1 Store item in "var1"
7010 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 */
7012 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007013compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007015 char_u *arg;
7016 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007017 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007018 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007019 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007020 int var_count = 0;
7021 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 size_t varlen;
7023 garray_T *instr = &cctx->ctx_instr;
7024 garray_T *stack = &cctx->ctx_type_stack;
7025 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007026 lvar_T *loop_lvar; // loop iteration variable
7027 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007029 type_T *item_type = &t_any;
7030 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031
Bram Moolenaar792f7862020-11-23 08:31:18 +01007032 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007033 if (p == NULL)
7034 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007035 if (var_count == 0)
7036 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037
7038 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007039 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007040 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7041 return NULL;
7042 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 {
7044 emsg(_(e_missing_in));
7045 return NULL;
7046 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007047 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007048 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7049 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051 scope = new_scope(cctx, FOR_SCOPE);
7052 if (scope == NULL)
7053 return NULL;
7054
Bram Moolenaar792f7862020-11-23 08:31:18 +01007055 // Reserve a variable to store the loop iteration counter and initialize it
7056 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007057 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7058 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007059 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007060 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007061 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007063 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007064 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065
7066 // compile "expr", it remains on the stack until "endfor"
7067 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007068 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007069 {
7070 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007072 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007073 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007075 // Now that we know the type of "var", check that it is a list, now or at
7076 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007078 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007080 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081 return NULL;
7082 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007083
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007084 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007085 {
7086 if (var_count == 1)
7087 item_type = vartype->tt_member;
7088 else if (vartype->tt_member->tt_type == VAR_LIST
7089 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7090 item_type = vartype->tt_member->tt_member;
7091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
7093 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007094 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007095 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096
Bram Moolenaar792f7862020-11-23 08:31:18 +01007097 arg = arg_start;
7098 if (var_count > 1)
7099 {
7100 generate_UNPACK(cctx, var_count, semicolon);
7101 arg = skipwhite(arg + 1); // skip white after '['
7102
7103 // the list item is replaced by a number of items
7104 if (ga_grow(stack, var_count - 1) == FAIL)
7105 {
7106 drop_scope(cctx);
7107 return NULL;
7108 }
7109 --stack->ga_len;
7110 for (idx = 0; idx < var_count; ++idx)
7111 {
7112 ((type_T **)stack->ga_data)[stack->ga_len] =
7113 (semicolon && idx == 0) ? vartype : item_type;
7114 ++stack->ga_len;
7115 }
7116 }
7117
7118 for (idx = 0; idx < var_count; ++idx)
7119 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007120 assign_dest_T dest = dest_local;
7121 int opt_flags = 0;
7122 int vimvaridx = -1;
7123 type_T *type = &t_any;
7124
7125 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007126 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007127 name = vim_strnsave(arg, varlen);
7128 if (name == NULL)
7129 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007130
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007131 // TODO: script var not supported?
7132 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7133 &vimvaridx, &type, cctx) == FAIL)
7134 goto failed;
7135 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007136 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007137 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7138 0, 0, type, name) == FAIL)
7139 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007140 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007141 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007142 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007143 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007144 {
7145 semsg(_(e_variable_already_declared), arg);
7146 goto failed;
7147 }
7148
Bram Moolenaarea870692020-12-02 14:24:30 +01007149 if (STRNCMP(name, "s:", 2) == 0)
7150 {
7151 semsg(_(e_cannot_declare_script_variable_in_function), name);
7152 goto failed;
7153 }
7154
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007155 // Reserve a variable to store "var".
7156 // TODO: check for type
7157 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7158 if (var_lvar == NULL)
7159 // out of memory or used as an argument
7160 goto failed;
7161
7162 if (semicolon && idx == var_count - 1)
7163 var_lvar->lv_type = vartype;
7164 else
7165 var_lvar->lv_type = item_type;
7166 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7167 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007168
Bram Moolenaar036d0712021-01-17 20:23:38 +01007169 if (*p == ':')
7170 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007171 if (*p == ',' || *p == ';')
7172 ++p;
7173 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007174 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007175 }
7176
7177 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007178
7179failed:
7180 vim_free(name);
7181 drop_scope(cctx);
7182 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007183}
7184
7185/*
7186 * compile "endfor"
7187 */
7188 static char_u *
7189compile_endfor(char_u *arg, cctx_T *cctx)
7190{
7191 garray_T *instr = &cctx->ctx_instr;
7192 scope_T *scope = cctx->ctx_scope;
7193 forscope_T *forscope;
7194 isn_T *isn;
7195
7196 if (scope == NULL || scope->se_type != FOR_SCOPE)
7197 {
7198 emsg(_(e_for));
7199 return NULL;
7200 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007201 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007203 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007204
7205 // At end of ":for" scope jump back to the FOR instruction.
7206 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7207
7208 // Fill in the "end" label in the FOR statement so it can jump here
7209 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7210 isn->isn_arg.forloop.for_end = instr->ga_len;
7211
7212 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007213 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007214
7215 // Below the ":for" scope drop the "expr" list from the stack.
7216 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7217 return NULL;
7218
7219 vim_free(scope);
7220
7221 return arg;
7222}
7223
7224/*
7225 * compile "while expr"
7226 *
7227 * Produces instructions:
7228 * top: EVAL expr Push result of "expr"
7229 * JUMP_IF_FALSE end jump if false
7230 * ... body ...
7231 * JUMP top Jump back to repeat
7232 * end:
7233 *
7234 */
7235 static char_u *
7236compile_while(char_u *arg, cctx_T *cctx)
7237{
7238 char_u *p = arg;
7239 garray_T *instr = &cctx->ctx_instr;
7240 scope_T *scope;
7241
7242 scope = new_scope(cctx, WHILE_SCOPE);
7243 if (scope == NULL)
7244 return NULL;
7245
Bram Moolenaarb2049902021-01-24 12:53:53 +01007246 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007247 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007248#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007249 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7250 .isn_type == ISN_PROF_START)
7251 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007252#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007253
7254 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007255 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007257 if (!ends_excmd2(arg, skipwhite(p)))
7258 {
7259 semsg(_(e_trailing_arg), p);
7260 return NULL;
7261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007262
Bram Moolenaar13106602020-10-04 16:06:05 +02007263 if (bool_on_stack(cctx) == FAIL)
7264 return FAIL;
7265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007266 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007267 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268 JUMP_IF_FALSE, cctx) == FAIL)
7269 return FAIL;
7270
7271 return p;
7272}
7273
7274/*
7275 * compile "endwhile"
7276 */
7277 static char_u *
7278compile_endwhile(char_u *arg, cctx_T *cctx)
7279{
7280 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007281 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007282
7283 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7284 {
7285 emsg(_(e_while));
7286 return NULL;
7287 }
7288 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007289 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290
Bram Moolenaarf002a412021-01-24 13:34:18 +01007291#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007292 // count the endwhile before jumping
7293 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007294#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007297 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007298
7299 // Fill in the "end" label in the WHILE statement so it can jump here.
7300 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007301 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7302 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007303
7304 vim_free(scope);
7305
7306 return arg;
7307}
7308
7309/*
7310 * compile "continue"
7311 */
7312 static char_u *
7313compile_continue(char_u *arg, cctx_T *cctx)
7314{
7315 scope_T *scope = cctx->ctx_scope;
7316
7317 for (;;)
7318 {
7319 if (scope == NULL)
7320 {
7321 emsg(_(e_continue));
7322 return NULL;
7323 }
7324 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7325 break;
7326 scope = scope->se_outer;
7327 }
7328
7329 // Jump back to the FOR or WHILE instruction.
7330 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007331 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7332 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 return arg;
7334}
7335
7336/*
7337 * compile "break"
7338 */
7339 static char_u *
7340compile_break(char_u *arg, cctx_T *cctx)
7341{
7342 scope_T *scope = cctx->ctx_scope;
7343 endlabel_T **el;
7344
7345 for (;;)
7346 {
7347 if (scope == NULL)
7348 {
7349 emsg(_(e_break));
7350 return NULL;
7351 }
7352 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7353 break;
7354 scope = scope->se_outer;
7355 }
7356
7357 // Jump to the end of the FOR or WHILE loop.
7358 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007359 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007361 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7363 return FAIL;
7364
7365 return arg;
7366}
7367
7368/*
7369 * compile "{" start of block
7370 */
7371 static char_u *
7372compile_block(char_u *arg, cctx_T *cctx)
7373{
7374 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7375 return NULL;
7376 return skipwhite(arg + 1);
7377}
7378
7379/*
7380 * compile end of block: drop one scope
7381 */
7382 static void
7383compile_endblock(cctx_T *cctx)
7384{
7385 scope_T *scope = cctx->ctx_scope;
7386
7387 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007388 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007389 vim_free(scope);
7390}
7391
7392/*
7393 * compile "try"
7394 * Creates a new scope for the try-endtry, pointing to the first catch and
7395 * finally.
7396 * Creates another scope for the "try" block itself.
7397 * TRY instruction sets up exception handling at runtime.
7398 *
7399 * "try"
7400 * TRY -> catch1, -> finally push trystack entry
7401 * ... try block
7402 * "throw {exception}"
7403 * EVAL {exception}
7404 * THROW create exception
7405 * ... try block
7406 * " catch {expr}"
7407 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007408 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007409 * EVAL {expr}
7410 * MATCH
7411 * JUMP nomatch -> catch2
7412 * CATCH remove exception
7413 * ... catch block
7414 * " catch"
7415 * JUMP -> finally
7416 * catch2: CATCH remove exception
7417 * ... catch block
7418 * " finally"
7419 * finally:
7420 * ... finally block
7421 * " endtry"
7422 * ENDTRY pop trystack entry, may rethrow
7423 */
7424 static char_u *
7425compile_try(char_u *arg, cctx_T *cctx)
7426{
7427 garray_T *instr = &cctx->ctx_instr;
7428 scope_T *try_scope;
7429 scope_T *scope;
7430
7431 // scope that holds the jumps that go to catch/finally/endtry
7432 try_scope = new_scope(cctx, TRY_SCOPE);
7433 if (try_scope == NULL)
7434 return NULL;
7435
Bram Moolenaar69f70502021-01-01 16:10:46 +01007436 if (cctx->ctx_skip != SKIP_YES)
7437 {
7438 // "catch" is set when the first ":catch" is found.
7439 // "finally" is set when ":finally" or ":endtry" is found
7440 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7441 if (generate_instr(cctx, ISN_TRY) == NULL)
7442 return NULL;
7443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444
7445 // scope for the try block itself
7446 scope = new_scope(cctx, BLOCK_SCOPE);
7447 if (scope == NULL)
7448 return NULL;
7449
7450 return arg;
7451}
7452
7453/*
7454 * compile "catch {expr}"
7455 */
7456 static char_u *
7457compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7458{
7459 scope_T *scope = cctx->ctx_scope;
7460 garray_T *instr = &cctx->ctx_instr;
7461 char_u *p;
7462 isn_T *isn;
7463
7464 // end block scope from :try or :catch
7465 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7466 compile_endblock(cctx);
7467 scope = cctx->ctx_scope;
7468
7469 // Error if not in a :try scope
7470 if (scope == NULL || scope->se_type != TRY_SCOPE)
7471 {
7472 emsg(_(e_catch));
7473 return NULL;
7474 }
7475
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007476 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007478 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 return NULL;
7480 }
7481
Bram Moolenaar69f70502021-01-01 16:10:46 +01007482 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007484#ifdef FEAT_PROFILE
7485 // the profile-start should be after the jump
7486 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7487 .isn_type == ISN_PROF_START)
7488 --instr->ga_len;
7489#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007490 // Jump from end of previous block to :finally or :endtry
7491 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7492 JUMP_ALWAYS, cctx) == FAIL)
7493 return NULL;
7494
7495 // End :try or :catch scope: set value in ISN_TRY instruction
7496 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7497 if (isn->isn_arg.try.try_catch == 0)
7498 isn->isn_arg.try.try_catch = instr->ga_len;
7499 if (scope->se_u.se_try.ts_catch_label != 0)
7500 {
7501 // Previous catch without match jumps here
7502 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7503 isn->isn_arg.jump.jump_where = instr->ga_len;
7504 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007505#ifdef FEAT_PROFILE
7506 if (cctx->ctx_profiling)
7507 {
7508 // a "throw" that jumps here needs to be counted
7509 generate_instr(cctx, ISN_PROF_END);
7510 // the "catch" is also counted
7511 generate_instr(cctx, ISN_PROF_START);
7512 }
7513#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007514 }
7515
7516 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007517 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007518 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007519 scope->se_u.se_try.ts_caught_all = TRUE;
7520 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 }
7522 else
7523 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007524 char_u *end;
7525 char_u *pat;
7526 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007527 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007528 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007530 // Push v:exception, push {expr} and MATCH
7531 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7532
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007533 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007534 if (*end != *p)
7535 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007536 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007537 vim_free(tofree);
7538 return FAIL;
7539 }
7540 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007541 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007542 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007543 len = (int)(end - tofree);
7544 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007545 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007546 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007547 if (pat == NULL)
7548 return FAIL;
7549 if (generate_PUSHS(cctx, pat) == FAIL)
7550 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7553 return NULL;
7554
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007555 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7557 return NULL;
7558 }
7559
Bram Moolenaar69f70502021-01-01 16:10:46 +01007560 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561 return NULL;
7562
7563 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7564 return NULL;
7565 return p;
7566}
7567
7568 static char_u *
7569compile_finally(char_u *arg, cctx_T *cctx)
7570{
7571 scope_T *scope = cctx->ctx_scope;
7572 garray_T *instr = &cctx->ctx_instr;
7573 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007574 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575
7576 // end block scope from :try or :catch
7577 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7578 compile_endblock(cctx);
7579 scope = cctx->ctx_scope;
7580
7581 // Error if not in a :try scope
7582 if (scope == NULL || scope->se_type != TRY_SCOPE)
7583 {
7584 emsg(_(e_finally));
7585 return NULL;
7586 }
7587
7588 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007589 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 if (isn->isn_arg.try.try_finally != 0)
7591 {
7592 emsg(_(e_finally_dup));
7593 return NULL;
7594 }
7595
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007596 this_instr = instr->ga_len;
7597#ifdef FEAT_PROFILE
7598 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7599 .isn_type == ISN_PROF_START)
7600 // jump to the profile start of the "finally"
7601 --this_instr;
7602#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007603
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007604 // Fill in the "end" label in jumps at the end of the blocks.
7605 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7606 this_instr, cctx);
7607
7608 isn->isn_arg.try.try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007609 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007610 {
7611 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007612 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007613 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007614 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007615 }
7616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617 // TODO: set index in ts_finally_label jumps
7618
7619 return arg;
7620}
7621
7622 static char_u *
7623compile_endtry(char_u *arg, cctx_T *cctx)
7624{
7625 scope_T *scope = cctx->ctx_scope;
7626 garray_T *instr = &cctx->ctx_instr;
7627 isn_T *isn;
7628
7629 // end block scope from :catch or :finally
7630 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7631 compile_endblock(cctx);
7632 scope = cctx->ctx_scope;
7633
7634 // Error if not in a :try scope
7635 if (scope == NULL || scope->se_type != TRY_SCOPE)
7636 {
7637 if (scope == NULL)
7638 emsg(_(e_no_endtry));
7639 else if (scope->se_type == WHILE_SCOPE)
7640 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007641 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007642 emsg(_(e_endfor));
7643 else
7644 emsg(_(e_endif));
7645 return NULL;
7646 }
7647
Bram Moolenaar69f70502021-01-01 16:10:46 +01007648 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007650 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7651 if (isn->isn_arg.try.try_catch == 0
7652 && isn->isn_arg.try.try_finally == 0)
7653 {
7654 emsg(_(e_missing_catch_or_finally));
7655 return NULL;
7656 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007657
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007658#ifdef FEAT_PROFILE
7659 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7660 .isn_type == ISN_PROF_START)
7661 // move the profile start after "endtry" so that it's not counted when
7662 // the exception is rethrown.
7663 --instr->ga_len;
7664#endif
7665
Bram Moolenaar69f70502021-01-01 16:10:46 +01007666 // Fill in the "end" label in jumps at the end of the blocks, if not
7667 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007668 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7669 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007670
Bram Moolenaar69f70502021-01-01 16:10:46 +01007671 // End :catch or :finally scope: set value in ISN_TRY instruction
7672 if (isn->isn_arg.try.try_catch == 0)
7673 isn->isn_arg.try.try_catch = instr->ga_len;
7674 if (isn->isn_arg.try.try_finally == 0)
7675 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007676
Bram Moolenaar69f70502021-01-01 16:10:46 +01007677 if (scope->se_u.se_try.ts_catch_label != 0)
7678 {
7679 // Last catch without match jumps here
7680 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7681 isn->isn_arg.jump.jump_where = instr->ga_len;
7682 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007683 }
7684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007685 compile_endblock(cctx);
7686
Bram Moolenaar69f70502021-01-01 16:10:46 +01007687 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007688 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007689#ifdef FEAT_PROFILE
7690 if (cctx->ctx_profiling)
7691 generate_instr(cctx, ISN_PROF_START);
7692#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007693 return arg;
7694}
7695
7696/*
7697 * compile "throw {expr}"
7698 */
7699 static char_u *
7700compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7701{
7702 char_u *p = skipwhite(arg);
7703
Bram Moolenaara5565e42020-05-09 15:44:01 +02007704 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007705 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007706 if (cctx->ctx_skip == SKIP_YES)
7707 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 if (may_generate_2STRING(-1, cctx) == FAIL)
7709 return NULL;
7710 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7711 return NULL;
7712
7713 return p;
7714}
7715
7716/*
7717 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007718 * compile "echomsg expr"
7719 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007720 * compile "execute expr"
7721 */
7722 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007723compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007724{
7725 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007726 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007727 int count = 0;
7728
7729 for (;;)
7730 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007731 if (ends_excmd2(prev, p))
7732 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007733 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007734 return NULL;
7735 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007736 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007737 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007738 }
7739
Bram Moolenaare4984292020-12-13 14:19:25 +01007740 if (count > 0)
7741 {
7742 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7743 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7744 else if (cmdidx == CMD_execute)
7745 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7746 else if (cmdidx == CMD_echomsg)
7747 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7748 else
7749 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 return p;
7752}
7753
7754/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007755 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007756 * instruction to compute it and return OK.
7757 * Otherwise return FAIL, the caller must deal with any range.
7758 */
7759 static int
7760compile_variable_range(exarg_T *eap, cctx_T *cctx)
7761{
7762 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7763 char_u *p = skipdigits(eap->cmd);
7764
7765 if (p == range_end)
7766 return FAIL;
7767 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7768}
7769
7770/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007771 * :put r
7772 * :put ={expr}
7773 */
7774 static char_u *
7775compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7776{
7777 char_u *line = arg;
7778 linenr_T lnum;
7779 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007780 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007781
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007782 eap->regname = *line;
7783
7784 if (eap->regname == '=')
7785 {
7786 char_u *p = line + 1;
7787
7788 if (compile_expr0(&p, cctx) == FAIL)
7789 return NULL;
7790 line = p;
7791 }
7792 else if (eap->regname != NUL)
7793 ++line;
7794
Bram Moolenaar08597872020-12-10 19:43:40 +01007795 if (compile_variable_range(eap, cctx) == OK)
7796 {
7797 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7798 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007799 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007800 {
7801 // Either no range or a number.
7802 // "errormsg" will not be set because the range is ADDR_LINES.
7803 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007804 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007805 return NULL;
7806 if (eap->addr_count == 0)
7807 lnum = -1;
7808 else
7809 lnum = eap->line2;
7810 if (above)
7811 --lnum;
7812 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007813
7814 generate_PUT(cctx, eap->regname, lnum);
7815 return line;
7816}
7817
7818/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007819 * A command that is not compiled, execute with legacy code.
7820 */
7821 static char_u *
7822compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7823{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007824 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007825 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007826 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007827
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007828 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007829 goto theend;
7830
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007831 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007832 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007833 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007834 int usefilter = FALSE;
7835
7836 has_expr = argt & (EX_XFILE | EX_EXPAND);
7837
7838 // If the command can be followed by a bar, find the bar and truncate
7839 // it, so that the following command can be compiled.
7840 // The '|' is overwritten with a NUL, it is put back below.
7841 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7842 && *eap->arg == '!')
7843 // :w !filter or :r !filter or :r! filter
7844 usefilter = TRUE;
7845 if ((argt & EX_TRLBAR) && !usefilter)
7846 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007847 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007848 separate_nextcmd(eap);
7849 if (eap->nextcmd != NULL)
7850 nextcmd = eap->nextcmd;
7851 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007852 else if (eap->cmdidx == CMD_wincmd)
7853 {
7854 p = eap->arg;
7855 if (*p != NUL)
7856 ++p;
7857 if (*p == 'g' || *p == Ctrl_G)
7858 ++p;
7859 p = skipwhite(p);
7860 if (*p == '|')
7861 {
7862 *p = NUL;
7863 nextcmd = p + 1;
7864 }
7865 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007866 }
7867
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007868 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7869 {
7870 // expand filename in "syntax include [@group] filename"
7871 has_expr = TRUE;
7872 eap->arg = skipwhite(eap->arg + 7);
7873 if (*eap->arg == '@')
7874 eap->arg = skiptowhite(eap->arg);
7875 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007876
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007877 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7878 && STRLEN(eap->arg) > 4)
7879 {
7880 int delim = *eap->arg;
7881
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007882 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007883 if (*p == delim)
7884 {
7885 eap->arg = p + 1;
7886 has_expr = TRUE;
7887 }
7888 }
7889
Bram Moolenaarecac5912021-01-05 19:23:28 +01007890 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7891 {
7892 // TODO: should only expand when appropriate for the command
7893 eap->arg = skiptowhite(eap->arg);
7894 has_expr = TRUE;
7895 }
7896
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007897 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007898 {
7899 int count = 0;
7900 char_u *start = skipwhite(line);
7901
7902 // :cmd xxx`=expr1`yyy`=expr2`zzz
7903 // PUSHS ":cmd xxx"
7904 // eval expr1
7905 // PUSHS "yyy"
7906 // eval expr2
7907 // PUSHS "zzz"
7908 // EXECCONCAT 5
7909 for (;;)
7910 {
7911 if (p > start)
7912 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007913 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007914 ++count;
7915 }
7916 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007917 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007918 return NULL;
7919 may_generate_2STRING(-1, cctx);
7920 ++count;
7921 p = skipwhite(p);
7922 if (*p != '`')
7923 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007924 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007925 return NULL;
7926 }
7927 start = p + 1;
7928
7929 p = (char_u *)strstr((char *)start, "`=");
7930 if (p == NULL)
7931 {
7932 if (*skipwhite(start) != NUL)
7933 {
7934 generate_PUSHS(cctx, vim_strsave(start));
7935 ++count;
7936 }
7937 break;
7938 }
7939 }
7940 generate_EXECCONCAT(cctx, count);
7941 }
7942 else
7943 generate_EXEC(cctx, line);
7944
7945theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007946 if (*nextcmd != NUL)
7947 {
7948 // the parser expects a pointer to the bar, put it back
7949 --nextcmd;
7950 *nextcmd = '|';
7951 }
7952
7953 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007954}
7955
7956/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007957 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007958 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007959 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007960 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007961add_def_function(ufunc_T *ufunc)
7962{
7963 dfunc_T *dfunc;
7964
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007965 if (def_functions.ga_len == 0)
7966 {
7967 // The first position is not used, so that a zero uf_dfunc_idx means it
7968 // wasn't set.
7969 if (ga_grow(&def_functions, 1) == FAIL)
7970 return FAIL;
7971 ++def_functions.ga_len;
7972 }
7973
Bram Moolenaar09689a02020-05-09 22:50:08 +02007974 // Add the function to "def_functions".
7975 if (ga_grow(&def_functions, 1) == FAIL)
7976 return FAIL;
7977 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7978 CLEAR_POINTER(dfunc);
7979 dfunc->df_idx = def_functions.ga_len;
7980 ufunc->uf_dfunc_idx = dfunc->df_idx;
7981 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007982 dfunc->df_name = vim_strsave(ufunc->uf_name);
7983 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007984 ++def_functions.ga_len;
7985 return OK;
7986}
7987
7988/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 * After ex_function() has collected all the function lines: parse and compile
7990 * the lines into instructions.
7991 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007992 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7993 * the return statement (used for lambda). When uf_ret_type is already set
7994 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01007995 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007996 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007997 * This can be used recursively through compile_lambda(), which may reallocate
7998 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007999 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008001 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008002compile_def_function(
8003 ufunc_T *ufunc,
8004 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008005 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008006 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008007{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008 char_u *line = NULL;
8009 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 cctx_T cctx;
8012 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008013 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008014 int ret = FAIL;
8015 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008016 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008017 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008018 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008019#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008020 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008021#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008022
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008023 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008024 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008025 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8028 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008029 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008031 else
8032 {
8033 if (add_def_function(ufunc) == FAIL)
8034 return FAIL;
8035 new_def_function = TRUE;
8036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037
Bram Moolenaar985116a2020-07-12 17:31:09 +02008038 ufunc->uf_def_status = UF_COMPILING;
8039
Bram Moolenaara80faa82020-04-12 19:37:17 +02008040 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008041
Bram Moolenaarf002a412021-01-24 13:34:18 +01008042#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008043 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008044#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008045 cctx.ctx_ufunc = ufunc;
8046 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008047 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008048 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8049 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8050 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8051 cctx.ctx_type_list = &ufunc->uf_type_list;
8052 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8053 instr = &cctx.ctx_instr;
8054
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008055 // Set the context to the function, it may be compiled when called from
8056 // another script. Set the script version to the most modern one.
8057 // The line number will be set in next_line_from_context().
8058 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008059 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8060
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008061 // Make sure error messages are OK.
8062 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8063 if (do_estack_push)
8064 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008065 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008066
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008067 if (ufunc->uf_def_args.ga_len > 0)
8068 {
8069 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008070 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008071 int i;
8072 char_u *arg;
8073 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008074 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008075
8076 // Produce instructions for the default values of optional arguments.
8077 // Store the instruction index in uf_def_arg_idx[] so that we know
8078 // where to start when the function is called, depending on the number
8079 // of arguments.
8080 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8081 if (ufunc->uf_def_arg_idx == NULL)
8082 goto erret;
8083 for (i = 0; i < count; ++i)
8084 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008085 garray_T *stack = &cctx.ctx_type_stack;
8086 type_T *val_type;
8087 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008088 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008089
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008090 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8091 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008092 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008093 goto erret;
8094
8095 // If no type specified use the type of the default value.
8096 // Otherwise check that the default value type matches the
8097 // specified type.
8098 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008099 where.wt_index = arg_idx + 1;
8100 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008101 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008102 {
8103 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008104 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008105 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008106 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008107 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008108 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008109
8110 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008111 goto erret;
8112 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008113 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008114
8115 if (did_set_arg_type)
8116 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008117 }
8118
8119 /*
8120 * Loop over all the lines of the function and generate instructions.
8121 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 for (;;)
8123 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008124 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008125 int starts_with_colon = FALSE;
8126 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008127 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008128
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008129 // Bail out on the first error to avoid a flood of errors and report
8130 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008131 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008132 goto erret;
8133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 if (line != NULL && *line == '|')
8135 // the line continues after a '|'
8136 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008137 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008138 && !(*line == '#' && (line == cctx.ctx_line_start
8139 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008140 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008141 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 goto erret;
8143 }
8144 else
8145 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008146 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008147 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008148 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008149 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008150#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008151 if (cctx.ctx_skip != SKIP_YES)
8152 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008153#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008154 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 }
8157
Bram Moolenaara80faa82020-04-12 19:37:17 +02008158 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008159 ea.cmdlinep = &line;
8160 ea.cmd = skipwhite(line);
8161
Bram Moolenaarb2049902021-01-24 12:53:53 +01008162 if (*ea.cmd == '#')
8163 {
8164 // "#" starts a comment
8165 line = (char_u *)"";
8166 continue;
8167 }
8168
Bram Moolenaarf002a412021-01-24 13:34:18 +01008169#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008170 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8171 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008172 {
8173 may_generate_prof_end(&cctx, prof_lnum);
8174
8175 prof_lnum = cctx.ctx_lnum;
8176 generate_instr(&cctx, ISN_PROF_START);
8177 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008178#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008179
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008180 // Some things can be recognized by the first character.
8181 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008183 case '}':
8184 {
8185 // "}" ends a block scope
8186 scopetype_T stype = cctx.ctx_scope == NULL
8187 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008189 if (stype == BLOCK_SCOPE)
8190 {
8191 compile_endblock(&cctx);
8192 line = ea.cmd;
8193 }
8194 else
8195 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008196 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008197 goto erret;
8198 }
8199 if (line != NULL)
8200 line = skipwhite(ea.cmd + 1);
8201 continue;
8202 }
8203
8204 case '{':
8205 // "{" starts a block scope
8206 // "{'a': 1}->func() is something else
8207 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8208 {
8209 line = compile_block(ea.cmd, &cctx);
8210 continue;
8211 }
8212 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008213 }
8214
8215 /*
8216 * COMMAND MODIFIERS
8217 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008218 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008219 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8220 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008221 {
8222 if (errormsg != NULL)
8223 goto erret;
8224 // empty line or comment
8225 line = (char_u *)"";
8226 continue;
8227 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008228 generate_cmdmods(&cctx, &local_cmdmod);
8229 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008230
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008231 // Check if there was a colon after the last command modifier or before
8232 // the current position.
8233 for (p = ea.cmd; p >= line; --p)
8234 {
8235 if (*p == ':')
8236 starts_with_colon = TRUE;
8237 if (p < ea.cmd && !VIM_ISWHITE(*p))
8238 break;
8239 }
8240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008242 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008244 {
8245 if (*ea.cmd == '(')
8246 // not for "call()"
8247 ea.cmd = p;
8248 else
8249 ea.cmd = skipwhite(ea.cmd);
8250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008251
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008252 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008254 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008255
Bram Moolenaar17126b12021-01-07 22:03:02 +01008256 // Check for assignment after command modifiers.
8257 assign = may_compile_assignment(&ea, &line, &cctx);
8258 if (assign == OK)
8259 goto nextline;
8260 if (assign == FAIL)
8261 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008262 }
8263
8264 /*
8265 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008266 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008268 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008269 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008270 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008271 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008272 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008273 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008274 if (!starts_with_colon)
8275 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008276 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008277 goto erret;
8278 }
8279 if (ends_excmd2(line, ea.cmd))
8280 {
8281 // A range without a command: jump to the line.
8282 // TODO: compile to a more efficient command, possibly
8283 // calling parse_cmd_address().
8284 ea.cmdidx = CMD_SIZE;
8285 line = compile_exec(line, &ea, &cctx);
8286 goto nextline;
8287 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008288 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008289 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008290 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008291 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008292 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008293
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008294 if (p == NULL)
8295 {
8296 if (cctx.ctx_skip != SKIP_YES)
8297 emsg(_(e_ambiguous_use_of_user_defined_command));
8298 goto erret;
8299 }
8300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008301 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8302 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008303 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008304 {
8305 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008306 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008307 }
8308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008309 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008310 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008312 // CMD_var cannot happen, compile_assignment() above would be
8313 // used. Most likely an assignment to a non-existing variable.
8314 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008315 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008317 }
8318
Bram Moolenaar3988f642020-08-27 22:43:03 +02008319 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008320 && ea.cmdidx != CMD_elseif
8321 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008322 && ea.cmdidx != CMD_endif
8323 && ea.cmdidx != CMD_endfor
8324 && ea.cmdidx != CMD_endwhile
8325 && ea.cmdidx != CMD_catch
8326 && ea.cmdidx != CMD_finally
8327 && ea.cmdidx != CMD_endtry)
8328 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008329 emsg(_(e_unreachable_code_after_return));
8330 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008331 }
8332
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008333 p = skipwhite(p);
8334 if (ea.cmdidx != CMD_SIZE
8335 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8336 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008337 if (ea.cmdidx >= 0)
8338 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008339 if ((ea.argt & EX_BANG) && *p == '!')
8340 {
8341 ea.forceit = TRUE;
8342 p = skipwhite(p + 1);
8343 }
8344 }
8345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008346 switch (ea.cmdidx)
8347 {
8348 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008349 ea.arg = p;
8350 line = compile_nested_function(&ea, &cctx);
8351 break;
8352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008353 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008354 // TODO: should we allow this, e.g. to declare a global
8355 // function?
8356 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357 goto erret;
8358
8359 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008360 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008361 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008362 break;
8363
8364 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008365 emsg(_(e_cannot_use_let_in_vim9_script));
8366 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008367 case CMD_var:
8368 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008369 case CMD_const:
8370 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008371 if (line == p)
8372 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008373 break;
8374
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008375 case CMD_unlet:
8376 case CMD_unlockvar:
8377 case CMD_lockvar:
8378 line = compile_unletlock(p, &ea, &cctx);
8379 break;
8380
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 case CMD_import:
8382 line = compile_import(p, &cctx);
8383 break;
8384
8385 case CMD_if:
8386 line = compile_if(p, &cctx);
8387 break;
8388 case CMD_elseif:
8389 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008390 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 break;
8392 case CMD_else:
8393 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008394 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395 break;
8396 case CMD_endif:
8397 line = compile_endif(p, &cctx);
8398 break;
8399
8400 case CMD_while:
8401 line = compile_while(p, &cctx);
8402 break;
8403 case CMD_endwhile:
8404 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008405 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 break;
8407
8408 case CMD_for:
8409 line = compile_for(p, &cctx);
8410 break;
8411 case CMD_endfor:
8412 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008413 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008414 break;
8415 case CMD_continue:
8416 line = compile_continue(p, &cctx);
8417 break;
8418 case CMD_break:
8419 line = compile_break(p, &cctx);
8420 break;
8421
8422 case CMD_try:
8423 line = compile_try(p, &cctx);
8424 break;
8425 case CMD_catch:
8426 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008427 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 break;
8429 case CMD_finally:
8430 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008431 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008432 break;
8433 case CMD_endtry:
8434 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008435 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008436 break;
8437 case CMD_throw:
8438 line = compile_throw(p, &cctx);
8439 break;
8440
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008441 case CMD_eval:
8442 if (compile_expr0(&p, &cctx) == FAIL)
8443 goto erret;
8444
Bram Moolenaar3988f642020-08-27 22:43:03 +02008445 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008446 generate_instr_drop(&cctx, ISN_DROP, 1);
8447
8448 line = skipwhite(p);
8449 break;
8450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008451 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008453 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008454 case CMD_echomsg:
8455 case CMD_echoerr:
8456 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008459 case CMD_put:
8460 ea.cmd = cmd;
8461 line = compile_put(p, &ea, &cctx);
8462 break;
8463
Bram Moolenaar3988f642020-08-27 22:43:03 +02008464 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008465
Bram Moolenaarae616492020-07-28 20:07:27 +02008466 case CMD_append:
8467 case CMD_change:
8468 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008469 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008470 case CMD_xit:
8471 not_in_vim9(&ea);
8472 goto erret;
8473
Bram Moolenaar002262f2020-07-08 17:47:57 +02008474 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008475 if (cctx.ctx_skip != SKIP_YES)
8476 {
8477 semsg(_(e_invalid_command_str), ea.cmd);
8478 goto erret;
8479 }
8480 // We don't check for a next command here.
8481 line = (char_u *)"";
8482 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008484 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008485 if (cctx.ctx_skip == SKIP_YES)
8486 {
8487 // We don't check for a next command here.
8488 line = (char_u *)"";
8489 }
8490 else
8491 {
8492 // Not recognized, execute with do_cmdline_cmd().
8493 ea.arg = p;
8494 line = compile_exec(line, &ea, &cctx);
8495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496 break;
8497 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008498nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 if (line == NULL)
8500 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008501 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008503 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008504 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008506 if (cctx.ctx_type_stack.ga_len < 0)
8507 {
8508 iemsg("Type stack underflow");
8509 goto erret;
8510 }
8511 }
8512
8513 if (cctx.ctx_scope != NULL)
8514 {
8515 if (cctx.ctx_scope->se_type == IF_SCOPE)
8516 emsg(_(e_endif));
8517 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8518 emsg(_(e_endwhile));
8519 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8520 emsg(_(e_endfor));
8521 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008522 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523 goto erret;
8524 }
8525
Bram Moolenaarefd88552020-06-18 20:50:10 +02008526 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008527 {
8528 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8529 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008530 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 goto erret;
8532 }
8533
8534 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008535 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536 }
8537
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008538 {
8539 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8540 + ufunc->uf_dfunc_idx;
8541 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008542 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008543#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008544 if (cctx.ctx_profiling)
8545 {
8546 dfunc->df_instr_prof = instr->ga_data;
8547 dfunc->df_instr_prof_count = instr->ga_len;
8548 }
8549 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008550#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008551 {
8552 dfunc->df_instr = instr->ga_data;
8553 dfunc->df_instr_count = instr->ga_len;
8554 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008555 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008556 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008557 if (cctx.ctx_outer_used)
8558 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008559 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008561
8562 ret = OK;
8563
8564erret:
8565 if (ret == FAIL)
8566 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008567 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8569 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008570
8571 for (idx = 0; idx < instr->ga_len; ++idx)
8572 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008573 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008574 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008575
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008576 // If using the last entry in the table and it was added above, we
8577 // might as well remove it.
8578 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008579 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008580 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008581 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008582 ufunc->uf_dfunc_idx = 0;
8583 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008584 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008585
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008586 while (cctx.ctx_scope != NULL)
8587 drop_scope(&cctx);
8588
Bram Moolenaar20431c92020-03-20 18:39:46 +01008589 // Don't execute this function body.
8590 ga_clear_strings(&ufunc->uf_lines);
8591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008592 if (errormsg != NULL)
8593 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008594 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008595 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008596 }
8597
8598 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008599 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008600 if (do_estack_push)
8601 estack_pop();
8602
Bram Moolenaar20431c92020-03-20 18:39:46 +01008603 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008604 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008606 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008607}
8608
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008609 void
8610set_function_type(ufunc_T *ufunc)
8611{
8612 int varargs = ufunc->uf_va_name != NULL;
8613 int argcount = ufunc->uf_args.ga_len;
8614
8615 // Create a type for the function, with the return type and any
8616 // argument types.
8617 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8618 // The type is included in "tt_args".
8619 if (argcount > 0 || varargs)
8620 {
8621 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8622 argcount, &ufunc->uf_type_list);
8623 // Add argument types to the function type.
8624 if (func_type_add_arg_types(ufunc->uf_func_type,
8625 argcount + varargs,
8626 &ufunc->uf_type_list) == FAIL)
8627 return;
8628 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8629 ufunc->uf_func_type->tt_min_argcount =
8630 argcount - ufunc->uf_def_args.ga_len;
8631 if (ufunc->uf_arg_types == NULL)
8632 {
8633 int i;
8634
8635 // lambda does not have argument types.
8636 for (i = 0; i < argcount; ++i)
8637 ufunc->uf_func_type->tt_args[i] = &t_any;
8638 }
8639 else
8640 mch_memmove(ufunc->uf_func_type->tt_args,
8641 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8642 if (varargs)
8643 {
8644 ufunc->uf_func_type->tt_args[argcount] =
8645 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8646 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8647 }
8648 }
8649 else
8650 // No arguments, can use a predefined type.
8651 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8652 argcount, &ufunc->uf_type_list);
8653}
8654
8655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008656/*
8657 * Delete an instruction, free what it contains.
8658 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008659 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008660delete_instr(isn_T *isn)
8661{
8662 switch (isn->isn_type)
8663 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008664 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008665 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008666 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008667 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 case ISN_LOADENV:
8669 case ISN_LOADG:
8670 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008671 case ISN_LOADT:
8672 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008673 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008674 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008675 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008676 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008677 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008678 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008679 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008680 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008681 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008682 case ISN_STOREW:
8683 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008684 vim_free(isn->isn_arg.string);
8685 break;
8686
8687 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008688 case ISN_STORES:
8689 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008690 break;
8691
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008692 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008693 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008694 vim_free(isn->isn_arg.unlet.ul_name);
8695 break;
8696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008697 case ISN_STOREOPT:
8698 vim_free(isn->isn_arg.storeopt.so_name);
8699 break;
8700
8701 case ISN_PUSHBLOB: // push blob isn_arg.blob
8702 blob_unref(isn->isn_arg.blob);
8703 break;
8704
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008705 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008706#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008707 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008708#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008709 break;
8710
8711 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008712#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008713 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008714#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008715 break;
8716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008717 case ISN_UCALL:
8718 vim_free(isn->isn_arg.ufunc.cuf_name);
8719 break;
8720
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008721 case ISN_FUNCREF:
8722 {
8723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8724 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008725 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008726
Bram Moolenaar077a4232020-12-22 18:33:27 +01008727 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8728 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008729 }
8730 break;
8731
8732 case ISN_DCALL:
8733 {
8734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8735 + isn->isn_arg.dfunc.cdf_idx;
8736
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008737 if (dfunc->df_ufunc != NULL
8738 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008739 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008740 }
8741 break;
8742
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008743 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008744 {
8745 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8746 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8747
8748 if (ufunc != NULL)
8749 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008750 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008751 func_ptr_unref(ufunc);
8752 }
8753
8754 vim_free(lambda);
8755 vim_free(isn->isn_arg.newfunc.nf_global);
8756 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008757 break;
8758
Bram Moolenaar5e654232020-09-16 15:22:00 +02008759 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008760 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008761 free_type(isn->isn_arg.type.ct_type);
8762 break;
8763
Bram Moolenaar02194d22020-10-24 23:08:38 +02008764 case ISN_CMDMOD:
8765 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8766 ->cmod_filter_regmatch.regprog);
8767 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8768 break;
8769
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008770 case ISN_LOADSCRIPT:
8771 case ISN_STORESCRIPT:
8772 vim_free(isn->isn_arg.script.scriptref);
8773 break;
8774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008775 case ISN_2BOOL:
8776 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008777 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008778 case ISN_ADDBLOB:
8779 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008780 case ISN_ANYINDEX:
8781 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008782 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008783 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008784 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008785 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008786 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008787 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008788 case ISN_COMPAREANY:
8789 case ISN_COMPAREBLOB:
8790 case ISN_COMPAREBOOL:
8791 case ISN_COMPAREDICT:
8792 case ISN_COMPAREFLOAT:
8793 case ISN_COMPAREFUNC:
8794 case ISN_COMPARELIST:
8795 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008796 case ISN_COMPARESPECIAL:
8797 case ISN_COMPARESTRING:
8798 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008799 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008800 case ISN_DROP:
8801 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008802 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008803 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008805 case ISN_EXECCONCAT:
8806 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008807 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008808 case ISN_GETITEM:
8809 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008810 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008811 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008812 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008814 case ISN_LOADBDICT:
8815 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008816 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008818 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008820 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008821 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008822 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008823 case ISN_NEGATENR:
8824 case ISN_NEWDICT:
8825 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008826 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008827 case ISN_OPFLOAT:
8828 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008830 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008831 case ISN_PROF_END:
8832 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008833 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008834 case ISN_PUSHF:
8835 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008836 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008837 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008838 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008839 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008840 case ISN_SHUFFLE:
8841 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008842 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008843 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008844 case ISN_STORENR:
8845 case ISN_STOREOUTER:
8846 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008847 case ISN_STOREV:
8848 case ISN_STRINDEX:
8849 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008850 case ISN_THROW:
8851 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008852 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008853 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008854 // nothing allocated
8855 break;
8856 }
8857}
8858
8859/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008860 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008861 */
8862 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008863delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008864{
8865 int idx;
8866
8867 ga_clear(&dfunc->df_def_args_isn);
8868
8869 if (dfunc->df_instr != NULL)
8870 {
8871 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8872 delete_instr(dfunc->df_instr + idx);
8873 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008874 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008875 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01008876#ifdef FEAT_PROFILE
8877 if (dfunc->df_instr_prof != NULL)
8878 {
8879 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
8880 delete_instr(dfunc->df_instr_prof + idx);
8881 VIM_CLEAR(dfunc->df_instr_prof);
8882 dfunc->df_instr_prof = NULL;
8883 }
8884#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01008885
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008886 if (mark_deleted)
8887 dfunc->df_deleted = TRUE;
8888 if (dfunc->df_ufunc != NULL)
8889 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008890}
8891
8892/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008893 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008894 * function, unless another user function still uses it.
8895 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008896 */
8897 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008898unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008900 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008901 {
8902 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8903 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008905 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008906 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008907 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008908 ufunc->uf_dfunc_idx = 0;
8909 if (dfunc->df_ufunc == ufunc)
8910 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008911 }
8912}
8913
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008914/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008915 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008916 */
8917 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008918link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008919{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008920 if (ufunc->uf_dfunc_idx > 0)
8921 {
8922 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8923 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008924
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008925 ++dfunc->df_refcount;
8926 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008927}
8928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008929#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008930/*
8931 * Free all functions defined with ":def".
8932 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008933 void
8934free_def_functions(void)
8935{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008936 int idx;
8937
8938 for (idx = 0; idx < def_functions.ga_len; ++idx)
8939 {
8940 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8941
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008942 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008943 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008944 }
8945
8946 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008947}
8948#endif
8949
8950
8951#endif // FEAT_EVAL