blob: 3c24fdb06c4d33b3e4dda66d9f8f788733515ef4 [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 Moolenaar4ed124c2020-09-09 20:03:46 +0200896 if (expected == &t_bool && actual != &t_bool
897 && (actual->tt_flags & TTFLAG_BOOL_OK))
898 {
899 // Using "0", "1" or the result of an expression with "&&" or "||" as a
900 // boolean is OK but requires a conversion.
901 generate_2BOOL(cctx, FALSE);
902 return OK;
903 }
904
Bram Moolenaar351ead02021-01-16 16:07:01 +0100905 if (check_type(expected, actual, FALSE, arg_idx) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200906 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200907
908 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200909 // If it's a constant a runtime check makes no sense.
910 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200911 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100912 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200913 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200914 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200915
916 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100917 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200918 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200919}
920
921/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100922 * Check that the top of the type stack has a type that can be used as a
923 * condition. Give an error and return FAIL if not.
924 */
925 static int
926bool_on_stack(cctx_T *cctx)
927{
928 garray_T *stack = &cctx->ctx_type_stack;
929 type_T *type;
930
931 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
932 if (type == &t_bool)
933 return OK;
934
935 if (type == &t_any || type == &t_number)
936 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
937 // This requires a runtime type check.
938 return generate_COND2BOOL(cctx);
939
Bram Moolenaar351ead02021-01-16 16:07:01 +0100940 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100941}
942
943/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100944 * Generate an ISN_PUSHNR instruction.
945 */
946 static int
947generate_PUSHNR(cctx_T *cctx, varnumber_T number)
948{
949 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200950 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951
Bram Moolenaar080457c2020-03-03 21:53:32 +0100952 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
954 return FAIL;
955 isn->isn_arg.number = number;
956
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200957 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200958 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100959 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 return OK;
961}
962
963/*
964 * Generate an ISN_PUSHBOOL instruction.
965 */
966 static int
967generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
968{
969 isn_T *isn;
970
Bram Moolenaar080457c2020-03-03 21:53:32 +0100971 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
973 return FAIL;
974 isn->isn_arg.number = number;
975
976 return OK;
977}
978
979/*
980 * Generate an ISN_PUSHSPEC instruction.
981 */
982 static int
983generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
984{
985 isn_T *isn;
986
Bram Moolenaar080457c2020-03-03 21:53:32 +0100987 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
989 return FAIL;
990 isn->isn_arg.number = number;
991
992 return OK;
993}
994
995#ifdef FEAT_FLOAT
996/*
997 * Generate an ISN_PUSHF instruction.
998 */
999 static int
1000generate_PUSHF(cctx_T *cctx, float_T fnumber)
1001{
1002 isn_T *isn;
1003
Bram Moolenaar080457c2020-03-03 21:53:32 +01001004 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.fnumber = fnumber;
1008
1009 return OK;
1010}
1011#endif
1012
1013/*
1014 * Generate an ISN_PUSHS instruction.
1015 * Consumes "str".
1016 */
1017 static int
1018generate_PUSHS(cctx_T *cctx, char_u *str)
1019{
1020 isn_T *isn;
1021
Bram Moolenaar508b5612021-01-02 18:17:26 +01001022 if (cctx->ctx_skip == SKIP_YES)
1023 {
1024 vim_free(str);
1025 return OK;
1026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1028 return FAIL;
1029 isn->isn_arg.string = str;
1030
1031 return OK;
1032}
1033
1034/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001035 * Generate an ISN_PUSHCHANNEL instruction.
1036 * Consumes "channel".
1037 */
1038 static int
1039generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1040{
1041 isn_T *isn;
1042
Bram Moolenaar080457c2020-03-03 21:53:32 +01001043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001044 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1045 return FAIL;
1046 isn->isn_arg.channel = channel;
1047
1048 return OK;
1049}
1050
1051/*
1052 * Generate an ISN_PUSHJOB instruction.
1053 * Consumes "job".
1054 */
1055 static int
1056generate_PUSHJOB(cctx_T *cctx, job_T *job)
1057{
1058 isn_T *isn;
1059
Bram Moolenaar080457c2020-03-03 21:53:32 +01001060 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001061 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 return FAIL;
1063 isn->isn_arg.job = job;
1064
1065 return OK;
1066}
1067
1068/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 * Generate an ISN_PUSHBLOB instruction.
1070 * Consumes "blob".
1071 */
1072 static int
1073generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1074{
1075 isn_T *isn;
1076
Bram Moolenaar080457c2020-03-03 21:53:32 +01001077 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1079 return FAIL;
1080 isn->isn_arg.blob = blob;
1081
1082 return OK;
1083}
1084
1085/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001086 * Generate an ISN_PUSHFUNC instruction with name "name".
1087 * Consumes "name".
1088 */
1089 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001090generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001091{
1092 isn_T *isn;
1093
Bram Moolenaar080457c2020-03-03 21:53:32 +01001094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001095 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001096 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001097 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001103 * Generate an ISN_GETITEM instruction with "index".
1104 */
1105 static int
1106generate_GETITEM(cctx_T *cctx, int index)
1107{
1108 isn_T *isn;
1109 garray_T *stack = &cctx->ctx_type_stack;
1110 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1111 type_T *item_type = &t_any;
1112
1113 RETURN_OK_IF_SKIP(cctx);
1114
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001115 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001116 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001117 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001118 emsg(_(e_listreq));
1119 return FAIL;
1120 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001121 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001122 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1123 return FAIL;
1124 isn->isn_arg.number = index;
1125
1126 // add the item type to the type stack
1127 if (ga_grow(stack, 1) == FAIL)
1128 return FAIL;
1129 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1130 ++stack->ga_len;
1131 return OK;
1132}
1133
1134/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001135 * Generate an ISN_SLICE instruction with "count".
1136 */
1137 static int
1138generate_SLICE(cctx_T *cctx, int count)
1139{
1140 isn_T *isn;
1141
1142 RETURN_OK_IF_SKIP(cctx);
1143 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1144 return FAIL;
1145 isn->isn_arg.number = count;
1146 return OK;
1147}
1148
1149/*
1150 * Generate an ISN_CHECKLEN instruction with "min_len".
1151 */
1152 static int
1153generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1154{
1155 isn_T *isn;
1156
1157 RETURN_OK_IF_SKIP(cctx);
1158
1159 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1160 return FAIL;
1161 isn->isn_arg.checklen.cl_min_len = min_len;
1162 isn->isn_arg.checklen.cl_more_OK = more_OK;
1163
1164 return OK;
1165}
1166
1167/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 * Generate an ISN_STORE instruction.
1169 */
1170 static int
1171generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1177 return FAIL;
1178 if (name != NULL)
1179 isn->isn_arg.string = vim_strsave(name);
1180 else
1181 isn->isn_arg.number = idx;
1182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001187 * Generate an ISN_STOREOUTER instruction.
1188 */
1189 static int
1190generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1191{
1192 isn_T *isn;
1193
1194 RETURN_OK_IF_SKIP(cctx);
1195 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1196 return FAIL;
1197 isn->isn_arg.outer.outer_idx = idx;
1198 isn->isn_arg.outer.outer_depth = level;
1199
1200 return OK;
1201}
1202
1203/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1205 */
1206 static int
1207generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1208{
1209 isn_T *isn;
1210
Bram Moolenaar080457c2020-03-03 21:53:32 +01001211 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1213 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001214 isn->isn_arg.storenr.stnr_idx = idx;
1215 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216
1217 return OK;
1218}
1219
1220/*
1221 * Generate an ISN_STOREOPT instruction
1222 */
1223 static int
1224generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1225{
1226 isn_T *isn;
1227
Bram Moolenaar080457c2020-03-03 21:53:32 +01001228 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001229 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 return FAIL;
1231 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1232 isn->isn_arg.storeopt.so_flags = opt_flags;
1233
1234 return OK;
1235}
1236
1237/*
1238 * Generate an ISN_LOAD or similar instruction.
1239 */
1240 static int
1241generate_LOAD(
1242 cctx_T *cctx,
1243 isntype_T isn_type,
1244 int idx,
1245 char_u *name,
1246 type_T *type)
1247{
1248 isn_T *isn;
1249
Bram Moolenaar080457c2020-03-03 21:53:32 +01001250 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1252 return FAIL;
1253 if (name != NULL)
1254 isn->isn_arg.string = vim_strsave(name);
1255 else
1256 isn->isn_arg.number = idx;
1257
1258 return OK;
1259}
1260
1261/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001262 * Generate an ISN_LOADOUTER instruction
1263 */
1264 static int
1265generate_LOADOUTER(
1266 cctx_T *cctx,
1267 int idx,
1268 int nesting,
1269 type_T *type)
1270{
1271 isn_T *isn;
1272
1273 RETURN_OK_IF_SKIP(cctx);
1274 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1275 return FAIL;
1276 isn->isn_arg.outer.outer_idx = idx;
1277 isn->isn_arg.outer.outer_depth = nesting;
1278
1279 return OK;
1280}
1281
1282/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001283 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001284 */
1285 static int
1286generate_LOADV(
1287 cctx_T *cctx,
1288 char_u *name,
1289 int error)
1290{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001291 int di_flags;
1292 int vidx = find_vim_var(name, &di_flags);
1293 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001294
Bram Moolenaar080457c2020-03-03 21:53:32 +01001295 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296 if (vidx < 0)
1297 {
1298 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001299 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001300 return FAIL;
1301 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001302 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001303
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001304 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001305}
1306
1307/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001308 * Generate an ISN_UNLET instruction.
1309 */
1310 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001311generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001312{
1313 isn_T *isn;
1314
1315 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001316 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001317 return FAIL;
1318 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1319 isn->isn_arg.unlet.ul_forceit = forceit;
1320
1321 return OK;
1322}
1323
1324/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001325 * Generate an ISN_LOCKCONST instruction.
1326 */
1327 static int
1328generate_LOCKCONST(cctx_T *cctx)
1329{
1330 isn_T *isn;
1331
1332 RETURN_OK_IF_SKIP(cctx);
1333 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1334 return FAIL;
1335 return OK;
1336}
1337
1338/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 * Generate an ISN_LOADS instruction.
1340 */
1341 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001344 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001346 int sid,
1347 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348{
1349 isn_T *isn;
1350
Bram Moolenaar080457c2020-03-03 21:53:32 +01001351 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352 if (isn_type == ISN_LOADS)
1353 isn = generate_instr_type(cctx, isn_type, type);
1354 else
1355 isn = generate_instr_drop(cctx, isn_type, 1);
1356 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001358 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1359 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360
1361 return OK;
1362}
1363
1364/*
1365 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1366 */
1367 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001368generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 cctx_T *cctx,
1370 isntype_T isn_type,
1371 int sid,
1372 int idx,
1373 type_T *type)
1374{
1375 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001376 scriptref_T *sref;
1377 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378
Bram Moolenaar080457c2020-03-03 21:53:32 +01001379 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 if (isn_type == ISN_LOADSCRIPT)
1381 isn = generate_instr_type(cctx, isn_type, type);
1382 else
1383 isn = generate_instr_drop(cctx, isn_type, 1);
1384 if (isn == NULL)
1385 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001386
1387 // This requires three arguments, which doesn't fit in an instruction, thus
1388 // we need to allocate a struct for this.
1389 sref = ALLOC_ONE(scriptref_T);
1390 if (sref == NULL)
1391 return FAIL;
1392 isn->isn_arg.script.scriptref = sref;
1393 sref->sref_sid = sid;
1394 sref->sref_idx = idx;
1395 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001396 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 return OK;
1398}
1399
1400/*
1401 * Generate an ISN_NEWLIST instruction.
1402 */
1403 static int
1404generate_NEWLIST(cctx_T *cctx, int count)
1405{
1406 isn_T *isn;
1407 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 type_T *type;
1409 type_T *member;
1410
Bram Moolenaar080457c2020-03-03 21:53:32 +01001411 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1413 return FAIL;
1414 isn->isn_arg.number = count;
1415
Bram Moolenaar127542b2020-08-09 17:22:04 +02001416 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001417 if (count == 0)
1418 member = &t_void;
1419 else
1420 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001421 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1422 cctx->ctx_type_list);
1423 type = get_list_type(member, cctx->ctx_type_list);
1424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 // drop the value types
1426 stack->ga_len -= count;
1427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 // add the list type to the type stack
1429 if (ga_grow(stack, 1) == FAIL)
1430 return FAIL;
1431 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1432 ++stack->ga_len;
1433
1434 return OK;
1435}
1436
1437/*
1438 * Generate an ISN_NEWDICT instruction.
1439 */
1440 static int
1441generate_NEWDICT(cctx_T *cctx, int count)
1442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 type_T *type;
1446 type_T *member;
1447
Bram Moolenaar080457c2020-03-03 21:53:32 +01001448 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1450 return FAIL;
1451 isn->isn_arg.number = count;
1452
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001453 if (count == 0)
1454 member = &t_void;
1455 else
1456 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001457 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1458 cctx->ctx_type_list);
1459 type = get_dict_type(member, cctx->ctx_type_list);
1460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 // drop the key and value types
1462 stack->ga_len -= 2 * count;
1463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 // add the dict type to the type stack
1465 if (ga_grow(stack, 1) == FAIL)
1466 return FAIL;
1467 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1468 ++stack->ga_len;
1469
1470 return OK;
1471}
1472
1473/*
1474 * Generate an ISN_FUNCREF instruction.
1475 */
1476 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001477generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478{
1479 isn_T *isn;
1480 garray_T *stack = &cctx->ctx_type_stack;
1481
Bram Moolenaar080457c2020-03-03 21:53:32 +01001482 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1484 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001485 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001486 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487
Bram Moolenaarab360522021-01-10 14:02:28 +01001488 // if the referenced function is a closure, it may use items further up in
1489 // the nested context, including this one.
1490 if (ufunc->uf_flags & FC_CLOSURE)
1491 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 if (ga_grow(stack, 1) == FAIL)
1494 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001495 ((type_T **)stack->ga_data)[stack->ga_len] =
1496 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 ++stack->ga_len;
1498
1499 return OK;
1500}
1501
1502/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001503 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001504 * "lambda_name" and "func_name" must be in allocated memory and will be
1505 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001506 */
1507 static int
1508generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1509{
1510 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001511
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001512 if (cctx->ctx_skip == SKIP_YES)
1513 {
1514 vim_free(lambda_name);
1515 vim_free(func_name);
1516 return OK;
1517 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001518 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001519 {
1520 vim_free(lambda_name);
1521 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001522 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001523 }
1524 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001525 isn->isn_arg.newfunc.nf_global = func_name;
1526
1527 return OK;
1528}
1529
1530/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001531 * Generate an ISN_DEF instruction: list functions
1532 */
1533 static int
1534generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1535{
1536 isn_T *isn;
1537
1538 RETURN_OK_IF_SKIP(cctx);
1539 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1540 return FAIL;
1541 if (len > 0)
1542 {
1543 isn->isn_arg.string = vim_strnsave(name, len);
1544 if (isn->isn_arg.string == NULL)
1545 return FAIL;
1546 }
1547 return OK;
1548}
1549
1550/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 * Generate an ISN_JUMP instruction.
1552 */
1553 static int
1554generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1555{
1556 isn_T *isn;
1557 garray_T *stack = &cctx->ctx_type_stack;
1558
Bram Moolenaar080457c2020-03-03 21:53:32 +01001559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1561 return FAIL;
1562 isn->isn_arg.jump.jump_when = when;
1563 isn->isn_arg.jump.jump_where = where;
1564
1565 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1566 --stack->ga_len;
1567
1568 return OK;
1569}
1570
1571 static int
1572generate_FOR(cctx_T *cctx, int loop_idx)
1573{
1574 isn_T *isn;
1575 garray_T *stack = &cctx->ctx_type_stack;
1576
Bram Moolenaar080457c2020-03-03 21:53:32 +01001577 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1579 return FAIL;
1580 isn->isn_arg.forloop.for_idx = loop_idx;
1581
1582 if (ga_grow(stack, 1) == FAIL)
1583 return FAIL;
1584 // type doesn't matter, will be stored next
1585 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1586 ++stack->ga_len;
1587
1588 return OK;
1589}
1590
1591/*
1592 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001593 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 * Return FAIL if the number of arguments is wrong.
1595 */
1596 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001597generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598{
1599 isn_T *isn;
1600 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001601 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001602 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001603 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604
Bram Moolenaar080457c2020-03-03 21:53:32 +01001605 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001606 argoff = check_internal_func(func_idx, argcount);
1607 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 return FAIL;
1609
Bram Moolenaar389df252020-07-09 21:20:47 +02001610 if (method_call && argoff > 1)
1611 {
1612 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1613 return FAIL;
1614 isn->isn_arg.shuffle.shfl_item = argcount;
1615 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1616 }
1617
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001618 if (argcount > 0)
1619 {
1620 // Check the types of the arguments.
1621 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001622 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1623 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001624 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001625 if (internal_func_is_map(func_idx))
1626 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001627 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1630 return FAIL;
1631 isn->isn_arg.bfunc.cbf_idx = func_idx;
1632 isn->isn_arg.bfunc.cbf_argcount = argcount;
1633
Bram Moolenaar94738d82020-10-21 14:25:07 +02001634 // Drop the argument types and push the return type.
1635 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 if (ga_grow(stack, 1) == FAIL)
1637 return FAIL;
1638 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001639 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001640 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001642 if (maptype != NULL && maptype->tt_member != NULL
1643 && maptype->tt_member != &t_any)
1644 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001645 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 return OK;
1648}
1649
1650/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001651 * Generate an ISN_LISTAPPEND instruction. Works like add().
1652 * Argument count is already checked.
1653 */
1654 static int
1655generate_LISTAPPEND(cctx_T *cctx)
1656{
1657 garray_T *stack = &cctx->ctx_type_stack;
1658 type_T *list_type;
1659 type_T *item_type;
1660 type_T *expected;
1661
1662 // Caller already checked that list_type is a list.
1663 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1664 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1665 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001666 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001667 return FAIL;
1668
1669 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1670 return FAIL;
1671
1672 --stack->ga_len; // drop the argument
1673 return OK;
1674}
1675
1676/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001677 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1678 * Argument count is already checked.
1679 */
1680 static int
1681generate_BLOBAPPEND(cctx_T *cctx)
1682{
1683 garray_T *stack = &cctx->ctx_type_stack;
1684 type_T *item_type;
1685
1686 // Caller already checked that blob_type is a blob.
1687 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001688 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001689 return FAIL;
1690
1691 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1692 return FAIL;
1693
1694 --stack->ga_len; // drop the argument
1695 return OK;
1696}
1697
1698/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001699 * Return TRUE if "ufunc" should be compiled, taking into account whether
1700 * "profile" indicates profiling is to be done.
1701 */
1702 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001703func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001704{
1705 switch (ufunc->uf_def_status)
1706 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001707 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001708 case UF_TO_BE_COMPILED: return TRUE;
1709 case UF_COMPILED:
1710 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001711#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001712 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1713 + ufunc->uf_dfunc_idx;
1714
1715 return profile ? dfunc->df_instr_prof == NULL
1716 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001717#else
1718 break;
1719#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001720 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001721 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001722 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001723 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001724}
1725
1726/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 * Generate an ISN_DCALL or ISN_UCALL instruction.
1728 * Return FAIL if the number of arguments is wrong.
1729 */
1730 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001731generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732{
1733 isn_T *isn;
1734 garray_T *stack = &cctx->ctx_type_stack;
1735 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001736 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737
Bram Moolenaar080457c2020-03-03 21:53:32 +01001738 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if (argcount > regular_args && !has_varargs(ufunc))
1740 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001741 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 return FAIL;
1743 }
1744 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1745 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001746 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 return FAIL;
1748 }
1749
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001750 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001751 {
1752 int i;
1753
1754 for (i = 0; i < argcount; ++i)
1755 {
1756 type_T *expected;
1757 type_T *actual;
1758
1759 if (i < regular_args)
1760 {
1761 if (ufunc->uf_arg_types == NULL)
1762 continue;
1763 expected = ufunc->uf_arg_types[i];
1764 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001765 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1766 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001767 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001768 else
1769 expected = ufunc->uf_va_type->tt_member;
1770 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001771 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001772 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001773 {
1774 arg_type_mismatch(expected, actual, i + 1);
1775 return FAIL;
1776 }
1777 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001778 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001779 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001780 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001781 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001782 }
1783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001785 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001786 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001788 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 {
1790 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1791 isn->isn_arg.dfunc.cdf_argcount = argcount;
1792 }
1793 else
1794 {
1795 // A user function may be deleted and redefined later, can't use the
1796 // ufunc pointer, need to look it up again at runtime.
1797 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1798 isn->isn_arg.ufunc.cuf_argcount = argcount;
1799 }
1800
1801 stack->ga_len -= argcount; // drop the arguments
1802 if (ga_grow(stack, 1) == FAIL)
1803 return FAIL;
1804 // add return value
1805 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1806 ++stack->ga_len;
1807
1808 return OK;
1809}
1810
1811/*
1812 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1813 */
1814 static int
1815generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1816{
1817 isn_T *isn;
1818 garray_T *stack = &cctx->ctx_type_stack;
1819
Bram Moolenaar080457c2020-03-03 21:53:32 +01001820 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1822 return FAIL;
1823 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1824 isn->isn_arg.ufunc.cuf_argcount = argcount;
1825
1826 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001827 if (ga_grow(stack, 1) == FAIL)
1828 return FAIL;
1829 // add return value
1830 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1831 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832
1833 return OK;
1834}
1835
1836/*
1837 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001838 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 */
1840 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001841generate_PCALL(
1842 cctx_T *cctx,
1843 int argcount,
1844 char_u *name,
1845 type_T *type,
1846 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847{
1848 isn_T *isn;
1849 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001850 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851
Bram Moolenaar080457c2020-03-03 21:53:32 +01001852 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001853
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001854 if (type->tt_type == VAR_ANY)
1855 ret_type = &t_any;
1856 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001857 {
1858 if (type->tt_argcount != -1)
1859 {
1860 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1861
1862 if (argcount < type->tt_min_argcount - varargs)
1863 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001864 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001865 return FAIL;
1866 }
1867 if (!varargs && argcount > type->tt_argcount)
1868 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001869 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001870 return FAIL;
1871 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001872 if (type->tt_args != NULL)
1873 {
1874 int i;
1875
1876 for (i = 0; i < argcount; ++i)
1877 {
1878 int offset = -argcount + i - 1;
1879 type_T *actual = ((type_T **)stack->ga_data)[
1880 stack->ga_len + offset];
1881 type_T *expected;
1882
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001883 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001884 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001885 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001886 else
1887 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001888 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001889 cctx, TRUE, FALSE) == FAIL)
1890 {
1891 arg_type_mismatch(expected, actual, i + 1);
1892 return FAIL;
1893 }
1894 }
1895 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001896 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001897 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001898 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001899 else
1900 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001901 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001902 return FAIL;
1903 }
1904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1906 return FAIL;
1907 isn->isn_arg.pfunc.cpf_top = at_top;
1908 isn->isn_arg.pfunc.cpf_argcount = argcount;
1909
1910 stack->ga_len -= argcount; // drop the arguments
1911
1912 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001913 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001915 // If partial is above the arguments it must be cleared and replaced with
1916 // the return value.
1917 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1918 return FAIL;
1919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 return OK;
1921}
1922
1923/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001924 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 */
1926 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001927generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928{
1929 isn_T *isn;
1930 garray_T *stack = &cctx->ctx_type_stack;
1931 type_T *type;
1932
Bram Moolenaar080457c2020-03-03 21:53:32 +01001933 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001934 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001936 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001938 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001940 if (type->tt_type != VAR_DICT && type != &t_any)
1941 {
1942 emsg(_(e_dictreq));
1943 return FAIL;
1944 }
1945 // change dict type to dict member type
1946 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001947 {
1948 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1949 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951
1952 return OK;
1953}
1954
1955/*
1956 * Generate an ISN_ECHO instruction.
1957 */
1958 static int
1959generate_ECHO(cctx_T *cctx, int with_white, int count)
1960{
1961 isn_T *isn;
1962
Bram Moolenaar080457c2020-03-03 21:53:32 +01001963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1965 return FAIL;
1966 isn->isn_arg.echo.echo_with_white = with_white;
1967 isn->isn_arg.echo.echo_count = count;
1968
1969 return OK;
1970}
1971
Bram Moolenaarad39c092020-02-26 18:23:43 +01001972/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001973 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001974 */
1975 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001976generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001977{
1978 isn_T *isn;
1979
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001980 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001981 return FAIL;
1982 isn->isn_arg.number = count;
1983
1984 return OK;
1985}
1986
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001987/*
1988 * Generate an ISN_PUT instruction.
1989 */
1990 static int
1991generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1992{
1993 isn_T *isn;
1994
1995 RETURN_OK_IF_SKIP(cctx);
1996 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1997 return FAIL;
1998 isn->isn_arg.put.put_regname = regname;
1999 isn->isn_arg.put.put_lnum = lnum;
2000 return OK;
2001}
2002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 static int
2004generate_EXEC(cctx_T *cctx, char_u *line)
2005{
2006 isn_T *isn;
2007
Bram Moolenaar080457c2020-03-03 21:53:32 +01002008 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2010 return FAIL;
2011 isn->isn_arg.string = vim_strsave(line);
2012 return OK;
2013}
2014
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002015 static int
2016generate_EXECCONCAT(cctx_T *cctx, int count)
2017{
2018 isn_T *isn;
2019
2020 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2021 return FAIL;
2022 isn->isn_arg.number = count;
2023 return OK;
2024}
2025
Bram Moolenaar08597872020-12-10 19:43:40 +01002026/*
2027 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2028 */
2029 static int
2030generate_RANGE(cctx_T *cctx, char_u *range)
2031{
2032 isn_T *isn;
2033 garray_T *stack = &cctx->ctx_type_stack;
2034
2035 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2036 return FAIL;
2037 isn->isn_arg.string = range;
2038
2039 if (ga_grow(stack, 1) == FAIL)
2040 return FAIL;
2041 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2042 ++stack->ga_len;
2043 return OK;
2044}
2045
Bram Moolenaar792f7862020-11-23 08:31:18 +01002046 static int
2047generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2048{
2049 isn_T *isn;
2050
2051 RETURN_OK_IF_SKIP(cctx);
2052 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2053 return FAIL;
2054 isn->isn_arg.unpack.unp_count = var_count;
2055 isn->isn_arg.unpack.unp_semicolon = semicolon;
2056 return OK;
2057}
2058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002060 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002061 */
2062 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002063generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002064{
2065 isn_T *isn;
2066
Bram Moolenaar02194d22020-10-24 23:08:38 +02002067 if (cmod->cmod_flags != 0
2068 || cmod->cmod_split != 0
2069 || cmod->cmod_verbose != 0
2070 || cmod->cmod_tab != 0
2071 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002072 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002073 cctx->ctx_has_cmdmod = TRUE;
2074
2075 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002076 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002077 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2078 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2079 return FAIL;
2080 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002081 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002082 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002083 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002084
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002085 return OK;
2086}
2087
2088 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002089generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002090{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002091 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2092 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002093 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002094 return OK;
2095}
2096
Bram Moolenaarf002a412021-01-24 13:34:18 +01002097#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002098 static void
2099may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2100{
2101 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002102 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002103}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002104#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002105
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002106/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002108 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002110 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002111reserve_local(
2112 cctx_T *cctx,
2113 char_u *name,
2114 size_t len,
2115 int isConst,
2116 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 lvar_T *lvar;
2119
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002120 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002122 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002123 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 }
2125
2126 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002127 return NULL;
2128 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002129 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002131 // Every local variable uses the next entry on the stack. We could re-use
2132 // the last ones when leaving a scope, but then variables used in a closure
2133 // might get overwritten. To keep things simple do not re-use stack
2134 // entries. This is less efficient, but memory is cheap these days.
2135 lvar->lv_idx = cctx->ctx_locals_count++;
2136
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002137 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 lvar->lv_const = isConst;
2139 lvar->lv_type = type;
2140
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002141 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142}
2143
2144/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002145 * Remove local variables above "new_top".
2146 */
2147 static void
2148unwind_locals(cctx_T *cctx, int new_top)
2149{
2150 if (cctx->ctx_locals.ga_len > new_top)
2151 {
2152 int idx;
2153 lvar_T *lvar;
2154
2155 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2156 {
2157 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2158 vim_free(lvar->lv_name);
2159 }
2160 }
2161 cctx->ctx_locals.ga_len = new_top;
2162}
2163
2164/*
2165 * Free all local variables.
2166 */
2167 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002168free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002169{
2170 unwind_locals(cctx, 0);
2171 ga_clear(&cctx->ctx_locals);
2172}
2173
2174/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002175 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2176 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2177 * error if the variable was defined with :const.
2178 */
2179 static int
2180check_item_writable(svar_T *sv, int check_writable, char_u *name)
2181{
2182 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2183 || (check_writable == ASSIGN_FINAL
2184 && sv->sv_const == ASSIGN_CONST))
2185 {
2186 semsg(_(e_readonlyvar), name);
2187 return FAIL;
2188 }
2189 return OK;
2190}
2191
2192/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002194 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 * Returns the index in "sn_var_vals" if found.
2196 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002197 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 */
2199 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002200get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201{
2202 hashtab_T *ht;
2203 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002204 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002205 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 int idx;
2207
Bram Moolenaare3d46852020-08-29 13:39:17 +02002208 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002210 if (sid == current_sctx.sc_sid)
2211 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002212 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002213
2214 if (sav == NULL)
2215 return -2;
2216 idx = sav->sav_var_vals_idx;
2217 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002218 if (check_item_writable(sv, check_writable, name) == FAIL)
2219 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002220 return idx;
2221 }
2222
2223 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 ht = &SCRIPT_VARS(sid);
2225 di = find_var_in_ht(ht, 0, name, TRUE);
2226 if (di == NULL)
2227 return -2;
2228
2229 // Now find the svar_T index in sn_var_vals.
2230 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2231 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002232 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 if (sv->sv_tv == &di->di_tv)
2234 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002235 if (check_item_writable(sv, check_writable, name) == FAIL)
2236 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 return idx;
2238 }
2239 }
2240 return -1;
2241}
2242
2243/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002244 * Find "name" in imported items of the current script or in "cctx" if not
2245 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 */
2247 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002248find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 int idx;
2251
Bram Moolenaare3d46852020-08-29 13:39:17 +02002252 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002253 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 if (cctx != NULL)
2255 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2256 {
2257 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2258 + idx;
2259
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002260 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2261 : STRLEN(import->imp_name) == len
2262 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 return import;
2264 }
2265
Bram Moolenaarefa94442020-08-08 22:16:00 +02002266 return find_imported_in_script(name, len, current_sctx.sc_sid);
2267}
2268
2269 imported_T *
2270find_imported_in_script(char_u *name, size_t len, int sid)
2271{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002272 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002273 int idx;
2274
Bram Moolenaare3d46852020-08-29 13:39:17 +02002275 if (!SCRIPT_ID_VALID(sid))
2276 return NULL;
2277 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2279 {
2280 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002282 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2283 : STRLEN(import->imp_name) == len
2284 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 return import;
2286 }
2287 return NULL;
2288}
2289
2290/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002291 * Free all imported variables.
2292 */
2293 static void
2294free_imported(cctx_T *cctx)
2295{
2296 int idx;
2297
2298 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2299 {
2300 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2301
2302 vim_free(import->imp_name);
2303 }
2304 ga_clear(&cctx->ctx_imports);
2305}
2306
2307/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002308 * Return a pointer to the next line that isn't empty or only contains a
2309 * comment. Skips over white space.
2310 * Returns NULL if there is none.
2311 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002312 char_u *
2313peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002314{
2315 int lnum = cctx->ctx_lnum;
2316
2317 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2318 {
2319 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002320 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002321
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002322 // ignore NULLs inserted for continuation lines
2323 if (line != NULL)
2324 {
2325 p = skipwhite(line);
2326 if (*p != NUL && !vim9_comment_start(p))
2327 return p;
2328 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002329 }
2330 return NULL;
2331}
2332
2333/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002334 * Called when checking for a following operator at "arg". When the rest of
2335 * the line is empty or only a comment, peek the next line. If there is a next
2336 * line return a pointer to it and set "nextp".
2337 * Otherwise skip over white space.
2338 */
2339 static char_u *
2340may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2341{
2342 char_u *p = skipwhite(arg);
2343
2344 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002345 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002346 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002347 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002348 if (*nextp != NULL)
2349 return *nextp;
2350 }
2351 return p;
2352}
2353
2354/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002355 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002356 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002357 * Returns NULL when at the end.
2358 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002359 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002360next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002361{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002362 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002363
2364 do
2365 {
2366 ++cctx->ctx_lnum;
2367 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002368 {
2369 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002370 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002371 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002372 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002373 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002374 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002375 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002376 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002377 return line;
2378}
2379
2380/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002381 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002382 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002383 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002384 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2385 */
2386 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002387may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002388{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002389 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002390 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002391 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002392 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002393
2394 if (next == NULL)
2395 return FAIL;
2396 *arg = skipwhite(next);
2397 }
2398 return OK;
2399}
2400
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002401/*
2402 * Idem, and give an error when failed.
2403 */
2404 static int
2405may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2406{
2407 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2408 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002409 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002410 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002411 return FAIL;
2412 }
2413 return OK;
2414}
2415
2416
Bram Moolenaara5565e42020-05-09 15:44:01 +02002417// Structure passed between the compile_expr* functions to keep track of
2418// constants that have been parsed but for which no code was produced yet. If
2419// possible expressions on these constants are applied at compile time. If
2420// that is not possible, the code to push the constants needs to be generated
2421// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002422// Using 50 should be more than enough of 5 levels of ().
2423#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002424typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002425 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002426 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002427 int pp_is_const; // all generated code was constants, used for a
2428 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002429} ppconst_T;
2430
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002431static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002432static int compile_expr0(char_u **arg, cctx_T *cctx);
2433static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2434
Bram Moolenaara5565e42020-05-09 15:44:01 +02002435/*
2436 * Generate a PUSH instruction for "tv".
2437 * "tv" will be consumed or cleared.
2438 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2439 */
2440 static int
2441generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2442{
2443 if (tv != NULL)
2444 {
2445 switch (tv->v_type)
2446 {
2447 case VAR_UNKNOWN:
2448 break;
2449 case VAR_BOOL:
2450 generate_PUSHBOOL(cctx, tv->vval.v_number);
2451 break;
2452 case VAR_SPECIAL:
2453 generate_PUSHSPEC(cctx, tv->vval.v_number);
2454 break;
2455 case VAR_NUMBER:
2456 generate_PUSHNR(cctx, tv->vval.v_number);
2457 break;
2458#ifdef FEAT_FLOAT
2459 case VAR_FLOAT:
2460 generate_PUSHF(cctx, tv->vval.v_float);
2461 break;
2462#endif
2463 case VAR_BLOB:
2464 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2465 tv->vval.v_blob = NULL;
2466 break;
2467 case VAR_STRING:
2468 generate_PUSHS(cctx, tv->vval.v_string);
2469 tv->vval.v_string = NULL;
2470 break;
2471 default:
2472 iemsg("constant type not supported");
2473 clear_tv(tv);
2474 return FAIL;
2475 }
2476 tv->v_type = VAR_UNKNOWN;
2477 }
2478 return OK;
2479}
2480
2481/*
2482 * Generate code for any ppconst entries.
2483 */
2484 static int
2485generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2486{
2487 int i;
2488 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002489 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002490
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002491 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002492 for (i = 0; i < ppconst->pp_used; ++i)
2493 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2494 ret = FAIL;
2495 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002496 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497 return ret;
2498}
2499
2500/*
2501 * Clear ppconst constants. Used when failing.
2502 */
2503 static void
2504clear_ppconst(ppconst_T *ppconst)
2505{
2506 int i;
2507
2508 for (i = 0; i < ppconst->pp_used; ++i)
2509 clear_tv(&ppconst->pp_tv[i]);
2510 ppconst->pp_used = 0;
2511}
2512
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002513/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002514 * Generate an instruction to load script-local variable "name", without the
2515 * leading "s:".
2516 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 */
2518 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002519compile_load_scriptvar(
2520 cctx_T *cctx,
2521 char_u *name, // variable NUL terminated
2522 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002523 char_u **end, // end of variable
2524 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002526 scriptitem_T *si;
2527 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 imported_T *import;
2529
Bram Moolenaare3d46852020-08-29 13:39:17 +02002530 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2531 return FAIL;
2532 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002533 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002534 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002536 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002537 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2538 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 }
2540 if (idx >= 0)
2541 {
2542 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2543
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002544 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 current_sctx.sc_sid, idx, sv->sv_type);
2546 return OK;
2547 }
2548
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002549 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 if (import != NULL)
2551 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002552 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002553 {
2554 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002555 char_u *exp_name;
2556 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002557 ufunc_T *ufunc;
2558 type_T *type;
2559
2560 // Used "import * as Name", need to lookup the member.
2561 if (*p != '.')
2562 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002563 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002564 return FAIL;
2565 }
2566 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002567 if (VIM_ISWHITE(*p))
2568 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002569 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002570 return FAIL;
2571 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002572
Bram Moolenaar1c991142020-07-04 13:15:31 +02002573 // isolate one name
2574 exp_name = p;
2575 while (eval_isnamec(*p))
2576 ++p;
2577 cc = *p;
2578 *p = NUL;
2579
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002580 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002581 *p = cc;
2582 p = skipwhite(p);
2583
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002584 // TODO: what if it is a function?
2585 if (idx < 0)
2586 return FAIL;
2587 *end = p;
2588
2589 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2590 import->imp_sid,
2591 idx,
2592 type);
2593 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002594 else if (import->imp_funcname != NULL)
2595 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002596 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002597 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2598 import->imp_sid,
2599 import->imp_var_vals_idx,
2600 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 return OK;
2602 }
2603
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002604 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002605 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 return FAIL;
2607}
2608
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002609 static int
2610generate_funcref(cctx_T *cctx, char_u *name)
2611{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002612 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002613
2614 if (ufunc == NULL)
2615 return FAIL;
2616
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002617 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002618 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2619 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002620 == FAIL)
2621 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002622 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002623}
2624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625/*
2626 * Compile a variable name into a load instruction.
2627 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002628 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 * When "error" is FALSE do not give an error when not found.
2630 */
2631 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002632compile_load(
2633 char_u **arg,
2634 char_u *end_arg,
2635 cctx_T *cctx,
2636 int is_expr,
2637 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638{
2639 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002640 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002641 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002643 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644
2645 if (*(*arg + 1) == ':')
2646 {
2647 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002648 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002649 {
2650 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002652 switch (**arg)
2653 {
2654 case 'g': isn_type = ISN_LOADGDICT; break;
2655 case 'w': isn_type = ISN_LOADWDICT; break;
2656 case 't': isn_type = ISN_LOADTDICT; break;
2657 case 'b': isn_type = ISN_LOADBDICT; break;
2658 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002659 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002660 goto theend;
2661 }
2662 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2663 goto theend;
2664 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 else
2667 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002668 isntype_T isn_type = ISN_DROP;
2669
2670 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2671 if (name == NULL)
2672 return FAIL;
2673
2674 switch (**arg)
2675 {
2676 case 'v': res = generate_LOADV(cctx, name, error);
2677 break;
2678 case 's': res = compile_load_scriptvar(cctx, name,
2679 NULL, NULL, error);
2680 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002681 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2682 isn_type = ISN_LOADG;
2683 else
2684 {
2685 isn_type = ISN_LOADAUTO;
2686 vim_free(name);
2687 name = vim_strnsave(*arg, end - *arg);
2688 if (name == NULL)
2689 return FAIL;
2690 }
2691 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002692 case 'w': isn_type = ISN_LOADW; break;
2693 case 't': isn_type = ISN_LOADT; break;
2694 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002695 default: // cannot happen, just in case
2696 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002697 goto theend;
2698 }
2699 if (isn_type != ISN_DROP)
2700 {
2701 // Global, Buffer-local, Window-local and Tabpage-local
2702 // variables can be defined later, thus we don't check if it
2703 // exists, give error at runtime.
2704 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 }
2707 }
2708 else
2709 {
2710 size_t len = end - *arg;
2711 int idx;
2712 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002713 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714
2715 name = vim_strnsave(*arg, end - *arg);
2716 if (name == NULL)
2717 return FAIL;
2718
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002719 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002721 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002722 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 }
2724 else
2725 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002726 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002727
Bram Moolenaar709664c2020-12-12 14:33:41 +01002728 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002730 type = lvar.lv_type;
2731 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002732 if (lvar.lv_from_outer != 0)
2733 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002734 else
2735 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 }
2737 else
2738 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002739 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002740 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002741 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002742 || find_imported(name, 0, cctx) != NULL)
2743 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002744
Bram Moolenaar0f769812020-09-12 18:32:34 +02002745 // When evaluating an expression and the name starts with an
2746 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002747 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002748 if (res == FAIL && is_expr
2749 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002750 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
2752 }
2753 if (gen_load)
2754 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002755 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002756 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002757 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002758 cctx->ctx_outer_used = TRUE;
2759 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 }
2761
2762 *arg = end;
2763
2764theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002765 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002766 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 vim_free(name);
2768 return res;
2769}
2770
2771/*
2772 * Compile the argument expressions.
2773 * "arg" points to just after the "(" and is advanced to after the ")"
2774 */
2775 static int
2776compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2777{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002778 char_u *p = *arg;
2779 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002780 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781
Bram Moolenaare6085c52020-04-12 20:19:16 +02002782 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002784 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2785 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002786 if (*p == ')')
2787 {
2788 *arg = p + 1;
2789 return OK;
2790 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002791 if (must_end)
2792 {
2793 semsg(_(e_missing_comma_before_argument_str), p);
2794 return FAIL;
2795 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002796
Bram Moolenaara5565e42020-05-09 15:44:01 +02002797 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 return FAIL;
2799 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002800
2801 if (*p != ',' && *skipwhite(p) == ',')
2802 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002803 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002804 p = skipwhite(p);
2805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002806 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002807 {
2808 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002809 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002810 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002811 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002812 else
2813 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002814 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002815 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002817failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002818 emsg(_(e_missing_close));
2819 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820}
2821
2822/*
2823 * Compile a function call: name(arg1, arg2)
2824 * "arg" points to "name", "arg + varlen" to the "(".
2825 * "argcount_init" is 1 for "value->method()"
2826 * Instructions:
2827 * EVAL arg1
2828 * EVAL arg2
2829 * BCALL / DCALL / UCALL
2830 */
2831 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002832compile_call(
2833 char_u **arg,
2834 size_t varlen,
2835 cctx_T *cctx,
2836 ppconst_T *ppconst,
2837 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838{
2839 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002840 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 int argcount = argcount_init;
2842 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002843 char_u fname_buf[FLEN_FIXED + 1];
2844 char_u *tofree = NULL;
2845 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002846 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002847 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002848 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849
Bram Moolenaara5565e42020-05-09 15:44:01 +02002850 // we can evaluate "has('name')" at compile time
2851 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2852 {
2853 char_u *s = skipwhite(*arg + varlen + 1);
2854 typval_T argvars[2];
2855
2856 argvars[0].v_type = VAR_UNKNOWN;
2857 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002858 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002859 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002860 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002861 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002862 if (*s == ')' && argvars[0].v_type == VAR_STRING
2863 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002864 {
2865 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2866
2867 *arg = s + 1;
2868 argvars[1].v_type = VAR_UNKNOWN;
2869 tv->v_type = VAR_NUMBER;
2870 tv->vval.v_number = 0;
2871 f_has(argvars, tv);
2872 clear_tv(&argvars[0]);
2873 ++ppconst->pp_used;
2874 return OK;
2875 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002876 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002877 }
2878
2879 if (generate_ppconst(cctx, ppconst) == FAIL)
2880 return FAIL;
2881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (varlen >= sizeof(namebuf))
2883 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002884 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 return FAIL;
2886 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002887 vim_strncpy(namebuf, *arg, varlen);
2888 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
2890 *arg = skipwhite(*arg + varlen + 1);
2891 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002892 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893
Bram Moolenaar03290b82020-12-19 16:30:44 +01002894 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002895 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 {
2897 int idx;
2898
2899 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002900 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002902 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002903 if (STRCMP(name, "flatten") == 0)
2904 {
2905 emsg(_(e_cannot_use_flatten_in_vim9_script));
2906 goto theend;
2907 }
2908
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002909 if (STRCMP(name, "add") == 0 && argcount == 2)
2910 {
2911 garray_T *stack = &cctx->ctx_type_stack;
2912 type_T *type = ((type_T **)stack->ga_data)[
2913 stack->ga_len - 2];
2914
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002915 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002916 if (type->tt_type == VAR_LIST)
2917 {
2918 // inline "add(list, item)" so that the type can be checked
2919 res = generate_LISTAPPEND(cctx);
2920 idx = -1;
2921 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002922 else if (type->tt_type == VAR_BLOB)
2923 {
2924 // inline "add(blob, nr)" so that the type can be checked
2925 res = generate_BLOBAPPEND(cctx);
2926 idx = -1;
2927 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002928 }
2929
2930 if (idx >= 0)
2931 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2932 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002933 else
2934 semsg(_(e_unknownfunc), namebuf);
2935 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 }
2937
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002938 // An argument or local variable can be a function reference, this
2939 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002940 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002941 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002942 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002943 // If we can find the function by name generate the right call.
2944 // Skip global functions here, a local funcref takes precedence.
2945 ufunc = find_func(name, FALSE, cctx);
2946 if (ufunc != NULL && !func_is_global(ufunc))
2947 {
2948 res = generate_CALL(cctx, ufunc, argcount);
2949 goto theend;
2950 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952
2953 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002954 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002955 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002957 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002958 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002959 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002960 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002961 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002962
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002963 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002964 goto theend;
2965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966
Bram Moolenaar0f769812020-09-12 18:32:34 +02002967 // If we can find a global function by name generate the right call.
2968 if (ufunc != NULL)
2969 {
2970 res = generate_CALL(cctx, ufunc, argcount);
2971 goto theend;
2972 }
2973
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002974 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002975 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002976 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002977 res = generate_UCALL(cctx, name, argcount);
2978 else
2979 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002980
2981theend:
2982 vim_free(tofree);
2983 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984}
2985
2986// like NAMESPACE_CHAR but with 'a' and 'l'.
2987#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2988
2989/*
2990 * Find the end of a variable or function name. Unlike find_name_end() this
2991 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002992 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 * Return a pointer to just after the name. Equal to "arg" if there is no
2994 * valid name.
2995 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002996 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002997to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998{
2999 char_u *p;
3000
3001 // Quick check for valid starting character.
3002 if (!eval_isnamec1(*arg))
3003 return arg;
3004
3005 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3006 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3007 // and can be used in slice "[n:]".
3008 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003009 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3011 break;
3012 return p;
3013}
3014
3015/*
3016 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003017 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 */
3019 char_u *
3020to_name_const_end(char_u *arg)
3021{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003022 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 typval_T rettv;
3024
3025 if (p == arg && *arg == '[')
3026 {
3027
3028 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003029 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 p = arg;
3031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 return p;
3033}
3034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035/*
3036 * parse a list: [expr, expr]
3037 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003038 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 */
3040 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003041compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042{
3043 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003044 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003046 int is_const;
3047 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003049 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003051 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003052 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003053 semsg(_(e_list_end), *arg);
3054 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003055 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003056 if (*p == ',')
3057 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003058 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003059 return FAIL;
3060 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003061 if (*p == ']')
3062 {
3063 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003064 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003065 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003066 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003067 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003068 if (!is_const)
3069 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 ++count;
3071 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003072 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003074 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3075 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003076 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003077 return FAIL;
3078 }
3079 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003080 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 p = skipwhite(p);
3082 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003083 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003085 ppconst->pp_is_const = is_all_const;
3086 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087}
3088
3089/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003090 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003092 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 */
3094 static int
3095compile_lambda(char_u **arg, cctx_T *cctx)
3096{
Bram Moolenaare462f522020-12-27 14:43:30 +01003097 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 typval_T rettv;
3099 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003100 evalarg_T evalarg;
3101
3102 CLEAR_FIELD(evalarg);
3103 evalarg.eval_flags = EVAL_EVALUATE;
3104 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105
3106 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003107 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3108 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003109 {
3110 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003111 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003112 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003113
Bram Moolenaar65c44152020-12-24 15:14:01 +01003114 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003116 ++ufunc->uf_refcount;
3117 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118
Bram Moolenaar65c44152020-12-24 15:14:01 +01003119 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003120 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003122 clear_evalarg(&evalarg, NULL);
3123
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003124 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003125 {
3126 // The return type will now be known.
3127 set_function_type(ufunc);
3128
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003129 // The function reference count will be 1. When the ISN_FUNCREF
3130 // instruction is deleted the reference count is decremented and the
3131 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003132 return generate_FUNCREF(cctx, ufunc);
3133 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003134
3135 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 return FAIL;
3137}
3138
3139/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003140 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003142 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 */
3144 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003145compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146{
3147 garray_T *instr = &cctx->ctx_instr;
3148 int count = 0;
3149 dict_T *d = dict_alloc();
3150 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003151 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003152 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003153 int is_const;
3154 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 if (d == NULL)
3157 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003158 if (generate_ppconst(cctx, ppconst) == FAIL)
3159 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003162 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
Bram Moolenaar23c55272020-06-21 16:58:13 +02003164 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 *arg = NULL;
3167 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 }
3169
3170 if (**arg == '}')
3171 break;
3172
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003173 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003175 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003177 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003178 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003179 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003182 if (isn->isn_type == ISN_PUSHNR)
3183 {
3184 char buf[NUMBUFLEN];
3185
3186 // Convert to string at compile time.
3187 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3188 isn->isn_type = ISN_PUSHS;
3189 isn->isn_arg.string = vim_strsave((char_u *)buf);
3190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 if (isn->isn_type == ISN_PUSHS)
3192 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003193 else if (may_generate_2STRING(-1, cctx) == FAIL)
3194 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003195 *arg = skipwhite(*arg);
3196 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003197 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003198 emsg(_(e_missing_matching_bracket_after_dict_key));
3199 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003200 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003201 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003203 else
3204 {
3205 // {"name": value},
3206 // {'name': value},
3207 // {name: value} use "name" as a literal key
3208 key = get_literal_key(arg);
3209 if (key == NULL)
3210 return FAIL;
3211 if (generate_PUSHS(cctx, key) == FAIL)
3212 return FAIL;
3213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214
3215 // Check for duplicate keys, if using string keys.
3216 if (key != NULL)
3217 {
3218 item = dict_find(d, key, -1);
3219 if (item != NULL)
3220 {
3221 semsg(_(e_duplicate_key), key);
3222 goto failret;
3223 }
3224 item = dictitem_alloc(key);
3225 if (item != NULL)
3226 {
3227 item->di_tv.v_type = VAR_UNKNOWN;
3228 item->di_tv.v_lock = 0;
3229 if (dict_add(d, item) == FAIL)
3230 dictitem_free(item);
3231 }
3232 }
3233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 if (**arg != ':')
3235 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003236 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003237 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003238 else
3239 semsg(_(e_missing_dict_colon), *arg);
3240 return FAIL;
3241 }
3242 whitep = *arg + 1;
3243 if (!IS_WHITE_OR_NUL(*whitep))
3244 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003245 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 }
3248
Bram Moolenaar23c55272020-06-21 16:58:13 +02003249 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003250 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003251 *arg = NULL;
3252 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 }
3254
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003255 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003257 if (!is_const)
3258 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 ++count;
3260
Bram Moolenaar2c330432020-04-13 14:41:35 +02003261 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003262 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003263 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003264 *arg = NULL;
3265 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 if (**arg == '}')
3268 break;
3269 if (**arg != ',')
3270 {
3271 semsg(_(e_missing_dict_comma), *arg);
3272 goto failret;
3273 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003274 if (IS_WHITE_OR_NUL(*whitep))
3275 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003276 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003277 return FAIL;
3278 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003279 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003280 if (!IS_WHITE_OR_NUL(*whitep))
3281 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003282 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003283 return FAIL;
3284 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003285 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 }
3287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 *arg = *arg + 1;
3289
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003291 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003292 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003293 *arg += STRLEN(*arg);
3294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003296 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 return generate_NEWDICT(cctx, count);
3298
3299failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003300 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003301 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003302 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003303 *arg = (char_u *)"";
3304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 dict_unref(d);
3306 return FAIL;
3307}
3308
3309/*
3310 * Compile "&option".
3311 */
3312 static int
3313compile_get_option(char_u **arg, cctx_T *cctx)
3314{
3315 typval_T rettv;
3316 char_u *start = *arg;
3317 int ret;
3318
3319 // parse the option and get the current value to get the type.
3320 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003321 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003322 if (ret == OK)
3323 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003324 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003325 char_u *name = vim_strnsave(start, *arg - start);
3326 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3327 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328
3329 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3330 vim_free(name);
3331 }
3332 clear_tv(&rettv);
3333
3334 return ret;
3335}
3336
3337/*
3338 * Compile "$VAR".
3339 */
3340 static int
3341compile_get_env(char_u **arg, cctx_T *cctx)
3342{
3343 char_u *start = *arg;
3344 int len;
3345 int ret;
3346 char_u *name;
3347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 ++*arg;
3349 len = get_env_len(arg);
3350 if (len == 0)
3351 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003352 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 return FAIL;
3354 }
3355
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003356 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 name = vim_strnsave(start, len + 1);
3358 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3359 vim_free(name);
3360 return ret;
3361}
3362
3363/*
3364 * Compile "@r".
3365 */
3366 static int
3367compile_get_register(char_u **arg, cctx_T *cctx)
3368{
3369 int ret;
3370
3371 ++*arg;
3372 if (**arg == NUL)
3373 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003374 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 return FAIL;
3376 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003377 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 {
3379 emsg_invreg(**arg);
3380 return FAIL;
3381 }
3382 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3383 ++*arg;
3384 return ret;
3385}
3386
3387/*
3388 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003389 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 */
3391 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003392apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003394 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395
3396 // this works from end to start
3397 while (p > start)
3398 {
3399 --p;
3400 if (*p == '-' || *p == '+')
3401 {
3402 // only '-' has an effect, for '+' we only check the type
3403#ifdef FEAT_FLOAT
3404 if (rettv->v_type == VAR_FLOAT)
3405 {
3406 if (*p == '-')
3407 rettv->vval.v_float = -rettv->vval.v_float;
3408 }
3409 else
3410#endif
3411 {
3412 varnumber_T val;
3413 int error = FALSE;
3414
3415 // tv_get_number_chk() accepts a string, but we don't want that
3416 // here
3417 if (check_not_string(rettv) == FAIL)
3418 return FAIL;
3419 val = tv_get_number_chk(rettv, &error);
3420 clear_tv(rettv);
3421 if (error)
3422 return FAIL;
3423 if (*p == '-')
3424 val = -val;
3425 rettv->v_type = VAR_NUMBER;
3426 rettv->vval.v_number = val;
3427 }
3428 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003429 else if (numeric_only)
3430 {
3431 ++p;
3432 break;
3433 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003434 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 {
3436 int v = tv2bool(rettv);
3437
3438 // '!' is permissive in the type.
3439 clear_tv(rettv);
3440 rettv->v_type = VAR_BOOL;
3441 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3442 }
3443 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003444 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 return OK;
3446}
3447
3448/*
3449 * Recognize v: variables that are constants and set "rettv".
3450 */
3451 static void
3452get_vim_constant(char_u **arg, typval_T *rettv)
3453{
3454 if (STRNCMP(*arg, "v:true", 6) == 0)
3455 {
3456 rettv->v_type = VAR_BOOL;
3457 rettv->vval.v_number = VVAL_TRUE;
3458 *arg += 6;
3459 }
3460 else if (STRNCMP(*arg, "v:false", 7) == 0)
3461 {
3462 rettv->v_type = VAR_BOOL;
3463 rettv->vval.v_number = VVAL_FALSE;
3464 *arg += 7;
3465 }
3466 else if (STRNCMP(*arg, "v:null", 6) == 0)
3467 {
3468 rettv->v_type = VAR_SPECIAL;
3469 rettv->vval.v_number = VVAL_NULL;
3470 *arg += 6;
3471 }
3472 else if (STRNCMP(*arg, "v:none", 6) == 0)
3473 {
3474 rettv->v_type = VAR_SPECIAL;
3475 rettv->vval.v_number = VVAL_NONE;
3476 *arg += 6;
3477 }
3478}
3479
Bram Moolenaar657137c2021-01-09 15:45:23 +01003480 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003481get_compare_type(char_u *p, int *len, int *type_is)
3482{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003483 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003484 int i;
3485
3486 switch (p[0])
3487 {
3488 case '=': if (p[1] == '=')
3489 type = EXPR_EQUAL;
3490 else if (p[1] == '~')
3491 type = EXPR_MATCH;
3492 break;
3493 case '!': if (p[1] == '=')
3494 type = EXPR_NEQUAL;
3495 else if (p[1] == '~')
3496 type = EXPR_NOMATCH;
3497 break;
3498 case '>': if (p[1] != '=')
3499 {
3500 type = EXPR_GREATER;
3501 *len = 1;
3502 }
3503 else
3504 type = EXPR_GEQUAL;
3505 break;
3506 case '<': if (p[1] != '=')
3507 {
3508 type = EXPR_SMALLER;
3509 *len = 1;
3510 }
3511 else
3512 type = EXPR_SEQUAL;
3513 break;
3514 case 'i': if (p[1] == 's')
3515 {
3516 // "is" and "isnot"; but not a prefix of a name
3517 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3518 *len = 5;
3519 i = p[*len];
3520 if (!isalnum(i) && i != '_')
3521 {
3522 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3523 *type_is = TRUE;
3524 }
3525 }
3526 break;
3527 }
3528 return type;
3529}
3530
3531/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003532 * Skip over an expression, ignoring most errors.
3533 */
3534 static void
3535skip_expr_cctx(char_u **arg, cctx_T *cctx)
3536{
3537 evalarg_T evalarg;
3538
3539 CLEAR_FIELD(evalarg);
3540 evalarg.eval_cctx = cctx;
3541 skip_expr(arg, &evalarg);
3542}
3543
3544/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003546 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 */
3548 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003549compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003551 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
3553 // this works from end to start
3554 while (p > start)
3555 {
3556 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003557 while (VIM_ISWHITE(*p))
3558 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 if (*p == '-' || *p == '+')
3560 {
3561 int negate = *p == '-';
3562 isn_T *isn;
3563
3564 // TODO: check type
3565 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3566 {
3567 --p;
3568 if (*p == '-')
3569 negate = !negate;
3570 }
3571 // only '-' has an effect, for '+' we only check the type
3572 if (negate)
3573 isn = generate_instr(cctx, ISN_NEGATENR);
3574 else
3575 isn = generate_instr(cctx, ISN_CHECKNR);
3576 if (isn == NULL)
3577 return FAIL;
3578 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003579 else if (numeric_only)
3580 {
3581 ++p;
3582 break;
3583 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 else
3585 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003586 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003588 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003590 if (p[-1] == '!')
3591 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
3594 if (generate_2BOOL(cctx, invert) == FAIL)
3595 return FAIL;
3596 }
3597 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003598 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 return OK;
3600}
3601
3602/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003603 * Compile "(expression)": recursive!
3604 * Return FAIL/OK.
3605 */
3606 static int
3607compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3608{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003609 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003610 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003611
Bram Moolenaar24156692021-01-14 20:35:49 +01003612 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3613 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003614 if (ppconst->pp_used <= PPSIZE - 10)
3615 {
3616 ret = compile_expr1(arg, cctx, ppconst);
3617 }
3618 else
3619 {
3620 // Not enough space in ppconst, flush constants.
3621 if (generate_ppconst(cctx, ppconst) == FAIL)
3622 return FAIL;
3623 ret = compile_expr0(arg, cctx);
3624 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003625 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3626 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003627 if (**arg == ')')
3628 ++*arg;
3629 else if (ret == OK)
3630 {
3631 emsg(_(e_missing_close));
3632 ret = FAIL;
3633 }
3634 return ret;
3635}
3636
3637/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003639 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 */
3641 static int
3642compile_subscript(
3643 char_u **arg,
3644 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003645 char_u *start_leader,
3646 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003647 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003649 char_u *name_start = *end_leader;
3650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 for (;;)
3652 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003653 char_u *p = skipwhite(*arg);
3654
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003655 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003656 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003657 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003658
3659 // If a following line starts with "->{" or "->X" advance to that
3660 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003661 // Also if a following line starts with ".x".
3662 if (next != NULL &&
3663 ((next[0] == '-' && next[1] == '>'
3664 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003665 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003666 {
3667 next = next_line_from_context(cctx, TRUE);
3668 if (next == NULL)
3669 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003670 *arg = next;
3671 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003672 }
3673 }
3674
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003675 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003676 // not a function call.
3677 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003679 garray_T *stack = &cctx->ctx_type_stack;
3680 type_T *type;
3681 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003683 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003684 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003685 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003688 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3689
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003690 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3692 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003693 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 return FAIL;
3695 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003696 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003698 char_u *pstart = p;
3699
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003700 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003701 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003702 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 // something->method()
3705 // Apply the '!', '-' and '+' first:
3706 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003707 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003710 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003711 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003712 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003713 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003714 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003715 int argcount = 1;
3716 char_u *expr;
3717 garray_T *stack;
3718 type_T *type;
3719
3720 // Funcref call: list->(Refs[2])(arg)
3721 // or lambda: list->((arg) => expr)(arg)
3722 // Fist compile the arguments.
3723 expr = *arg;
3724 *arg = skipwhite(*arg + 1);
3725 skip_expr_cctx(arg, cctx);
3726 *arg = skipwhite(*arg);
3727 if (**arg != ')')
3728 {
3729 semsg(_(e_missing_paren), *arg);
3730 return FAIL;
3731 }
3732 ++*arg;
3733 if (**arg != '(')
3734 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003735 if (*skipwhite(*arg) == '(')
3736 emsg(_(e_nowhitespace));
3737 else
3738 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003739 return FAIL;
3740 }
3741
3742 *arg = skipwhite(*arg + 1);
3743 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3744 return FAIL;
3745
3746 // Compile the function expression.
3747 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3748 return FAIL;
3749
3750 stack = &cctx->ctx_type_stack;
3751 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3752 if (generate_PCALL(cctx, argcount,
3753 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 return FAIL;
3755 }
3756 else
3757 {
3758 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003759 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003760 if (!eval_isnamec1(*p))
3761 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003762 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003763 return FAIL;
3764 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003765 if (ASCII_ISALPHA(*p) && p[1] == ':')
3766 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003767 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 ;
3769 if (*p != '(')
3770 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003771 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 return FAIL;
3773 }
3774 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003775 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 }
3778 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003779 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003781 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003782 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003783 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003784 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003785 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003786
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003788 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003789 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003790 // TODO: blob index
3791 // TODO: more arguments
3792 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003793 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003794 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003795 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003796
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003797 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003798 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003799 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003800 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003801 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003802 // missing first index is equal to zero
3803 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003804 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003805 else
3806 {
3807 if (compile_expr0(arg, cctx) == FAIL)
3808 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003809 if (**arg == ':')
3810 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003811 semsg(_(e_white_space_required_before_and_after_str_at_str),
3812 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003813 return FAIL;
3814 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003815 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003816 return FAIL;
3817 *arg = skipwhite(*arg);
3818 }
3819 if (**arg == ':')
3820 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003821 is_slice = TRUE;
3822 ++*arg;
3823 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3824 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003825 semsg(_(e_white_space_required_before_and_after_str_at_str),
3826 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003827 return FAIL;
3828 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003829 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003830 return FAIL;
3831 if (**arg == ']')
3832 // missing second index is equal to end of string
3833 generate_PUSHNR(cctx, -1);
3834 else
3835 {
3836 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003837 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003838 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003839 return FAIL;
3840 *arg = skipwhite(*arg);
3841 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
3844 if (**arg != ']')
3845 {
3846 emsg(_(e_missbrac));
3847 return FAIL;
3848 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003849 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003851 // We can index a list and a dict. If we don't know the type
3852 // we can use the index value type.
3853 // TODO: If we don't know use an instruction to figure it out at
3854 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003855 typep = ((type_T **)stack->ga_data) + stack->ga_len
3856 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003857 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003858 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3859 // If the index is a string, the variable must be a Dict.
3860 if (*typep == &t_any && valtype == &t_string)
3861 vtype = VAR_DICT;
3862 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003863 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003864 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003865 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003866 return FAIL;
3867 if (is_slice)
3868 {
3869 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003870 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003871 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003872 return FAIL;
3873 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003874 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003875
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003876 if (vtype == VAR_DICT)
3877 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003878 if (is_slice)
3879 {
3880 emsg(_(e_cannot_slice_dictionary));
3881 return FAIL;
3882 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003883 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003884 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003885 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003886 if (*typep == &t_unknown)
3887 // empty dict was used
3888 *typep = &t_any;
3889 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003890 else
3891 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003892 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003893 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003894 return FAIL;
3895 *typep = &t_any;
3896 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003897 if (may_generate_2STRING(-1, cctx) == FAIL)
3898 return FAIL;
3899 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3900 return FAIL;
3901 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003902 else if (vtype == VAR_STRING)
3903 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003904 *typep = &t_string;
3905 if ((is_slice
3906 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3907 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003908 return FAIL;
3909 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003910 else if (vtype == VAR_BLOB)
3911 {
3912 emsg("Sorry, blob index and slice not implemented yet");
3913 return FAIL;
3914 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003915 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003916 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003917 if (is_slice)
3918 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003919 if (generate_instr_drop(cctx,
3920 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3921 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003922 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003923 }
Bram Moolenaared591872020-08-15 22:14:53 +02003924 else
3925 {
3926 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003927 {
Bram Moolenaared591872020-08-15 22:14:53 +02003928 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003929 if (*typep == &t_unknown)
3930 // empty list was used
3931 *typep = &t_any;
3932 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003933 if (generate_instr_drop(cctx,
3934 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3935 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003936 return FAIL;
3937 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003938 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003939 else
3940 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003941 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003942 return FAIL;
3943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003945 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003947 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003948 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003949 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003950 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003951
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003952 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003953 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003954 {
3955 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003956 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003957 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003958 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003959 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 while (eval_isnamec(*p))
3961 MB_PTR_ADV(p);
3962 if (p == *arg)
3963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003964 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 return FAIL;
3966 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003967 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 return FAIL;
3969 *arg = p;
3970 }
3971 else
3972 break;
3973 }
3974
3975 // TODO - see handle_subscript():
3976 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3977 // Don't do this when "Func" is already a partial that was bound
3978 // explicitly (pt_auto is FALSE).
3979
3980 return OK;
3981}
3982
3983/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3985 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003987 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003988 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 *
3990 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 */
3992
3993/*
3994 * number number constant
3995 * 0zFFFFFFFF Blob constant
3996 * "string" string constant
3997 * 'string' literal string constant
3998 * &option-name option value
3999 * @r register contents
4000 * identifier variable value
4001 * function() function call
4002 * $VAR environment variable
4003 * (expression) nested expression
4004 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004005 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 *
4007 * Also handle:
4008 * ! in front logical NOT
4009 * - in front unary minus
4010 * + in front unary plus (ignored)
4011 * trailing (arg) funcref/partial call
4012 * trailing [] subscript in String or List
4013 * trailing .name entry in Dictionary
4014 * trailing ->name() method call
4015 */
4016 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004017compile_expr7(
4018 char_u **arg,
4019 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004020 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 char_u *start_leader, *end_leader;
4023 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004024 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004025 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004027 ppconst->pp_is_const = FALSE;
4028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 /*
4030 * Skip '!', '-' and '+' characters. They are handled later.
4031 */
4032 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004033 if (eval_leader(arg, TRUE) == FAIL)
4034 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 end_leader = *arg;
4036
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004037 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 switch (**arg)
4039 {
4040 /*
4041 * Number constant.
4042 */
4043 case '0': // also for blob starting with 0z
4044 case '1':
4045 case '2':
4046 case '3':
4047 case '4':
4048 case '5':
4049 case '6':
4050 case '7':
4051 case '8':
4052 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004053 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004055 // Apply "-" and "+" just before the number now, right to
4056 // left. Matters especially when "->" follows. Stops at
4057 // '!'.
4058 if (apply_leader(rettv, TRUE,
4059 start_leader, &end_leader) == FAIL)
4060 {
4061 clear_tv(rettv);
4062 return FAIL;
4063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 break;
4065
4066 /*
4067 * String constant: "string".
4068 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004069 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 return FAIL;
4071 break;
4072
4073 /*
4074 * Literal string constant: 'str''ing'.
4075 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004076 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 return FAIL;
4078 break;
4079
4080 /*
4081 * Constant Vim variable.
4082 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004083 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 ret = NOTDONE;
4085 break;
4086
4087 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004088 * "true" constant
4089 */
4090 case 't': if (STRNCMP(*arg, "true", 4) == 0
4091 && !eval_isnamec((*arg)[4]))
4092 {
4093 *arg += 4;
4094 rettv->v_type = VAR_BOOL;
4095 rettv->vval.v_number = VVAL_TRUE;
4096 }
4097 else
4098 ret = NOTDONE;
4099 break;
4100
4101 /*
4102 * "false" constant
4103 */
4104 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4105 && !eval_isnamec((*arg)[5]))
4106 {
4107 *arg += 5;
4108 rettv->v_type = VAR_BOOL;
4109 rettv->vval.v_number = VVAL_FALSE;
4110 }
4111 else
4112 ret = NOTDONE;
4113 break;
4114
4115 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004116 * "null" constant
4117 */
4118 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4119 && !eval_isnamec((*arg)[5]))
4120 {
4121 *arg += 4;
4122 rettv->v_type = VAR_SPECIAL;
4123 rettv->vval.v_number = VVAL_NULL;
4124 }
4125 else
4126 ret = NOTDONE;
4127 break;
4128
4129 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 * List: [expr, expr]
4131 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004132 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4133 return FAIL;
4134 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 break;
4136
4137 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 * Dictionary: {'key': val, 'key': val}
4139 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004140 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4141 return FAIL;
4142 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 break;
4144
4145 /*
4146 * Option value: &name
4147 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004148 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4149 return FAIL;
4150 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 break;
4152
4153 /*
4154 * Environment variable: $VAR.
4155 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004156 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4157 return FAIL;
4158 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 break;
4160
4161 /*
4162 * Register contents: @r.
4163 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004164 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4165 return FAIL;
4166 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 break;
4168 /*
4169 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004170 * lambda: (arg, arg) => expr
4171 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004173 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4174 ret = compile_lambda(arg, cctx);
4175 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004176 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 break;
4178
4179 default: ret = NOTDONE;
4180 break;
4181 }
4182 if (ret == FAIL)
4183 return FAIL;
4184
Bram Moolenaar1c747212020-05-09 18:28:34 +02004185 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004187 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004188 clear_tv(rettv);
4189 else
4190 // A constant expression can possibly be handled compile time,
4191 // return the value instead of generating code.
4192 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 }
4194 else if (ret == NOTDONE)
4195 {
4196 char_u *p;
4197 int r;
4198
4199 if (!eval_isnamec1(**arg))
4200 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004201 if (ends_excmd(*skipwhite(*arg)))
4202 semsg(_(e_empty_expression_str), *arg);
4203 else
4204 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 return FAIL;
4206 }
4207
4208 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004209 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004211 {
4212 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004215 {
4216 if (generate_ppconst(cctx, ppconst) == FAIL)
4217 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004218 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 if (r == FAIL)
4221 return FAIL;
4222 }
4223
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004224 // Handle following "[]", ".member", etc.
4225 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004226 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 ppconst) == FAIL)
4228 return FAIL;
4229 if (ppconst->pp_used > 0)
4230 {
4231 // apply the '!', '-' and '+' before the constant
4232 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004233 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004234 return FAIL;
4235 return OK;
4236 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004237 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004239 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240}
4241
4242/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004243 * Give the "white on both sides" error, taking the operator from "p[len]".
4244 */
4245 void
4246error_white_both(char_u *op, int len)
4247{
4248 char_u buf[10];
4249
4250 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004251 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004252}
4253
4254/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004255 * <type>expr7: runtime type check / conversion
4256 */
4257 static int
4258compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4259{
4260 type_T *want_type = NULL;
4261
4262 // Recognize <type>
4263 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4264 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004265 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004266 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4267 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004268 return FAIL;
4269
4270 if (**arg != '>')
4271 {
4272 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004273 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004274 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004275 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004276 return FAIL;
4277 }
4278 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004279 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004280 return FAIL;
4281 }
4282
4283 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4284 return FAIL;
4285
4286 if (want_type != NULL)
4287 {
4288 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004289 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004290
Bram Moolenaard1103582020-08-14 22:44:25 +02004291 generate_ppconst(cctx, ppconst);
4292 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004293 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004294 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004295 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004296 return FAIL;
4297 }
4298 }
4299
4300 return OK;
4301}
4302
4303/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 * * number multiplication
4305 * / number division
4306 * % number modulo
4307 */
4308 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004309compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310{
4311 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004312 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004313 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004315 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004316 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 return FAIL;
4318
4319 /*
4320 * Repeat computing, until no "*", "/" or "%" is following.
4321 */
4322 for (;;)
4323 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004324 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 if (*op != '*' && *op != '/' && *op != '%')
4326 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004327 if (next != NULL)
4328 {
4329 *arg = next_line_from_context(cctx, TRUE);
4330 op = skipwhite(*arg);
4331 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004332
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004333 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004335 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004338 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004339 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004341 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004342 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004344
4345 if (ppconst->pp_used == ppconst_used + 2
4346 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4347 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004348 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004349 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4350 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4351 varnumber_T res = 0;
4352 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354 // both are numbers: compute the result
4355 switch (*op)
4356 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004357 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004358 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004359 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004360 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004361 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004362 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004363 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004364 break;
4365 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004366 if (failed)
4367 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004368 tv1->vval.v_number = res;
4369 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004370 }
4371 else
4372 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004373 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004374 generate_two_op(cctx, op);
4375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377
4378 return OK;
4379}
4380
4381/*
4382 * + number addition
4383 * - number subtraction
4384 * .. string concatenation
4385 */
4386 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004387compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388{
4389 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004390 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004392 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
4394 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 return FAIL;
4397
4398 /*
4399 * Repeat computing, until no "+", "-" or ".." is following.
4400 */
4401 for (;;)
4402 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004403 op = may_peek_next_line(cctx, *arg, &next);
4404 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 break;
4406 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004407 if (next != NULL)
4408 {
4409 *arg = next_line_from_context(cctx, TRUE);
4410 op = skipwhite(*arg);
4411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004413 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004415 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004416 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 }
4418
Bram Moolenaare0de1712020-12-02 17:36:54 +01004419 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004420 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004422 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004423 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 return FAIL;
4425
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004427 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004428 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4429 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4430 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4431 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4434 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004436 // concat/subtract/add constant numbers
4437 if (*op == '+')
4438 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4439 else if (*op == '-')
4440 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4441 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004443 // concatenate constant strings
4444 char_u *s1 = tv1->vval.v_string;
4445 char_u *s2 = tv2->vval.v_string;
4446 size_t len1 = STRLEN(s1);
4447
4448 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4449 if (tv1->vval.v_string == NULL)
4450 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004452 return FAIL;
4453 }
4454 mch_memmove(tv1->vval.v_string, s1, len1);
4455 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004456 vim_free(s1);
4457 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004458 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 }
4461 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004462 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004463 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004464 if (*op == '.')
4465 {
4466 if (may_generate_2STRING(-2, cctx) == FAIL
4467 || may_generate_2STRING(-1, cctx) == FAIL)
4468 return FAIL;
4469 generate_instr_drop(cctx, ISN_CONCAT, 1);
4470 }
4471 else
4472 generate_two_op(cctx, op);
4473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 }
4475
4476 return OK;
4477}
4478
4479/*
4480 * expr5a == expr5b
4481 * expr5a =~ expr5b
4482 * expr5a != expr5b
4483 * expr5a !~ expr5b
4484 * expr5a > expr5b
4485 * expr5a >= expr5b
4486 * expr5a < expr5b
4487 * expr5a <= expr5b
4488 * expr5a is expr5b
4489 * expr5a isnot expr5b
4490 *
4491 * Produces instructions:
4492 * EVAL expr5a Push result of "expr5a"
4493 * EVAL expr5b Push result of "expr5b"
4494 * COMPARE one of the compare instructions
4495 */
4496 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004499 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004501 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004503 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004504 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505
4506 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 return FAIL;
4509
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004510 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004511 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512
4513 /*
4514 * If there is a comparative operator, use it.
4515 */
4516 if (type != EXPR_UNKNOWN)
4517 {
4518 int ic = FALSE; // Default: do not ignore case
4519
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004520 if (next != NULL)
4521 {
4522 *arg = next_line_from_context(cctx, TRUE);
4523 p = skipwhite(*arg);
4524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 if (type_is && (p[len] == '?' || p[len] == '#'))
4526 {
4527 semsg(_(e_invexpr2), *arg);
4528 return FAIL;
4529 }
4530 // extra question mark appended: ignore case
4531 if (p[len] == '?')
4532 {
4533 ic = TRUE;
4534 ++len;
4535 }
4536 // extra '#' appended: match case (ignored)
4537 else if (p[len] == '#')
4538 ++len;
4539 // nothing appended: match case
4540
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004541 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004543 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004544 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546
4547 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004548 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004549 return FAIL;
4550
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 return FAIL;
4553
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 if (ppconst->pp_used == ppconst_used + 2)
4555 {
4556 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4557 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4558 int ret;
4559
4560 // Both sides are a constant, compute the result now.
4561 // First check for a valid combination of types, this is more
4562 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004563 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004564 ret = FAIL;
4565 else
4566 {
4567 ret = typval_compare(tv1, tv2, type, ic);
4568 tv1->v_type = VAR_BOOL;
4569 tv1->vval.v_number = tv1->vval.v_number
4570 ? VVAL_TRUE : VVAL_FALSE;
4571 clear_tv(tv2);
4572 --ppconst->pp_used;
4573 }
4574 return ret;
4575 }
4576
4577 generate_ppconst(cctx, ppconst);
4578 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 }
4580
4581 return OK;
4582}
4583
Bram Moolenaar7f141552020-05-09 17:35:53 +02004584static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586/*
4587 * Compile || or &&.
4588 */
4589 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004590compile_and_or(
4591 char_u **arg,
4592 cctx_T *cctx,
4593 char *op,
4594 ppconst_T *ppconst,
4595 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004597 char_u *next;
4598 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 int opchar = *op;
4600
4601 if (p[0] == opchar && p[1] == opchar)
4602 {
4603 garray_T *instr = &cctx->ctx_instr;
4604 garray_T end_ga;
4605
4606 /*
4607 * Repeat until there is no following "||" or "&&"
4608 */
4609 ga_init2(&end_ga, sizeof(int), 10);
4610 while (p[0] == opchar && p[1] == opchar)
4611 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004612 if (next != NULL)
4613 {
4614 *arg = next_line_from_context(cctx, TRUE);
4615 p = skipwhite(*arg);
4616 }
4617
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004618 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4619 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004620 semsg(_(e_white_space_required_before_and_after_str_at_str),
4621 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004622 return FAIL;
4623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004625 // TODO: use ppconst if the value is a constant and check
4626 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004627 generate_ppconst(cctx, ppconst);
4628
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004629 // Every part must evaluate to a bool.
4630 if (bool_on_stack(cctx) == FAIL)
4631 {
4632 ga_clear(&end_ga);
4633 return FAIL;
4634 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 if (ga_grow(&end_ga, 1) == FAIL)
4637 {
4638 ga_clear(&end_ga);
4639 return FAIL;
4640 }
4641 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4642 ++end_ga.ga_len;
4643 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004644 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645
4646 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004647 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004648 {
4649 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004650 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004651 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004652
Bram Moolenaara5565e42020-05-09 15:44:01 +02004653 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4654 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 {
4656 ga_clear(&end_ga);
4657 return FAIL;
4658 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004659
4660 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004664 // Every part must evaluate to a bool.
4665 if (bool_on_stack(cctx) == FAIL)
4666 {
4667 ga_clear(&end_ga);
4668 return FAIL;
4669 }
4670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 // Fill in the end label in all jumps.
4672 while (end_ga.ga_len > 0)
4673 {
4674 isn_T *isn;
4675
4676 --end_ga.ga_len;
4677 isn = ((isn_T *)instr->ga_data)
4678 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4679 isn->isn_arg.jump.jump_where = instr->ga_len;
4680 }
4681 ga_clear(&end_ga);
4682 }
4683
4684 return OK;
4685}
4686
4687/*
4688 * expr4a && expr4a && expr4a logical AND
4689 *
4690 * Produces instructions:
4691 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004692 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004693 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004695 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 * EVAL expr4c Push result of "expr4c"
4697 * end:
4698 */
4699 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702 int ppconst_used = ppconst->pp_used;
4703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004705 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 return FAIL;
4707
4708 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004709 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710}
4711
4712/*
4713 * expr3a || expr3b || expr3c logical OR
4714 *
4715 * Produces instructions:
4716 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004717 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004718 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004720 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 * EVAL expr3c Push result of "expr3c"
4722 * end:
4723 */
4724 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 int ppconst_used = ppconst->pp_used;
4728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 return FAIL;
4732
4733 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735}
4736
4737/*
4738 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004740 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004741 * JUMP_IF_FALSE alt jump if false
4742 * EVAL expr1a
4743 * JUMP_ALWAYS end
4744 * alt: EVAL expr1b
4745 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004746 *
4747 * Toplevel expression: expr2 ?? expr1
4748 * Produces instructions:
4749 * EVAL expr2 Push result of "expr2"
4750 * JUMP_AND_KEEP_IF_TRUE end jump if true
4751 * EVAL expr1
4752 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 */
4754 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004755compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756{
4757 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004758 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004759 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760
Bram Moolenaar3988f642020-08-27 22:43:03 +02004761 // Ignore all kinds of errors when not producing code.
4762 if (cctx->ctx_skip == SKIP_YES)
4763 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004764 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004765 return OK;
4766 }
4767
Bram Moolenaar61a89812020-05-07 16:58:17 +02004768 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004769 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 return FAIL;
4771
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004772 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 if (*p == '?')
4774 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004775 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 garray_T *instr = &cctx->ctx_instr;
4777 garray_T *stack = &cctx->ctx_type_stack;
4778 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004779 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004781 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004782 int has_const_expr = FALSE;
4783 int const_value = FALSE;
4784 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004786 if (next != NULL)
4787 {
4788 *arg = next_line_from_context(cctx, TRUE);
4789 p = skipwhite(*arg);
4790 }
4791
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004792 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004793 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004794 semsg(_(e_white_space_required_before_and_after_str_at_str),
4795 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004796 return FAIL;
4797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Bram Moolenaara5565e42020-05-09 15:44:01 +02004799 if (ppconst->pp_used == ppconst_used + 1)
4800 {
4801 // the condition is a constant, we know whether the ? or the :
4802 // expression is to be evaluated.
4803 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004804 if (op_falsy)
4805 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4806 else
4807 {
4808 int error = FALSE;
4809
4810 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4811 &error);
4812 if (error)
4813 return FAIL;
4814 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004815 cctx->ctx_skip = save_skip == SKIP_YES ||
4816 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4817
4818 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4819 // "left ?? right" and "left" is truthy: produce "left"
4820 generate_ppconst(cctx, ppconst);
4821 else
4822 {
4823 clear_tv(&ppconst->pp_tv[ppconst_used]);
4824 --ppconst->pp_used;
4825 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004826 }
4827 else
4828 {
4829 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004830 if (op_falsy)
4831 end_idx = instr->ga_len;
4832 generate_JUMP(cctx, op_falsy
4833 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4834 if (op_falsy)
4835 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837
4838 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004839 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004840 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004841 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004842 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 if (!has_const_expr)
4845 {
4846 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004848 if (!op_falsy)
4849 {
4850 // remember the type and drop it
4851 --stack->ga_len;
4852 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004854 end_idx = instr->ga_len;
4855 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004856
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004857 // jump here from JUMP_IF_FALSE
4858 isn = ((isn_T *)instr->ga_data) + alt_idx;
4859 isn->isn_arg.jump.jump_where = instr->ga_len;
4860 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004863 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004865 // Check for the ":".
4866 p = may_peek_next_line(cctx, *arg, &next);
4867 if (*p != ':')
4868 {
4869 emsg(_(e_missing_colon));
4870 return FAIL;
4871 }
4872 if (next != NULL)
4873 {
4874 *arg = next_line_from_context(cctx, TRUE);
4875 p = skipwhite(*arg);
4876 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004877
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004878 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4879 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004880 semsg(_(e_white_space_required_before_and_after_str_at_str),
4881 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004882 return FAIL;
4883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004885 // evaluate the third expression
4886 if (has_const_expr)
4887 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004888 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004889 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004890 return FAIL;
4891 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4892 return FAIL;
4893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
Bram Moolenaara5565e42020-05-09 15:44:01 +02004895 if (!has_const_expr)
4896 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004897 type_T **typep;
4898
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900
Bram Moolenaara5565e42020-05-09 15:44:01 +02004901 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004902 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4903 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004904
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004905 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004906 isn = ((isn_T *)instr->ga_data) + end_idx;
4907 isn->isn_arg.jump.jump_where = instr->ga_len;
4908 }
4909
4910 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 }
4912 return OK;
4913}
4914
4915/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004916 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004917 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4918 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004919 */
4920 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004921compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004922{
4923 ppconst_T ppconst;
4924
4925 CLEAR_FIELD(ppconst);
4926 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4927 {
4928 clear_ppconst(&ppconst);
4929 return FAIL;
4930 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004931 if (is_const != NULL)
4932 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004933 if (generate_ppconst(cctx, &ppconst) == FAIL)
4934 return FAIL;
4935 return OK;
4936}
4937
4938/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004939 * Toplevel expression.
4940 */
4941 static int
4942compile_expr0(char_u **arg, cctx_T *cctx)
4943{
4944 return compile_expr0_ext(arg, cctx, NULL);
4945}
4946
4947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 * compile "return [expr]"
4949 */
4950 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004951compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952{
4953 char_u *p = arg;
4954 garray_T *stack = &cctx->ctx_type_stack;
4955 type_T *stack_type;
4956
4957 if (*p != NUL && *p != '|' && *p != '\n')
4958 {
4959 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004960 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 return NULL;
4962
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004963 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004964 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004965 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004966 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004967 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4968 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004969 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004970 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004971 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004972 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004973 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004974 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4975 && stack_type->tt_type != VAR_VOID
4976 && stack_type->tt_type != VAR_UNKNOWN)
4977 {
4978 emsg(_(e_returning_value_in_function_without_return_type));
4979 return NULL;
4980 }
4981 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004982 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004983 return NULL;
4984 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 }
4987 else
4988 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004989 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004990 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004991 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4992 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004994 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 return NULL;
4996 }
4997
4998 // No argument, return zero.
4999 generate_PUSHNR(cctx, 0);
5000 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005001
5002 // Undo any command modifiers.
5003 generate_undo_cmdmods(cctx);
5004
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005005 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 return NULL;
5007
5008 // "return val | endif" is possible
5009 return skipwhite(p);
5010}
5011
5012/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005013 * Get a line from the compilation context, compatible with exarg_T getline().
5014 * Return a pointer to the line in allocated memory.
5015 * Return NULL for end-of-file or some error.
5016 */
5017 static char_u *
5018exarg_getline(
5019 int c UNUSED,
5020 void *cookie,
5021 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005022 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005023{
5024 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005025 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005026
Bram Moolenaar66250c92020-08-20 15:02:42 +02005027 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005028 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005029 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005030 return NULL;
5031 ++cctx->ctx_lnum;
5032 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5033 // Comment lines result in NULL pointers, skip them.
5034 if (p != NULL)
5035 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005036 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037}
5038
5039/*
5040 * Compile a nested :def command.
5041 */
5042 static char_u *
5043compile_nested_function(exarg_T *eap, cctx_T *cctx)
5044{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005045 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005046 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005047 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005048 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005049 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005050 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005051
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005052 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005053 {
5054 emsg(_(e_cannot_use_bang_with_nested_def));
5055 return NULL;
5056 }
5057
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005058 if (*name_start == '/')
5059 {
5060 name_end = skip_regexp(name_start + 1, '/', TRUE);
5061 if (*name_end == '/')
5062 ++name_end;
5063 eap->nextcmd = check_nextcmd(name_end);
5064 }
5065 if (name_end == name_start || *skipwhite(name_end) != '(')
5066 {
5067 if (!ends_excmd2(name_start, name_end))
5068 {
5069 semsg(_(e_invalid_command_str), eap->cmd);
5070 return NULL;
5071 }
5072
5073 // "def" or "def Name": list functions
5074 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5075 return NULL;
5076 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5077 }
5078
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005079 // Only g:Func() can use a namespace.
5080 if (name_start[1] == ':' && !is_global)
5081 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005082 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005083 return NULL;
5084 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005085 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5086 return NULL;
5087
Bram Moolenaar04b12692020-05-04 23:24:44 +02005088 eap->arg = name_end;
5089 eap->getline = exarg_getline;
5090 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005091 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005092 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005093 lambda_name = vim_strsave(get_lambda_name());
5094 if (lambda_name == NULL)
5095 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005096 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005097
Bram Moolenaar822ba242020-05-24 23:00:18 +02005098 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005099 {
5100 r = eap->skip ? OK : FAIL;
5101 goto theend;
5102 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005103 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5104 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005105 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005106 {
5107 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005108 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005109 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005110
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005111 if (is_global)
5112 {
5113 char_u *func_name = vim_strnsave(name_start + 2,
5114 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005115
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005116 if (func_name == NULL)
5117 r = FAIL;
5118 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005119 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005120 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005121 lambda_name = NULL;
5122 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005123 }
5124 else
5125 {
5126 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005127 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005128 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005129 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005130
Bram Moolenaareef21022020-08-01 22:16:43 +02005131 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005132 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005133 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005134 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005135 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005136
5137 // copy over the block scope IDs
5138 if (block_depth > 0)
5139 {
5140 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5141 if (ufunc->uf_block_ids != NULL)
5142 {
5143 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5144 sizeof(int) * block_depth);
5145 ufunc->uf_block_depth = block_depth;
5146 }
5147 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005148 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005149 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005150
5151theend:
5152 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005153 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005154}
5155
5156/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157 * Return the length of an assignment operator, or zero if there isn't one.
5158 */
5159 int
5160assignment_len(char_u *p, int *heredoc)
5161{
5162 if (*p == '=')
5163 {
5164 if (p[1] == '<' && p[2] == '<')
5165 {
5166 *heredoc = TRUE;
5167 return 3;
5168 }
5169 return 1;
5170 }
5171 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5172 return 2;
5173 if (STRNCMP(p, "..=", 3) == 0)
5174 return 3;
5175 return 0;
5176}
5177
5178// words that cannot be used as a variable
5179static char *reserved[] = {
5180 "true",
5181 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005182 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 NULL
5184};
5185
Bram Moolenaar752fc692021-01-04 21:57:11 +01005186// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005187typedef enum {
5188 dest_local,
5189 dest_option,
5190 dest_env,
5191 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005192 dest_buffer,
5193 dest_window,
5194 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005195 dest_vimvar,
5196 dest_script,
5197 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005198 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005199} assign_dest_T;
5200
Bram Moolenaar752fc692021-01-04 21:57:11 +01005201// Used by compile_lhs() to store information about the LHS of an assignment
5202// and one argument of ":unlet" with an index.
5203typedef struct {
5204 assign_dest_T lhs_dest; // type of destination
5205
5206 char_u *lhs_name; // allocated name including
5207 // "[expr]" or ".name".
5208 size_t lhs_varlen; // length of the variable without
5209 // "[expr]" or ".name"
5210 char_u *lhs_dest_end; // end of the destination, including
5211 // "[expr]" or ".name".
5212
5213 int lhs_has_index; // has "[expr]" or ".name"
5214
5215 int lhs_new_local; // create new local variable
5216 int lhs_opt_flags; // for when destination is an option
5217 int lhs_vimvaridx; // for when destination is a v:var
5218
5219 lvar_T lhs_local_lvar; // used for existing local destination
5220 lvar_T lhs_arg_lvar; // used for argument destination
5221 lvar_T *lhs_lvar; // points to destination lvar
5222 int lhs_scriptvar_sid;
5223 int lhs_scriptvar_idx;
5224
5225 int lhs_has_type; // type was specified
5226 type_T *lhs_type;
5227 type_T *lhs_member_type;
5228} lhs_T;
5229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005231 * Generate the load instruction for "name".
5232 */
5233 static void
5234generate_loadvar(
5235 cctx_T *cctx,
5236 assign_dest_T dest,
5237 char_u *name,
5238 lvar_T *lvar,
5239 type_T *type)
5240{
5241 switch (dest)
5242 {
5243 case dest_option:
5244 // TODO: check the option exists
5245 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5246 break;
5247 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005248 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5249 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5250 else
5251 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005252 break;
5253 case dest_buffer:
5254 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5255 break;
5256 case dest_window:
5257 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5258 break;
5259 case dest_tab:
5260 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5261 break;
5262 case dest_script:
5263 compile_load_scriptvar(cctx,
5264 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5265 break;
5266 case dest_env:
5267 // Include $ in the name here
5268 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5269 break;
5270 case dest_reg:
5271 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5272 break;
5273 case dest_vimvar:
5274 generate_LOADV(cctx, name + 2, TRUE);
5275 break;
5276 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005277 if (lvar->lv_from_outer > 0)
5278 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5279 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005280 else
5281 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5282 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005283 case dest_expr:
5284 // list or dict value should already be on the stack.
5285 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005286 }
5287}
5288
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005289/*
5290 * Skip over "[expr]" or ".member".
5291 * Does not check for any errors.
5292 */
5293 static char_u *
5294skip_index(char_u *start)
5295{
5296 char_u *p = start;
5297
5298 if (*p == '[')
5299 {
5300 p = skipwhite(p + 1);
5301 (void)skip_expr(&p, NULL);
5302 p = skipwhite(p);
5303 if (*p == ']')
5304 return p + 1;
5305 return p;
5306 }
5307 // if (*p == '.')
5308 return to_name_end(p + 1, TRUE);
5309}
5310
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005311 void
5312vim9_declare_error(char_u *name)
5313{
5314 char *scope = "";
5315
5316 switch (*name)
5317 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005318 case 'g': scope = _("global"); break;
5319 case 'b': scope = _("buffer"); break;
5320 case 'w': scope = _("window"); break;
5321 case 't': scope = _("tab"); break;
5322 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005323 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005324 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005325 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005326 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005327 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005328 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005329 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005330 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005331 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005332}
5333
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005334/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005335 * For one assignment figure out the type of destination. Return it in "dest".
5336 * When not recognized "dest" is not set.
5337 * For an option "opt_flags" is set.
5338 * For a v:var "vimvaridx" is set.
5339 * "type" is set to the destination type if known, unchanted otherwise.
5340 * Return FAIL if an error message was given.
5341 */
5342 static int
5343get_var_dest(
5344 char_u *name,
5345 assign_dest_T *dest,
5346 int cmdidx,
5347 int *opt_flags,
5348 int *vimvaridx,
5349 type_T **type,
5350 cctx_T *cctx)
5351{
5352 char_u *p;
5353
5354 if (*name == '&')
5355 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005356 int cc;
5357 long numval;
5358 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005359
5360 *dest = dest_option;
5361 if (cmdidx == CMD_final || cmdidx == CMD_const)
5362 {
5363 emsg(_(e_const_option));
5364 return FAIL;
5365 }
5366 p = name;
5367 p = find_option_end(&p, opt_flags);
5368 if (p == NULL)
5369 {
5370 // cannot happen?
5371 emsg(_(e_letunexp));
5372 return FAIL;
5373 }
5374 cc = *p;
5375 *p = NUL;
5376 opt_type = get_option_value(skip_option_env_lead(name),
5377 &numval, NULL, *opt_flags);
5378 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005379 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005380 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005381 case gov_unknown:
5382 semsg(_(e_unknown_option), name);
5383 return FAIL;
5384 case gov_string:
5385 case gov_hidden_string:
5386 *type = &t_string;
5387 break;
5388 case gov_bool:
5389 case gov_hidden_bool:
5390 *type = &t_bool;
5391 break;
5392 case gov_number:
5393 case gov_hidden_number:
5394 *type = &t_number;
5395 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005396 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005397 }
5398 else if (*name == '$')
5399 {
5400 *dest = dest_env;
5401 *type = &t_string;
5402 }
5403 else if (*name == '@')
5404 {
5405 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5406 {
5407 emsg_invreg(name[1]);
5408 return FAIL;
5409 }
5410 *dest = dest_reg;
5411 *type = &t_string;
5412 }
5413 else if (STRNCMP(name, "g:", 2) == 0)
5414 {
5415 *dest = dest_global;
5416 }
5417 else if (STRNCMP(name, "b:", 2) == 0)
5418 {
5419 *dest = dest_buffer;
5420 }
5421 else if (STRNCMP(name, "w:", 2) == 0)
5422 {
5423 *dest = dest_window;
5424 }
5425 else if (STRNCMP(name, "t:", 2) == 0)
5426 {
5427 *dest = dest_tab;
5428 }
5429 else if (STRNCMP(name, "v:", 2) == 0)
5430 {
5431 typval_T *vtv;
5432 int di_flags;
5433
5434 *vimvaridx = find_vim_var(name + 2, &di_flags);
5435 if (*vimvaridx < 0)
5436 {
5437 semsg(_(e_variable_not_found_str), name);
5438 return FAIL;
5439 }
5440 // We use the current value of "sandbox" here, is that OK?
5441 if (var_check_ro(di_flags, name, FALSE))
5442 return FAIL;
5443 *dest = dest_vimvar;
5444 vtv = get_vim_var_tv(*vimvaridx);
5445 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5446 }
5447 return OK;
5448}
5449
5450/*
5451 * Generate a STORE instruction for "dest", not being "dest_local".
5452 * Return FAIL when out of memory.
5453 */
5454 static int
5455generate_store_var(
5456 cctx_T *cctx,
5457 assign_dest_T dest,
5458 int opt_flags,
5459 int vimvaridx,
5460 int scriptvar_idx,
5461 int scriptvar_sid,
5462 type_T *type,
5463 char_u *name)
5464{
5465 switch (dest)
5466 {
5467 case dest_option:
5468 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5469 opt_flags);
5470 case dest_global:
5471 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005472 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5473 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005474 case dest_buffer:
5475 // include b: with the name, easier to execute that way
5476 return generate_STORE(cctx, ISN_STOREB, 0, name);
5477 case dest_window:
5478 // include w: with the name, easier to execute that way
5479 return generate_STORE(cctx, ISN_STOREW, 0, name);
5480 case dest_tab:
5481 // include t: with the name, easier to execute that way
5482 return generate_STORE(cctx, ISN_STORET, 0, name);
5483 case dest_env:
5484 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5485 case dest_reg:
5486 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5487 case dest_vimvar:
5488 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5489 case dest_script:
5490 if (scriptvar_idx < 0)
5491 {
5492 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005493 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005494
Bram Moolenaar41d61962020-12-06 20:12:43 +01005495 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005496 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5497 scriptvar_sid, type);
5498 if (name_s != name)
5499 vim_free(name_s);
5500 return r;
5501 }
5502 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5503 scriptvar_sid, scriptvar_idx, type);
5504 case dest_local:
5505 case dest_expr:
5506 // cannot happen
5507 break;
5508 }
5509 return FAIL;
5510}
5511
Bram Moolenaar752fc692021-01-04 21:57:11 +01005512 static int
5513is_decl_command(int cmdidx)
5514{
5515 return cmdidx == CMD_let || cmdidx == CMD_var
5516 || cmdidx == CMD_final || cmdidx == CMD_const;
5517}
5518
5519/*
5520 * Figure out the LHS type and other properties for an assignment or one item
5521 * of ":unlet" with an index.
5522 * Returns OK or FAIL.
5523 */
5524 static int
5525compile_lhs(
5526 char_u *var_start,
5527 lhs_T *lhs,
5528 int cmdidx,
5529 int heredoc,
5530 int oplen,
5531 cctx_T *cctx)
5532{
5533 char_u *var_end;
5534 int is_decl = is_decl_command(cmdidx);
5535
5536 CLEAR_POINTER(lhs);
5537 lhs->lhs_dest = dest_local;
5538 lhs->lhs_vimvaridx = -1;
5539 lhs->lhs_scriptvar_idx = -1;
5540
5541 // "dest_end" is the end of the destination, including "[expr]" or
5542 // ".name".
5543 // "var_end" is the end of the variable/option/etc. name.
5544 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5545 if (*var_start == '@')
5546 var_end = var_start + 2;
5547 else
5548 {
5549 // skip over the leading "&", "&l:", "&g:" and "$"
5550 var_end = skip_option_env_lead(var_start);
5551 var_end = to_name_end(var_end, TRUE);
5552 }
5553
5554 // "a: type" is declaring variable "a" with a type, not dict "a:".
5555 if (is_decl && lhs->lhs_dest_end == var_start + 2
5556 && lhs->lhs_dest_end[-1] == ':')
5557 --lhs->lhs_dest_end;
5558 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5559 --var_end;
5560
5561 // compute the length of the destination without "[expr]" or ".name"
5562 lhs->lhs_varlen = var_end - var_start;
5563 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5564 if (lhs->lhs_name == NULL)
5565 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005566
5567 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5568 // Something follows after the variable: "var[idx]" or "var.key".
5569 lhs->lhs_has_index = TRUE;
5570
Bram Moolenaar752fc692021-01-04 21:57:11 +01005571 if (heredoc)
5572 lhs->lhs_type = &t_list_string;
5573 else
5574 lhs->lhs_type = &t_any;
5575
5576 if (cctx->ctx_skip != SKIP_YES)
5577 {
5578 int declare_error = FALSE;
5579
5580 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5581 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5582 &lhs->lhs_type, cctx) == FAIL)
5583 return FAIL;
5584 if (lhs->lhs_dest != dest_local)
5585 {
5586 // Specific kind of variable recognized.
5587 declare_error = is_decl;
5588 }
5589 else
5590 {
5591 int idx;
5592
5593 // No specific kind of variable recognized, just a name.
5594 for (idx = 0; reserved[idx] != NULL; ++idx)
5595 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5596 {
5597 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5598 return FAIL;
5599 }
5600
5601
5602 if (lookup_local(var_start, lhs->lhs_varlen,
5603 &lhs->lhs_local_lvar, cctx) == OK)
5604 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5605 else
5606 {
5607 CLEAR_FIELD(lhs->lhs_arg_lvar);
5608 if (arg_exists(var_start, lhs->lhs_varlen,
5609 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5610 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5611 {
5612 if (is_decl)
5613 {
5614 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5615 return FAIL;
5616 }
5617 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5618 }
5619 }
5620 if (lhs->lhs_lvar != NULL)
5621 {
5622 if (is_decl)
5623 {
5624 semsg(_(e_variable_already_declared), lhs->lhs_name);
5625 return FAIL;
5626 }
5627 }
5628 else
5629 {
5630 int script_namespace = lhs->lhs_varlen > 1
5631 && STRNCMP(var_start, "s:", 2) == 0;
5632 int script_var = (script_namespace
5633 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5634 FALSE, cctx)
5635 : script_var_exists(var_start, lhs->lhs_varlen,
5636 TRUE, cctx)) == OK;
5637 imported_T *import =
5638 find_imported(var_start, lhs->lhs_varlen, cctx);
5639
5640 if (script_namespace || script_var || import != NULL)
5641 {
5642 char_u *rawname = lhs->lhs_name
5643 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5644
5645 if (is_decl)
5646 {
5647 if (script_namespace)
5648 semsg(_(e_cannot_declare_script_variable_in_function),
5649 lhs->lhs_name);
5650 else
5651 semsg(_(e_variable_already_declared_in_script),
5652 lhs->lhs_name);
5653 return FAIL;
5654 }
5655 else if (cctx->ctx_ufunc->uf_script_ctx_version
5656 == SCRIPT_VERSION_VIM9
5657 && script_namespace
5658 && !script_var && import == NULL)
5659 {
5660 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5661 return FAIL;
5662 }
5663
5664 lhs->lhs_dest = dest_script;
5665
5666 // existing script-local variables should have a type
5667 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5668 if (import != NULL)
5669 lhs->lhs_scriptvar_sid = import->imp_sid;
5670 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5671 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005672 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005673 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005674 lhs->lhs_scriptvar_sid, rawname,
5675 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5676 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005677 if (lhs->lhs_scriptvar_idx >= 0)
5678 {
5679 scriptitem_T *si = SCRIPT_ITEM(
5680 lhs->lhs_scriptvar_sid);
5681 svar_T *sv =
5682 ((svar_T *)si->sn_var_vals.ga_data)
5683 + lhs->lhs_scriptvar_idx;
5684 lhs->lhs_type = sv->sv_type;
5685 }
5686 }
5687 }
5688 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5689 == FAIL)
5690 return FAIL;
5691 }
5692 }
5693
5694 if (declare_error)
5695 {
5696 vim9_declare_error(lhs->lhs_name);
5697 return FAIL;
5698 }
5699 }
5700
5701 // handle "a:name" as a name, not index "name" on "a"
5702 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5703 var_end = lhs->lhs_dest_end;
5704
5705 if (lhs->lhs_dest != dest_option)
5706 {
5707 if (is_decl && *var_end == ':')
5708 {
5709 char_u *p;
5710
5711 // parse optional type: "let var: type = expr"
5712 if (!VIM_ISWHITE(var_end[1]))
5713 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005714 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005715 return FAIL;
5716 }
5717 p = skipwhite(var_end + 1);
5718 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5719 if (lhs->lhs_type == NULL)
5720 return FAIL;
5721 lhs->lhs_has_type = TRUE;
5722 }
5723 else if (lhs->lhs_lvar != NULL)
5724 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5725 }
5726
5727 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5728 && lhs->lhs_type->tt_type != VAR_STRING
5729 && lhs->lhs_type->tt_type != VAR_ANY)
5730 {
5731 emsg(_(e_can_only_concatenate_to_string));
5732 return FAIL;
5733 }
5734
5735 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5736 && cctx->ctx_skip != SKIP_YES)
5737 {
5738 if (oplen > 1 && !heredoc)
5739 {
5740 // +=, /=, etc. require an existing variable
5741 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5742 return FAIL;
5743 }
5744 if (!is_decl)
5745 {
5746 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5747 return FAIL;
5748 }
5749
5750 // new local variable
5751 if ((lhs->lhs_type->tt_type == VAR_FUNC
5752 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5753 && var_wrong_func_name(lhs->lhs_name, TRUE))
5754 return FAIL;
5755 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5756 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5757 if (lhs->lhs_lvar == NULL)
5758 return FAIL;
5759 lhs->lhs_new_local = TRUE;
5760 }
5761
5762 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005763 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005764 {
5765 // Something follows after the variable: "var[idx]" or "var.key".
5766 // TODO: should we also handle "->func()" here?
5767 if (is_decl)
5768 {
5769 emsg(_(e_cannot_use_index_when_declaring_variable));
5770 return FAIL;
5771 }
5772
5773 if (var_start[lhs->lhs_varlen] == '['
5774 || var_start[lhs->lhs_varlen] == '.')
5775 {
5776 char_u *after = var_start + lhs->lhs_varlen;
5777 char_u *p;
5778
5779 // Only the last index is used below, if there are others
5780 // before it generate code for the expression. Thus for
5781 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5782 for (;;)
5783 {
5784 p = skip_index(after);
5785 if (*p != '[' && *p != '.')
5786 break;
5787 after = p;
5788 }
5789 if (after > var_start + lhs->lhs_varlen)
5790 {
5791 lhs->lhs_varlen = after - var_start;
5792 lhs->lhs_dest = dest_expr;
5793 // We don't know the type before evaluating the expression,
5794 // use "any" until then.
5795 lhs->lhs_type = &t_any;
5796 }
5797
Bram Moolenaar752fc692021-01-04 21:57:11 +01005798 if (lhs->lhs_type->tt_member == NULL)
5799 lhs->lhs_member_type = &t_any;
5800 else
5801 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5802 }
5803 else
5804 {
5805 semsg("Not supported yet: %s", var_start);
5806 return FAIL;
5807 }
5808 }
5809 return OK;
5810}
5811
5812/*
5813 * Assignment to a list or dict member, or ":unlet" for the item, using the
5814 * information in "lhs".
5815 * Returns OK or FAIL.
5816 */
5817 static int
5818compile_assign_unlet(
5819 char_u *var_start,
5820 lhs_T *lhs,
5821 int is_assign,
5822 type_T *rhs_type,
5823 cctx_T *cctx)
5824{
5825 char_u *p;
5826 int r;
5827 vartype_T dest_type;
5828 size_t varlen = lhs->lhs_varlen;
5829 garray_T *stack = &cctx->ctx_type_stack;
5830
5831 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5832 p = var_start + varlen;
5833 if (*p == '[')
5834 {
5835 p = skipwhite(p + 1);
5836 r = compile_expr0(&p, cctx);
5837 if (r == OK && *skipwhite(p) != ']')
5838 {
5839 // this should not happen
5840 emsg(_(e_missbrac));
5841 r = FAIL;
5842 }
5843 }
5844 else // if (*p == '.')
5845 {
5846 char_u *key_end = to_name_end(p + 1, TRUE);
5847 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5848
5849 r = generate_PUSHS(cctx, key);
5850 }
5851 if (r == FAIL)
5852 return FAIL;
5853
5854 if (lhs->lhs_type == &t_any)
5855 {
5856 // Index on variable of unknown type: check at runtime.
5857 dest_type = VAR_ANY;
5858 }
5859 else
5860 {
5861 dest_type = lhs->lhs_type->tt_type;
5862 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5863 return FAIL;
5864 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005865 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5866 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005867 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005868 }
5869
5870 // Load the dict or list. On the stack we then have:
5871 // - value (for assignment, not for :unlet)
5872 // - index
5873 // - variable
5874 if (lhs->lhs_dest == dest_expr)
5875 {
5876 int c = var_start[varlen];
5877
5878 // Evaluate "ll[expr]" of "ll[expr][idx]"
5879 p = var_start;
5880 var_start[varlen] = NUL;
5881 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5882 {
5883 // this should not happen
5884 emsg(_(e_missbrac));
5885 return FAIL;
5886 }
5887 var_start[varlen] = c;
5888
5889 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5890 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5891 // now we can properly check the type
5892 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005893 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005894 FALSE, FALSE) == FAIL)
5895 return FAIL;
5896 }
5897 else
5898 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5899 lhs->lhs_lvar, lhs->lhs_type);
5900
5901 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5902 {
5903 if (is_assign)
5904 {
5905 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5906
5907 if (isn == NULL)
5908 return FAIL;
5909 isn->isn_arg.vartype = dest_type;
5910 }
5911 else
5912 {
5913 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5914 return FAIL;
5915 }
5916 }
5917 else
5918 {
5919 emsg(_(e_indexable_type_required));
5920 return FAIL;
5921 }
5922
5923 return OK;
5924}
5925
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005926/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005927 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005928 * "let name"
5929 * "var name = expr"
5930 * "final name = expr"
5931 * "const name = expr"
5932 * "name = expr"
5933 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005934 * Return NULL for an error.
5935 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 */
5937 static char_u *
5938compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5939{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005940 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005942 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 char_u *ret = NULL;
5944 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005945 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005946 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005948 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 int oplen = 0;
5951 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005952 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005954 int is_decl = is_decl_command(cmdidx);
5955 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005957 // Skip over the "var" or "[var, var]" to get to any "=".
5958 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5959 if (p == NULL)
5960 return *arg == '[' ? arg : NULL;
5961
5962 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005963 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005964 // TODO: should we allow this, and figure out type inference from list
5965 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005966 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005967 return NULL;
5968 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005969 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 sp = p;
5972 p = skipwhite(p);
5973 op = p;
5974 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975
5976 if (var_count > 0 && oplen == 0)
5977 // can be something like "[1, 2]->func()"
5978 return arg;
5979
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005980 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005982 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005984 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 if (heredoc)
5987 {
5988 list_T *l;
5989 listitem_T *li;
5990
5991 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005992 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005994 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005995 if (l == NULL)
5996 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997
Bram Moolenaar078269b2020-09-21 20:35:55 +02005998 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006000 // Push each line and the create the list.
6001 FOR_ALL_LIST_ITEMS(l, li)
6002 {
6003 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6004 li->li_tv.vval.v_string = NULL;
6005 }
6006 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 list_free(l);
6009 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006010 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006012 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006014 char_u *wp;
6015
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006016 // for "[var, var] = expr" evaluate the expression here, loop over the
6017 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006018 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006019
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006020 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006021 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006022 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006023 if (compile_expr0(&p, cctx) == FAIL)
6024 return NULL;
6025 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006027 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006029 type_T *stacktype;
6030
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006031 stacktype = stack->ga_len == 0 ? &t_void
6032 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006033 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006035 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006036 goto theend;
6037 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006038 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006039 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006040 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006041 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006042 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6043 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006044 if (stacktype->tt_member != NULL)
6045 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006046 }
6047 }
6048
6049 /*
6050 * Loop over variables in "[var, var] = expr".
6051 * For "var = expr" and "let var: type" this is done only once.
6052 */
6053 if (var_count > 0)
6054 var_start = skipwhite(arg + 1); // skip over the "["
6055 else
6056 var_start = arg;
6057 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6058 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006059 int instr_count = -1;
6060
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 vim_free(lhs.lhs_name);
6062
6063 /*
6064 * Figure out the LHS type and other properties.
6065 */
6066 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6067 goto theend;
6068
6069 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006070 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006071 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006072 goto theend;
6073 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006074 if (!is_decl && lhs.lhs_lvar != NULL
6075 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006076 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006077 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006078 goto theend;
6079 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006080
6081 if (!heredoc)
6082 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006083 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006085 if (oplen > 0 && var_count == 0)
6086 {
6087 // skip over the "=" and the expression
6088 p = skipwhite(op + oplen);
6089 compile_expr0(&p, cctx);
6090 }
6091 }
6092 else if (oplen > 0)
6093 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006094 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006095 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006096
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006097 // For "var = expr" evaluate the expression.
6098 if (var_count == 0)
6099 {
6100 int r;
6101
6102 // for "+=", "*=", "..=" etc. first load the current value
6103 if (*op != '=')
6104 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006105 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6106 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006107
Bram Moolenaar752fc692021-01-04 21:57:11 +01006108 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006109 {
6110 // TODO: get member from list or dict
6111 emsg("Index with operation not supported yet");
6112 goto theend;
6113 }
6114 }
6115
6116 // Compile the expression. Temporarily hide the new local
6117 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006119 --cctx->ctx_locals.ga_len;
6120 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006121 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006122 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006123 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006124 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006125 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006126 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006127 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006128 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006129 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006130 ++cctx->ctx_locals.ga_len;
6131 if (r == FAIL)
6132 goto theend;
6133 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006134 else if (semicolon && var_idx == var_count - 1)
6135 {
6136 // For "[var; var] = expr" get the rest of the list
6137 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6138 goto theend;
6139 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006140 else
6141 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006142 // For "[var, var] = expr" get the "var_idx" item from the
6143 // list.
6144 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006145 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006146 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006147
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006148 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006149 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006150 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006151 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006152 if ((rhs_type->tt_type == VAR_FUNC
6153 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006154 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006155 goto theend;
6156
Bram Moolenaar752fc692021-01-04 21:57:11 +01006157 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006158 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006159 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006160 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006161 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006162 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006163 }
6164 else
6165 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006166 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006167 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006168 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006170 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006171 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006172 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006173 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006174 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006177 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006178 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006179 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006180 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006181
Bram Moolenaar71abe482020-09-14 22:28:30 +02006182 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006183 if (lhs.lhs_has_index)
6184 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006185 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006186 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006187 goto theend;
6188 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006189 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006190 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006191 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006192 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006193 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006194 else if (cmdidx == CMD_final)
6195 {
6196 emsg(_(e_final_requires_a_value));
6197 goto theend;
6198 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006199 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006200 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006201 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006202 goto theend;
6203 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006204 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006205 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006206 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006207 goto theend;
6208 }
6209 else
6210 {
6211 // variables are always initialized
6212 if (ga_grow(instr, 1) == FAIL)
6213 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006214 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006215 {
6216 case VAR_BOOL:
6217 generate_PUSHBOOL(cctx, VVAL_FALSE);
6218 break;
6219 case VAR_FLOAT:
6220#ifdef FEAT_FLOAT
6221 generate_PUSHF(cctx, 0.0);
6222#endif
6223 break;
6224 case VAR_STRING:
6225 generate_PUSHS(cctx, NULL);
6226 break;
6227 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006228 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006229 break;
6230 case VAR_FUNC:
6231 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6232 break;
6233 case VAR_LIST:
6234 generate_NEWLIST(cctx, 0);
6235 break;
6236 case VAR_DICT:
6237 generate_NEWDICT(cctx, 0);
6238 break;
6239 case VAR_JOB:
6240 generate_PUSHJOB(cctx, NULL);
6241 break;
6242 case VAR_CHANNEL:
6243 generate_PUSHCHANNEL(cctx, NULL);
6244 break;
6245 case VAR_NUMBER:
6246 case VAR_UNKNOWN:
6247 case VAR_ANY:
6248 case VAR_PARTIAL:
6249 case VAR_VOID:
6250 case VAR_SPECIAL: // cannot happen
6251 generate_PUSHNR(cctx, 0);
6252 break;
6253 }
6254 }
6255 if (var_count == 0)
6256 end = p;
6257 }
6258
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006259 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006260 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006261 break;
6262
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006263 if (oplen > 0 && *op != '=')
6264 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006265 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006266 type_T *stacktype;
6267
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006268 if (*op == '.')
6269 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006270 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006271 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006272 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006273 if (
6274#ifdef FEAT_FLOAT
6275 // If variable is float operation with number is OK.
6276 !(expected == &t_float && stacktype == &t_number) &&
6277#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006278 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006279 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006280 goto theend;
6281
6282 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006283 {
6284 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6285 goto theend;
6286 }
6287 else if (*op == '+')
6288 {
6289 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006290 operator_type(lhs.lhs_member_type, stacktype),
6291 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006292 goto theend;
6293 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006294 else if (generate_two_op(cctx, op) == FAIL)
6295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297
Bram Moolenaar752fc692021-01-04 21:57:11 +01006298 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006299 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006300 // Use the info in "lhs" to store the value at the index in the
6301 // list or dict.
6302 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6303 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006305 }
6306 else
6307 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6309 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006310 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006311 generate_LOCKCONST(cctx);
6312
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006313 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006314 && (lhs.lhs_type->tt_type == VAR_DICT
6315 || lhs.lhs_type->tt_type == VAR_LIST)
6316 && lhs.lhs_type->tt_member != NULL
6317 && lhs.lhs_type->tt_member != &t_any
6318 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006319 // Set the type in the list or dict, so that it can be checked,
6320 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006321 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006322
Bram Moolenaar752fc692021-01-04 21:57:11 +01006323 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006324 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006325 if (generate_store_var(cctx, lhs.lhs_dest,
6326 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6327 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6328 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006329 goto theend;
6330 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006332 {
6333 isn_T *isn = ((isn_T *)instr->ga_data)
6334 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006335
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006336 // optimization: turn "var = 123" from ISN_PUSHNR +
6337 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006338 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006339 && instr->ga_len == instr_count + 1
6340 && isn->isn_type == ISN_PUSHNR)
6341 {
6342 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006343
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006344 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006345 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006346 isn->isn_arg.storenr.stnr_val = val;
6347 if (stack->ga_len > 0)
6348 --stack->ga_len;
6349 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006350 else if (lhs.lhs_lvar->lv_from_outer > 0)
6351 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6352 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006353 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006354 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006355 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006356 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006357
6358 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006359 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006361
6362 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006363 if (var_count > 0 && !semicolon)
6364 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006365 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006366 goto theend;
6367 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006368
Bram Moolenaarb2097502020-07-19 17:17:02 +02006369 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006370
6371theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006372 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006373 return ret;
6374}
6375
6376/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006377 * Check for an assignment at "eap->cmd", compile it if found.
6378 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6379 */
6380 static int
6381may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6382{
6383 char_u *pskip;
6384 char_u *p;
6385
6386 // Assuming the command starts with a variable or function name,
6387 // find what follows.
6388 // Skip over "var.member", "var[idx]" and the like.
6389 // Also "&opt = val", "$ENV = val" and "@r = val".
6390 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6391 ? eap->cmd + 1 : eap->cmd;
6392 p = to_name_end(pskip, TRUE);
6393 if (p > eap->cmd && *p != NUL)
6394 {
6395 char_u *var_end;
6396 int oplen;
6397 int heredoc;
6398
6399 if (eap->cmd[0] == '@')
6400 var_end = eap->cmd + 2;
6401 else
6402 var_end = find_name_end(pskip, NULL, NULL,
6403 FNE_CHECK_START | FNE_INCL_BR);
6404 oplen = assignment_len(skipwhite(var_end), &heredoc);
6405 if (oplen > 0)
6406 {
6407 size_t len = p - eap->cmd;
6408
6409 // Recognize an assignment if we recognize the variable
6410 // name:
6411 // "g:var = expr"
6412 // "local = expr" where "local" is a local var.
6413 // "script = expr" where "script" is a script-local var.
6414 // "import = expr" where "import" is an imported var
6415 // "&opt = expr"
6416 // "$ENV = expr"
6417 // "@r = expr"
6418 if (*eap->cmd == '&'
6419 || *eap->cmd == '$'
6420 || *eap->cmd == '@'
6421 || ((len) > 2 && eap->cmd[1] == ':')
6422 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6423 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6424 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6425 || find_imported(eap->cmd, len, cctx) != NULL)
6426 {
6427 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6428 if (*line == NULL || *line == eap->cmd)
6429 return FAIL;
6430 return OK;
6431 }
6432 }
6433 }
6434
6435 if (*eap->cmd == '[')
6436 {
6437 // [var, var] = expr
6438 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6439 if (*line == NULL)
6440 return FAIL;
6441 if (*line != eap->cmd)
6442 return OK;
6443 }
6444 return NOTDONE;
6445}
6446
6447/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006448 * Check if "name" can be "unlet".
6449 */
6450 int
6451check_vim9_unlet(char_u *name)
6452{
6453 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6454 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006455 // "unlet s:var" is allowed in legacy script.
6456 if (*name == 's' && !script_is_vim9())
6457 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006458 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006459 return FAIL;
6460 }
6461 return OK;
6462}
6463
6464/*
6465 * Callback passed to ex_unletlock().
6466 */
6467 static int
6468compile_unlet(
6469 lval_T *lvp,
6470 char_u *name_end,
6471 exarg_T *eap,
6472 int deep UNUSED,
6473 void *coookie)
6474{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006475 cctx_T *cctx = coookie;
6476 char_u *p = lvp->ll_name;
6477 int cc = *name_end;
6478 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006479
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 if (cctx->ctx_skip == SKIP_YES)
6481 return OK;
6482
6483 *name_end = NUL;
6484 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006485 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006486 // :unlet $ENV_VAR
6487 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6488 }
6489 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6490 {
6491 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006492
Bram Moolenaar752fc692021-01-04 21:57:11 +01006493 // This is similar to assigning: lookup the list/dict, compile the
6494 // idx/key. Then instead of storing the value unlet the item.
6495 // unlet {list}[idx]
6496 // unlet {dict}[key] dict.key
6497 //
6498 // Figure out the LHS type and other properties.
6499 //
6500 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6501
6502 // : unlet an indexed item
6503 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006504 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 iemsg("called compile_lhs() without an index");
6506 ret = FAIL;
6507 }
6508 else
6509 {
6510 // Use the info in "lhs" to unlet the item at the index in the
6511 // list or dict.
6512 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006513 }
6514
Bram Moolenaar752fc692021-01-04 21:57:11 +01006515 vim_free(lhs.lhs_name);
6516 }
6517 else if (check_vim9_unlet(p) == FAIL)
6518 {
6519 ret = FAIL;
6520 }
6521 else
6522 {
6523 // Normal name. Only supports g:, w:, t: and b: namespaces.
6524 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006525 }
6526
Bram Moolenaar752fc692021-01-04 21:57:11 +01006527 *name_end = cc;
6528 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006529}
6530
6531/*
6532 * compile "unlet var", "lock var" and "unlock var"
6533 * "arg" points to "var".
6534 */
6535 static char_u *
6536compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6537{
6538 char_u *p = arg;
6539
6540 if (eap->cmdidx != CMD_unlet)
6541 {
6542 emsg("Sorry, :lock and unlock not implemented yet");
6543 return NULL;
6544 }
6545
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006546 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6547 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006548 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6549}
6550
6551/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552 * Compile an :import command.
6553 */
6554 static char_u *
6555compile_import(char_u *arg, cctx_T *cctx)
6556{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006557 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006558}
6559
6560/*
6561 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6562 */
6563 static int
6564compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6565{
6566 garray_T *instr = &cctx->ctx_instr;
6567 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6568
6569 if (endlabel == NULL)
6570 return FAIL;
6571 endlabel->el_next = *el;
6572 *el = endlabel;
6573 endlabel->el_end_label = instr->ga_len;
6574
6575 generate_JUMP(cctx, when, 0);
6576 return OK;
6577}
6578
6579 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006580compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006581{
6582 garray_T *instr = &cctx->ctx_instr;
6583
6584 while (*el != NULL)
6585 {
6586 endlabel_T *cur = (*el);
6587 isn_T *isn;
6588
6589 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006590 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006591 *el = cur->el_next;
6592 vim_free(cur);
6593 }
6594}
6595
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006596 static void
6597compile_free_jump_to_end(endlabel_T **el)
6598{
6599 while (*el != NULL)
6600 {
6601 endlabel_T *cur = (*el);
6602
6603 *el = cur->el_next;
6604 vim_free(cur);
6605 }
6606}
6607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006608/*
6609 * Create a new scope and set up the generic items.
6610 */
6611 static scope_T *
6612new_scope(cctx_T *cctx, scopetype_T type)
6613{
6614 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6615
6616 if (scope == NULL)
6617 return NULL;
6618 scope->se_outer = cctx->ctx_scope;
6619 cctx->ctx_scope = scope;
6620 scope->se_type = type;
6621 scope->se_local_count = cctx->ctx_locals.ga_len;
6622 return scope;
6623}
6624
6625/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006626 * Free the current scope and go back to the outer scope.
6627 */
6628 static void
6629drop_scope(cctx_T *cctx)
6630{
6631 scope_T *scope = cctx->ctx_scope;
6632
6633 if (scope == NULL)
6634 {
6635 iemsg("calling drop_scope() without a scope");
6636 return;
6637 }
6638 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006639 switch (scope->se_type)
6640 {
6641 case IF_SCOPE:
6642 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6643 case FOR_SCOPE:
6644 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6645 case WHILE_SCOPE:
6646 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6647 case TRY_SCOPE:
6648 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6649 case NO_SCOPE:
6650 case BLOCK_SCOPE:
6651 break;
6652 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006653 vim_free(scope);
6654}
6655
6656/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006657 * compile "if expr"
6658 *
6659 * "if expr" Produces instructions:
6660 * EVAL expr Push result of "expr"
6661 * JUMP_IF_FALSE end
6662 * ... body ...
6663 * end:
6664 *
6665 * "if expr | else" Produces instructions:
6666 * EVAL expr Push result of "expr"
6667 * JUMP_IF_FALSE else
6668 * ... body ...
6669 * JUMP_ALWAYS end
6670 * else:
6671 * ... body ...
6672 * end:
6673 *
6674 * "if expr1 | elseif expr2 | else" Produces instructions:
6675 * EVAL expr Push result of "expr"
6676 * JUMP_IF_FALSE elseif
6677 * ... body ...
6678 * JUMP_ALWAYS end
6679 * elseif:
6680 * EVAL expr Push result of "expr"
6681 * JUMP_IF_FALSE else
6682 * ... body ...
6683 * JUMP_ALWAYS end
6684 * else:
6685 * ... body ...
6686 * end:
6687 */
6688 static char_u *
6689compile_if(char_u *arg, cctx_T *cctx)
6690{
6691 char_u *p = arg;
6692 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006693 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006694 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006695 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006696 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006697
Bram Moolenaara5565e42020-05-09 15:44:01 +02006698 CLEAR_FIELD(ppconst);
6699 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006700 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006701 clear_ppconst(&ppconst);
6702 return NULL;
6703 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006704 if (cctx->ctx_skip == SKIP_YES)
6705 clear_ppconst(&ppconst);
6706 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006707 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006708 int error = FALSE;
6709 int v;
6710
Bram Moolenaara5565e42020-05-09 15:44:01 +02006711 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006712 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006713 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006714 if (error)
6715 return NULL;
6716 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006717 }
6718 else
6719 {
6720 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006721 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006722 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006723 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006724 if (bool_on_stack(cctx) == FAIL)
6725 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727
6728 scope = new_scope(cctx, IF_SCOPE);
6729 if (scope == NULL)
6730 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006731 scope->se_skip_save = skip_save;
6732 // "is_had_return" will be reset if any block does not end in :return
6733 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006734
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006735 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006736 {
6737 // "where" is set when ":elseif", "else" or ":endif" is found
6738 scope->se_u.se_if.is_if_label = instr->ga_len;
6739 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6740 }
6741 else
6742 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006743
Bram Moolenaarced68a02021-01-24 17:53:47 +01006744#ifdef FEAT_PROFILE
6745 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6746 && skip_save != SKIP_YES)
6747 {
6748 // generated a profile start, need to generate a profile end, since it
6749 // won't be done after returning
6750 cctx->ctx_skip = SKIP_NOT;
6751 generate_instr(cctx, ISN_PROF_END);
6752 cctx->ctx_skip = SKIP_YES;
6753 }
6754#endif
6755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756 return p;
6757}
6758
6759 static char_u *
6760compile_elseif(char_u *arg, cctx_T *cctx)
6761{
6762 char_u *p = arg;
6763 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006764 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006765 isn_T *isn;
6766 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006767 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006768 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769
6770 if (scope == NULL || scope->se_type != IF_SCOPE)
6771 {
6772 emsg(_(e_elseif_without_if));
6773 return NULL;
6774 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006775 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006776 if (!cctx->ctx_had_return)
6777 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778
Bram Moolenaarced68a02021-01-24 17:53:47 +01006779 if (cctx->ctx_skip == SKIP_NOT)
6780 {
6781 // previous block was executed, this one and following will not
6782 cctx->ctx_skip = SKIP_YES;
6783 scope->se_u.se_if.is_seen_skip_not = TRUE;
6784 }
6785 if (scope->se_u.se_if.is_seen_skip_not)
6786 {
6787 // A previous block was executed, skip over expression and bail out.
6788 // Do not count the "elseif" for profiling.
6789#ifdef FEAT_PROFILE
6790 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6791 .isn_type == ISN_PROF_START)
6792 --instr->ga_len;
6793#endif
6794 skip_expr_cctx(&p, cctx);
6795 return p;
6796 }
6797
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006798 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006799 {
6800 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006802 return NULL;
6803 // previous "if" or "elseif" jumps here
6804 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6805 isn->isn_arg.jump.jump_where = instr->ga_len;
6806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006807
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006808 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006809 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006810 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006811 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006812 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006813#ifdef FEAT_PROFILE
6814 if (cctx->ctx_profiling)
6815 {
6816 // the previous block was skipped, need to profile this line
6817 generate_instr(cctx, ISN_PROF_START);
6818 instr_count = instr->ga_len;
6819 }
6820#endif
6821 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006822 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006823 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006824 clear_ppconst(&ppconst);
6825 return NULL;
6826 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006827 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006828 if (scope->se_skip_save == SKIP_YES)
6829 clear_ppconst(&ppconst);
6830 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006831 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006832 int error = FALSE;
6833 int v;
6834
Bram Moolenaar7f141552020-05-09 17:35:53 +02006835 // The expression results in a constant.
6836 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006837 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6838 if (error)
6839 return NULL;
6840 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006841 clear_ppconst(&ppconst);
6842 scope->se_u.se_if.is_if_label = -1;
6843 }
6844 else
6845 {
6846 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006847 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006848 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006849 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006850 if (bool_on_stack(cctx) == FAIL)
6851 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006852
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006853 // "where" is set when ":elseif", "else" or ":endif" is found
6854 scope->se_u.se_if.is_if_label = instr->ga_len;
6855 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
6858 return p;
6859}
6860
6861 static char_u *
6862compile_else(char_u *arg, cctx_T *cctx)
6863{
6864 char_u *p = arg;
6865 garray_T *instr = &cctx->ctx_instr;
6866 isn_T *isn;
6867 scope_T *scope = cctx->ctx_scope;
6868
6869 if (scope == NULL || scope->se_type != IF_SCOPE)
6870 {
6871 emsg(_(e_else_without_if));
6872 return NULL;
6873 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006874 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006875 if (!cctx->ctx_had_return)
6876 scope->se_u.se_if.is_had_return = FALSE;
6877 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878
Bram Moolenaarced68a02021-01-24 17:53:47 +01006879#ifdef FEAT_PROFILE
6880 if (cctx->ctx_profiling)
6881 {
6882 if (cctx->ctx_skip == SKIP_NOT
6883 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6884 .isn_type == ISN_PROF_START)
6885 // the previous block was executed, do not count "else" for profiling
6886 --instr->ga_len;
6887 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
6888 {
6889 // the previous block was not executed, this one will, do count the
6890 // "else" for profiling
6891 cctx->ctx_skip = SKIP_NOT;
6892 generate_instr(cctx, ISN_PROF_END);
6893 generate_instr(cctx, ISN_PROF_START);
6894 cctx->ctx_skip = SKIP_YES;
6895 }
6896 }
6897#endif
6898
6899 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006900 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006901 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006902 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006903 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006904 if (!cctx->ctx_had_return
6905 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6906 JUMP_ALWAYS, cctx) == FAIL)
6907 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006908 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006909
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006910 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006911 {
6912 if (scope->se_u.se_if.is_if_label >= 0)
6913 {
6914 // previous "if" or "elseif" jumps here
6915 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6916 isn->isn_arg.jump.jump_where = instr->ga_len;
6917 scope->se_u.se_if.is_if_label = -1;
6918 }
6919 }
6920
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006921 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006922 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6923 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006924
6925 return p;
6926}
6927
6928 static char_u *
6929compile_endif(char_u *arg, cctx_T *cctx)
6930{
6931 scope_T *scope = cctx->ctx_scope;
6932 ifscope_T *ifscope;
6933 garray_T *instr = &cctx->ctx_instr;
6934 isn_T *isn;
6935
6936 if (scope == NULL || scope->se_type != IF_SCOPE)
6937 {
6938 emsg(_(e_endif_without_if));
6939 return NULL;
6940 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006941 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006942 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006943 if (!cctx->ctx_had_return)
6944 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006945
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006946 if (scope->se_u.se_if.is_if_label >= 0)
6947 {
6948 // previous "if" or "elseif" jumps here
6949 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6950 isn->isn_arg.jump.jump_where = instr->ga_len;
6951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006952 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006953 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01006954
6955#ifdef FEAT_PROFILE
6956 // even when skipping we count the endif as executed, unless the block it's
6957 // in is skipped
6958 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6959 && scope->se_skip_save != SKIP_YES)
6960 {
6961 cctx->ctx_skip = SKIP_NOT;
6962 generate_instr(cctx, ISN_PROF_START);
6963 }
6964#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02006965 cctx->ctx_skip = scope->se_skip_save;
6966
6967 // If all the blocks end in :return and there is an :else then the
6968 // had_return flag is set.
6969 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006971 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 return arg;
6973}
6974
6975/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006976 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 *
6978 * Produces instructions:
6979 * PUSHNR -1
6980 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006981 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6983 * - if beyond end, jump to "end"
6984 * - otherwise get item from list and push it
6985 * STORE var Store item in "var"
6986 * ... body ...
6987 * JUMP top Jump back to repeat
6988 * end: DROP Drop the result of "expr"
6989 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006990 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6991 * UNPACK 2 Split item in 2
6992 * STORE var1 Store item in "var1"
6993 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 */
6995 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006996compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006998 char_u *arg;
6999 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007000 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007002 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007003 int var_count = 0;
7004 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 size_t varlen;
7006 garray_T *instr = &cctx->ctx_instr;
7007 garray_T *stack = &cctx->ctx_type_stack;
7008 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007009 lvar_T *loop_lvar; // loop iteration variable
7010 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007011 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007012 type_T *item_type = &t_any;
7013 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014
Bram Moolenaar792f7862020-11-23 08:31:18 +01007015 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007016 if (p == NULL)
7017 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007018 if (var_count == 0)
7019 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020
7021 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007022 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007023 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7024 return NULL;
7025 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007026 {
7027 emsg(_(e_missing_in));
7028 return NULL;
7029 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007030 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007031 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7032 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 scope = new_scope(cctx, FOR_SCOPE);
7035 if (scope == NULL)
7036 return NULL;
7037
Bram Moolenaar792f7862020-11-23 08:31:18 +01007038 // Reserve a variable to store the loop iteration counter and initialize it
7039 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007040 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7041 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007042 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007043 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007044 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007045 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007046 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007047 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048
7049 // compile "expr", it remains on the stack until "endfor"
7050 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007051 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007052 {
7053 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007055 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007056 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007057
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007058 // Now that we know the type of "var", check that it is a list, now or at
7059 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007061 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007063 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 return NULL;
7065 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007066
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007067 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007068 {
7069 if (var_count == 1)
7070 item_type = vartype->tt_member;
7071 else if (vartype->tt_member->tt_type == VAR_LIST
7072 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7073 item_type = vartype->tt_member->tt_member;
7074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075
7076 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007077 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007078 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079
Bram Moolenaar792f7862020-11-23 08:31:18 +01007080 arg = arg_start;
7081 if (var_count > 1)
7082 {
7083 generate_UNPACK(cctx, var_count, semicolon);
7084 arg = skipwhite(arg + 1); // skip white after '['
7085
7086 // the list item is replaced by a number of items
7087 if (ga_grow(stack, var_count - 1) == FAIL)
7088 {
7089 drop_scope(cctx);
7090 return NULL;
7091 }
7092 --stack->ga_len;
7093 for (idx = 0; idx < var_count; ++idx)
7094 {
7095 ((type_T **)stack->ga_data)[stack->ga_len] =
7096 (semicolon && idx == 0) ? vartype : item_type;
7097 ++stack->ga_len;
7098 }
7099 }
7100
7101 for (idx = 0; idx < var_count; ++idx)
7102 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007103 assign_dest_T dest = dest_local;
7104 int opt_flags = 0;
7105 int vimvaridx = -1;
7106 type_T *type = &t_any;
7107
7108 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007109 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007110 name = vim_strnsave(arg, varlen);
7111 if (name == NULL)
7112 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007113
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007114 // TODO: script var not supported?
7115 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7116 &vimvaridx, &type, cctx) == FAIL)
7117 goto failed;
7118 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007119 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007120 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7121 0, 0, type, name) == FAIL)
7122 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007123 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007124 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007125 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007126 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007127 {
7128 semsg(_(e_variable_already_declared), arg);
7129 goto failed;
7130 }
7131
Bram Moolenaarea870692020-12-02 14:24:30 +01007132 if (STRNCMP(name, "s:", 2) == 0)
7133 {
7134 semsg(_(e_cannot_declare_script_variable_in_function), name);
7135 goto failed;
7136 }
7137
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007138 // Reserve a variable to store "var".
7139 // TODO: check for type
7140 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7141 if (var_lvar == NULL)
7142 // out of memory or used as an argument
7143 goto failed;
7144
7145 if (semicolon && idx == var_count - 1)
7146 var_lvar->lv_type = vartype;
7147 else
7148 var_lvar->lv_type = item_type;
7149 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7150 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007151
Bram Moolenaar036d0712021-01-17 20:23:38 +01007152 if (*p == ':')
7153 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007154 if (*p == ',' || *p == ';')
7155 ++p;
7156 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007157 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007158 }
7159
7160 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007161
7162failed:
7163 vim_free(name);
7164 drop_scope(cctx);
7165 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166}
7167
7168/*
7169 * compile "endfor"
7170 */
7171 static char_u *
7172compile_endfor(char_u *arg, cctx_T *cctx)
7173{
7174 garray_T *instr = &cctx->ctx_instr;
7175 scope_T *scope = cctx->ctx_scope;
7176 forscope_T *forscope;
7177 isn_T *isn;
7178
7179 if (scope == NULL || scope->se_type != FOR_SCOPE)
7180 {
7181 emsg(_(e_for));
7182 return NULL;
7183 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007184 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007185 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007186 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007187
7188 // At end of ":for" scope jump back to the FOR instruction.
7189 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7190
7191 // Fill in the "end" label in the FOR statement so it can jump here
7192 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7193 isn->isn_arg.forloop.for_end = instr->ga_len;
7194
7195 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007196 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197
7198 // Below the ":for" scope drop the "expr" list from the stack.
7199 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7200 return NULL;
7201
7202 vim_free(scope);
7203
7204 return arg;
7205}
7206
7207/*
7208 * compile "while expr"
7209 *
7210 * Produces instructions:
7211 * top: EVAL expr Push result of "expr"
7212 * JUMP_IF_FALSE end jump if false
7213 * ... body ...
7214 * JUMP top Jump back to repeat
7215 * end:
7216 *
7217 */
7218 static char_u *
7219compile_while(char_u *arg, cctx_T *cctx)
7220{
7221 char_u *p = arg;
7222 garray_T *instr = &cctx->ctx_instr;
7223 scope_T *scope;
7224
7225 scope = new_scope(cctx, WHILE_SCOPE);
7226 if (scope == NULL)
7227 return NULL;
7228
Bram Moolenaarb2049902021-01-24 12:53:53 +01007229 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007230 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007231#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007232 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7233 .isn_type == ISN_PROF_START)
7234 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007235#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007236
7237 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007238 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007239 return NULL;
7240
Bram Moolenaar13106602020-10-04 16:06:05 +02007241 if (bool_on_stack(cctx) == FAIL)
7242 return FAIL;
7243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007244 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007245 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007246 JUMP_IF_FALSE, cctx) == FAIL)
7247 return FAIL;
7248
7249 return p;
7250}
7251
7252/*
7253 * compile "endwhile"
7254 */
7255 static char_u *
7256compile_endwhile(char_u *arg, cctx_T *cctx)
7257{
7258 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007259 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260
7261 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7262 {
7263 emsg(_(e_while));
7264 return NULL;
7265 }
7266 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007267 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007268
Bram Moolenaarf002a412021-01-24 13:34:18 +01007269#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007270 // count the endwhile before jumping
7271 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007272#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007275 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276
7277 // Fill in the "end" label in the WHILE statement so it can jump here.
7278 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007279 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7280 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281
7282 vim_free(scope);
7283
7284 return arg;
7285}
7286
7287/*
7288 * compile "continue"
7289 */
7290 static char_u *
7291compile_continue(char_u *arg, cctx_T *cctx)
7292{
7293 scope_T *scope = cctx->ctx_scope;
7294
7295 for (;;)
7296 {
7297 if (scope == NULL)
7298 {
7299 emsg(_(e_continue));
7300 return NULL;
7301 }
7302 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7303 break;
7304 scope = scope->se_outer;
7305 }
7306
7307 // Jump back to the FOR or WHILE instruction.
7308 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007309 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7310 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007311 return arg;
7312}
7313
7314/*
7315 * compile "break"
7316 */
7317 static char_u *
7318compile_break(char_u *arg, cctx_T *cctx)
7319{
7320 scope_T *scope = cctx->ctx_scope;
7321 endlabel_T **el;
7322
7323 for (;;)
7324 {
7325 if (scope == NULL)
7326 {
7327 emsg(_(e_break));
7328 return NULL;
7329 }
7330 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7331 break;
7332 scope = scope->se_outer;
7333 }
7334
7335 // Jump to the end of the FOR or WHILE loop.
7336 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007337 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007339 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007340 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7341 return FAIL;
7342
7343 return arg;
7344}
7345
7346/*
7347 * compile "{" start of block
7348 */
7349 static char_u *
7350compile_block(char_u *arg, cctx_T *cctx)
7351{
7352 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7353 return NULL;
7354 return skipwhite(arg + 1);
7355}
7356
7357/*
7358 * compile end of block: drop one scope
7359 */
7360 static void
7361compile_endblock(cctx_T *cctx)
7362{
7363 scope_T *scope = cctx->ctx_scope;
7364
7365 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007366 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 vim_free(scope);
7368}
7369
7370/*
7371 * compile "try"
7372 * Creates a new scope for the try-endtry, pointing to the first catch and
7373 * finally.
7374 * Creates another scope for the "try" block itself.
7375 * TRY instruction sets up exception handling at runtime.
7376 *
7377 * "try"
7378 * TRY -> catch1, -> finally push trystack entry
7379 * ... try block
7380 * "throw {exception}"
7381 * EVAL {exception}
7382 * THROW create exception
7383 * ... try block
7384 * " catch {expr}"
7385 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007386 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 * EVAL {expr}
7388 * MATCH
7389 * JUMP nomatch -> catch2
7390 * CATCH remove exception
7391 * ... catch block
7392 * " catch"
7393 * JUMP -> finally
7394 * catch2: CATCH remove exception
7395 * ... catch block
7396 * " finally"
7397 * finally:
7398 * ... finally block
7399 * " endtry"
7400 * ENDTRY pop trystack entry, may rethrow
7401 */
7402 static char_u *
7403compile_try(char_u *arg, cctx_T *cctx)
7404{
7405 garray_T *instr = &cctx->ctx_instr;
7406 scope_T *try_scope;
7407 scope_T *scope;
7408
7409 // scope that holds the jumps that go to catch/finally/endtry
7410 try_scope = new_scope(cctx, TRY_SCOPE);
7411 if (try_scope == NULL)
7412 return NULL;
7413
Bram Moolenaar69f70502021-01-01 16:10:46 +01007414 if (cctx->ctx_skip != SKIP_YES)
7415 {
7416 // "catch" is set when the first ":catch" is found.
7417 // "finally" is set when ":finally" or ":endtry" is found
7418 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7419 if (generate_instr(cctx, ISN_TRY) == NULL)
7420 return NULL;
7421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422
7423 // scope for the try block itself
7424 scope = new_scope(cctx, BLOCK_SCOPE);
7425 if (scope == NULL)
7426 return NULL;
7427
7428 return arg;
7429}
7430
7431/*
7432 * compile "catch {expr}"
7433 */
7434 static char_u *
7435compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7436{
7437 scope_T *scope = cctx->ctx_scope;
7438 garray_T *instr = &cctx->ctx_instr;
7439 char_u *p;
7440 isn_T *isn;
7441
7442 // end block scope from :try or :catch
7443 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7444 compile_endblock(cctx);
7445 scope = cctx->ctx_scope;
7446
7447 // Error if not in a :try scope
7448 if (scope == NULL || scope->se_type != TRY_SCOPE)
7449 {
7450 emsg(_(e_catch));
7451 return NULL;
7452 }
7453
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007454 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007456 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 return NULL;
7458 }
7459
Bram Moolenaar69f70502021-01-01 16:10:46 +01007460 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007462#ifdef FEAT_PROFILE
7463 // the profile-start should be after the jump
7464 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7465 .isn_type == ISN_PROF_START)
7466 --instr->ga_len;
7467#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007468 // Jump from end of previous block to :finally or :endtry
7469 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7470 JUMP_ALWAYS, cctx) == FAIL)
7471 return NULL;
7472
7473 // End :try or :catch scope: set value in ISN_TRY instruction
7474 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7475 if (isn->isn_arg.try.try_catch == 0)
7476 isn->isn_arg.try.try_catch = instr->ga_len;
7477 if (scope->se_u.se_try.ts_catch_label != 0)
7478 {
7479 // Previous catch without match jumps here
7480 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7481 isn->isn_arg.jump.jump_where = instr->ga_len;
7482 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007483#ifdef FEAT_PROFILE
7484 if (cctx->ctx_profiling)
7485 {
7486 // a "throw" that jumps here needs to be counted
7487 generate_instr(cctx, ISN_PROF_END);
7488 // the "catch" is also counted
7489 generate_instr(cctx, ISN_PROF_START);
7490 }
7491#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 }
7493
7494 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007495 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007497 scope->se_u.se_try.ts_caught_all = TRUE;
7498 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007499 }
7500 else
7501 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007502 char_u *end;
7503 char_u *pat;
7504 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007505 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007506 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007507
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508 // Push v:exception, push {expr} and MATCH
7509 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7510
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007511 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007512 if (*end != *p)
7513 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007514 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007515 vim_free(tofree);
7516 return FAIL;
7517 }
7518 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007519 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007520 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007521 len = (int)(end - tofree);
7522 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007523 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007524 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007525 if (pat == NULL)
7526 return FAIL;
7527 if (generate_PUSHS(cctx, pat) == FAIL)
7528 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007530 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7531 return NULL;
7532
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007533 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7535 return NULL;
7536 }
7537
Bram Moolenaar69f70502021-01-01 16:10:46 +01007538 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539 return NULL;
7540
7541 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7542 return NULL;
7543 return p;
7544}
7545
7546 static char_u *
7547compile_finally(char_u *arg, cctx_T *cctx)
7548{
7549 scope_T *scope = cctx->ctx_scope;
7550 garray_T *instr = &cctx->ctx_instr;
7551 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007552 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553
7554 // end block scope from :try or :catch
7555 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7556 compile_endblock(cctx);
7557 scope = cctx->ctx_scope;
7558
7559 // Error if not in a :try scope
7560 if (scope == NULL || scope->se_type != TRY_SCOPE)
7561 {
7562 emsg(_(e_finally));
7563 return NULL;
7564 }
7565
7566 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007567 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 if (isn->isn_arg.try.try_finally != 0)
7569 {
7570 emsg(_(e_finally_dup));
7571 return NULL;
7572 }
7573
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007574 this_instr = instr->ga_len;
7575#ifdef FEAT_PROFILE
7576 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7577 .isn_type == ISN_PROF_START)
7578 // jump to the profile start of the "finally"
7579 --this_instr;
7580#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007581
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007582 // Fill in the "end" label in jumps at the end of the blocks.
7583 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7584 this_instr, cctx);
7585
7586 isn->isn_arg.try.try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007587 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588 {
7589 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007591 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007592 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007593 }
7594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 // TODO: set index in ts_finally_label jumps
7596
7597 return arg;
7598}
7599
7600 static char_u *
7601compile_endtry(char_u *arg, cctx_T *cctx)
7602{
7603 scope_T *scope = cctx->ctx_scope;
7604 garray_T *instr = &cctx->ctx_instr;
7605 isn_T *isn;
7606
7607 // end block scope from :catch or :finally
7608 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7609 compile_endblock(cctx);
7610 scope = cctx->ctx_scope;
7611
7612 // Error if not in a :try scope
7613 if (scope == NULL || scope->se_type != TRY_SCOPE)
7614 {
7615 if (scope == NULL)
7616 emsg(_(e_no_endtry));
7617 else if (scope->se_type == WHILE_SCOPE)
7618 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007619 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007620 emsg(_(e_endfor));
7621 else
7622 emsg(_(e_endif));
7623 return NULL;
7624 }
7625
Bram Moolenaar69f70502021-01-01 16:10:46 +01007626 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007627 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007628 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7629 if (isn->isn_arg.try.try_catch == 0
7630 && isn->isn_arg.try.try_finally == 0)
7631 {
7632 emsg(_(e_missing_catch_or_finally));
7633 return NULL;
7634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007635
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007636#ifdef FEAT_PROFILE
7637 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7638 .isn_type == ISN_PROF_START)
7639 // move the profile start after "endtry" so that it's not counted when
7640 // the exception is rethrown.
7641 --instr->ga_len;
7642#endif
7643
Bram Moolenaar69f70502021-01-01 16:10:46 +01007644 // Fill in the "end" label in jumps at the end of the blocks, if not
7645 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007646 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7647 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007648
Bram Moolenaar69f70502021-01-01 16:10:46 +01007649 // End :catch or :finally scope: set value in ISN_TRY instruction
7650 if (isn->isn_arg.try.try_catch == 0)
7651 isn->isn_arg.try.try_catch = instr->ga_len;
7652 if (isn->isn_arg.try.try_finally == 0)
7653 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007654
Bram Moolenaar69f70502021-01-01 16:10:46 +01007655 if (scope->se_u.se_try.ts_catch_label != 0)
7656 {
7657 // Last catch without match jumps here
7658 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7659 isn->isn_arg.jump.jump_where = instr->ga_len;
7660 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007661 }
7662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007663 compile_endblock(cctx);
7664
Bram Moolenaar69f70502021-01-01 16:10:46 +01007665 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007666 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007667#ifdef FEAT_PROFILE
7668 if (cctx->ctx_profiling)
7669 generate_instr(cctx, ISN_PROF_START);
7670#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671 return arg;
7672}
7673
7674/*
7675 * compile "throw {expr}"
7676 */
7677 static char_u *
7678compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7679{
7680 char_u *p = skipwhite(arg);
7681
Bram Moolenaara5565e42020-05-09 15:44:01 +02007682 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007684 if (cctx->ctx_skip == SKIP_YES)
7685 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 if (may_generate_2STRING(-1, cctx) == FAIL)
7687 return NULL;
7688 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7689 return NULL;
7690
7691 return p;
7692}
7693
7694/*
7695 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007696 * compile "echomsg expr"
7697 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007698 * compile "execute expr"
7699 */
7700 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007701compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007702{
7703 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007704 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007705 int count = 0;
7706
7707 for (;;)
7708 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007709 if (ends_excmd2(prev, p))
7710 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007711 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007712 return NULL;
7713 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007714 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007715 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007716 }
7717
Bram Moolenaare4984292020-12-13 14:19:25 +01007718 if (count > 0)
7719 {
7720 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7721 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7722 else if (cmdidx == CMD_execute)
7723 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7724 else if (cmdidx == CMD_echomsg)
7725 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7726 else
7727 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 return p;
7730}
7731
7732/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007733 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007734 * instruction to compute it and return OK.
7735 * Otherwise return FAIL, the caller must deal with any range.
7736 */
7737 static int
7738compile_variable_range(exarg_T *eap, cctx_T *cctx)
7739{
7740 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7741 char_u *p = skipdigits(eap->cmd);
7742
7743 if (p == range_end)
7744 return FAIL;
7745 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7746}
7747
7748/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007749 * :put r
7750 * :put ={expr}
7751 */
7752 static char_u *
7753compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7754{
7755 char_u *line = arg;
7756 linenr_T lnum;
7757 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007758 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007759
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007760 eap->regname = *line;
7761
7762 if (eap->regname == '=')
7763 {
7764 char_u *p = line + 1;
7765
7766 if (compile_expr0(&p, cctx) == FAIL)
7767 return NULL;
7768 line = p;
7769 }
7770 else if (eap->regname != NUL)
7771 ++line;
7772
Bram Moolenaar08597872020-12-10 19:43:40 +01007773 if (compile_variable_range(eap, cctx) == OK)
7774 {
7775 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7776 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007777 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007778 {
7779 // Either no range or a number.
7780 // "errormsg" will not be set because the range is ADDR_LINES.
7781 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007782 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007783 return NULL;
7784 if (eap->addr_count == 0)
7785 lnum = -1;
7786 else
7787 lnum = eap->line2;
7788 if (above)
7789 --lnum;
7790 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007791
7792 generate_PUT(cctx, eap->regname, lnum);
7793 return line;
7794}
7795
7796/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007797 * A command that is not compiled, execute with legacy code.
7798 */
7799 static char_u *
7800compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7801{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007802 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007803 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007804 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007805
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007806 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007807 goto theend;
7808
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007809 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007810 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007811 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007812 int usefilter = FALSE;
7813
7814 has_expr = argt & (EX_XFILE | EX_EXPAND);
7815
7816 // If the command can be followed by a bar, find the bar and truncate
7817 // it, so that the following command can be compiled.
7818 // The '|' is overwritten with a NUL, it is put back below.
7819 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7820 && *eap->arg == '!')
7821 // :w !filter or :r !filter or :r! filter
7822 usefilter = TRUE;
7823 if ((argt & EX_TRLBAR) && !usefilter)
7824 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007825 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007826 separate_nextcmd(eap);
7827 if (eap->nextcmd != NULL)
7828 nextcmd = eap->nextcmd;
7829 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007830 else if (eap->cmdidx == CMD_wincmd)
7831 {
7832 p = eap->arg;
7833 if (*p != NUL)
7834 ++p;
7835 if (*p == 'g' || *p == Ctrl_G)
7836 ++p;
7837 p = skipwhite(p);
7838 if (*p == '|')
7839 {
7840 *p = NUL;
7841 nextcmd = p + 1;
7842 }
7843 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007844 }
7845
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007846 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7847 {
7848 // expand filename in "syntax include [@group] filename"
7849 has_expr = TRUE;
7850 eap->arg = skipwhite(eap->arg + 7);
7851 if (*eap->arg == '@')
7852 eap->arg = skiptowhite(eap->arg);
7853 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007854
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007855 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7856 && STRLEN(eap->arg) > 4)
7857 {
7858 int delim = *eap->arg;
7859
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007860 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007861 if (*p == delim)
7862 {
7863 eap->arg = p + 1;
7864 has_expr = TRUE;
7865 }
7866 }
7867
Bram Moolenaarecac5912021-01-05 19:23:28 +01007868 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7869 {
7870 // TODO: should only expand when appropriate for the command
7871 eap->arg = skiptowhite(eap->arg);
7872 has_expr = TRUE;
7873 }
7874
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007875 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007876 {
7877 int count = 0;
7878 char_u *start = skipwhite(line);
7879
7880 // :cmd xxx`=expr1`yyy`=expr2`zzz
7881 // PUSHS ":cmd xxx"
7882 // eval expr1
7883 // PUSHS "yyy"
7884 // eval expr2
7885 // PUSHS "zzz"
7886 // EXECCONCAT 5
7887 for (;;)
7888 {
7889 if (p > start)
7890 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007891 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007892 ++count;
7893 }
7894 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007895 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007896 return NULL;
7897 may_generate_2STRING(-1, cctx);
7898 ++count;
7899 p = skipwhite(p);
7900 if (*p != '`')
7901 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007902 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007903 return NULL;
7904 }
7905 start = p + 1;
7906
7907 p = (char_u *)strstr((char *)start, "`=");
7908 if (p == NULL)
7909 {
7910 if (*skipwhite(start) != NUL)
7911 {
7912 generate_PUSHS(cctx, vim_strsave(start));
7913 ++count;
7914 }
7915 break;
7916 }
7917 }
7918 generate_EXECCONCAT(cctx, count);
7919 }
7920 else
7921 generate_EXEC(cctx, line);
7922
7923theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007924 if (*nextcmd != NUL)
7925 {
7926 // the parser expects a pointer to the bar, put it back
7927 --nextcmd;
7928 *nextcmd = '|';
7929 }
7930
7931 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007932}
7933
7934/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007935 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007936 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007937 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007938 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007939add_def_function(ufunc_T *ufunc)
7940{
7941 dfunc_T *dfunc;
7942
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007943 if (def_functions.ga_len == 0)
7944 {
7945 // The first position is not used, so that a zero uf_dfunc_idx means it
7946 // wasn't set.
7947 if (ga_grow(&def_functions, 1) == FAIL)
7948 return FAIL;
7949 ++def_functions.ga_len;
7950 }
7951
Bram Moolenaar09689a02020-05-09 22:50:08 +02007952 // Add the function to "def_functions".
7953 if (ga_grow(&def_functions, 1) == FAIL)
7954 return FAIL;
7955 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7956 CLEAR_POINTER(dfunc);
7957 dfunc->df_idx = def_functions.ga_len;
7958 ufunc->uf_dfunc_idx = dfunc->df_idx;
7959 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007960 dfunc->df_name = vim_strsave(ufunc->uf_name);
7961 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007962 ++def_functions.ga_len;
7963 return OK;
7964}
7965
7966/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007967 * After ex_function() has collected all the function lines: parse and compile
7968 * the lines into instructions.
7969 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007970 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7971 * the return statement (used for lambda). When uf_ret_type is already set
7972 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01007973 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007974 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007975 * This can be used recursively through compile_lambda(), which may reallocate
7976 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007977 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007978 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007979 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01007980compile_def_function(
7981 ufunc_T *ufunc,
7982 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01007983 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01007984 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007985{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007986 char_u *line = NULL;
7987 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007988 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007989 cctx_T cctx;
7990 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007991 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007992 int ret = FAIL;
7993 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007994 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007995 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007996 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007997#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007998 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007999#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008000
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008001 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008002 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008003 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008004 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008005 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8006 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008007 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008009 else
8010 {
8011 if (add_def_function(ufunc) == FAIL)
8012 return FAIL;
8013 new_def_function = TRUE;
8014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015
Bram Moolenaar985116a2020-07-12 17:31:09 +02008016 ufunc->uf_def_status = UF_COMPILING;
8017
Bram Moolenaara80faa82020-04-12 19:37:17 +02008018 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008019
Bram Moolenaarf002a412021-01-24 13:34:18 +01008020#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008021 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008022#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023 cctx.ctx_ufunc = ufunc;
8024 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008025 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008026 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8027 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8028 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8029 cctx.ctx_type_list = &ufunc->uf_type_list;
8030 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8031 instr = &cctx.ctx_instr;
8032
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008033 // Set the context to the function, it may be compiled when called from
8034 // another script. Set the script version to the most modern one.
8035 // The line number will be set in next_line_from_context().
8036 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8038
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008039 // Make sure error messages are OK.
8040 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8041 if (do_estack_push)
8042 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008043 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008044
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008045 if (ufunc->uf_def_args.ga_len > 0)
8046 {
8047 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008048 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008049 int i;
8050 char_u *arg;
8051 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008052 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008053
8054 // Produce instructions for the default values of optional arguments.
8055 // Store the instruction index in uf_def_arg_idx[] so that we know
8056 // where to start when the function is called, depending on the number
8057 // of arguments.
8058 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8059 if (ufunc->uf_def_arg_idx == NULL)
8060 goto erret;
8061 for (i = 0; i < count; ++i)
8062 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008063 garray_T *stack = &cctx.ctx_type_stack;
8064 type_T *val_type;
8065 int arg_idx = first_def_arg + i;
8066
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008067 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8068 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008069 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008070 goto erret;
8071
8072 // If no type specified use the type of the default value.
8073 // Otherwise check that the default value type matches the
8074 // specified type.
8075 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
8076 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008077 {
8078 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008079 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008080 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008081 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
8082 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008083 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008084
8085 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008086 goto erret;
8087 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008088 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008089
8090 if (did_set_arg_type)
8091 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008092 }
8093
8094 /*
8095 * Loop over all the lines of the function and generate instructions.
8096 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008097 for (;;)
8098 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008099 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008100 int starts_with_colon = FALSE;
8101 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008102 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008103
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008104 // Bail out on the first error to avoid a flood of errors and report
8105 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008106 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008107 goto erret;
8108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 if (line != NULL && *line == '|')
8110 // the line continues after a '|'
8111 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008112 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008113 && !(*line == '#' && (line == cctx.ctx_line_start
8114 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008116 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 goto erret;
8118 }
8119 else
8120 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008121 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008122 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008123 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008124 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008125#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008126 if (cctx.ctx_skip != SKIP_YES)
8127 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008128#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008131 }
8132
Bram Moolenaara80faa82020-04-12 19:37:17 +02008133 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008134 ea.cmdlinep = &line;
8135 ea.cmd = skipwhite(line);
8136
Bram Moolenaarb2049902021-01-24 12:53:53 +01008137 if (*ea.cmd == '#')
8138 {
8139 // "#" starts a comment
8140 line = (char_u *)"";
8141 continue;
8142 }
8143
Bram Moolenaarf002a412021-01-24 13:34:18 +01008144#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008145 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8146 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008147 {
8148 may_generate_prof_end(&cctx, prof_lnum);
8149
8150 prof_lnum = cctx.ctx_lnum;
8151 generate_instr(&cctx, ISN_PROF_START);
8152 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008153#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008154
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008155 // Some things can be recognized by the first character.
8156 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008158 case '}':
8159 {
8160 // "}" ends a block scope
8161 scopetype_T stype = cctx.ctx_scope == NULL
8162 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008163
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008164 if (stype == BLOCK_SCOPE)
8165 {
8166 compile_endblock(&cctx);
8167 line = ea.cmd;
8168 }
8169 else
8170 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008171 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008172 goto erret;
8173 }
8174 if (line != NULL)
8175 line = skipwhite(ea.cmd + 1);
8176 continue;
8177 }
8178
8179 case '{':
8180 // "{" starts a block scope
8181 // "{'a': 1}->func() is something else
8182 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8183 {
8184 line = compile_block(ea.cmd, &cctx);
8185 continue;
8186 }
8187 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 }
8189
8190 /*
8191 * COMMAND MODIFIERS
8192 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008193 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008194 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8195 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 {
8197 if (errormsg != NULL)
8198 goto erret;
8199 // empty line or comment
8200 line = (char_u *)"";
8201 continue;
8202 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008203 generate_cmdmods(&cctx, &local_cmdmod);
8204 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008206 // Check if there was a colon after the last command modifier or before
8207 // the current position.
8208 for (p = ea.cmd; p >= line; --p)
8209 {
8210 if (*p == ':')
8211 starts_with_colon = TRUE;
8212 if (p < ea.cmd && !VIM_ISWHITE(*p))
8213 break;
8214 }
8215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008216 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008217 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008218 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008219 {
8220 if (*ea.cmd == '(')
8221 // not for "call()"
8222 ea.cmd = p;
8223 else
8224 ea.cmd = skipwhite(ea.cmd);
8225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008226
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008227 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008229 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008230
Bram Moolenaar17126b12021-01-07 22:03:02 +01008231 // Check for assignment after command modifiers.
8232 assign = may_compile_assignment(&ea, &line, &cctx);
8233 if (assign == OK)
8234 goto nextline;
8235 if (assign == FAIL)
8236 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 }
8238
8239 /*
8240 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008241 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008243 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008244 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008245 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008246 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008247 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008248 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008249 if (!starts_with_colon)
8250 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008251 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008252 goto erret;
8253 }
8254 if (ends_excmd2(line, ea.cmd))
8255 {
8256 // A range without a command: jump to the line.
8257 // TODO: compile to a more efficient command, possibly
8258 // calling parse_cmd_address().
8259 ea.cmdidx = CMD_SIZE;
8260 line = compile_exec(line, &ea, &cctx);
8261 goto nextline;
8262 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008263 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008264 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008265 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008266 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008267 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008268
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008269 if (p == NULL)
8270 {
8271 if (cctx.ctx_skip != SKIP_YES)
8272 emsg(_(e_ambiguous_use_of_user_defined_command));
8273 goto erret;
8274 }
8275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008276 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8277 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008278 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008279 {
8280 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008281 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008282 }
8283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008284 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008285 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008286 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008287 // CMD_var cannot happen, compile_assignment() above would be
8288 // used. Most likely an assignment to a non-existing variable.
8289 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008290 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008292 }
8293
Bram Moolenaar3988f642020-08-27 22:43:03 +02008294 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008295 && ea.cmdidx != CMD_elseif
8296 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008297 && ea.cmdidx != CMD_endif
8298 && ea.cmdidx != CMD_endfor
8299 && ea.cmdidx != CMD_endwhile
8300 && ea.cmdidx != CMD_catch
8301 && ea.cmdidx != CMD_finally
8302 && ea.cmdidx != CMD_endtry)
8303 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008304 emsg(_(e_unreachable_code_after_return));
8305 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008306 }
8307
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008308 p = skipwhite(p);
8309 if (ea.cmdidx != CMD_SIZE
8310 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8311 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008312 if (ea.cmdidx >= 0)
8313 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008314 if ((ea.argt & EX_BANG) && *p == '!')
8315 {
8316 ea.forceit = TRUE;
8317 p = skipwhite(p + 1);
8318 }
8319 }
8320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 switch (ea.cmdidx)
8322 {
8323 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008324 ea.arg = p;
8325 line = compile_nested_function(&ea, &cctx);
8326 break;
8327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008328 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008329 // TODO: should we allow this, e.g. to declare a global
8330 // function?
8331 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008332 goto erret;
8333
8334 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008335 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008336 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337 break;
8338
8339 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008340 emsg(_(e_cannot_use_let_in_vim9_script));
8341 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008342 case CMD_var:
8343 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008344 case CMD_const:
8345 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008346 if (line == p)
8347 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008348 break;
8349
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008350 case CMD_unlet:
8351 case CMD_unlockvar:
8352 case CMD_lockvar:
8353 line = compile_unletlock(p, &ea, &cctx);
8354 break;
8355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008356 case CMD_import:
8357 line = compile_import(p, &cctx);
8358 break;
8359
8360 case CMD_if:
8361 line = compile_if(p, &cctx);
8362 break;
8363 case CMD_elseif:
8364 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008365 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008366 break;
8367 case CMD_else:
8368 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008369 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370 break;
8371 case CMD_endif:
8372 line = compile_endif(p, &cctx);
8373 break;
8374
8375 case CMD_while:
8376 line = compile_while(p, &cctx);
8377 break;
8378 case CMD_endwhile:
8379 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008380 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008381 break;
8382
8383 case CMD_for:
8384 line = compile_for(p, &cctx);
8385 break;
8386 case CMD_endfor:
8387 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008388 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008389 break;
8390 case CMD_continue:
8391 line = compile_continue(p, &cctx);
8392 break;
8393 case CMD_break:
8394 line = compile_break(p, &cctx);
8395 break;
8396
8397 case CMD_try:
8398 line = compile_try(p, &cctx);
8399 break;
8400 case CMD_catch:
8401 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008402 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008403 break;
8404 case CMD_finally:
8405 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008406 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 break;
8408 case CMD_endtry:
8409 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008410 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411 break;
8412 case CMD_throw:
8413 line = compile_throw(p, &cctx);
8414 break;
8415
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008416 case CMD_eval:
8417 if (compile_expr0(&p, &cctx) == FAIL)
8418 goto erret;
8419
Bram Moolenaar3988f642020-08-27 22:43:03 +02008420 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008421 generate_instr_drop(&cctx, ISN_DROP, 1);
8422
8423 line = skipwhite(p);
8424 break;
8425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008428 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008429 case CMD_echomsg:
8430 case CMD_echoerr:
8431 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008432 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008434 case CMD_put:
8435 ea.cmd = cmd;
8436 line = compile_put(p, &ea, &cctx);
8437 break;
8438
Bram Moolenaar3988f642020-08-27 22:43:03 +02008439 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008440
Bram Moolenaarae616492020-07-28 20:07:27 +02008441 case CMD_append:
8442 case CMD_change:
8443 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008444 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008445 case CMD_xit:
8446 not_in_vim9(&ea);
8447 goto erret;
8448
Bram Moolenaar002262f2020-07-08 17:47:57 +02008449 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008450 if (cctx.ctx_skip != SKIP_YES)
8451 {
8452 semsg(_(e_invalid_command_str), ea.cmd);
8453 goto erret;
8454 }
8455 // We don't check for a next command here.
8456 line = (char_u *)"";
8457 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008459 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008460 if (cctx.ctx_skip == SKIP_YES)
8461 {
8462 // We don't check for a next command here.
8463 line = (char_u *)"";
8464 }
8465 else
8466 {
8467 // Not recognized, execute with do_cmdline_cmd().
8468 ea.arg = p;
8469 line = compile_exec(line, &ea, &cctx);
8470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008471 break;
8472 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008473nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008474 if (line == NULL)
8475 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008476 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008477
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008478 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008479 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008481 if (cctx.ctx_type_stack.ga_len < 0)
8482 {
8483 iemsg("Type stack underflow");
8484 goto erret;
8485 }
8486 }
8487
8488 if (cctx.ctx_scope != NULL)
8489 {
8490 if (cctx.ctx_scope->se_type == IF_SCOPE)
8491 emsg(_(e_endif));
8492 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8493 emsg(_(e_endwhile));
8494 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8495 emsg(_(e_endfor));
8496 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008497 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008498 goto erret;
8499 }
8500
Bram Moolenaarefd88552020-06-18 20:50:10 +02008501 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 {
8503 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8504 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008505 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008506 goto erret;
8507 }
8508
8509 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008510 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511 }
8512
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008513 {
8514 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8515 + ufunc->uf_dfunc_idx;
8516 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008517 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008518#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008519 if (cctx.ctx_profiling)
8520 {
8521 dfunc->df_instr_prof = instr->ga_data;
8522 dfunc->df_instr_prof_count = instr->ga_len;
8523 }
8524 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008525#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008526 {
8527 dfunc->df_instr = instr->ga_data;
8528 dfunc->df_instr_count = instr->ga_len;
8529 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008530 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008531 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008532 if (cctx.ctx_outer_used)
8533 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008534 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008535 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008536
8537 ret = OK;
8538
8539erret:
8540 if (ret == FAIL)
8541 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008542 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008543 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8544 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008545
8546 for (idx = 0; idx < instr->ga_len; ++idx)
8547 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008548 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008549 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008550
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008551 // If using the last entry in the table and it was added above, we
8552 // might as well remove it.
8553 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008554 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008555 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008556 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008557 ufunc->uf_dfunc_idx = 0;
8558 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008559 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008560
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008561 while (cctx.ctx_scope != NULL)
8562 drop_scope(&cctx);
8563
Bram Moolenaar20431c92020-03-20 18:39:46 +01008564 // Don't execute this function body.
8565 ga_clear_strings(&ufunc->uf_lines);
8566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 if (errormsg != NULL)
8568 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008569 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008570 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008571 }
8572
8573 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008574 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008575 if (do_estack_push)
8576 estack_pop();
8577
Bram Moolenaar20431c92020-03-20 18:39:46 +01008578 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008579 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008580 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008581 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582}
8583
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008584 void
8585set_function_type(ufunc_T *ufunc)
8586{
8587 int varargs = ufunc->uf_va_name != NULL;
8588 int argcount = ufunc->uf_args.ga_len;
8589
8590 // Create a type for the function, with the return type and any
8591 // argument types.
8592 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8593 // The type is included in "tt_args".
8594 if (argcount > 0 || varargs)
8595 {
8596 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8597 argcount, &ufunc->uf_type_list);
8598 // Add argument types to the function type.
8599 if (func_type_add_arg_types(ufunc->uf_func_type,
8600 argcount + varargs,
8601 &ufunc->uf_type_list) == FAIL)
8602 return;
8603 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8604 ufunc->uf_func_type->tt_min_argcount =
8605 argcount - ufunc->uf_def_args.ga_len;
8606 if (ufunc->uf_arg_types == NULL)
8607 {
8608 int i;
8609
8610 // lambda does not have argument types.
8611 for (i = 0; i < argcount; ++i)
8612 ufunc->uf_func_type->tt_args[i] = &t_any;
8613 }
8614 else
8615 mch_memmove(ufunc->uf_func_type->tt_args,
8616 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8617 if (varargs)
8618 {
8619 ufunc->uf_func_type->tt_args[argcount] =
8620 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8621 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8622 }
8623 }
8624 else
8625 // No arguments, can use a predefined type.
8626 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8627 argcount, &ufunc->uf_type_list);
8628}
8629
8630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008631/*
8632 * Delete an instruction, free what it contains.
8633 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008634 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008635delete_instr(isn_T *isn)
8636{
8637 switch (isn->isn_type)
8638 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008639 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008640 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008641 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008642 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 case ISN_LOADENV:
8644 case ISN_LOADG:
8645 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008646 case ISN_LOADT:
8647 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008648 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008649 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008650 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008651 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008652 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008653 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008654 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008656 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008657 case ISN_STOREW:
8658 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008659 vim_free(isn->isn_arg.string);
8660 break;
8661
8662 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008663 case ISN_STORES:
8664 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008665 break;
8666
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008667 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008668 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008669 vim_free(isn->isn_arg.unlet.ul_name);
8670 break;
8671
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008672 case ISN_STOREOPT:
8673 vim_free(isn->isn_arg.storeopt.so_name);
8674 break;
8675
8676 case ISN_PUSHBLOB: // push blob isn_arg.blob
8677 blob_unref(isn->isn_arg.blob);
8678 break;
8679
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008680 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008682 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008683#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008684 break;
8685
8686 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008687#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008688 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008689#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008690 break;
8691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008692 case ISN_UCALL:
8693 vim_free(isn->isn_arg.ufunc.cuf_name);
8694 break;
8695
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008696 case ISN_FUNCREF:
8697 {
8698 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8699 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008700 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008701
Bram Moolenaar077a4232020-12-22 18:33:27 +01008702 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8703 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008704 }
8705 break;
8706
8707 case ISN_DCALL:
8708 {
8709 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8710 + isn->isn_arg.dfunc.cdf_idx;
8711
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008712 if (dfunc->df_ufunc != NULL
8713 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008714 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008715 }
8716 break;
8717
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008718 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008719 {
8720 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8721 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8722
8723 if (ufunc != NULL)
8724 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008725 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008726 func_ptr_unref(ufunc);
8727 }
8728
8729 vim_free(lambda);
8730 vim_free(isn->isn_arg.newfunc.nf_global);
8731 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008732 break;
8733
Bram Moolenaar5e654232020-09-16 15:22:00 +02008734 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008735 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008736 free_type(isn->isn_arg.type.ct_type);
8737 break;
8738
Bram Moolenaar02194d22020-10-24 23:08:38 +02008739 case ISN_CMDMOD:
8740 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8741 ->cmod_filter_regmatch.regprog);
8742 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8743 break;
8744
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008745 case ISN_LOADSCRIPT:
8746 case ISN_STORESCRIPT:
8747 vim_free(isn->isn_arg.script.scriptref);
8748 break;
8749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750 case ISN_2BOOL:
8751 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008752 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008753 case ISN_ADDBLOB:
8754 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008755 case ISN_ANYINDEX:
8756 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008757 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008758 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008759 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008760 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008761 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008762 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008763 case ISN_COMPAREANY:
8764 case ISN_COMPAREBLOB:
8765 case ISN_COMPAREBOOL:
8766 case ISN_COMPAREDICT:
8767 case ISN_COMPAREFLOAT:
8768 case ISN_COMPAREFUNC:
8769 case ISN_COMPARELIST:
8770 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008771 case ISN_COMPARESPECIAL:
8772 case ISN_COMPARESTRING:
8773 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008774 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008775 case ISN_DROP:
8776 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008777 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008778 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008779 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008780 case ISN_EXECCONCAT:
8781 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008782 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008783 case ISN_GETITEM:
8784 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008785 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008786 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008787 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008788 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008789 case ISN_LOADBDICT:
8790 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008791 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008792 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008793 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008794 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008795 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008796 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008797 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008798 case ISN_NEGATENR:
8799 case ISN_NEWDICT:
8800 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008802 case ISN_OPFLOAT:
8803 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008805 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008806 case ISN_PROF_END:
8807 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008808 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809 case ISN_PUSHF:
8810 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008811 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008812 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008813 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008814 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008815 case ISN_SHUFFLE:
8816 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008817 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008818 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008819 case ISN_STORENR:
8820 case ISN_STOREOUTER:
8821 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008822 case ISN_STOREV:
8823 case ISN_STRINDEX:
8824 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008825 case ISN_THROW:
8826 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008827 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008828 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008829 // nothing allocated
8830 break;
8831 }
8832}
8833
8834/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008835 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008836 */
8837 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008838delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008839{
8840 int idx;
8841
8842 ga_clear(&dfunc->df_def_args_isn);
8843
8844 if (dfunc->df_instr != NULL)
8845 {
8846 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8847 delete_instr(dfunc->df_instr + idx);
8848 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008849 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008850 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01008851#ifdef FEAT_PROFILE
8852 if (dfunc->df_instr_prof != NULL)
8853 {
8854 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
8855 delete_instr(dfunc->df_instr_prof + idx);
8856 VIM_CLEAR(dfunc->df_instr_prof);
8857 dfunc->df_instr_prof = NULL;
8858 }
8859#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01008860
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008861 if (mark_deleted)
8862 dfunc->df_deleted = TRUE;
8863 if (dfunc->df_ufunc != NULL)
8864 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008865}
8866
8867/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008868 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008869 * function, unless another user function still uses it.
8870 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008871 */
8872 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008873unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008875 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008876 {
8877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8878 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008879
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008880 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008881 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008882 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008883 ufunc->uf_dfunc_idx = 0;
8884 if (dfunc->df_ufunc == ufunc)
8885 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008886 }
8887}
8888
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008889/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008890 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008891 */
8892 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008893link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008894{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008895 if (ufunc->uf_dfunc_idx > 0)
8896 {
8897 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8898 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008899
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008900 ++dfunc->df_refcount;
8901 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008902}
8903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008904#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008905/*
8906 * Free all functions defined with ":def".
8907 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008908 void
8909free_def_functions(void)
8910{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008911 int idx;
8912
8913 for (idx = 0; idx < def_functions.ga_len; ++idx)
8914 {
8915 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8916
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008917 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008918 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008919 }
8920
8921 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008922}
8923#endif
8924
8925
8926#endif // FEAT_EVAL