blob: c75ead9d957951750436024400fcc1d667236a21 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9compile.c: :def and dealing with instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#define DEFINE_VIM9_GLOBALS
24#include "vim9.h"
25
Bram Moolenaarefd88552020-06-18 20:50:10 +020026// values for ctx_skip
27typedef enum {
28 SKIP_NOT, // condition is a constant, produce code
29 SKIP_YES, // condition is a constant, do NOT produce code
Bram Moolenaar280b0dc2020-06-20 13:29:03 +020030 SKIP_UNKNOWN // condition is not a constant, produce code
Bram Moolenaarefd88552020-06-18 20:50:10 +020031} skip_T;
32
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010033/*
34 * Chain of jump instructions where the end label needs to be set.
35 */
36typedef struct endlabel_S endlabel_T;
37struct endlabel_S {
38 endlabel_T *el_next; // chain end_label locations
39 int el_end_label; // instruction idx where to set end
40};
41
42/*
43 * info specific for the scope of :if / elseif / else
44 */
45typedef struct {
Bram Moolenaarefd88552020-06-18 20:50:10 +020046 int is_seen_else;
Bram Moolenaarced68a02021-01-24 17:53:47 +010047 int is_seen_skip_not; // a block was unconditionally executed
Bram Moolenaarefd88552020-06-18 20:50:10 +020048 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010049 int is_if_label; // instruction idx at IF or ELSEIF
50 endlabel_T *is_end_label; // instructions to set end label
51} ifscope_T;
52
53/*
54 * info specific for the scope of :while
55 */
56typedef struct {
57 int ws_top_label; // instruction idx at WHILE
58 endlabel_T *ws_end_label; // instructions to set end
59} whilescope_T;
60
61/*
62 * info specific for the scope of :for
63 */
64typedef struct {
65 int fs_top_label; // instruction idx at FOR
66 endlabel_T *fs_end_label; // break instructions
67} forscope_T;
68
69/*
70 * info specific for the scope of :try
71 */
72typedef struct {
73 int ts_try_label; // instruction idx at TRY
74 endlabel_T *ts_end_label; // jump to :finally or :endtry
75 int ts_catch_label; // instruction idx of last CATCH
76 int ts_caught_all; // "catch" without argument encountered
77} tryscope_T;
78
79typedef enum {
80 NO_SCOPE,
81 IF_SCOPE,
82 WHILE_SCOPE,
83 FOR_SCOPE,
84 TRY_SCOPE,
85 BLOCK_SCOPE
86} scopetype_T;
87
88/*
89 * Info for one scope, pointed to by "ctx_scope".
90 */
91typedef struct scope_S scope_T;
92struct scope_S {
93 scope_T *se_outer; // scope containing this one
94 scopetype_T se_type;
95 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020096 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010097 union {
98 ifscope_T se_if;
99 whilescope_T se_while;
100 forscope_T se_for;
101 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100102 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100103};
104
105/*
106 * Entry for "ctx_locals". Used for arguments and local variables.
107 */
108typedef struct {
109 char_u *lv_name;
110 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200111 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100112 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200113 int lv_const; // when TRUE cannot be assigned to
114 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115} lvar_T;
116
117/*
118 * Context for compiling lines of Vim script.
119 * Stores info about the local variables and condition stack.
120 */
121struct cctx_S {
122 ufunc_T *ctx_ufunc; // current function
123 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200124 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125 garray_T ctx_instr; // generated instructions
126
Bram Moolenaarb2049902021-01-24 12:53:53 +0100127 int ctx_profiling; // when TRUE generate ISN_PROF_START
128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200130 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100131
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200132 int ctx_has_closure; // set to one if a closures was created in
133 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 garray_T ctx_imports; // imported items
136
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200137 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200139 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141 cctx_T *ctx_outer; // outer scope for lambda or nested
142 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200143 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200146 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200147
Bram Moolenaar02194d22020-10-24 23:08:38 +0200148 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149};
150
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100151static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152
153/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100154 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100155 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 * If "lvar" is NULL only check if the variable can be found.
157 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100159 static int
160lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161{
162 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100165 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100166 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200167
168 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100171 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
172 if (STRNCMP(name, lvp->lv_name, len) == 0
173 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200174 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100175 if (lvar != NULL)
176 {
177 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100178 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100179 }
180 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200183
184 // Find local in outer function scope.
185 if (cctx->ctx_outer != NULL)
186 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100187 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200188 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100189 if (lvar != NULL)
190 {
191 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100192 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100193 }
194 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200195 }
196 }
197
Bram Moolenaar709664c2020-12-12 14:33:41 +0100198 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100199}
200
201/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200202 * Lookup an argument in the current function and an enclosing function.
203 * Returns the argument index in "idxp"
204 * Returns the argument type in "type"
205 * Sets "gen_load_outer" to TRUE if found in outer scope.
206 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 */
208 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200209arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200210 char_u *name,
211 size_t len,
212 int *idxp,
213 type_T **type,
214 int *gen_load_outer,
215 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216{
217 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100220 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200221 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
223 {
224 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
225
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200226 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
227 {
228 if (idxp != NULL)
229 {
230 // Arguments are located above the frame pointer. One further
231 // if there is a vararg argument
232 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
233 + STACK_FRAME_SIZE)
234 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
235
236 if (cctx->ctx_ufunc->uf_arg_types != NULL)
237 *type = cctx->ctx_ufunc->uf_arg_types[idx];
238 else
239 *type = &t_any;
240 }
241 return OK;
242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200245 va_name = cctx->ctx_ufunc->uf_va_name;
246 if (va_name != NULL
247 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
248 {
249 if (idxp != NULL)
250 {
251 // varargs is always the last argument
252 *idxp = -STACK_FRAME_SIZE - 1;
253 *type = cctx->ctx_ufunc->uf_va_type;
254 }
255 return OK;
256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200258 if (cctx->ctx_outer != NULL)
259 {
260 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200261 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200262 == OK)
263 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100264 if (gen_load_outer != NULL)
265 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200266 return OK;
267 }
268 }
269
270 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271}
272
273/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200274 * Lookup a script-local variable in the current script, possibly defined in a
275 * block that contains the function "cctx->ctx_ufunc".
276 * "cctx" is NULL at the script level.
277 * if "len" is <= 0 "name" must be NUL terminated.
278 * Return NULL when not found.
279 */
280 static sallvar_T *
281find_script_var(char_u *name, size_t len, cctx_T *cctx)
282{
283 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
284 hashitem_T *hi;
285 int cc;
286 sallvar_T *sav;
287 ufunc_T *ufunc;
288
289 // Find the list of all script variables with the right name.
290 if (len > 0)
291 {
292 cc = name[len];
293 name[len] = NUL;
294 }
295 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
296 if (len > 0)
297 name[len] = cc;
298 if (HASHITEM_EMPTY(hi))
299 return NULL;
300
301 sav = HI2SAV(hi);
302 if (sav->sav_block_id == 0 || cctx == NULL)
303 // variable defined in the script scope or not in a function.
304 return sav;
305
306 // Go over the variables with this name and find one that was visible
307 // from the function.
308 ufunc = cctx->ctx_ufunc;
309 while (sav != NULL)
310 {
311 int idx;
312
313 // Go over the blocks that this function was defined in. If the
314 // variable block ID matches it was visible to the function.
315 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
316 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
317 return sav;
318 sav = sav->sav_next;
319 }
320
321 return NULL;
322}
323
324/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100325 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200326 */
327 static int
328script_is_vim9()
329{
330 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
331}
332
333/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200334 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200335 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
336 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338 * Returns OK or FAIL.
339 */
340 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200341script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200345 if (current_sctx.sc_sid <= 0)
346 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200347 is_vim9_script = script_is_vim9();
348 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200349 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200350 if (is_vim9_script)
351 {
352 // Check script variables that were visible where the function was
353 // defined.
354 if (find_script_var(name, len, cctx) != NULL)
355 return OK;
356 }
357 else
358 {
359 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
360 dictitem_T *di;
361 int cc;
362
363 // Check script variables that are currently visible
364 cc = name[len];
365 name[len] = NUL;
366 di = find_var_in_ht(ht, 0, name, TRUE);
367 name[len] = cc;
368 if (di != NULL)
369 return OK;
370 }
371
372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373}
374
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100376 * Return TRUE if "name" is a local variable, argument, script variable or
377 * imported.
378 */
379 static int
380variable_exists(char_u *name, size_t len, cctx_T *cctx)
381{
382 return lookup_local(name, len, NULL, cctx) == OK
383 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
384 || script_var_exists(name, len, FALSE, cctx) == OK
385 || find_imported(name, len, cctx) != NULL;
386}
387
388/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100389 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200390 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100392 * Return FAIL and give an error if it defined.
393 */
394 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200395check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100396{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200397 int c = p[len];
398 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200399
400 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200401 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100402 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100403 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200404 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200405 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200406 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100407 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200408 // A local or script-local function can shadow a global function.
409 if (ufunc == NULL || !func_is_global(ufunc)
410 || (p[0] == 'g' && p[1] == ':'))
411 {
412 p[len] = c;
413 semsg(_(e_name_already_defined_str), p);
414 return FAIL;
415 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100416 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200417 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100418 return OK;
419}
420
Bram Moolenaar65b95452020-07-19 14:03:09 +0200421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422/////////////////////////////////////////////////////////////////////
423// Following generate_ functions expect the caller to call ga_grow().
424
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200425#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
426#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428/*
429 * Generate an instruction without arguments.
430 * Returns a pointer to the new instruction, NULL if failed.
431 */
432 static isn_T *
433generate_instr(cctx_T *cctx, isntype_T isn_type)
434{
435 garray_T *instr = &cctx->ctx_instr;
436 isn_T *isn;
437
Bram Moolenaar080457c2020-03-03 21:53:32 +0100438 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439 if (ga_grow(instr, 1) == FAIL)
440 return NULL;
441 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
442 isn->isn_type = isn_type;
443 isn->isn_lnum = cctx->ctx_lnum + 1;
444 ++instr->ga_len;
445
446 return isn;
447}
448
449/*
450 * Generate an instruction without arguments.
451 * "drop" will be removed from the stack.
452 * Returns a pointer to the new instruction, NULL if failed.
453 */
454 static isn_T *
455generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
456{
457 garray_T *stack = &cctx->ctx_type_stack;
458
Bram Moolenaar080457c2020-03-03 21:53:32 +0100459 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 stack->ga_len -= drop;
461 return generate_instr(cctx, isn_type);
462}
463
464/*
465 * Generate instruction "isn_type" and put "type" on the type stack.
466 */
467 static isn_T *
468generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
469{
470 isn_T *isn;
471 garray_T *stack = &cctx->ctx_type_stack;
472
473 if ((isn = generate_instr(cctx, isn_type)) == NULL)
474 return NULL;
475
476 if (ga_grow(stack, 1) == FAIL)
477 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200478 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100479 ++stack->ga_len;
480
481 return isn;
482}
483
484/*
485 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200486 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 */
488 static int
489may_generate_2STRING(int offset, cctx_T *cctx)
490{
491 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200492 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100494 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100496 RETURN_OK_IF_SKIP(cctx);
497 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200498 switch ((*type)->tt_type)
499 {
500 // nothing to be done
501 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200503 // conversion possible
504 case VAR_SPECIAL:
505 case VAR_BOOL:
506 case VAR_NUMBER:
507 case VAR_FLOAT:
508 break;
509
510 // conversion possible (with runtime check)
511 case VAR_ANY:
512 case VAR_UNKNOWN:
513 isntype = ISN_2STRING_ANY;
514 break;
515
516 // conversion not possible
517 case VAR_VOID:
518 case VAR_BLOB:
519 case VAR_FUNC:
520 case VAR_PARTIAL:
521 case VAR_LIST:
522 case VAR_DICT:
523 case VAR_JOB:
524 case VAR_CHANNEL:
525 to_string_error((*type)->tt_type);
526 return FAIL;
527 }
528
529 *type = &t_string;
530 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 return FAIL;
532 isn->isn_arg.number = offset;
533
534 return OK;
535}
536
537 static int
538check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
539{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200540 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200542 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 {
544 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200545 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200547 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 return FAIL;
549 }
550 return OK;
551}
552
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200553 static int
554generate_add_instr(
555 cctx_T *cctx,
556 vartype_T vartype,
557 type_T *type1,
558 type_T *type2)
559{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560 garray_T *stack = &cctx->ctx_type_stack;
561 isn_T *isn = generate_instr_drop(cctx,
562 vartype == VAR_NUMBER ? ISN_OPNR
563 : vartype == VAR_LIST ? ISN_ADDLIST
564 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200565#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100566 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200567#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100568 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200569
570 if (vartype != VAR_LIST && vartype != VAR_BLOB
571 && type1->tt_type != VAR_ANY
572 && type2->tt_type != VAR_ANY
573 && check_number_or_float(
574 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
575 return FAIL;
576
577 if (isn != NULL)
578 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100579
580 // When concatenating two lists with different member types the member type
581 // becomes "any".
582 if (vartype == VAR_LIST
583 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
584 && type1->tt_member != type2->tt_member)
585 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
586
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200587 return isn == NULL ? FAIL : OK;
588}
589
590/*
591 * Get the type to use for an instruction for an operation on "type1" and
592 * "type2". If they are matching use a type-specific instruction. Otherwise
593 * fall back to runtime type checking.
594 */
595 static vartype_T
596operator_type(type_T *type1, type_T *type2)
597{
598 if (type1->tt_type == type2->tt_type
599 && (type1->tt_type == VAR_NUMBER
600 || type1->tt_type == VAR_LIST
601#ifdef FEAT_FLOAT
602 || type1->tt_type == VAR_FLOAT
603#endif
604 || type1->tt_type == VAR_BLOB))
605 return type1->tt_type;
606 return VAR_ANY;
607}
608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609/*
610 * Generate an instruction with two arguments. The instruction depends on the
611 * type of the arguments.
612 */
613 static int
614generate_two_op(cctx_T *cctx, char_u *op)
615{
616 garray_T *stack = &cctx->ctx_type_stack;
617 type_T *type1;
618 type_T *type2;
619 vartype_T vartype;
620 isn_T *isn;
621
Bram Moolenaar080457c2020-03-03 21:53:32 +0100622 RETURN_OK_IF_SKIP(cctx);
623
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200624 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
626 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200627 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628
629 switch (*op)
630 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200631 case '+':
632 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 break;
635
636 case '-':
637 case '*':
638 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
639 op) == FAIL)
640 return FAIL;
641 if (vartype == VAR_NUMBER)
642 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
643#ifdef FEAT_FLOAT
644 else if (vartype == VAR_FLOAT)
645 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
646#endif
647 else
648 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
649 if (isn != NULL)
650 isn->isn_arg.op.op_type = *op == '*'
651 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
652 break;
653
Bram Moolenaar4c683752020-04-05 21:38:23 +0200654 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200656 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 && type2->tt_type != VAR_NUMBER))
658 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200659 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 return FAIL;
661 }
662 isn = generate_instr_drop(cctx,
663 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
664 if (isn != NULL)
665 isn->isn_arg.op.op_type = EXPR_REM;
666 break;
667 }
668
669 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200670 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 {
672 type_T *type = &t_any;
673
674#ifdef FEAT_FLOAT
675 // float+number and number+float results in float
676 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
677 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
678 type = &t_float;
679#endif
680 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
681 }
682
683 return OK;
684}
685
686/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200687 * Get the instruction to use for comparing "type1" with "type2"
688 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200690 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100691get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692{
693 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694
Bram Moolenaar4c683752020-04-05 21:38:23 +0200695 if (type1 == VAR_UNKNOWN)
696 type1 = VAR_ANY;
697 if (type2 == VAR_UNKNOWN)
698 type2 = VAR_ANY;
699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 if (type1 == type2)
701 {
702 switch (type1)
703 {
704 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
705 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
706 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
707 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
708 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
709 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
710 case VAR_LIST: isntype = ISN_COMPARELIST; break;
711 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
712 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 default: isntype = ISN_COMPAREANY; break;
714 }
715 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200716 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
718 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
719 isntype = ISN_COMPAREANY;
720
Bram Moolenaar657137c2021-01-09 15:45:23 +0100721 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 && (isntype == ISN_COMPAREBOOL
723 || isntype == ISN_COMPARESPECIAL
724 || isntype == ISN_COMPARENR
725 || isntype == ISN_COMPAREFLOAT))
726 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200727 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100728 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200729 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 }
731 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100732 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
734 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100735 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
736 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 && (type1 == VAR_BLOB || type2 == VAR_BLOB
738 || type1 == VAR_LIST || type2 == VAR_LIST))))
739 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200740 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200742 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200744 return isntype;
745}
746
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200747 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100748check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200749{
750 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
751 return FAIL;
752 return OK;
753}
754
Bram Moolenaara5565e42020-05-09 15:44:01 +0200755/*
756 * Generate an ISN_COMPARE* instruction with a boolean result.
757 */
758 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100759generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200760{
761 isntype_T isntype;
762 isn_T *isn;
763 garray_T *stack = &cctx->ctx_type_stack;
764 vartype_T type1;
765 vartype_T type2;
766
767 RETURN_OK_IF_SKIP(cctx);
768
769 // Get the known type of the two items on the stack. If they are matching
770 // use a type-specific instruction. Otherwise fall back to runtime type
771 // checking.
772 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
773 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100774 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200775 if (isntype == ISN_DROP)
776 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777
778 if ((isn = generate_instr(cctx, isntype)) == NULL)
779 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100780 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 isn->isn_arg.op.op_ic = ic;
782
783 // takes two arguments, puts one bool back
784 if (stack->ga_len >= 2)
785 {
786 --stack->ga_len;
787 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
788 }
789
790 return OK;
791}
792
793/*
794 * Generate an ISN_2BOOL instruction.
795 */
796 static int
797generate_2BOOL(cctx_T *cctx, int invert)
798{
799 isn_T *isn;
800 garray_T *stack = &cctx->ctx_type_stack;
801
Bram Moolenaar080457c2020-03-03 21:53:32 +0100802 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
804 return FAIL;
805 isn->isn_arg.number = invert;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200813/*
814 * Generate an ISN_COND2BOOL instruction.
815 */
816 static int
817generate_COND2BOOL(cctx_T *cctx)
818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
822 RETURN_OK_IF_SKIP(cctx);
823 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
824 return FAIL;
825
826 // type becomes bool
827 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
828
829 return OK;
830}
831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200833generate_TYPECHECK(
834 cctx_T *cctx,
835 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100836 int offset,
837 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838{
839 isn_T *isn;
840 garray_T *stack = &cctx->ctx_type_stack;
841
Bram Moolenaar080457c2020-03-03 21:53:32 +0100842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
844 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200845 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100846 isn->isn_arg.type.ct_off = (int8_T)offset;
847 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848
Bram Moolenaar5e654232020-09-16 15:22:00 +0200849 // type becomes expected
850 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851
852 return OK;
853}
854
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100855 static int
856generate_SETTYPE(
857 cctx_T *cctx,
858 type_T *expected)
859{
860 isn_T *isn;
861
862 RETURN_OK_IF_SKIP(cctx);
863 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
864 return FAIL;
865 isn->isn_arg.type.ct_type = alloc_type(expected);
866 return OK;
867}
868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200870 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
871 * used. Return FALSE if the types will never match.
872 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100873 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200874use_typecheck(type_T *actual, type_T *expected)
875{
876 if (actual->tt_type == VAR_ANY
877 || actual->tt_type == VAR_UNKNOWN
878 || (actual->tt_type == VAR_FUNC
879 && (expected->tt_type == VAR_FUNC
880 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100881 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
882 && ((actual->tt_member == &t_void)
883 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200884 return TRUE;
885 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
886 && actual->tt_type == expected->tt_type)
887 // This takes care of a nested list or dict.
888 return use_typecheck(actual->tt_member, expected->tt_member);
889 return FALSE;
890}
891
892/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200893 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200894 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200895 * - "actual" is a type that can be "expected" type: add a runtime check; or
896 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200897 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
898 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200899 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100900 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200901need_type(
902 type_T *actual,
903 type_T *expected,
904 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100905 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200906 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200907 int silent,
908 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200909{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100910 where_T where;
911
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200912 if (expected == &t_bool && actual != &t_bool
913 && (actual->tt_flags & TTFLAG_BOOL_OK))
914 {
915 // Using "0", "1" or the result of an expression with "&&" or "||" as a
916 // boolean is OK but requires a conversion.
917 generate_2BOOL(cctx, FALSE);
918 return OK;
919 }
920
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100921 where.wt_index = arg_idx;
922 where.wt_variable = FALSE;
923 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200924 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200925
926 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200927 // If it's a constant a runtime check makes no sense.
928 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200929 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100930 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200931 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200932 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200933
934 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100935 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200936 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200937}
938
939/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100940 * Check that the top of the type stack has a type that can be used as a
941 * condition. Give an error and return FAIL if not.
942 */
943 static int
944bool_on_stack(cctx_T *cctx)
945{
946 garray_T *stack = &cctx->ctx_type_stack;
947 type_T *type;
948
949 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
950 if (type == &t_bool)
951 return OK;
952
953 if (type == &t_any || type == &t_number)
954 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
955 // This requires a runtime type check.
956 return generate_COND2BOOL(cctx);
957
Bram Moolenaar351ead02021-01-16 16:07:01 +0100958 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100959}
960
961/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 * Generate an ISN_PUSHNR instruction.
963 */
964 static int
965generate_PUSHNR(cctx_T *cctx, varnumber_T number)
966{
967 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200968 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
972 return FAIL;
973 isn->isn_arg.number = number;
974
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200975 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200976 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100977 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 return OK;
979}
980
981/*
982 * Generate an ISN_PUSHBOOL instruction.
983 */
984 static int
985generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
991 return FAIL;
992 isn->isn_arg.number = number;
993
994 return OK;
995}
996
997/*
998 * Generate an ISN_PUSHSPEC instruction.
999 */
1000 static int
1001generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
1002{
1003 isn_T *isn;
1004
Bram Moolenaar080457c2020-03-03 21:53:32 +01001005 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
1007 return FAIL;
1008 isn->isn_arg.number = number;
1009
1010 return OK;
1011}
1012
1013#ifdef FEAT_FLOAT
1014/*
1015 * Generate an ISN_PUSHF instruction.
1016 */
1017 static int
1018generate_PUSHF(cctx_T *cctx, float_T fnumber)
1019{
1020 isn_T *isn;
1021
Bram Moolenaar080457c2020-03-03 21:53:32 +01001022 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1024 return FAIL;
1025 isn->isn_arg.fnumber = fnumber;
1026
1027 return OK;
1028}
1029#endif
1030
1031/*
1032 * Generate an ISN_PUSHS instruction.
1033 * Consumes "str".
1034 */
1035 static int
1036generate_PUSHS(cctx_T *cctx, char_u *str)
1037{
1038 isn_T *isn;
1039
Bram Moolenaar508b5612021-01-02 18:17:26 +01001040 if (cctx->ctx_skip == SKIP_YES)
1041 {
1042 vim_free(str);
1043 return OK;
1044 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1046 return FAIL;
1047 isn->isn_arg.string = str;
1048
1049 return OK;
1050}
1051
1052/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001053 * Generate an ISN_PUSHCHANNEL instruction.
1054 * Consumes "channel".
1055 */
1056 static int
1057generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1058{
1059 isn_T *isn;
1060
Bram Moolenaar080457c2020-03-03 21:53:32 +01001061 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001062 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1063 return FAIL;
1064 isn->isn_arg.channel = channel;
1065
1066 return OK;
1067}
1068
1069/*
1070 * Generate an ISN_PUSHJOB instruction.
1071 * Consumes "job".
1072 */
1073 static int
1074generate_PUSHJOB(cctx_T *cctx, job_T *job)
1075{
1076 isn_T *isn;
1077
Bram Moolenaar080457c2020-03-03 21:53:32 +01001078 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001079 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001080 return FAIL;
1081 isn->isn_arg.job = job;
1082
1083 return OK;
1084}
1085
1086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * Generate an ISN_PUSHBLOB instruction.
1088 * Consumes "blob".
1089 */
1090 static int
1091generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1092{
1093 isn_T *isn;
1094
Bram Moolenaar080457c2020-03-03 21:53:32 +01001095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1097 return FAIL;
1098 isn->isn_arg.blob = blob;
1099
1100 return OK;
1101}
1102
1103/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001104 * Generate an ISN_PUSHFUNC instruction with name "name".
1105 * Consumes "name".
1106 */
1107 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001108generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001109{
1110 isn_T *isn;
1111
Bram Moolenaar080457c2020-03-03 21:53:32 +01001112 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001113 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001114 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001115 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001116
1117 return OK;
1118}
1119
1120/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001121 * Generate an ISN_GETITEM instruction with "index".
1122 */
1123 static int
1124generate_GETITEM(cctx_T *cctx, int index)
1125{
1126 isn_T *isn;
1127 garray_T *stack = &cctx->ctx_type_stack;
1128 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1129 type_T *item_type = &t_any;
1130
1131 RETURN_OK_IF_SKIP(cctx);
1132
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001133 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001134 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001135 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001136 emsg(_(e_listreq));
1137 return FAIL;
1138 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001139 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001140 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1141 return FAIL;
1142 isn->isn_arg.number = index;
1143
1144 // add the item type to the type stack
1145 if (ga_grow(stack, 1) == FAIL)
1146 return FAIL;
1147 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1148 ++stack->ga_len;
1149 return OK;
1150}
1151
1152/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001153 * Generate an ISN_SLICE instruction with "count".
1154 */
1155 static int
1156generate_SLICE(cctx_T *cctx, int count)
1157{
1158 isn_T *isn;
1159
1160 RETURN_OK_IF_SKIP(cctx);
1161 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1162 return FAIL;
1163 isn->isn_arg.number = count;
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_CHECKLEN instruction with "min_len".
1169 */
1170 static int
1171generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1172{
1173 isn_T *isn;
1174
1175 RETURN_OK_IF_SKIP(cctx);
1176
1177 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1178 return FAIL;
1179 isn->isn_arg.checklen.cl_min_len = min_len;
1180 isn->isn_arg.checklen.cl_more_OK = more_OK;
1181
1182 return OK;
1183}
1184
1185/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 * Generate an ISN_STORE instruction.
1187 */
1188 static int
1189generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1190{
1191 isn_T *isn;
1192
Bram Moolenaar080457c2020-03-03 21:53:32 +01001193 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1195 return FAIL;
1196 if (name != NULL)
1197 isn->isn_arg.string = vim_strsave(name);
1198 else
1199 isn->isn_arg.number = idx;
1200
1201 return OK;
1202}
1203
1204/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001205 * Generate an ISN_STOREOUTER instruction.
1206 */
1207 static int
1208generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1209{
1210 isn_T *isn;
1211
1212 RETURN_OK_IF_SKIP(cctx);
1213 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1214 return FAIL;
1215 isn->isn_arg.outer.outer_idx = idx;
1216 isn->isn_arg.outer.outer_depth = level;
1217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1223 */
1224 static int
1225generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1226{
1227 isn_T *isn;
1228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1231 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001232 isn->isn_arg.storenr.stnr_idx = idx;
1233 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
1235 return OK;
1236}
1237
1238/*
1239 * Generate an ISN_STOREOPT instruction
1240 */
1241 static int
1242generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1243{
1244 isn_T *isn;
1245
Bram Moolenaar080457c2020-03-03 21:53:32 +01001246 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001247 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 return FAIL;
1249 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1250 isn->isn_arg.storeopt.so_flags = opt_flags;
1251
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_LOAD or similar instruction.
1257 */
1258 static int
1259generate_LOAD(
1260 cctx_T *cctx,
1261 isntype_T isn_type,
1262 int idx,
1263 char_u *name,
1264 type_T *type)
1265{
1266 isn_T *isn;
1267
Bram Moolenaar080457c2020-03-03 21:53:32 +01001268 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1270 return FAIL;
1271 if (name != NULL)
1272 isn->isn_arg.string = vim_strsave(name);
1273 else
1274 isn->isn_arg.number = idx;
1275
1276 return OK;
1277}
1278
1279/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001280 * Generate an ISN_LOADOUTER instruction
1281 */
1282 static int
1283generate_LOADOUTER(
1284 cctx_T *cctx,
1285 int idx,
1286 int nesting,
1287 type_T *type)
1288{
1289 isn_T *isn;
1290
1291 RETURN_OK_IF_SKIP(cctx);
1292 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1293 return FAIL;
1294 isn->isn_arg.outer.outer_idx = idx;
1295 isn->isn_arg.outer.outer_depth = nesting;
1296
1297 return OK;
1298}
1299
1300/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001301 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302 */
1303 static int
1304generate_LOADV(
1305 cctx_T *cctx,
1306 char_u *name,
1307 int error)
1308{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001309 int di_flags;
1310 int vidx = find_vim_var(name, &di_flags);
1311 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001312
Bram Moolenaar080457c2020-03-03 21:53:32 +01001313 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001314 if (vidx < 0)
1315 {
1316 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001317 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001318 return FAIL;
1319 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001320 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001321
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001322 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001323}
1324
1325/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001326 * Generate an ISN_UNLET instruction.
1327 */
1328 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001329generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001330{
1331 isn_T *isn;
1332
1333 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001334 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001335 return FAIL;
1336 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1337 isn->isn_arg.unlet.ul_forceit = forceit;
1338
1339 return OK;
1340}
1341
1342/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001343 * Generate an ISN_LOCKCONST instruction.
1344 */
1345 static int
1346generate_LOCKCONST(cctx_T *cctx)
1347{
1348 isn_T *isn;
1349
1350 RETURN_OK_IF_SKIP(cctx);
1351 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1352 return FAIL;
1353 return OK;
1354}
1355
1356/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 * Generate an ISN_LOADS instruction.
1358 */
1359 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001360generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364 int sid,
1365 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366{
1367 isn_T *isn;
1368
Bram Moolenaar080457c2020-03-03 21:53:32 +01001369 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001370 if (isn_type == ISN_LOADS)
1371 isn = generate_instr_type(cctx, isn_type, type);
1372 else
1373 isn = generate_instr_drop(cctx, isn_type, 1);
1374 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001376 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1377 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378
1379 return OK;
1380}
1381
1382/*
1383 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1384 */
1385 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001386generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 cctx_T *cctx,
1388 isntype_T isn_type,
1389 int sid,
1390 int idx,
1391 type_T *type)
1392{
1393 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001394 scriptref_T *sref;
1395 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396
Bram Moolenaar080457c2020-03-03 21:53:32 +01001397 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 if (isn_type == ISN_LOADSCRIPT)
1399 isn = generate_instr_type(cctx, isn_type, type);
1400 else
1401 isn = generate_instr_drop(cctx, isn_type, 1);
1402 if (isn == NULL)
1403 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001404
1405 // This requires three arguments, which doesn't fit in an instruction, thus
1406 // we need to allocate a struct for this.
1407 sref = ALLOC_ONE(scriptref_T);
1408 if (sref == NULL)
1409 return FAIL;
1410 isn->isn_arg.script.scriptref = sref;
1411 sref->sref_sid = sid;
1412 sref->sref_idx = idx;
1413 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001414 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 return OK;
1416}
1417
1418/*
1419 * Generate an ISN_NEWLIST instruction.
1420 */
1421 static int
1422generate_NEWLIST(cctx_T *cctx, int count)
1423{
1424 isn_T *isn;
1425 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 type_T *type;
1427 type_T *member;
1428
Bram Moolenaar080457c2020-03-03 21:53:32 +01001429 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1431 return FAIL;
1432 isn->isn_arg.number = count;
1433
Bram Moolenaar127542b2020-08-09 17:22:04 +02001434 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001435 if (count == 0)
1436 member = &t_void;
1437 else
1438 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001439 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1440 cctx->ctx_type_list);
1441 type = get_list_type(member, cctx->ctx_type_list);
1442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 // drop the value types
1444 stack->ga_len -= count;
1445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 // add the list type to the type stack
1447 if (ga_grow(stack, 1) == FAIL)
1448 return FAIL;
1449 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1450 ++stack->ga_len;
1451
1452 return OK;
1453}
1454
1455/*
1456 * Generate an ISN_NEWDICT instruction.
1457 */
1458 static int
1459generate_NEWDICT(cctx_T *cctx, int count)
1460{
1461 isn_T *isn;
1462 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 type_T *type;
1464 type_T *member;
1465
Bram Moolenaar080457c2020-03-03 21:53:32 +01001466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1468 return FAIL;
1469 isn->isn_arg.number = count;
1470
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001471 if (count == 0)
1472 member = &t_void;
1473 else
1474 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001475 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1476 cctx->ctx_type_list);
1477 type = get_dict_type(member, cctx->ctx_type_list);
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // drop the key and value types
1480 stack->ga_len -= 2 * count;
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 // add the dict type to the type stack
1483 if (ga_grow(stack, 1) == FAIL)
1484 return FAIL;
1485 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1486 ++stack->ga_len;
1487
1488 return OK;
1489}
1490
1491/*
1492 * Generate an ISN_FUNCREF instruction.
1493 */
1494 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001495generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496{
1497 isn_T *isn;
1498 garray_T *stack = &cctx->ctx_type_stack;
1499
Bram Moolenaar080457c2020-03-03 21:53:32 +01001500 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1502 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001503 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001504 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505
Bram Moolenaarab360522021-01-10 14:02:28 +01001506 // if the referenced function is a closure, it may use items further up in
1507 // the nested context, including this one.
1508 if (ufunc->uf_flags & FC_CLOSURE)
1509 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if (ga_grow(stack, 1) == FAIL)
1512 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001513 ((type_T **)stack->ga_data)[stack->ga_len] =
1514 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 ++stack->ga_len;
1516
1517 return OK;
1518}
1519
1520/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001521 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001522 * "lambda_name" and "func_name" must be in allocated memory and will be
1523 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001524 */
1525 static int
1526generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1527{
1528 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001529
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001530 if (cctx->ctx_skip == SKIP_YES)
1531 {
1532 vim_free(lambda_name);
1533 vim_free(func_name);
1534 return OK;
1535 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001536 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001537 {
1538 vim_free(lambda_name);
1539 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001540 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001541 }
1542 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001543 isn->isn_arg.newfunc.nf_global = func_name;
1544
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001549 * Generate an ISN_DEF instruction: list functions
1550 */
1551 static int
1552generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1553{
1554 isn_T *isn;
1555
1556 RETURN_OK_IF_SKIP(cctx);
1557 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1558 return FAIL;
1559 if (len > 0)
1560 {
1561 isn->isn_arg.string = vim_strnsave(name, len);
1562 if (isn->isn_arg.string == NULL)
1563 return FAIL;
1564 }
1565 return OK;
1566}
1567
1568/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 * Generate an ISN_JUMP instruction.
1570 */
1571 static int
1572generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
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_JUMP)) == NULL)
1579 return FAIL;
1580 isn->isn_arg.jump.jump_when = when;
1581 isn->isn_arg.jump.jump_where = where;
1582
1583 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1584 --stack->ga_len;
1585
1586 return OK;
1587}
1588
1589 static int
1590generate_FOR(cctx_T *cctx, int loop_idx)
1591{
1592 isn_T *isn;
1593 garray_T *stack = &cctx->ctx_type_stack;
1594
Bram Moolenaar080457c2020-03-03 21:53:32 +01001595 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1597 return FAIL;
1598 isn->isn_arg.forloop.for_idx = loop_idx;
1599
1600 if (ga_grow(stack, 1) == FAIL)
1601 return FAIL;
1602 // type doesn't matter, will be stored next
1603 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1604 ++stack->ga_len;
1605
1606 return OK;
1607}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001608/*
1609 * Generate an ISN_TRYCONT instruction.
1610 */
1611 static int
1612generate_TRYCONT(cctx_T *cctx, int levels, int where)
1613{
1614 isn_T *isn;
1615
1616 RETURN_OK_IF_SKIP(cctx);
1617 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1618 return FAIL;
1619 isn->isn_arg.trycont.tct_levels = levels;
1620 isn->isn_arg.trycont.tct_where = where;
1621
1622 return OK;
1623}
1624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625
1626/*
1627 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001628 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 * Return FAIL if the number of arguments is wrong.
1630 */
1631 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001632generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633{
1634 isn_T *isn;
1635 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001636 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001637 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001638 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639
Bram Moolenaar080457c2020-03-03 21:53:32 +01001640 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001641 argoff = check_internal_func(func_idx, argcount);
1642 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001643 return FAIL;
1644
Bram Moolenaar389df252020-07-09 21:20:47 +02001645 if (method_call && argoff > 1)
1646 {
1647 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1648 return FAIL;
1649 isn->isn_arg.shuffle.shfl_item = argcount;
1650 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1651 }
1652
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001653 if (argcount > 0)
1654 {
1655 // Check the types of the arguments.
1656 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001657 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1658 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001659 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001660 if (internal_func_is_map(func_idx))
1661 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001662 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1665 return FAIL;
1666 isn->isn_arg.bfunc.cbf_idx = func_idx;
1667 isn->isn_arg.bfunc.cbf_argcount = argcount;
1668
Bram Moolenaar94738d82020-10-21 14:25:07 +02001669 // Drop the argument types and push the return type.
1670 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if (ga_grow(stack, 1) == FAIL)
1672 return FAIL;
1673 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001674 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001675 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001677 if (maptype != NULL && maptype->tt_member != NULL
1678 && maptype->tt_member != &t_any)
1679 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001680 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 return OK;
1683}
1684
1685/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001686 * Generate an ISN_LISTAPPEND instruction. Works like add().
1687 * Argument count is already checked.
1688 */
1689 static int
1690generate_LISTAPPEND(cctx_T *cctx)
1691{
1692 garray_T *stack = &cctx->ctx_type_stack;
1693 type_T *list_type;
1694 type_T *item_type;
1695 type_T *expected;
1696
1697 // Caller already checked that list_type is a list.
1698 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1699 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1700 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001701 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001702 return FAIL;
1703
1704 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1705 return FAIL;
1706
1707 --stack->ga_len; // drop the argument
1708 return OK;
1709}
1710
1711/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001712 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1713 * Argument count is already checked.
1714 */
1715 static int
1716generate_BLOBAPPEND(cctx_T *cctx)
1717{
1718 garray_T *stack = &cctx->ctx_type_stack;
1719 type_T *item_type;
1720
1721 // Caller already checked that blob_type is a blob.
1722 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001723 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001724 return FAIL;
1725
1726 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1727 return FAIL;
1728
1729 --stack->ga_len; // drop the argument
1730 return OK;
1731}
1732
1733/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001734 * Return TRUE if "ufunc" should be compiled, taking into account whether
1735 * "profile" indicates profiling is to be done.
1736 */
1737 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001738func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001739{
1740 switch (ufunc->uf_def_status)
1741 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001742 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001743 case UF_TO_BE_COMPILED: return TRUE;
1744 case UF_COMPILED:
1745 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001746#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001747 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1748 + ufunc->uf_dfunc_idx;
1749
1750 return profile ? dfunc->df_instr_prof == NULL
1751 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001752#else
1753 break;
1754#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001755 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001756 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001757 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001758 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001759}
1760
1761/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 * Generate an ISN_DCALL or ISN_UCALL instruction.
1763 * Return FAIL if the number of arguments is wrong.
1764 */
1765 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001766generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767{
1768 isn_T *isn;
1769 garray_T *stack = &cctx->ctx_type_stack;
1770 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001771 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772
Bram Moolenaar080457c2020-03-03 21:53:32 +01001773 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 if (argcount > regular_args && !has_varargs(ufunc))
1775 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001776 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 return FAIL;
1778 }
1779 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1780 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001781 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 return FAIL;
1783 }
1784
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001785 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001786 {
1787 int i;
1788
1789 for (i = 0; i < argcount; ++i)
1790 {
1791 type_T *expected;
1792 type_T *actual;
1793
1794 if (i < regular_args)
1795 {
1796 if (ufunc->uf_arg_types == NULL)
1797 continue;
1798 expected = ufunc->uf_arg_types[i];
1799 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001800 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1801 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001802 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001803 else
1804 expected = ufunc->uf_va_type->tt_member;
1805 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001806 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001807 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001808 {
1809 arg_type_mismatch(expected, actual, i + 1);
1810 return FAIL;
1811 }
1812 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001813 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001814 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001815 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001816 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001817 }
1818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001820 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001821 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001823 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 {
1825 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1826 isn->isn_arg.dfunc.cdf_argcount = argcount;
1827 }
1828 else
1829 {
1830 // A user function may be deleted and redefined later, can't use the
1831 // ufunc pointer, need to look it up again at runtime.
1832 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1833 isn->isn_arg.ufunc.cuf_argcount = argcount;
1834 }
1835
1836 stack->ga_len -= argcount; // drop the arguments
1837 if (ga_grow(stack, 1) == FAIL)
1838 return FAIL;
1839 // add return value
1840 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1841 ++stack->ga_len;
1842
1843 return OK;
1844}
1845
1846/*
1847 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1848 */
1849 static int
1850generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1851{
1852 isn_T *isn;
1853 garray_T *stack = &cctx->ctx_type_stack;
1854
Bram Moolenaar080457c2020-03-03 21:53:32 +01001855 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1857 return FAIL;
1858 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1859 isn->isn_arg.ufunc.cuf_argcount = argcount;
1860
1861 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001862 if (ga_grow(stack, 1) == FAIL)
1863 return FAIL;
1864 // add return value
1865 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1866 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867
1868 return OK;
1869}
1870
1871/*
1872 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001873 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 */
1875 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001876generate_PCALL(
1877 cctx_T *cctx,
1878 int argcount,
1879 char_u *name,
1880 type_T *type,
1881 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882{
1883 isn_T *isn;
1884 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001885 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886
Bram Moolenaar080457c2020-03-03 21:53:32 +01001887 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001888
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001889 if (type->tt_type == VAR_ANY)
1890 ret_type = &t_any;
1891 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001892 {
1893 if (type->tt_argcount != -1)
1894 {
1895 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1896
1897 if (argcount < type->tt_min_argcount - varargs)
1898 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001899 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001900 return FAIL;
1901 }
1902 if (!varargs && argcount > type->tt_argcount)
1903 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001904 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001905 return FAIL;
1906 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001907 if (type->tt_args != NULL)
1908 {
1909 int i;
1910
1911 for (i = 0; i < argcount; ++i)
1912 {
1913 int offset = -argcount + i - 1;
1914 type_T *actual = ((type_T **)stack->ga_data)[
1915 stack->ga_len + offset];
1916 type_T *expected;
1917
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001918 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001919 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001920 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001921 else
1922 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001923 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001924 cctx, TRUE, FALSE) == FAIL)
1925 {
1926 arg_type_mismatch(expected, actual, i + 1);
1927 return FAIL;
1928 }
1929 }
1930 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001931 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001932 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001933 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001934 else
1935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001936 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001937 return FAIL;
1938 }
1939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1941 return FAIL;
1942 isn->isn_arg.pfunc.cpf_top = at_top;
1943 isn->isn_arg.pfunc.cpf_argcount = argcount;
1944
1945 stack->ga_len -= argcount; // drop the arguments
1946
1947 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001948 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001950 // If partial is above the arguments it must be cleared and replaced with
1951 // the return value.
1952 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1953 return FAIL;
1954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 return OK;
1956}
1957
1958/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001959 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 */
1961 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001962generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963{
1964 isn_T *isn;
1965 garray_T *stack = &cctx->ctx_type_stack;
1966 type_T *type;
1967
Bram Moolenaar080457c2020-03-03 21:53:32 +01001968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001969 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001971 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001973 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001975 if (type->tt_type != VAR_DICT && type != &t_any)
1976 {
1977 emsg(_(e_dictreq));
1978 return FAIL;
1979 }
1980 // change dict type to dict member type
1981 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001982 {
1983 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1984 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986
1987 return OK;
1988}
1989
1990/*
1991 * Generate an ISN_ECHO instruction.
1992 */
1993 static int
1994generate_ECHO(cctx_T *cctx, int with_white, int count)
1995{
1996 isn_T *isn;
1997
Bram Moolenaar080457c2020-03-03 21:53:32 +01001998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2000 return FAIL;
2001 isn->isn_arg.echo.echo_with_white = with_white;
2002 isn->isn_arg.echo.echo_count = count;
2003
2004 return OK;
2005}
2006
Bram Moolenaarad39c092020-02-26 18:23:43 +01002007/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002008 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01002009 */
2010 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002011generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002012{
2013 isn_T *isn;
2014
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002015 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002016 return FAIL;
2017 isn->isn_arg.number = count;
2018
2019 return OK;
2020}
2021
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002022/*
2023 * Generate an ISN_PUT instruction.
2024 */
2025 static int
2026generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2027{
2028 isn_T *isn;
2029
2030 RETURN_OK_IF_SKIP(cctx);
2031 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2032 return FAIL;
2033 isn->isn_arg.put.put_regname = regname;
2034 isn->isn_arg.put.put_lnum = lnum;
2035 return OK;
2036}
2037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 static int
2039generate_EXEC(cctx_T *cctx, char_u *line)
2040{
2041 isn_T *isn;
2042
Bram Moolenaar080457c2020-03-03 21:53:32 +01002043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2045 return FAIL;
2046 isn->isn_arg.string = vim_strsave(line);
2047 return OK;
2048}
2049
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002050 static int
2051generate_EXECCONCAT(cctx_T *cctx, int count)
2052{
2053 isn_T *isn;
2054
2055 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2056 return FAIL;
2057 isn->isn_arg.number = count;
2058 return OK;
2059}
2060
Bram Moolenaar08597872020-12-10 19:43:40 +01002061/*
2062 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2063 */
2064 static int
2065generate_RANGE(cctx_T *cctx, char_u *range)
2066{
2067 isn_T *isn;
2068 garray_T *stack = &cctx->ctx_type_stack;
2069
2070 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2071 return FAIL;
2072 isn->isn_arg.string = range;
2073
2074 if (ga_grow(stack, 1) == FAIL)
2075 return FAIL;
2076 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2077 ++stack->ga_len;
2078 return OK;
2079}
2080
Bram Moolenaar792f7862020-11-23 08:31:18 +01002081 static int
2082generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2083{
2084 isn_T *isn;
2085
2086 RETURN_OK_IF_SKIP(cctx);
2087 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2088 return FAIL;
2089 isn->isn_arg.unpack.unp_count = var_count;
2090 isn->isn_arg.unpack.unp_semicolon = semicolon;
2091 return OK;
2092}
2093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002095 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002096 */
2097 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002098generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002099{
2100 isn_T *isn;
2101
Bram Moolenaar02194d22020-10-24 23:08:38 +02002102 if (cmod->cmod_flags != 0
2103 || cmod->cmod_split != 0
2104 || cmod->cmod_verbose != 0
2105 || cmod->cmod_tab != 0
2106 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002107 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002108 cctx->ctx_has_cmdmod = TRUE;
2109
2110 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002111 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002112 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2113 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2114 return FAIL;
2115 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002116 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002117 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002118 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002119
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002120 return OK;
2121}
2122
2123 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002124generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002125{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002126 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2127 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002128 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002129 return OK;
2130}
2131
Bram Moolenaarf002a412021-01-24 13:34:18 +01002132#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002133 static void
2134may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2135{
2136 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002137 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002138}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002139#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002140
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002141/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002143 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002145 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002146reserve_local(
2147 cctx_T *cctx,
2148 char_u *name,
2149 size_t len,
2150 int isConst,
2151 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 lvar_T *lvar;
2154
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002155 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002157 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002158 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 }
2160
2161 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002162 return NULL;
2163 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002164 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002166 // Every local variable uses the next entry on the stack. We could re-use
2167 // the last ones when leaving a scope, but then variables used in a closure
2168 // might get overwritten. To keep things simple do not re-use stack
2169 // entries. This is less efficient, but memory is cheap these days.
2170 lvar->lv_idx = cctx->ctx_locals_count++;
2171
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002172 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 lvar->lv_const = isConst;
2174 lvar->lv_type = type;
2175
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002176 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177}
2178
2179/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002180 * Remove local variables above "new_top".
2181 */
2182 static void
2183unwind_locals(cctx_T *cctx, int new_top)
2184{
2185 if (cctx->ctx_locals.ga_len > new_top)
2186 {
2187 int idx;
2188 lvar_T *lvar;
2189
2190 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2191 {
2192 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2193 vim_free(lvar->lv_name);
2194 }
2195 }
2196 cctx->ctx_locals.ga_len = new_top;
2197}
2198
2199/*
2200 * Free all local variables.
2201 */
2202 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002203free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002204{
2205 unwind_locals(cctx, 0);
2206 ga_clear(&cctx->ctx_locals);
2207}
2208
2209/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002210 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2211 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2212 * error if the variable was defined with :const.
2213 */
2214 static int
2215check_item_writable(svar_T *sv, int check_writable, char_u *name)
2216{
2217 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2218 || (check_writable == ASSIGN_FINAL
2219 && sv->sv_const == ASSIGN_CONST))
2220 {
2221 semsg(_(e_readonlyvar), name);
2222 return FAIL;
2223 }
2224 return OK;
2225}
2226
2227/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002229 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 * Returns the index in "sn_var_vals" if found.
2231 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002232 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 */
2234 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002235get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236{
2237 hashtab_T *ht;
2238 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002239 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002240 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 int idx;
2242
Bram Moolenaare3d46852020-08-29 13:39:17 +02002243 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002245 if (sid == current_sctx.sc_sid)
2246 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002247 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002248
2249 if (sav == NULL)
2250 return -2;
2251 idx = sav->sav_var_vals_idx;
2252 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002253 if (check_item_writable(sv, check_writable, name) == FAIL)
2254 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002255 return idx;
2256 }
2257
2258 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 ht = &SCRIPT_VARS(sid);
2260 di = find_var_in_ht(ht, 0, name, TRUE);
2261 if (di == NULL)
2262 return -2;
2263
2264 // Now find the svar_T index in sn_var_vals.
2265 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2266 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002267 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 if (sv->sv_tv == &di->di_tv)
2269 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002270 if (check_item_writable(sv, check_writable, name) == FAIL)
2271 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 return idx;
2273 }
2274 }
2275 return -1;
2276}
2277
2278/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002279 * Find "name" in imported items of the current script or in "cctx" if not
2280 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 */
2282 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002283find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 int idx;
2286
Bram Moolenaare3d46852020-08-29 13:39:17 +02002287 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002288 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 if (cctx != NULL)
2290 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2291 {
2292 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2293 + idx;
2294
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002295 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2296 : STRLEN(import->imp_name) == len
2297 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 return import;
2299 }
2300
Bram Moolenaarefa94442020-08-08 22:16:00 +02002301 return find_imported_in_script(name, len, current_sctx.sc_sid);
2302}
2303
2304 imported_T *
2305find_imported_in_script(char_u *name, size_t len, int sid)
2306{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002307 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002308 int idx;
2309
Bram Moolenaare3d46852020-08-29 13:39:17 +02002310 if (!SCRIPT_ID_VALID(sid))
2311 return NULL;
2312 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2314 {
2315 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2316
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002317 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2318 : STRLEN(import->imp_name) == len
2319 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 return import;
2321 }
2322 return NULL;
2323}
2324
2325/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002326 * Free all imported variables.
2327 */
2328 static void
2329free_imported(cctx_T *cctx)
2330{
2331 int idx;
2332
2333 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2334 {
2335 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2336
2337 vim_free(import->imp_name);
2338 }
2339 ga_clear(&cctx->ctx_imports);
2340}
2341
2342/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002343 * Return a pointer to the next line that isn't empty or only contains a
2344 * comment. Skips over white space.
2345 * Returns NULL if there is none.
2346 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002347 char_u *
2348peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002349{
2350 int lnum = cctx->ctx_lnum;
2351
2352 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2353 {
2354 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002355 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002356
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002357 // ignore NULLs inserted for continuation lines
2358 if (line != NULL)
2359 {
2360 p = skipwhite(line);
2361 if (*p != NUL && !vim9_comment_start(p))
2362 return p;
2363 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002364 }
2365 return NULL;
2366}
2367
2368/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002369 * Called when checking for a following operator at "arg". When the rest of
2370 * the line is empty or only a comment, peek the next line. If there is a next
2371 * line return a pointer to it and set "nextp".
2372 * Otherwise skip over white space.
2373 */
2374 static char_u *
2375may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2376{
2377 char_u *p = skipwhite(arg);
2378
2379 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002380 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002381 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002382 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002383 if (*nextp != NULL)
2384 return *nextp;
2385 }
2386 return p;
2387}
2388
2389/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002390 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002391 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002392 * Returns NULL when at the end.
2393 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002394 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002395next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002396{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002397 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002398
2399 do
2400 {
2401 ++cctx->ctx_lnum;
2402 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002403 {
2404 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002405 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002406 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002407 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002408 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002409 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002410 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002411 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002412 return line;
2413}
2414
2415/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002416 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002417 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002418 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002419 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2420 */
2421 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002422may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002423{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002424 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002425 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002426 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002427 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002428
2429 if (next == NULL)
2430 return FAIL;
2431 *arg = skipwhite(next);
2432 }
2433 return OK;
2434}
2435
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002436/*
2437 * Idem, and give an error when failed.
2438 */
2439 static int
2440may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2441{
2442 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2443 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002444 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002445 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002446 return FAIL;
2447 }
2448 return OK;
2449}
2450
2451
Bram Moolenaara5565e42020-05-09 15:44:01 +02002452// Structure passed between the compile_expr* functions to keep track of
2453// constants that have been parsed but for which no code was produced yet. If
2454// possible expressions on these constants are applied at compile time. If
2455// that is not possible, the code to push the constants needs to be generated
2456// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002457// Using 50 should be more than enough of 5 levels of ().
2458#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002459typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002460 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002461 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002462 int pp_is_const; // all generated code was constants, used for a
2463 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002464} ppconst_T;
2465
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002466static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002467static int compile_expr0(char_u **arg, cctx_T *cctx);
2468static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2469
Bram Moolenaara5565e42020-05-09 15:44:01 +02002470/*
2471 * Generate a PUSH instruction for "tv".
2472 * "tv" will be consumed or cleared.
2473 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2474 */
2475 static int
2476generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2477{
2478 if (tv != NULL)
2479 {
2480 switch (tv->v_type)
2481 {
2482 case VAR_UNKNOWN:
2483 break;
2484 case VAR_BOOL:
2485 generate_PUSHBOOL(cctx, tv->vval.v_number);
2486 break;
2487 case VAR_SPECIAL:
2488 generate_PUSHSPEC(cctx, tv->vval.v_number);
2489 break;
2490 case VAR_NUMBER:
2491 generate_PUSHNR(cctx, tv->vval.v_number);
2492 break;
2493#ifdef FEAT_FLOAT
2494 case VAR_FLOAT:
2495 generate_PUSHF(cctx, tv->vval.v_float);
2496 break;
2497#endif
2498 case VAR_BLOB:
2499 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2500 tv->vval.v_blob = NULL;
2501 break;
2502 case VAR_STRING:
2503 generate_PUSHS(cctx, tv->vval.v_string);
2504 tv->vval.v_string = NULL;
2505 break;
2506 default:
2507 iemsg("constant type not supported");
2508 clear_tv(tv);
2509 return FAIL;
2510 }
2511 tv->v_type = VAR_UNKNOWN;
2512 }
2513 return OK;
2514}
2515
2516/*
2517 * Generate code for any ppconst entries.
2518 */
2519 static int
2520generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2521{
2522 int i;
2523 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002524 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002525
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002526 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002527 for (i = 0; i < ppconst->pp_used; ++i)
2528 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2529 ret = FAIL;
2530 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002531 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002532 return ret;
2533}
2534
2535/*
2536 * Clear ppconst constants. Used when failing.
2537 */
2538 static void
2539clear_ppconst(ppconst_T *ppconst)
2540{
2541 int i;
2542
2543 for (i = 0; i < ppconst->pp_used; ++i)
2544 clear_tv(&ppconst->pp_tv[i]);
2545 ppconst->pp_used = 0;
2546}
2547
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002548/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002549 * Generate an instruction to load script-local variable "name", without the
2550 * leading "s:".
2551 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 */
2553 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002554compile_load_scriptvar(
2555 cctx_T *cctx,
2556 char_u *name, // variable NUL terminated
2557 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002558 char_u **end, // end of variable
2559 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002561 scriptitem_T *si;
2562 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 imported_T *import;
2564
Bram Moolenaare3d46852020-08-29 13:39:17 +02002565 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2566 return FAIL;
2567 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002568 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002569 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002571 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002572 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2573 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 }
2575 if (idx >= 0)
2576 {
2577 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2578
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002579 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 current_sctx.sc_sid, idx, sv->sv_type);
2581 return OK;
2582 }
2583
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002584 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 if (import != NULL)
2586 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002587 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002588 {
2589 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002590 char_u *exp_name;
2591 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002592 ufunc_T *ufunc;
2593 type_T *type;
2594
2595 // Used "import * as Name", need to lookup the member.
2596 if (*p != '.')
2597 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002598 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002599 return FAIL;
2600 }
2601 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002602 if (VIM_ISWHITE(*p))
2603 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002604 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002605 return FAIL;
2606 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002607
Bram Moolenaar1c991142020-07-04 13:15:31 +02002608 // isolate one name
2609 exp_name = p;
2610 while (eval_isnamec(*p))
2611 ++p;
2612 cc = *p;
2613 *p = NUL;
2614
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002615 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002616 *p = cc;
2617 p = skipwhite(p);
2618
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002619 // TODO: what if it is a function?
2620 if (idx < 0)
2621 return FAIL;
2622 *end = p;
2623
2624 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2625 import->imp_sid,
2626 idx,
2627 type);
2628 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002629 else if (import->imp_funcname != NULL)
2630 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002631 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002632 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2633 import->imp_sid,
2634 import->imp_var_vals_idx,
2635 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 return OK;
2637 }
2638
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002639 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002640 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 return FAIL;
2642}
2643
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002644 static int
2645generate_funcref(cctx_T *cctx, char_u *name)
2646{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002647 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002648
2649 if (ufunc == NULL)
2650 return FAIL;
2651
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002652 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002653 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2654 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002655 == FAIL)
2656 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002657 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002658}
2659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660/*
2661 * Compile a variable name into a load instruction.
2662 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002663 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 * When "error" is FALSE do not give an error when not found.
2665 */
2666 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002667compile_load(
2668 char_u **arg,
2669 char_u *end_arg,
2670 cctx_T *cctx,
2671 int is_expr,
2672 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673{
2674 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002675 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002676 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002678 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679
2680 if (*(*arg + 1) == ':')
2681 {
2682 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002683 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002684 {
2685 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002687 switch (**arg)
2688 {
2689 case 'g': isn_type = ISN_LOADGDICT; break;
2690 case 'w': isn_type = ISN_LOADWDICT; break;
2691 case 't': isn_type = ISN_LOADTDICT; break;
2692 case 'b': isn_type = ISN_LOADBDICT; break;
2693 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002694 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002695 goto theend;
2696 }
2697 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2698 goto theend;
2699 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 else
2702 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002703 isntype_T isn_type = ISN_DROP;
2704
2705 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2706 if (name == NULL)
2707 return FAIL;
2708
2709 switch (**arg)
2710 {
2711 case 'v': res = generate_LOADV(cctx, name, error);
2712 break;
2713 case 's': res = compile_load_scriptvar(cctx, name,
2714 NULL, NULL, error);
2715 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002716 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2717 isn_type = ISN_LOADG;
2718 else
2719 {
2720 isn_type = ISN_LOADAUTO;
2721 vim_free(name);
2722 name = vim_strnsave(*arg, end - *arg);
2723 if (name == NULL)
2724 return FAIL;
2725 }
2726 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002727 case 'w': isn_type = ISN_LOADW; break;
2728 case 't': isn_type = ISN_LOADT; break;
2729 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002730 default: // cannot happen, just in case
2731 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002732 goto theend;
2733 }
2734 if (isn_type != ISN_DROP)
2735 {
2736 // Global, Buffer-local, Window-local and Tabpage-local
2737 // variables can be defined later, thus we don't check if it
2738 // exists, give error at runtime.
2739 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2740 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 }
2742 }
2743 else
2744 {
2745 size_t len = end - *arg;
2746 int idx;
2747 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002748 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749
2750 name = vim_strnsave(*arg, end - *arg);
2751 if (name == NULL)
2752 return FAIL;
2753
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002754 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002756 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002757 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759 else
2760 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002761 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002762
Bram Moolenaar709664c2020-12-12 14:33:41 +01002763 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002765 type = lvar.lv_type;
2766 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002767 if (lvar.lv_from_outer != 0)
2768 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002769 else
2770 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 }
2772 else
2773 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002774 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002775 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002776 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002777 || find_imported(name, 0, cctx) != NULL)
2778 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002779
Bram Moolenaar0f769812020-09-12 18:32:34 +02002780 // When evaluating an expression and the name starts with an
2781 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002782 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002783 if (res == FAIL && is_expr
2784 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002785 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 }
2787 }
2788 if (gen_load)
2789 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002790 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002791 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002792 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002793 cctx->ctx_outer_used = TRUE;
2794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 }
2796
2797 *arg = end;
2798
2799theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002800 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002801 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 vim_free(name);
2803 return res;
2804}
2805
2806/*
2807 * Compile the argument expressions.
2808 * "arg" points to just after the "(" and is advanced to after the ")"
2809 */
2810 static int
2811compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2812{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002813 char_u *p = *arg;
2814 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002815 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816
Bram Moolenaare6085c52020-04-12 20:19:16 +02002817 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002819 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2820 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002821 if (*p == ')')
2822 {
2823 *arg = p + 1;
2824 return OK;
2825 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002826 if (must_end)
2827 {
2828 semsg(_(e_missing_comma_before_argument_str), p);
2829 return FAIL;
2830 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002831
Bram Moolenaara5565e42020-05-09 15:44:01 +02002832 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 return FAIL;
2834 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002835
2836 if (*p != ',' && *skipwhite(p) == ',')
2837 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002838 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002839 p = skipwhite(p);
2840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002842 {
2843 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002844 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002845 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002846 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002847 else
2848 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002849 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002850 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002852failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002853 emsg(_(e_missing_close));
2854 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855}
2856
2857/*
2858 * Compile a function call: name(arg1, arg2)
2859 * "arg" points to "name", "arg + varlen" to the "(".
2860 * "argcount_init" is 1 for "value->method()"
2861 * Instructions:
2862 * EVAL arg1
2863 * EVAL arg2
2864 * BCALL / DCALL / UCALL
2865 */
2866 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002867compile_call(
2868 char_u **arg,
2869 size_t varlen,
2870 cctx_T *cctx,
2871 ppconst_T *ppconst,
2872 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873{
2874 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002875 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 int argcount = argcount_init;
2877 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002878 char_u fname_buf[FLEN_FIXED + 1];
2879 char_u *tofree = NULL;
2880 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002881 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002882 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002883 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884
Bram Moolenaara5565e42020-05-09 15:44:01 +02002885 // we can evaluate "has('name')" at compile time
2886 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2887 {
2888 char_u *s = skipwhite(*arg + varlen + 1);
2889 typval_T argvars[2];
2890
2891 argvars[0].v_type = VAR_UNKNOWN;
2892 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002893 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002894 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002895 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002896 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002897 if (*s == ')' && argvars[0].v_type == VAR_STRING
2898 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002899 {
2900 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2901
2902 *arg = s + 1;
2903 argvars[1].v_type = VAR_UNKNOWN;
2904 tv->v_type = VAR_NUMBER;
2905 tv->vval.v_number = 0;
2906 f_has(argvars, tv);
2907 clear_tv(&argvars[0]);
2908 ++ppconst->pp_used;
2909 return OK;
2910 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002911 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002912 }
2913
2914 if (generate_ppconst(cctx, ppconst) == FAIL)
2915 return FAIL;
2916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 if (varlen >= sizeof(namebuf))
2918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002919 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 return FAIL;
2921 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002922 vim_strncpy(namebuf, *arg, varlen);
2923 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924
2925 *arg = skipwhite(*arg + varlen + 1);
2926 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002927 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928
Bram Moolenaar03290b82020-12-19 16:30:44 +01002929 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002930 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 {
2932 int idx;
2933
2934 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002935 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002937 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002938 if (STRCMP(name, "flatten") == 0)
2939 {
2940 emsg(_(e_cannot_use_flatten_in_vim9_script));
2941 goto theend;
2942 }
2943
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002944 if (STRCMP(name, "add") == 0 && argcount == 2)
2945 {
2946 garray_T *stack = &cctx->ctx_type_stack;
2947 type_T *type = ((type_T **)stack->ga_data)[
2948 stack->ga_len - 2];
2949
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002950 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002951 if (type->tt_type == VAR_LIST)
2952 {
2953 // inline "add(list, item)" so that the type can be checked
2954 res = generate_LISTAPPEND(cctx);
2955 idx = -1;
2956 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002957 else if (type->tt_type == VAR_BLOB)
2958 {
2959 // inline "add(blob, nr)" so that the type can be checked
2960 res = generate_BLOBAPPEND(cctx);
2961 idx = -1;
2962 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002963 }
2964
2965 if (idx >= 0)
2966 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2967 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002968 else
2969 semsg(_(e_unknownfunc), namebuf);
2970 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 }
2972
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002973 // An argument or local variable can be a function reference, this
2974 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002975 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002976 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002977 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002978 // If we can find the function by name generate the right call.
2979 // Skip global functions here, a local funcref takes precedence.
2980 ufunc = find_func(name, FALSE, cctx);
2981 if (ufunc != NULL && !func_is_global(ufunc))
2982 {
2983 res = generate_CALL(cctx, ufunc, argcount);
2984 goto theend;
2985 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987
2988 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002989 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002990 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002992 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002993 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002994 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002995 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002996 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002997
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002998 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002999 goto theend;
3000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001
Bram Moolenaar0f769812020-09-12 18:32:34 +02003002 // If we can find a global function by name generate the right call.
3003 if (ufunc != NULL)
3004 {
3005 res = generate_CALL(cctx, ufunc, argcount);
3006 goto theend;
3007 }
3008
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003009 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003010 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02003011 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02003012 res = generate_UCALL(cctx, name, argcount);
3013 else
3014 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003015
3016theend:
3017 vim_free(tofree);
3018 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019}
3020
3021// like NAMESPACE_CHAR but with 'a' and 'l'.
3022#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3023
3024/*
3025 * Find the end of a variable or function name. Unlike find_name_end() this
3026 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003027 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 * Return a pointer to just after the name. Equal to "arg" if there is no
3029 * valid name.
3030 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003031 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003032to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033{
3034 char_u *p;
3035
3036 // Quick check for valid starting character.
3037 if (!eval_isnamec1(*arg))
3038 return arg;
3039
3040 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3041 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3042 // and can be used in slice "[n:]".
3043 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003044 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3046 break;
3047 return p;
3048}
3049
3050/*
3051 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003052 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 */
3054 char_u *
3055to_name_const_end(char_u *arg)
3056{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003057 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 typval_T rettv;
3059
3060 if (p == arg && *arg == '[')
3061 {
3062
3063 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003064 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 p = arg;
3066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 return p;
3068}
3069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070/*
3071 * parse a list: [expr, expr]
3072 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003073 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 */
3075 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003076compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077{
3078 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003079 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003081 int is_const;
3082 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003084 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003086 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003087 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003088 semsg(_(e_list_end), *arg);
3089 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003090 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003091 if (*p == ',')
3092 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003093 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003094 return FAIL;
3095 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003096 if (*p == ']')
3097 {
3098 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003099 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003100 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003101 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003102 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003103 if (!is_const)
3104 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 ++count;
3106 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003107 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003109 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3110 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003111 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003112 return FAIL;
3113 }
3114 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003115 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 p = skipwhite(p);
3117 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003118 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003120 ppconst->pp_is_const = is_all_const;
3121 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122}
3123
3124/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003125 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003127 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 */
3129 static int
3130compile_lambda(char_u **arg, cctx_T *cctx)
3131{
Bram Moolenaare462f522020-12-27 14:43:30 +01003132 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 typval_T rettv;
3134 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003135 evalarg_T evalarg;
3136
3137 CLEAR_FIELD(evalarg);
3138 evalarg.eval_flags = EVAL_EVALUATE;
3139 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
3141 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003142 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3143 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003144 {
3145 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003146 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003147 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003148
Bram Moolenaar65c44152020-12-24 15:14:01 +01003149 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003151 ++ufunc->uf_refcount;
3152 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153
Bram Moolenaar65c44152020-12-24 15:14:01 +01003154 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003155 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003157 clear_evalarg(&evalarg, NULL);
3158
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003159 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003160 {
3161 // The return type will now be known.
3162 set_function_type(ufunc);
3163
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003164 // The function reference count will be 1. When the ISN_FUNCREF
3165 // instruction is deleted the reference count is decremented and the
3166 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003167 return generate_FUNCREF(cctx, ufunc);
3168 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003169
3170 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 return FAIL;
3172}
3173
3174/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003175 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003177 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 */
3179 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003180compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181{
3182 garray_T *instr = &cctx->ctx_instr;
3183 int count = 0;
3184 dict_T *d = dict_alloc();
3185 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003186 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003187 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003188 int is_const;
3189 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190
3191 if (d == NULL)
3192 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003193 if (generate_ppconst(cctx, ppconst) == FAIL)
3194 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003197 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
Bram Moolenaar23c55272020-06-21 16:58:13 +02003199 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003200 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003201 *arg = NULL;
3202 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003203 }
3204
3205 if (**arg == '}')
3206 break;
3207
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003208 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003210 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003212 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003213 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003214 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003217 if (isn->isn_type == ISN_PUSHNR)
3218 {
3219 char buf[NUMBUFLEN];
3220
3221 // Convert to string at compile time.
3222 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3223 isn->isn_type = ISN_PUSHS;
3224 isn->isn_arg.string = vim_strsave((char_u *)buf);
3225 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 if (isn->isn_type == ISN_PUSHS)
3227 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003228 else if (may_generate_2STRING(-1, cctx) == FAIL)
3229 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003230 *arg = skipwhite(*arg);
3231 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003232 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003233 emsg(_(e_missing_matching_bracket_after_dict_key));
3234 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003235 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003236 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003238 else
3239 {
3240 // {"name": value},
3241 // {'name': value},
3242 // {name: value} use "name" as a literal key
3243 key = get_literal_key(arg);
3244 if (key == NULL)
3245 return FAIL;
3246 if (generate_PUSHS(cctx, key) == FAIL)
3247 return FAIL;
3248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249
3250 // Check for duplicate keys, if using string keys.
3251 if (key != NULL)
3252 {
3253 item = dict_find(d, key, -1);
3254 if (item != NULL)
3255 {
3256 semsg(_(e_duplicate_key), key);
3257 goto failret;
3258 }
3259 item = dictitem_alloc(key);
3260 if (item != NULL)
3261 {
3262 item->di_tv.v_type = VAR_UNKNOWN;
3263 item->di_tv.v_lock = 0;
3264 if (dict_add(d, item) == FAIL)
3265 dictitem_free(item);
3266 }
3267 }
3268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 if (**arg != ':')
3270 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003271 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003272 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003273 else
3274 semsg(_(e_missing_dict_colon), *arg);
3275 return FAIL;
3276 }
3277 whitep = *arg + 1;
3278 if (!IS_WHITE_OR_NUL(*whitep))
3279 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003280 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 return FAIL;
3282 }
3283
Bram Moolenaar23c55272020-06-21 16:58:13 +02003284 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003285 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003286 *arg = NULL;
3287 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003288 }
3289
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003290 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003292 if (!is_const)
3293 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 ++count;
3295
Bram Moolenaar2c330432020-04-13 14:41:35 +02003296 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003297 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003298 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003299 *arg = NULL;
3300 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 if (**arg == '}')
3303 break;
3304 if (**arg != ',')
3305 {
3306 semsg(_(e_missing_dict_comma), *arg);
3307 goto failret;
3308 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003309 if (IS_WHITE_OR_NUL(*whitep))
3310 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003311 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003312 return FAIL;
3313 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003314 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003315 if (!IS_WHITE_OR_NUL(*whitep))
3316 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003317 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003318 return FAIL;
3319 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003320 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 }
3322
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 *arg = *arg + 1;
3324
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003325 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003326 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003327 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003328 *arg += STRLEN(*arg);
3329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003331 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 return generate_NEWDICT(cctx, count);
3333
3334failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003335 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003336 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003337 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003338 *arg = (char_u *)"";
3339 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 dict_unref(d);
3341 return FAIL;
3342}
3343
3344/*
3345 * Compile "&option".
3346 */
3347 static int
3348compile_get_option(char_u **arg, cctx_T *cctx)
3349{
3350 typval_T rettv;
3351 char_u *start = *arg;
3352 int ret;
3353
3354 // parse the option and get the current value to get the type.
3355 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003356 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 if (ret == OK)
3358 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003360 char_u *name = vim_strnsave(start, *arg - start);
3361 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3362 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
3364 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3365 vim_free(name);
3366 }
3367 clear_tv(&rettv);
3368
3369 return ret;
3370}
3371
3372/*
3373 * Compile "$VAR".
3374 */
3375 static int
3376compile_get_env(char_u **arg, cctx_T *cctx)
3377{
3378 char_u *start = *arg;
3379 int len;
3380 int ret;
3381 char_u *name;
3382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 ++*arg;
3384 len = get_env_len(arg);
3385 if (len == 0)
3386 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003387 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 return FAIL;
3389 }
3390
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003391 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 name = vim_strnsave(start, len + 1);
3393 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3394 vim_free(name);
3395 return ret;
3396}
3397
3398/*
3399 * Compile "@r".
3400 */
3401 static int
3402compile_get_register(char_u **arg, cctx_T *cctx)
3403{
3404 int ret;
3405
3406 ++*arg;
3407 if (**arg == NUL)
3408 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003409 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 return FAIL;
3411 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003412 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 {
3414 emsg_invreg(**arg);
3415 return FAIL;
3416 }
3417 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3418 ++*arg;
3419 return ret;
3420}
3421
3422/*
3423 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003424 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 */
3426 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003427apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003429 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430
3431 // this works from end to start
3432 while (p > start)
3433 {
3434 --p;
3435 if (*p == '-' || *p == '+')
3436 {
3437 // only '-' has an effect, for '+' we only check the type
3438#ifdef FEAT_FLOAT
3439 if (rettv->v_type == VAR_FLOAT)
3440 {
3441 if (*p == '-')
3442 rettv->vval.v_float = -rettv->vval.v_float;
3443 }
3444 else
3445#endif
3446 {
3447 varnumber_T val;
3448 int error = FALSE;
3449
3450 // tv_get_number_chk() accepts a string, but we don't want that
3451 // here
3452 if (check_not_string(rettv) == FAIL)
3453 return FAIL;
3454 val = tv_get_number_chk(rettv, &error);
3455 clear_tv(rettv);
3456 if (error)
3457 return FAIL;
3458 if (*p == '-')
3459 val = -val;
3460 rettv->v_type = VAR_NUMBER;
3461 rettv->vval.v_number = val;
3462 }
3463 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003464 else if (numeric_only)
3465 {
3466 ++p;
3467 break;
3468 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003469 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 {
3471 int v = tv2bool(rettv);
3472
3473 // '!' is permissive in the type.
3474 clear_tv(rettv);
3475 rettv->v_type = VAR_BOOL;
3476 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3477 }
3478 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003479 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 return OK;
3481}
3482
3483/*
3484 * Recognize v: variables that are constants and set "rettv".
3485 */
3486 static void
3487get_vim_constant(char_u **arg, typval_T *rettv)
3488{
3489 if (STRNCMP(*arg, "v:true", 6) == 0)
3490 {
3491 rettv->v_type = VAR_BOOL;
3492 rettv->vval.v_number = VVAL_TRUE;
3493 *arg += 6;
3494 }
3495 else if (STRNCMP(*arg, "v:false", 7) == 0)
3496 {
3497 rettv->v_type = VAR_BOOL;
3498 rettv->vval.v_number = VVAL_FALSE;
3499 *arg += 7;
3500 }
3501 else if (STRNCMP(*arg, "v:null", 6) == 0)
3502 {
3503 rettv->v_type = VAR_SPECIAL;
3504 rettv->vval.v_number = VVAL_NULL;
3505 *arg += 6;
3506 }
3507 else if (STRNCMP(*arg, "v:none", 6) == 0)
3508 {
3509 rettv->v_type = VAR_SPECIAL;
3510 rettv->vval.v_number = VVAL_NONE;
3511 *arg += 6;
3512 }
3513}
3514
Bram Moolenaar657137c2021-01-09 15:45:23 +01003515 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003516get_compare_type(char_u *p, int *len, int *type_is)
3517{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003518 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003519 int i;
3520
3521 switch (p[0])
3522 {
3523 case '=': if (p[1] == '=')
3524 type = EXPR_EQUAL;
3525 else if (p[1] == '~')
3526 type = EXPR_MATCH;
3527 break;
3528 case '!': if (p[1] == '=')
3529 type = EXPR_NEQUAL;
3530 else if (p[1] == '~')
3531 type = EXPR_NOMATCH;
3532 break;
3533 case '>': if (p[1] != '=')
3534 {
3535 type = EXPR_GREATER;
3536 *len = 1;
3537 }
3538 else
3539 type = EXPR_GEQUAL;
3540 break;
3541 case '<': if (p[1] != '=')
3542 {
3543 type = EXPR_SMALLER;
3544 *len = 1;
3545 }
3546 else
3547 type = EXPR_SEQUAL;
3548 break;
3549 case 'i': if (p[1] == 's')
3550 {
3551 // "is" and "isnot"; but not a prefix of a name
3552 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3553 *len = 5;
3554 i = p[*len];
3555 if (!isalnum(i) && i != '_')
3556 {
3557 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3558 *type_is = TRUE;
3559 }
3560 }
3561 break;
3562 }
3563 return type;
3564}
3565
3566/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003567 * Skip over an expression, ignoring most errors.
3568 */
3569 static void
3570skip_expr_cctx(char_u **arg, cctx_T *cctx)
3571{
3572 evalarg_T evalarg;
3573
3574 CLEAR_FIELD(evalarg);
3575 evalarg.eval_cctx = cctx;
3576 skip_expr(arg, &evalarg);
3577}
3578
3579/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003581 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 */
3583 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003584compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003586 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587
3588 // this works from end to start
3589 while (p > start)
3590 {
3591 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003592 while (VIM_ISWHITE(*p))
3593 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 if (*p == '-' || *p == '+')
3595 {
3596 int negate = *p == '-';
3597 isn_T *isn;
3598
3599 // TODO: check type
3600 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3601 {
3602 --p;
3603 if (*p == '-')
3604 negate = !negate;
3605 }
3606 // only '-' has an effect, for '+' we only check the type
3607 if (negate)
3608 isn = generate_instr(cctx, ISN_NEGATENR);
3609 else
3610 isn = generate_instr(cctx, ISN_CHECKNR);
3611 if (isn == NULL)
3612 return FAIL;
3613 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003614 else if (numeric_only)
3615 {
3616 ++p;
3617 break;
3618 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 else
3620 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003621 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003623 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003625 if (p[-1] == '!')
3626 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
3629 if (generate_2BOOL(cctx, invert) == FAIL)
3630 return FAIL;
3631 }
3632 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003633 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 return OK;
3635}
3636
3637/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003638 * Compile "(expression)": recursive!
3639 * Return FAIL/OK.
3640 */
3641 static int
3642compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3643{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003644 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003645 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003646
Bram Moolenaar24156692021-01-14 20:35:49 +01003647 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3648 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003649 if (ppconst->pp_used <= PPSIZE - 10)
3650 {
3651 ret = compile_expr1(arg, cctx, ppconst);
3652 }
3653 else
3654 {
3655 // Not enough space in ppconst, flush constants.
3656 if (generate_ppconst(cctx, ppconst) == FAIL)
3657 return FAIL;
3658 ret = compile_expr0(arg, cctx);
3659 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003660 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3661 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003662 if (**arg == ')')
3663 ++*arg;
3664 else if (ret == OK)
3665 {
3666 emsg(_(e_missing_close));
3667 ret = FAIL;
3668 }
3669 return ret;
3670}
3671
3672/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003674 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 */
3676 static int
3677compile_subscript(
3678 char_u **arg,
3679 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003680 char_u *start_leader,
3681 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003682 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003684 char_u *name_start = *end_leader;
3685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 for (;;)
3687 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003688 char_u *p = skipwhite(*arg);
3689
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003690 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003691 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003692 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003693
3694 // If a following line starts with "->{" or "->X" advance to that
3695 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003696 // Also if a following line starts with ".x".
3697 if (next != NULL &&
3698 ((next[0] == '-' && next[1] == '>'
3699 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003700 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003701 {
3702 next = next_line_from_context(cctx, TRUE);
3703 if (next == NULL)
3704 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003705 *arg = next;
3706 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003707 }
3708 }
3709
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003710 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003711 // not a function call.
3712 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003714 garray_T *stack = &cctx->ctx_type_stack;
3715 type_T *type;
3716 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003718 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003719 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003720 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003723 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3724
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003725 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3727 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003728 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 return FAIL;
3730 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003731 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003733 char_u *pstart = p;
3734
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003735 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003736 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003737 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 // something->method()
3740 // Apply the '!', '-' and '+' first:
3741 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003742 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003745 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003746 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003747 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003748 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003749 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003750 int argcount = 1;
3751 char_u *expr;
3752 garray_T *stack;
3753 type_T *type;
3754
3755 // Funcref call: list->(Refs[2])(arg)
3756 // or lambda: list->((arg) => expr)(arg)
3757 // Fist compile the arguments.
3758 expr = *arg;
3759 *arg = skipwhite(*arg + 1);
3760 skip_expr_cctx(arg, cctx);
3761 *arg = skipwhite(*arg);
3762 if (**arg != ')')
3763 {
3764 semsg(_(e_missing_paren), *arg);
3765 return FAIL;
3766 }
3767 ++*arg;
3768 if (**arg != '(')
3769 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003770 if (*skipwhite(*arg) == '(')
3771 emsg(_(e_nowhitespace));
3772 else
3773 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003774 return FAIL;
3775 }
3776
3777 *arg = skipwhite(*arg + 1);
3778 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3779 return FAIL;
3780
3781 // Compile the function expression.
3782 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3783 return FAIL;
3784
3785 stack = &cctx->ctx_type_stack;
3786 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3787 if (generate_PCALL(cctx, argcount,
3788 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 return FAIL;
3790 }
3791 else
3792 {
3793 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003794 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003795 if (!eval_isnamec1(*p))
3796 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003797 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003798 return FAIL;
3799 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003800 if (ASCII_ISALPHA(*p) && p[1] == ':')
3801 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003802 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 ;
3804 if (*p != '(')
3805 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003806 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 return FAIL;
3808 }
3809 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003810 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 return FAIL;
3812 }
3813 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003814 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003815 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003816 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003817 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003818 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003819 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003820 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003821
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003822 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003823 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003824 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003825 // TODO: blob index
3826 // TODO: more arguments
3827 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003828 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003829 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003830 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003831
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003832 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003833 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003834 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003835 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003836 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003837 // missing first index is equal to zero
3838 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003839 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003840 else
3841 {
3842 if (compile_expr0(arg, cctx) == FAIL)
3843 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003844 if (**arg == ':')
3845 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003846 semsg(_(e_white_space_required_before_and_after_str_at_str),
3847 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003848 return FAIL;
3849 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003850 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003851 return FAIL;
3852 *arg = skipwhite(*arg);
3853 }
3854 if (**arg == ':')
3855 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003856 is_slice = TRUE;
3857 ++*arg;
3858 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3859 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003860 semsg(_(e_white_space_required_before_and_after_str_at_str),
3861 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003862 return FAIL;
3863 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003864 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003865 return FAIL;
3866 if (**arg == ']')
3867 // missing second index is equal to end of string
3868 generate_PUSHNR(cctx, -1);
3869 else
3870 {
3871 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003872 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003873 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003874 return FAIL;
3875 *arg = skipwhite(*arg);
3876 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878
3879 if (**arg != ']')
3880 {
3881 emsg(_(e_missbrac));
3882 return FAIL;
3883 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003884 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003886 // We can index a list and a dict. If we don't know the type
3887 // we can use the index value type.
3888 // TODO: If we don't know use an instruction to figure it out at
3889 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003890 typep = ((type_T **)stack->ga_data) + stack->ga_len
3891 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003892 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003893 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3894 // If the index is a string, the variable must be a Dict.
3895 if (*typep == &t_any && valtype == &t_string)
3896 vtype = VAR_DICT;
3897 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003898 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003899 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003900 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003901 return FAIL;
3902 if (is_slice)
3903 {
3904 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003905 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003906 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003907 return FAIL;
3908 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003909 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003910
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003911 if (vtype == VAR_DICT)
3912 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003913 if (is_slice)
3914 {
3915 emsg(_(e_cannot_slice_dictionary));
3916 return FAIL;
3917 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003918 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003919 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003920 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003921 if (*typep == &t_unknown)
3922 // empty dict was used
3923 *typep = &t_any;
3924 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003925 else
3926 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003927 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003928 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003929 return FAIL;
3930 *typep = &t_any;
3931 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003932 if (may_generate_2STRING(-1, cctx) == FAIL)
3933 return FAIL;
3934 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3935 return FAIL;
3936 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003937 else if (vtype == VAR_STRING)
3938 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003939 *typep = &t_string;
3940 if ((is_slice
3941 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3942 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003943 return FAIL;
3944 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003945 else if (vtype == VAR_BLOB)
3946 {
3947 emsg("Sorry, blob index and slice not implemented yet");
3948 return FAIL;
3949 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003950 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003951 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003952 if (is_slice)
3953 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003954 if (generate_instr_drop(cctx,
3955 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3956 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003957 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003958 }
Bram Moolenaared591872020-08-15 22:14:53 +02003959 else
3960 {
3961 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003962 {
Bram Moolenaared591872020-08-15 22:14:53 +02003963 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003964 if (*typep == &t_unknown)
3965 // empty list was used
3966 *typep = &t_any;
3967 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 if (generate_instr_drop(cctx,
3969 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3970 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003971 return FAIL;
3972 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003973 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003974 else
3975 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003976 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003977 return FAIL;
3978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003980 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003982 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003983 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003984 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003985 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003986
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003987 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003988 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003989 {
3990 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003991 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003992 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003993 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003994 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 while (eval_isnamec(*p))
3996 MB_PTR_ADV(p);
3997 if (p == *arg)
3998 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003999 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 return FAIL;
4001 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004002 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 return FAIL;
4004 *arg = p;
4005 }
4006 else
4007 break;
4008 }
4009
4010 // TODO - see handle_subscript():
4011 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4012 // Don't do this when "Func" is already a partial that was bound
4013 // explicitly (pt_auto is FALSE).
4014
4015 return OK;
4016}
4017
4018/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004019 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4020 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004022 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004023 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004024 *
4025 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 */
4027
4028/*
4029 * number number constant
4030 * 0zFFFFFFFF Blob constant
4031 * "string" string constant
4032 * 'string' literal string constant
4033 * &option-name option value
4034 * @r register contents
4035 * identifier variable value
4036 * function() function call
4037 * $VAR environment variable
4038 * (expression) nested expression
4039 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004040 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 *
4042 * Also handle:
4043 * ! in front logical NOT
4044 * - in front unary minus
4045 * + in front unary plus (ignored)
4046 * trailing (arg) funcref/partial call
4047 * trailing [] subscript in String or List
4048 * trailing .name entry in Dictionary
4049 * trailing ->name() method call
4050 */
4051 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004052compile_expr7(
4053 char_u **arg,
4054 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004055 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 char_u *start_leader, *end_leader;
4058 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004059 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004060 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004062 ppconst->pp_is_const = FALSE;
4063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 /*
4065 * Skip '!', '-' and '+' characters. They are handled later.
4066 */
4067 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004068 if (eval_leader(arg, TRUE) == FAIL)
4069 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 end_leader = *arg;
4071
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004072 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 switch (**arg)
4074 {
4075 /*
4076 * Number constant.
4077 */
4078 case '0': // also for blob starting with 0z
4079 case '1':
4080 case '2':
4081 case '3':
4082 case '4':
4083 case '5':
4084 case '6':
4085 case '7':
4086 case '8':
4087 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004088 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004090 // Apply "-" and "+" just before the number now, right to
4091 // left. Matters especially when "->" follows. Stops at
4092 // '!'.
4093 if (apply_leader(rettv, TRUE,
4094 start_leader, &end_leader) == FAIL)
4095 {
4096 clear_tv(rettv);
4097 return FAIL;
4098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 break;
4100
4101 /*
4102 * String constant: "string".
4103 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004104 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 return FAIL;
4106 break;
4107
4108 /*
4109 * Literal string constant: 'str''ing'.
4110 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004111 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 return FAIL;
4113 break;
4114
4115 /*
4116 * Constant Vim variable.
4117 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004118 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 ret = NOTDONE;
4120 break;
4121
4122 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004123 * "true" constant
4124 */
4125 case 't': if (STRNCMP(*arg, "true", 4) == 0
4126 && !eval_isnamec((*arg)[4]))
4127 {
4128 *arg += 4;
4129 rettv->v_type = VAR_BOOL;
4130 rettv->vval.v_number = VVAL_TRUE;
4131 }
4132 else
4133 ret = NOTDONE;
4134 break;
4135
4136 /*
4137 * "false" constant
4138 */
4139 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4140 && !eval_isnamec((*arg)[5]))
4141 {
4142 *arg += 5;
4143 rettv->v_type = VAR_BOOL;
4144 rettv->vval.v_number = VVAL_FALSE;
4145 }
4146 else
4147 ret = NOTDONE;
4148 break;
4149
4150 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004151 * "null" constant
4152 */
4153 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4154 && !eval_isnamec((*arg)[5]))
4155 {
4156 *arg += 4;
4157 rettv->v_type = VAR_SPECIAL;
4158 rettv->vval.v_number = VVAL_NULL;
4159 }
4160 else
4161 ret = NOTDONE;
4162 break;
4163
4164 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 * List: [expr, expr]
4166 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004167 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4168 return FAIL;
4169 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 break;
4171
4172 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 * Dictionary: {'key': val, 'key': val}
4174 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004175 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4176 return FAIL;
4177 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 break;
4179
4180 /*
4181 * Option value: &name
4182 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004183 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4184 return FAIL;
4185 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 break;
4187
4188 /*
4189 * Environment variable: $VAR.
4190 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004191 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4192 return FAIL;
4193 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 break;
4195
4196 /*
4197 * Register contents: @r.
4198 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004199 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4200 return FAIL;
4201 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 break;
4203 /*
4204 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004205 * lambda: (arg, arg) => expr
4206 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004208 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4209 ret = compile_lambda(arg, cctx);
4210 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004211 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 break;
4213
4214 default: ret = NOTDONE;
4215 break;
4216 }
4217 if (ret == FAIL)
4218 return FAIL;
4219
Bram Moolenaar1c747212020-05-09 18:28:34 +02004220 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004222 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 clear_tv(rettv);
4224 else
4225 // A constant expression can possibly be handled compile time,
4226 // return the value instead of generating code.
4227 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004228 }
4229 else if (ret == NOTDONE)
4230 {
4231 char_u *p;
4232 int r;
4233
4234 if (!eval_isnamec1(**arg))
4235 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004236 if (ends_excmd(*skipwhite(*arg)))
4237 semsg(_(e_empty_expression_str), *arg);
4238 else
4239 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 return FAIL;
4241 }
4242
4243 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004244 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 {
4247 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004250 {
4251 if (generate_ppconst(cctx, ppconst) == FAIL)
4252 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004253 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 if (r == FAIL)
4256 return FAIL;
4257 }
4258
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004259 // Handle following "[]", ".member", etc.
4260 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004261 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004262 ppconst) == FAIL)
4263 return FAIL;
4264 if (ppconst->pp_used > 0)
4265 {
4266 // apply the '!', '-' and '+' before the constant
4267 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004268 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004269 return FAIL;
4270 return OK;
4271 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004272 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004274 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275}
4276
4277/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004278 * Give the "white on both sides" error, taking the operator from "p[len]".
4279 */
4280 void
4281error_white_both(char_u *op, int len)
4282{
4283 char_u buf[10];
4284
4285 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004286 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004287}
4288
4289/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004290 * <type>expr7: runtime type check / conversion
4291 */
4292 static int
4293compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4294{
4295 type_T *want_type = NULL;
4296
4297 // Recognize <type>
4298 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4299 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004300 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004301 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4302 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004303 return FAIL;
4304
4305 if (**arg != '>')
4306 {
4307 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004308 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004309 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004310 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004311 return FAIL;
4312 }
4313 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004314 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004315 return FAIL;
4316 }
4317
4318 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4319 return FAIL;
4320
4321 if (want_type != NULL)
4322 {
4323 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004324 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004325 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004326
Bram Moolenaard1103582020-08-14 22:44:25 +02004327 generate_ppconst(cctx, ppconst);
4328 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004329 where.wt_index = 0;
4330 where.wt_variable = FALSE;
4331 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004332 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004333 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004334 return FAIL;
4335 }
4336 }
4337
4338 return OK;
4339}
4340
4341/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * * number multiplication
4343 * / number division
4344 * % number modulo
4345 */
4346 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348{
4349 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004350 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004351 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004353 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004354 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 return FAIL;
4356
4357 /*
4358 * Repeat computing, until no "*", "/" or "%" is following.
4359 */
4360 for (;;)
4361 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004362 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 if (*op != '*' && *op != '/' && *op != '%')
4364 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004365 if (next != NULL)
4366 {
4367 *arg = next_line_from_context(cctx, TRUE);
4368 op = skipwhite(*arg);
4369 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004370
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004371 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004373 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004374 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004376 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004377 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004379 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004380 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004382
4383 if (ppconst->pp_used == ppconst_used + 2
4384 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4385 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004386 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004387 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4388 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4389 varnumber_T res = 0;
4390 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004392 // both are numbers: compute the result
4393 switch (*op)
4394 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004395 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004396 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004397 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004398 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004399 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004400 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004401 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004402 break;
4403 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004404 if (failed)
4405 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004406 tv1->vval.v_number = res;
4407 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004408 }
4409 else
4410 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004411 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004412 generate_two_op(cctx, op);
4413 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 }
4415
4416 return OK;
4417}
4418
4419/*
4420 * + number addition
4421 * - number subtraction
4422 * .. string concatenation
4423 */
4424 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004425compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426{
4427 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004428 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004430 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431
4432 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004433 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 return FAIL;
4435
4436 /*
4437 * Repeat computing, until no "+", "-" or ".." is following.
4438 */
4439 for (;;)
4440 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004441 op = may_peek_next_line(cctx, *arg, &next);
4442 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 break;
4444 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004445 if (next != NULL)
4446 {
4447 *arg = next_line_from_context(cctx, TRUE);
4448 op = skipwhite(*arg);
4449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004451 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004453 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004454 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 }
4456
Bram Moolenaare0de1712020-12-02 17:36:54 +01004457 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004458 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004460 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004461 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 return FAIL;
4463
Bram Moolenaara5565e42020-05-09 15:44:01 +02004464 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004465 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004466 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4467 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4468 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4469 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4472 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004473
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004474 // concat/subtract/add constant numbers
4475 if (*op == '+')
4476 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4477 else if (*op == '-')
4478 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4479 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004480 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004481 // concatenate constant strings
4482 char_u *s1 = tv1->vval.v_string;
4483 char_u *s2 = tv2->vval.v_string;
4484 size_t len1 = STRLEN(s1);
4485
4486 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4487 if (tv1->vval.v_string == NULL)
4488 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004489 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004490 return FAIL;
4491 }
4492 mch_memmove(tv1->vval.v_string, s1, len1);
4493 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004494 vim_free(s1);
4495 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004496 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 }
4499 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004500 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004502 if (*op == '.')
4503 {
4504 if (may_generate_2STRING(-2, cctx) == FAIL
4505 || may_generate_2STRING(-1, cctx) == FAIL)
4506 return FAIL;
4507 generate_instr_drop(cctx, ISN_CONCAT, 1);
4508 }
4509 else
4510 generate_two_op(cctx, op);
4511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 }
4513
4514 return OK;
4515}
4516
4517/*
4518 * expr5a == expr5b
4519 * expr5a =~ expr5b
4520 * expr5a != expr5b
4521 * expr5a !~ expr5b
4522 * expr5a > expr5b
4523 * expr5a >= expr5b
4524 * expr5a < expr5b
4525 * expr5a <= expr5b
4526 * expr5a is expr5b
4527 * expr5a isnot expr5b
4528 *
4529 * Produces instructions:
4530 * EVAL expr5a Push result of "expr5a"
4531 * EVAL expr5b Push result of "expr5b"
4532 * COMPARE one of the compare instructions
4533 */
4534 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004535compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004536{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004537 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004539 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004542 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543
4544 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 return FAIL;
4547
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004548 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004549 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550
4551 /*
4552 * If there is a comparative operator, use it.
4553 */
4554 if (type != EXPR_UNKNOWN)
4555 {
4556 int ic = FALSE; // Default: do not ignore case
4557
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004558 if (next != NULL)
4559 {
4560 *arg = next_line_from_context(cctx, TRUE);
4561 p = skipwhite(*arg);
4562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 if (type_is && (p[len] == '?' || p[len] == '#'))
4564 {
4565 semsg(_(e_invexpr2), *arg);
4566 return FAIL;
4567 }
4568 // extra question mark appended: ignore case
4569 if (p[len] == '?')
4570 {
4571 ic = TRUE;
4572 ++len;
4573 }
4574 // extra '#' appended: match case (ignored)
4575 else if (p[len] == '#')
4576 ++len;
4577 // nothing appended: match case
4578
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004579 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004581 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004582 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 }
4584
4585 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004586 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004587 return FAIL;
4588
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 return FAIL;
4591
Bram Moolenaara5565e42020-05-09 15:44:01 +02004592 if (ppconst->pp_used == ppconst_used + 2)
4593 {
4594 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4595 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4596 int ret;
4597
4598 // Both sides are a constant, compute the result now.
4599 // First check for a valid combination of types, this is more
4600 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004601 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004602 ret = FAIL;
4603 else
4604 {
4605 ret = typval_compare(tv1, tv2, type, ic);
4606 tv1->v_type = VAR_BOOL;
4607 tv1->vval.v_number = tv1->vval.v_number
4608 ? VVAL_TRUE : VVAL_FALSE;
4609 clear_tv(tv2);
4610 --ppconst->pp_used;
4611 }
4612 return ret;
4613 }
4614
4615 generate_ppconst(cctx, ppconst);
4616 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 }
4618
4619 return OK;
4620}
4621
Bram Moolenaar7f141552020-05-09 17:35:53 +02004622static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4623
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624/*
4625 * Compile || or &&.
4626 */
4627 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004628compile_and_or(
4629 char_u **arg,
4630 cctx_T *cctx,
4631 char *op,
4632 ppconst_T *ppconst,
4633 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004635 char_u *next;
4636 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 int opchar = *op;
4638
4639 if (p[0] == opchar && p[1] == opchar)
4640 {
4641 garray_T *instr = &cctx->ctx_instr;
4642 garray_T end_ga;
4643
4644 /*
4645 * Repeat until there is no following "||" or "&&"
4646 */
4647 ga_init2(&end_ga, sizeof(int), 10);
4648 while (p[0] == opchar && p[1] == opchar)
4649 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004650 if (next != NULL)
4651 {
4652 *arg = next_line_from_context(cctx, TRUE);
4653 p = skipwhite(*arg);
4654 }
4655
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004656 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4657 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004658 semsg(_(e_white_space_required_before_and_after_str_at_str),
4659 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004660 return FAIL;
4661 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004663 // TODO: use ppconst if the value is a constant and check
4664 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004665 generate_ppconst(cctx, ppconst);
4666
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004667 // Every part must evaluate to a bool.
4668 if (bool_on_stack(cctx) == FAIL)
4669 {
4670 ga_clear(&end_ga);
4671 return FAIL;
4672 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 if (ga_grow(&end_ga, 1) == FAIL)
4675 {
4676 ga_clear(&end_ga);
4677 return FAIL;
4678 }
4679 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4680 ++end_ga.ga_len;
4681 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004682 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
4684 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004685 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004686 {
4687 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004688 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004689 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004690
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4692 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 {
4694 ga_clear(&end_ga);
4695 return FAIL;
4696 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004697
4698 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004702 // Every part must evaluate to a bool.
4703 if (bool_on_stack(cctx) == FAIL)
4704 {
4705 ga_clear(&end_ga);
4706 return FAIL;
4707 }
4708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 // Fill in the end label in all jumps.
4710 while (end_ga.ga_len > 0)
4711 {
4712 isn_T *isn;
4713
4714 --end_ga.ga_len;
4715 isn = ((isn_T *)instr->ga_data)
4716 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4717 isn->isn_arg.jump.jump_where = instr->ga_len;
4718 }
4719 ga_clear(&end_ga);
4720 }
4721
4722 return OK;
4723}
4724
4725/*
4726 * expr4a && expr4a && expr4a logical AND
4727 *
4728 * Produces instructions:
4729 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004730 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004731 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004733 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 * EVAL expr4c Push result of "expr4c"
4735 * end:
4736 */
4737 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004740 int ppconst_used = ppconst->pp_used;
4741
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004743 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 return FAIL;
4745
4746 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004747 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748}
4749
4750/*
4751 * expr3a || expr3b || expr3c logical OR
4752 *
4753 * Produces instructions:
4754 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004755 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004756 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004758 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 * EVAL expr3c Push result of "expr3c"
4760 * end:
4761 */
4762 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004763compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004765 int ppconst_used = ppconst->pp_used;
4766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004768 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 return FAIL;
4770
4771 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004772 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773}
4774
4775/*
4776 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004778 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 * JUMP_IF_FALSE alt jump if false
4780 * EVAL expr1a
4781 * JUMP_ALWAYS end
4782 * alt: EVAL expr1b
4783 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004784 *
4785 * Toplevel expression: expr2 ?? expr1
4786 * Produces instructions:
4787 * EVAL expr2 Push result of "expr2"
4788 * JUMP_AND_KEEP_IF_TRUE end jump if true
4789 * EVAL expr1
4790 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 */
4792 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004793compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794{
4795 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004796 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004797 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Bram Moolenaar3988f642020-08-27 22:43:03 +02004799 // Ignore all kinds of errors when not producing code.
4800 if (cctx->ctx_skip == SKIP_YES)
4801 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004802 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004803 return OK;
4804 }
4805
Bram Moolenaar61a89812020-05-07 16:58:17 +02004806 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004807 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 return FAIL;
4809
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004810 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 if (*p == '?')
4812 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004813 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 garray_T *instr = &cctx->ctx_instr;
4815 garray_T *stack = &cctx->ctx_type_stack;
4816 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004817 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004818 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004819 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004820 int has_const_expr = FALSE;
4821 int const_value = FALSE;
4822 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004824 if (next != NULL)
4825 {
4826 *arg = next_line_from_context(cctx, TRUE);
4827 p = skipwhite(*arg);
4828 }
4829
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004830 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004831 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004832 semsg(_(e_white_space_required_before_and_after_str_at_str),
4833 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004834 return FAIL;
4835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (ppconst->pp_used == ppconst_used + 1)
4838 {
4839 // the condition is a constant, we know whether the ? or the :
4840 // expression is to be evaluated.
4841 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004842 if (op_falsy)
4843 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4844 else
4845 {
4846 int error = FALSE;
4847
4848 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4849 &error);
4850 if (error)
4851 return FAIL;
4852 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004853 cctx->ctx_skip = save_skip == SKIP_YES ||
4854 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4855
4856 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4857 // "left ?? right" and "left" is truthy: produce "left"
4858 generate_ppconst(cctx, ppconst);
4859 else
4860 {
4861 clear_tv(&ppconst->pp_tv[ppconst_used]);
4862 --ppconst->pp_used;
4863 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864 }
4865 else
4866 {
4867 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004868 if (op_falsy)
4869 end_idx = instr->ga_len;
4870 generate_JUMP(cctx, op_falsy
4871 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4872 if (op_falsy)
4873 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
4876 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004877 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004878 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004879 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004880 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881
Bram Moolenaara5565e42020-05-09 15:44:01 +02004882 if (!has_const_expr)
4883 {
4884 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004886 if (!op_falsy)
4887 {
4888 // remember the type and drop it
4889 --stack->ga_len;
4890 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004892 end_idx = instr->ga_len;
4893 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004895 // jump here from JUMP_IF_FALSE
4896 isn = ((isn_T *)instr->ga_data) + alt_idx;
4897 isn->isn_arg.jump.jump_where = instr->ga_len;
4898 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004901 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004903 // Check for the ":".
4904 p = may_peek_next_line(cctx, *arg, &next);
4905 if (*p != ':')
4906 {
4907 emsg(_(e_missing_colon));
4908 return FAIL;
4909 }
4910 if (next != NULL)
4911 {
4912 *arg = next_line_from_context(cctx, TRUE);
4913 p = skipwhite(*arg);
4914 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004915
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004916 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4917 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004918 semsg(_(e_white_space_required_before_and_after_str_at_str),
4919 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004920 return FAIL;
4921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004923 // evaluate the third expression
4924 if (has_const_expr)
4925 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004926 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004927 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004928 return FAIL;
4929 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4930 return FAIL;
4931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932
Bram Moolenaara5565e42020-05-09 15:44:01 +02004933 if (!has_const_expr)
4934 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004935 type_T **typep;
4936
Bram Moolenaara5565e42020-05-09 15:44:01 +02004937 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938
Bram Moolenaara5565e42020-05-09 15:44:01 +02004939 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004940 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4941 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004942
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004943 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004944 isn = ((isn_T *)instr->ga_data) + end_idx;
4945 isn->isn_arg.jump.jump_where = instr->ga_len;
4946 }
4947
4948 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 }
4950 return OK;
4951}
4952
4953/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004954 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004955 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4956 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004957 */
4958 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004959compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004960{
4961 ppconst_T ppconst;
4962
4963 CLEAR_FIELD(ppconst);
4964 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4965 {
4966 clear_ppconst(&ppconst);
4967 return FAIL;
4968 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004969 if (is_const != NULL)
4970 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004971 if (generate_ppconst(cctx, &ppconst) == FAIL)
4972 return FAIL;
4973 return OK;
4974}
4975
4976/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004977 * Toplevel expression.
4978 */
4979 static int
4980compile_expr0(char_u **arg, cctx_T *cctx)
4981{
4982 return compile_expr0_ext(arg, cctx, NULL);
4983}
4984
4985/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 * compile "return [expr]"
4987 */
4988 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004989compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990{
4991 char_u *p = arg;
4992 garray_T *stack = &cctx->ctx_type_stack;
4993 type_T *stack_type;
4994
4995 if (*p != NUL && *p != '|' && *p != '\n')
4996 {
4997 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004998 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 return NULL;
5000
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005001 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02005002 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005003 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01005004 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01005005 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
5006 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005007 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005008 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005009 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005010 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02005011 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005012 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5013 && stack_type->tt_type != VAR_VOID
5014 && stack_type->tt_type != VAR_UNKNOWN)
5015 {
5016 emsg(_(e_returning_value_in_function_without_return_type));
5017 return NULL;
5018 }
5019 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005020 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005021 return NULL;
5022 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 }
5025 else
5026 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005027 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005028 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005029 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5030 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005032 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 return NULL;
5034 }
5035
5036 // No argument, return zero.
5037 generate_PUSHNR(cctx, 0);
5038 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005039
5040 // Undo any command modifiers.
5041 generate_undo_cmdmods(cctx);
5042
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005043 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 return NULL;
5045
5046 // "return val | endif" is possible
5047 return skipwhite(p);
5048}
5049
5050/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005051 * Get a line from the compilation context, compatible with exarg_T getline().
5052 * Return a pointer to the line in allocated memory.
5053 * Return NULL for end-of-file or some error.
5054 */
5055 static char_u *
5056exarg_getline(
5057 int c UNUSED,
5058 void *cookie,
5059 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005060 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005061{
5062 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005063 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005064
Bram Moolenaar66250c92020-08-20 15:02:42 +02005065 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005066 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005067 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005068 return NULL;
5069 ++cctx->ctx_lnum;
5070 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5071 // Comment lines result in NULL pointers, skip them.
5072 if (p != NULL)
5073 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005074 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005075}
5076
5077/*
5078 * Compile a nested :def command.
5079 */
5080 static char_u *
5081compile_nested_function(exarg_T *eap, cctx_T *cctx)
5082{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005083 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005084 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005085 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005086 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005087 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005088 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005089
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005090 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005091 {
5092 emsg(_(e_cannot_use_bang_with_nested_def));
5093 return NULL;
5094 }
5095
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005096 if (*name_start == '/')
5097 {
5098 name_end = skip_regexp(name_start + 1, '/', TRUE);
5099 if (*name_end == '/')
5100 ++name_end;
5101 eap->nextcmd = check_nextcmd(name_end);
5102 }
5103 if (name_end == name_start || *skipwhite(name_end) != '(')
5104 {
5105 if (!ends_excmd2(name_start, name_end))
5106 {
5107 semsg(_(e_invalid_command_str), eap->cmd);
5108 return NULL;
5109 }
5110
5111 // "def" or "def Name": list functions
5112 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5113 return NULL;
5114 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5115 }
5116
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005117 // Only g:Func() can use a namespace.
5118 if (name_start[1] == ':' && !is_global)
5119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005120 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005121 return NULL;
5122 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005123 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5124 return NULL;
5125
Bram Moolenaar04b12692020-05-04 23:24:44 +02005126 eap->arg = name_end;
5127 eap->getline = exarg_getline;
5128 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005129 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005130 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005131 lambda_name = vim_strsave(get_lambda_name());
5132 if (lambda_name == NULL)
5133 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005134 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005135
Bram Moolenaar822ba242020-05-24 23:00:18 +02005136 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005137 {
5138 r = eap->skip ? OK : FAIL;
5139 goto theend;
5140 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005141 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5142 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005143 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005144 {
5145 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005146 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005147 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005148
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005149 if (is_global)
5150 {
5151 char_u *func_name = vim_strnsave(name_start + 2,
5152 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005153
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005154 if (func_name == NULL)
5155 r = FAIL;
5156 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005157 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005158 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005159 lambda_name = NULL;
5160 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005161 }
5162 else
5163 {
5164 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005165 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005166 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005167 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005168
Bram Moolenaareef21022020-08-01 22:16:43 +02005169 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005170 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005171 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005172 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005173 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005174
5175 // copy over the block scope IDs
5176 if (block_depth > 0)
5177 {
5178 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5179 if (ufunc->uf_block_ids != NULL)
5180 {
5181 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5182 sizeof(int) * block_depth);
5183 ufunc->uf_block_depth = block_depth;
5184 }
5185 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005186 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005187 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005188
5189theend:
5190 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005191 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005192}
5193
5194/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 * Return the length of an assignment operator, or zero if there isn't one.
5196 */
5197 int
5198assignment_len(char_u *p, int *heredoc)
5199{
5200 if (*p == '=')
5201 {
5202 if (p[1] == '<' && p[2] == '<')
5203 {
5204 *heredoc = TRUE;
5205 return 3;
5206 }
5207 return 1;
5208 }
5209 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5210 return 2;
5211 if (STRNCMP(p, "..=", 3) == 0)
5212 return 3;
5213 return 0;
5214}
5215
5216// words that cannot be used as a variable
5217static char *reserved[] = {
5218 "true",
5219 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005220 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 NULL
5222};
5223
Bram Moolenaar752fc692021-01-04 21:57:11 +01005224// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005225typedef enum {
5226 dest_local,
5227 dest_option,
5228 dest_env,
5229 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005230 dest_buffer,
5231 dest_window,
5232 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005233 dest_vimvar,
5234 dest_script,
5235 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005236 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005237} assign_dest_T;
5238
Bram Moolenaar752fc692021-01-04 21:57:11 +01005239// Used by compile_lhs() to store information about the LHS of an assignment
5240// and one argument of ":unlet" with an index.
5241typedef struct {
5242 assign_dest_T lhs_dest; // type of destination
5243
5244 char_u *lhs_name; // allocated name including
5245 // "[expr]" or ".name".
5246 size_t lhs_varlen; // length of the variable without
5247 // "[expr]" or ".name"
5248 char_u *lhs_dest_end; // end of the destination, including
5249 // "[expr]" or ".name".
5250
5251 int lhs_has_index; // has "[expr]" or ".name"
5252
5253 int lhs_new_local; // create new local variable
5254 int lhs_opt_flags; // for when destination is an option
5255 int lhs_vimvaridx; // for when destination is a v:var
5256
5257 lvar_T lhs_local_lvar; // used for existing local destination
5258 lvar_T lhs_arg_lvar; // used for argument destination
5259 lvar_T *lhs_lvar; // points to destination lvar
5260 int lhs_scriptvar_sid;
5261 int lhs_scriptvar_idx;
5262
5263 int lhs_has_type; // type was specified
5264 type_T *lhs_type;
5265 type_T *lhs_member_type;
5266} lhs_T;
5267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005269 * Generate the load instruction for "name".
5270 */
5271 static void
5272generate_loadvar(
5273 cctx_T *cctx,
5274 assign_dest_T dest,
5275 char_u *name,
5276 lvar_T *lvar,
5277 type_T *type)
5278{
5279 switch (dest)
5280 {
5281 case dest_option:
5282 // TODO: check the option exists
5283 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5284 break;
5285 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005286 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5287 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5288 else
5289 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005290 break;
5291 case dest_buffer:
5292 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5293 break;
5294 case dest_window:
5295 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5296 break;
5297 case dest_tab:
5298 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5299 break;
5300 case dest_script:
5301 compile_load_scriptvar(cctx,
5302 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5303 break;
5304 case dest_env:
5305 // Include $ in the name here
5306 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5307 break;
5308 case dest_reg:
5309 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5310 break;
5311 case dest_vimvar:
5312 generate_LOADV(cctx, name + 2, TRUE);
5313 break;
5314 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005315 if (lvar->lv_from_outer > 0)
5316 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5317 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005318 else
5319 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5320 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005321 case dest_expr:
5322 // list or dict value should already be on the stack.
5323 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 }
5325}
5326
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005327/*
5328 * Skip over "[expr]" or ".member".
5329 * Does not check for any errors.
5330 */
5331 static char_u *
5332skip_index(char_u *start)
5333{
5334 char_u *p = start;
5335
5336 if (*p == '[')
5337 {
5338 p = skipwhite(p + 1);
5339 (void)skip_expr(&p, NULL);
5340 p = skipwhite(p);
5341 if (*p == ']')
5342 return p + 1;
5343 return p;
5344 }
5345 // if (*p == '.')
5346 return to_name_end(p + 1, TRUE);
5347}
5348
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005349 void
5350vim9_declare_error(char_u *name)
5351{
5352 char *scope = "";
5353
5354 switch (*name)
5355 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005356 case 'g': scope = _("global"); break;
5357 case 'b': scope = _("buffer"); break;
5358 case 'w': scope = _("window"); break;
5359 case 't': scope = _("tab"); break;
5360 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005361 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005362 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005363 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005364 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005365 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005366 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005367 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005368 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005369 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005370}
5371
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005372/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005373 * For one assignment figure out the type of destination. Return it in "dest".
5374 * When not recognized "dest" is not set.
5375 * For an option "opt_flags" is set.
5376 * For a v:var "vimvaridx" is set.
5377 * "type" is set to the destination type if known, unchanted otherwise.
5378 * Return FAIL if an error message was given.
5379 */
5380 static int
5381get_var_dest(
5382 char_u *name,
5383 assign_dest_T *dest,
5384 int cmdidx,
5385 int *opt_flags,
5386 int *vimvaridx,
5387 type_T **type,
5388 cctx_T *cctx)
5389{
5390 char_u *p;
5391
5392 if (*name == '&')
5393 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005394 int cc;
5395 long numval;
5396 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005397
5398 *dest = dest_option;
5399 if (cmdidx == CMD_final || cmdidx == CMD_const)
5400 {
5401 emsg(_(e_const_option));
5402 return FAIL;
5403 }
5404 p = name;
5405 p = find_option_end(&p, opt_flags);
5406 if (p == NULL)
5407 {
5408 // cannot happen?
5409 emsg(_(e_letunexp));
5410 return FAIL;
5411 }
5412 cc = *p;
5413 *p = NUL;
5414 opt_type = get_option_value(skip_option_env_lead(name),
5415 &numval, NULL, *opt_flags);
5416 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005417 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005418 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005419 case gov_unknown:
5420 semsg(_(e_unknown_option), name);
5421 return FAIL;
5422 case gov_string:
5423 case gov_hidden_string:
5424 *type = &t_string;
5425 break;
5426 case gov_bool:
5427 case gov_hidden_bool:
5428 *type = &t_bool;
5429 break;
5430 case gov_number:
5431 case gov_hidden_number:
5432 *type = &t_number;
5433 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005434 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005435 }
5436 else if (*name == '$')
5437 {
5438 *dest = dest_env;
5439 *type = &t_string;
5440 }
5441 else if (*name == '@')
5442 {
5443 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5444 {
5445 emsg_invreg(name[1]);
5446 return FAIL;
5447 }
5448 *dest = dest_reg;
5449 *type = &t_string;
5450 }
5451 else if (STRNCMP(name, "g:", 2) == 0)
5452 {
5453 *dest = dest_global;
5454 }
5455 else if (STRNCMP(name, "b:", 2) == 0)
5456 {
5457 *dest = dest_buffer;
5458 }
5459 else if (STRNCMP(name, "w:", 2) == 0)
5460 {
5461 *dest = dest_window;
5462 }
5463 else if (STRNCMP(name, "t:", 2) == 0)
5464 {
5465 *dest = dest_tab;
5466 }
5467 else if (STRNCMP(name, "v:", 2) == 0)
5468 {
5469 typval_T *vtv;
5470 int di_flags;
5471
5472 *vimvaridx = find_vim_var(name + 2, &di_flags);
5473 if (*vimvaridx < 0)
5474 {
5475 semsg(_(e_variable_not_found_str), name);
5476 return FAIL;
5477 }
5478 // We use the current value of "sandbox" here, is that OK?
5479 if (var_check_ro(di_flags, name, FALSE))
5480 return FAIL;
5481 *dest = dest_vimvar;
5482 vtv = get_vim_var_tv(*vimvaridx);
5483 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5484 }
5485 return OK;
5486}
5487
5488/*
5489 * Generate a STORE instruction for "dest", not being "dest_local".
5490 * Return FAIL when out of memory.
5491 */
5492 static int
5493generate_store_var(
5494 cctx_T *cctx,
5495 assign_dest_T dest,
5496 int opt_flags,
5497 int vimvaridx,
5498 int scriptvar_idx,
5499 int scriptvar_sid,
5500 type_T *type,
5501 char_u *name)
5502{
5503 switch (dest)
5504 {
5505 case dest_option:
5506 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5507 opt_flags);
5508 case dest_global:
5509 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005510 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5511 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005512 case dest_buffer:
5513 // include b: with the name, easier to execute that way
5514 return generate_STORE(cctx, ISN_STOREB, 0, name);
5515 case dest_window:
5516 // include w: with the name, easier to execute that way
5517 return generate_STORE(cctx, ISN_STOREW, 0, name);
5518 case dest_tab:
5519 // include t: with the name, easier to execute that way
5520 return generate_STORE(cctx, ISN_STORET, 0, name);
5521 case dest_env:
5522 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5523 case dest_reg:
5524 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5525 case dest_vimvar:
5526 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5527 case dest_script:
5528 if (scriptvar_idx < 0)
5529 {
5530 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005531 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005532
Bram Moolenaar41d61962020-12-06 20:12:43 +01005533 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005534 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5535 scriptvar_sid, type);
5536 if (name_s != name)
5537 vim_free(name_s);
5538 return r;
5539 }
5540 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5541 scriptvar_sid, scriptvar_idx, type);
5542 case dest_local:
5543 case dest_expr:
5544 // cannot happen
5545 break;
5546 }
5547 return FAIL;
5548}
5549
Bram Moolenaar752fc692021-01-04 21:57:11 +01005550 static int
5551is_decl_command(int cmdidx)
5552{
5553 return cmdidx == CMD_let || cmdidx == CMD_var
5554 || cmdidx == CMD_final || cmdidx == CMD_const;
5555}
5556
5557/*
5558 * Figure out the LHS type and other properties for an assignment or one item
5559 * of ":unlet" with an index.
5560 * Returns OK or FAIL.
5561 */
5562 static int
5563compile_lhs(
5564 char_u *var_start,
5565 lhs_T *lhs,
5566 int cmdidx,
5567 int heredoc,
5568 int oplen,
5569 cctx_T *cctx)
5570{
5571 char_u *var_end;
5572 int is_decl = is_decl_command(cmdidx);
5573
5574 CLEAR_POINTER(lhs);
5575 lhs->lhs_dest = dest_local;
5576 lhs->lhs_vimvaridx = -1;
5577 lhs->lhs_scriptvar_idx = -1;
5578
5579 // "dest_end" is the end of the destination, including "[expr]" or
5580 // ".name".
5581 // "var_end" is the end of the variable/option/etc. name.
5582 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5583 if (*var_start == '@')
5584 var_end = var_start + 2;
5585 else
5586 {
5587 // skip over the leading "&", "&l:", "&g:" and "$"
5588 var_end = skip_option_env_lead(var_start);
5589 var_end = to_name_end(var_end, TRUE);
5590 }
5591
5592 // "a: type" is declaring variable "a" with a type, not dict "a:".
5593 if (is_decl && lhs->lhs_dest_end == var_start + 2
5594 && lhs->lhs_dest_end[-1] == ':')
5595 --lhs->lhs_dest_end;
5596 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5597 --var_end;
5598
5599 // compute the length of the destination without "[expr]" or ".name"
5600 lhs->lhs_varlen = var_end - var_start;
5601 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5602 if (lhs->lhs_name == NULL)
5603 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005604
5605 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5606 // Something follows after the variable: "var[idx]" or "var.key".
5607 lhs->lhs_has_index = TRUE;
5608
Bram Moolenaar752fc692021-01-04 21:57:11 +01005609 if (heredoc)
5610 lhs->lhs_type = &t_list_string;
5611 else
5612 lhs->lhs_type = &t_any;
5613
5614 if (cctx->ctx_skip != SKIP_YES)
5615 {
5616 int declare_error = FALSE;
5617
5618 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5619 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5620 &lhs->lhs_type, cctx) == FAIL)
5621 return FAIL;
5622 if (lhs->lhs_dest != dest_local)
5623 {
5624 // Specific kind of variable recognized.
5625 declare_error = is_decl;
5626 }
5627 else
5628 {
5629 int idx;
5630
5631 // No specific kind of variable recognized, just a name.
5632 for (idx = 0; reserved[idx] != NULL; ++idx)
5633 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5634 {
5635 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5636 return FAIL;
5637 }
5638
5639
5640 if (lookup_local(var_start, lhs->lhs_varlen,
5641 &lhs->lhs_local_lvar, cctx) == OK)
5642 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5643 else
5644 {
5645 CLEAR_FIELD(lhs->lhs_arg_lvar);
5646 if (arg_exists(var_start, lhs->lhs_varlen,
5647 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5648 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5649 {
5650 if (is_decl)
5651 {
5652 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5653 return FAIL;
5654 }
5655 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5656 }
5657 }
5658 if (lhs->lhs_lvar != NULL)
5659 {
5660 if (is_decl)
5661 {
5662 semsg(_(e_variable_already_declared), lhs->lhs_name);
5663 return FAIL;
5664 }
5665 }
5666 else
5667 {
5668 int script_namespace = lhs->lhs_varlen > 1
5669 && STRNCMP(var_start, "s:", 2) == 0;
5670 int script_var = (script_namespace
5671 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5672 FALSE, cctx)
5673 : script_var_exists(var_start, lhs->lhs_varlen,
5674 TRUE, cctx)) == OK;
5675 imported_T *import =
5676 find_imported(var_start, lhs->lhs_varlen, cctx);
5677
5678 if (script_namespace || script_var || import != NULL)
5679 {
5680 char_u *rawname = lhs->lhs_name
5681 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5682
5683 if (is_decl)
5684 {
5685 if (script_namespace)
5686 semsg(_(e_cannot_declare_script_variable_in_function),
5687 lhs->lhs_name);
5688 else
5689 semsg(_(e_variable_already_declared_in_script),
5690 lhs->lhs_name);
5691 return FAIL;
5692 }
5693 else if (cctx->ctx_ufunc->uf_script_ctx_version
5694 == SCRIPT_VERSION_VIM9
5695 && script_namespace
5696 && !script_var && import == NULL)
5697 {
5698 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5699 return FAIL;
5700 }
5701
5702 lhs->lhs_dest = dest_script;
5703
5704 // existing script-local variables should have a type
5705 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5706 if (import != NULL)
5707 lhs->lhs_scriptvar_sid = import->imp_sid;
5708 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5709 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005710 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005711 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005712 lhs->lhs_scriptvar_sid, rawname,
5713 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5714 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005715 if (lhs->lhs_scriptvar_idx >= 0)
5716 {
5717 scriptitem_T *si = SCRIPT_ITEM(
5718 lhs->lhs_scriptvar_sid);
5719 svar_T *sv =
5720 ((svar_T *)si->sn_var_vals.ga_data)
5721 + lhs->lhs_scriptvar_idx;
5722 lhs->lhs_type = sv->sv_type;
5723 }
5724 }
5725 }
5726 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5727 == FAIL)
5728 return FAIL;
5729 }
5730 }
5731
5732 if (declare_error)
5733 {
5734 vim9_declare_error(lhs->lhs_name);
5735 return FAIL;
5736 }
5737 }
5738
5739 // handle "a:name" as a name, not index "name" on "a"
5740 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5741 var_end = lhs->lhs_dest_end;
5742
5743 if (lhs->lhs_dest != dest_option)
5744 {
5745 if (is_decl && *var_end == ':')
5746 {
5747 char_u *p;
5748
5749 // parse optional type: "let var: type = expr"
5750 if (!VIM_ISWHITE(var_end[1]))
5751 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005752 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005753 return FAIL;
5754 }
5755 p = skipwhite(var_end + 1);
5756 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5757 if (lhs->lhs_type == NULL)
5758 return FAIL;
5759 lhs->lhs_has_type = TRUE;
5760 }
5761 else if (lhs->lhs_lvar != NULL)
5762 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5763 }
5764
5765 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5766 && lhs->lhs_type->tt_type != VAR_STRING
5767 && lhs->lhs_type->tt_type != VAR_ANY)
5768 {
5769 emsg(_(e_can_only_concatenate_to_string));
5770 return FAIL;
5771 }
5772
5773 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5774 && cctx->ctx_skip != SKIP_YES)
5775 {
5776 if (oplen > 1 && !heredoc)
5777 {
5778 // +=, /=, etc. require an existing variable
5779 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5780 return FAIL;
5781 }
5782 if (!is_decl)
5783 {
5784 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5785 return FAIL;
5786 }
5787
5788 // new local variable
5789 if ((lhs->lhs_type->tt_type == VAR_FUNC
5790 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5791 && var_wrong_func_name(lhs->lhs_name, TRUE))
5792 return FAIL;
5793 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5794 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5795 if (lhs->lhs_lvar == NULL)
5796 return FAIL;
5797 lhs->lhs_new_local = TRUE;
5798 }
5799
5800 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005801 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005802 {
5803 // Something follows after the variable: "var[idx]" or "var.key".
5804 // TODO: should we also handle "->func()" here?
5805 if (is_decl)
5806 {
5807 emsg(_(e_cannot_use_index_when_declaring_variable));
5808 return FAIL;
5809 }
5810
5811 if (var_start[lhs->lhs_varlen] == '['
5812 || var_start[lhs->lhs_varlen] == '.')
5813 {
5814 char_u *after = var_start + lhs->lhs_varlen;
5815 char_u *p;
5816
5817 // Only the last index is used below, if there are others
5818 // before it generate code for the expression. Thus for
5819 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5820 for (;;)
5821 {
5822 p = skip_index(after);
5823 if (*p != '[' && *p != '.')
5824 break;
5825 after = p;
5826 }
5827 if (after > var_start + lhs->lhs_varlen)
5828 {
5829 lhs->lhs_varlen = after - var_start;
5830 lhs->lhs_dest = dest_expr;
5831 // We don't know the type before evaluating the expression,
5832 // use "any" until then.
5833 lhs->lhs_type = &t_any;
5834 }
5835
Bram Moolenaar752fc692021-01-04 21:57:11 +01005836 if (lhs->lhs_type->tt_member == NULL)
5837 lhs->lhs_member_type = &t_any;
5838 else
5839 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5840 }
5841 else
5842 {
5843 semsg("Not supported yet: %s", var_start);
5844 return FAIL;
5845 }
5846 }
5847 return OK;
5848}
5849
5850/*
5851 * Assignment to a list or dict member, or ":unlet" for the item, using the
5852 * information in "lhs".
5853 * Returns OK or FAIL.
5854 */
5855 static int
5856compile_assign_unlet(
5857 char_u *var_start,
5858 lhs_T *lhs,
5859 int is_assign,
5860 type_T *rhs_type,
5861 cctx_T *cctx)
5862{
5863 char_u *p;
5864 int r;
5865 vartype_T dest_type;
5866 size_t varlen = lhs->lhs_varlen;
5867 garray_T *stack = &cctx->ctx_type_stack;
5868
5869 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5870 p = var_start + varlen;
5871 if (*p == '[')
5872 {
5873 p = skipwhite(p + 1);
5874 r = compile_expr0(&p, cctx);
5875 if (r == OK && *skipwhite(p) != ']')
5876 {
5877 // this should not happen
5878 emsg(_(e_missbrac));
5879 r = FAIL;
5880 }
5881 }
5882 else // if (*p == '.')
5883 {
5884 char_u *key_end = to_name_end(p + 1, TRUE);
5885 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5886
5887 r = generate_PUSHS(cctx, key);
5888 }
5889 if (r == FAIL)
5890 return FAIL;
5891
5892 if (lhs->lhs_type == &t_any)
5893 {
5894 // Index on variable of unknown type: check at runtime.
5895 dest_type = VAR_ANY;
5896 }
5897 else
5898 {
5899 dest_type = lhs->lhs_type->tt_type;
5900 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5901 return FAIL;
5902 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005903 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5904 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005905 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005906 }
5907
5908 // Load the dict or list. On the stack we then have:
5909 // - value (for assignment, not for :unlet)
5910 // - index
5911 // - variable
5912 if (lhs->lhs_dest == dest_expr)
5913 {
5914 int c = var_start[varlen];
5915
5916 // Evaluate "ll[expr]" of "ll[expr][idx]"
5917 p = var_start;
5918 var_start[varlen] = NUL;
5919 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5920 {
5921 // this should not happen
5922 emsg(_(e_missbrac));
5923 return FAIL;
5924 }
5925 var_start[varlen] = c;
5926
5927 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5928 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5929 // now we can properly check the type
5930 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005931 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005932 FALSE, FALSE) == FAIL)
5933 return FAIL;
5934 }
5935 else
5936 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5937 lhs->lhs_lvar, lhs->lhs_type);
5938
5939 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5940 {
5941 if (is_assign)
5942 {
5943 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5944
5945 if (isn == NULL)
5946 return FAIL;
5947 isn->isn_arg.vartype = dest_type;
5948 }
5949 else
5950 {
5951 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5952 return FAIL;
5953 }
5954 }
5955 else
5956 {
5957 emsg(_(e_indexable_type_required));
5958 return FAIL;
5959 }
5960
5961 return OK;
5962}
5963
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005964/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005966 * "let name"
5967 * "var name = expr"
5968 * "final name = expr"
5969 * "const name = expr"
5970 * "name = expr"
5971 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005972 * Return NULL for an error.
5973 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 */
5975 static char_u *
5976compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5977{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005980 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 char_u *ret = NULL;
5982 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005986 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 int oplen = 0;
5989 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005990 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005992 int is_decl = is_decl_command(cmdidx);
5993 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005995 // Skip over the "var" or "[var, var]" to get to any "=".
5996 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5997 if (p == NULL)
5998 return *arg == '[' ? arg : NULL;
5999
6000 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02006002 // TODO: should we allow this, and figure out type inference from list
6003 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006004 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 return NULL;
6006 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006007 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009 sp = p;
6010 p = skipwhite(p);
6011 op = p;
6012 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006013
6014 if (var_count > 0 && oplen == 0)
6015 // can be something like "[1, 2]->func()"
6016 return arg;
6017
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006018 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006020 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006021 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006022 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 if (heredoc)
6025 {
6026 list_T *l;
6027 listitem_T *li;
6028
6029 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006030 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006032 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006033 if (l == NULL)
6034 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006035
Bram Moolenaar078269b2020-09-21 20:35:55 +02006036 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006037 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006038 // Push each line and the create the list.
6039 FOR_ALL_LIST_ITEMS(l, li)
6040 {
6041 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6042 li->li_tv.vval.v_string = NULL;
6043 }
6044 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006046 list_free(l);
6047 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006048 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006050 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006052 char_u *wp;
6053
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006054 // for "[var, var] = expr" evaluate the expression here, loop over the
6055 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006056 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006057
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006058 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006059 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006060 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006061 if (compile_expr0(&p, cctx) == FAIL)
6062 return NULL;
6063 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006064
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006065 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006066 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006067 type_T *stacktype;
6068
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006069 stacktype = stack->ga_len == 0 ? &t_void
6070 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006071 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006073 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006074 goto theend;
6075 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006076 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006077 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006078 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006079 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006080 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6081 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006082 if (stacktype->tt_member != NULL)
6083 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 }
6085 }
6086
6087 /*
6088 * Loop over variables in "[var, var] = expr".
6089 * For "var = expr" and "let var: type" this is done only once.
6090 */
6091 if (var_count > 0)
6092 var_start = skipwhite(arg + 1); // skip over the "["
6093 else
6094 var_start = arg;
6095 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6096 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006097 int instr_count = -1;
6098
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 vim_free(lhs.lhs_name);
6100
6101 /*
6102 * Figure out the LHS type and other properties.
6103 */
6104 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6105 goto theend;
6106
6107 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006108 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006109 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006110 goto theend;
6111 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006112 if (!is_decl && lhs.lhs_lvar != NULL
6113 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006114 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006115 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006116 goto theend;
6117 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006118
6119 if (!heredoc)
6120 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006121 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006123 if (oplen > 0 && var_count == 0)
6124 {
6125 // skip over the "=" and the expression
6126 p = skipwhite(op + oplen);
6127 compile_expr0(&p, cctx);
6128 }
6129 }
6130 else if (oplen > 0)
6131 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006132 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006133 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006134
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006135 // For "var = expr" evaluate the expression.
6136 if (var_count == 0)
6137 {
6138 int r;
6139
6140 // for "+=", "*=", "..=" etc. first load the current value
6141 if (*op != '=')
6142 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006143 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6144 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006145
Bram Moolenaar752fc692021-01-04 21:57:11 +01006146 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006147 {
6148 // TODO: get member from list or dict
6149 emsg("Index with operation not supported yet");
6150 goto theend;
6151 }
6152 }
6153
6154 // Compile the expression. Temporarily hide the new local
6155 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006157 --cctx->ctx_locals.ga_len;
6158 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006159 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006160 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006161 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006162 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006163 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006164 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006165 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006166 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006168 ++cctx->ctx_locals.ga_len;
6169 if (r == FAIL)
6170 goto theend;
6171 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006172 else if (semicolon && var_idx == var_count - 1)
6173 {
6174 // For "[var; var] = expr" get the rest of the list
6175 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6176 goto theend;
6177 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006178 else
6179 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006180 // For "[var, var] = expr" get the "var_idx" item from the
6181 // list.
6182 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006183 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006184 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006185
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006186 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006187 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006188 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006189 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006190 if ((rhs_type->tt_type == VAR_FUNC
6191 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006192 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006193 goto theend;
6194
Bram Moolenaar752fc692021-01-04 21:57:11 +01006195 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006196 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006197 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006199 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006200 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006201 }
6202 else
6203 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006204 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006205 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006206 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006207 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006208 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006209 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006210 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006211 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006212 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006213 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006214 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006215 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006216 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006217 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006218 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006219
Bram Moolenaar71abe482020-09-14 22:28:30 +02006220 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006221 if (lhs.lhs_has_index)
6222 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006223 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006224 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006225 goto theend;
6226 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006227 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006228 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006229 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006230 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006231 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006232 else if (cmdidx == CMD_final)
6233 {
6234 emsg(_(e_final_requires_a_value));
6235 goto theend;
6236 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006237 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006238 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006239 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 goto theend;
6241 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006242 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006243 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006244 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006245 goto theend;
6246 }
6247 else
6248 {
6249 // variables are always initialized
6250 if (ga_grow(instr, 1) == FAIL)
6251 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006252 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006253 {
6254 case VAR_BOOL:
6255 generate_PUSHBOOL(cctx, VVAL_FALSE);
6256 break;
6257 case VAR_FLOAT:
6258#ifdef FEAT_FLOAT
6259 generate_PUSHF(cctx, 0.0);
6260#endif
6261 break;
6262 case VAR_STRING:
6263 generate_PUSHS(cctx, NULL);
6264 break;
6265 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006266 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006267 break;
6268 case VAR_FUNC:
6269 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6270 break;
6271 case VAR_LIST:
6272 generate_NEWLIST(cctx, 0);
6273 break;
6274 case VAR_DICT:
6275 generate_NEWDICT(cctx, 0);
6276 break;
6277 case VAR_JOB:
6278 generate_PUSHJOB(cctx, NULL);
6279 break;
6280 case VAR_CHANNEL:
6281 generate_PUSHCHANNEL(cctx, NULL);
6282 break;
6283 case VAR_NUMBER:
6284 case VAR_UNKNOWN:
6285 case VAR_ANY:
6286 case VAR_PARTIAL:
6287 case VAR_VOID:
6288 case VAR_SPECIAL: // cannot happen
6289 generate_PUSHNR(cctx, 0);
6290 break;
6291 }
6292 }
6293 if (var_count == 0)
6294 end = p;
6295 }
6296
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006297 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006298 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006299 break;
6300
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006301 if (oplen > 0 && *op != '=')
6302 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006303 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006304 type_T *stacktype;
6305
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006306 if (*op == '.')
6307 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006308 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006309 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006310 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006311 if (
6312#ifdef FEAT_FLOAT
6313 // If variable is float operation with number is OK.
6314 !(expected == &t_float && stacktype == &t_number) &&
6315#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006316 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006317 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006318 goto theend;
6319
6320 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006321 {
6322 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6323 goto theend;
6324 }
6325 else if (*op == '+')
6326 {
6327 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006328 operator_type(lhs.lhs_member_type, stacktype),
6329 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006330 goto theend;
6331 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006332 else if (generate_two_op(cctx, op) == FAIL)
6333 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335
Bram Moolenaar752fc692021-01-04 21:57:11 +01006336 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006337 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006338 // Use the info in "lhs" to store the value at the index in the
6339 // list or dict.
6340 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6341 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006342 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006343 }
6344 else
6345 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6347 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006348 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006349 generate_LOCKCONST(cctx);
6350
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006351 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006352 && (lhs.lhs_type->tt_type == VAR_DICT
6353 || lhs.lhs_type->tt_type == VAR_LIST)
6354 && lhs.lhs_type->tt_member != NULL
6355 && lhs.lhs_type->tt_member != &t_any
6356 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006357 // Set the type in the list or dict, so that it can be checked,
6358 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006359 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006360
Bram Moolenaar752fc692021-01-04 21:57:11 +01006361 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006362 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006363 if (generate_store_var(cctx, lhs.lhs_dest,
6364 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6365 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6366 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006367 goto theend;
6368 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006369 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006370 {
6371 isn_T *isn = ((isn_T *)instr->ga_data)
6372 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006373
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006374 // optimization: turn "var = 123" from ISN_PUSHNR +
6375 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006376 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006377 && instr->ga_len == instr_count + 1
6378 && isn->isn_type == ISN_PUSHNR)
6379 {
6380 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006381
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006382 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006383 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006384 isn->isn_arg.storenr.stnr_val = val;
6385 if (stack->ga_len > 0)
6386 --stack->ga_len;
6387 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006388 else if (lhs.lhs_lvar->lv_from_outer > 0)
6389 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6390 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006391 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006392 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006393 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006394 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006395
6396 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006399
6400 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006401 if (var_count > 0 && !semicolon)
6402 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006403 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006404 goto theend;
6405 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006406
Bram Moolenaarb2097502020-07-19 17:17:02 +02006407 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006408
6409theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006410 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006411 return ret;
6412}
6413
6414/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006415 * Check for an assignment at "eap->cmd", compile it if found.
6416 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6417 */
6418 static int
6419may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6420{
6421 char_u *pskip;
6422 char_u *p;
6423
6424 // Assuming the command starts with a variable or function name,
6425 // find what follows.
6426 // Skip over "var.member", "var[idx]" and the like.
6427 // Also "&opt = val", "$ENV = val" and "@r = val".
6428 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6429 ? eap->cmd + 1 : eap->cmd;
6430 p = to_name_end(pskip, TRUE);
6431 if (p > eap->cmd && *p != NUL)
6432 {
6433 char_u *var_end;
6434 int oplen;
6435 int heredoc;
6436
6437 if (eap->cmd[0] == '@')
6438 var_end = eap->cmd + 2;
6439 else
6440 var_end = find_name_end(pskip, NULL, NULL,
6441 FNE_CHECK_START | FNE_INCL_BR);
6442 oplen = assignment_len(skipwhite(var_end), &heredoc);
6443 if (oplen > 0)
6444 {
6445 size_t len = p - eap->cmd;
6446
6447 // Recognize an assignment if we recognize the variable
6448 // name:
6449 // "g:var = expr"
6450 // "local = expr" where "local" is a local var.
6451 // "script = expr" where "script" is a script-local var.
6452 // "import = expr" where "import" is an imported var
6453 // "&opt = expr"
6454 // "$ENV = expr"
6455 // "@r = expr"
6456 if (*eap->cmd == '&'
6457 || *eap->cmd == '$'
6458 || *eap->cmd == '@'
6459 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaare0890d62021-02-17 14:52:14 +01006460 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01006461 {
6462 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6463 if (*line == NULL || *line == eap->cmd)
6464 return FAIL;
6465 return OK;
6466 }
6467 }
6468 }
6469
6470 if (*eap->cmd == '[')
6471 {
6472 // [var, var] = expr
6473 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6474 if (*line == NULL)
6475 return FAIL;
6476 if (*line != eap->cmd)
6477 return OK;
6478 }
6479 return NOTDONE;
6480}
6481
6482/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006483 * Check if "name" can be "unlet".
6484 */
6485 int
6486check_vim9_unlet(char_u *name)
6487{
6488 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6489 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006490 // "unlet s:var" is allowed in legacy script.
6491 if (*name == 's' && !script_is_vim9())
6492 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006493 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006494 return FAIL;
6495 }
6496 return OK;
6497}
6498
6499/*
6500 * Callback passed to ex_unletlock().
6501 */
6502 static int
6503compile_unlet(
6504 lval_T *lvp,
6505 char_u *name_end,
6506 exarg_T *eap,
6507 int deep UNUSED,
6508 void *coookie)
6509{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006510 cctx_T *cctx = coookie;
6511 char_u *p = lvp->ll_name;
6512 int cc = *name_end;
6513 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006514
Bram Moolenaar752fc692021-01-04 21:57:11 +01006515 if (cctx->ctx_skip == SKIP_YES)
6516 return OK;
6517
6518 *name_end = NUL;
6519 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006520 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 // :unlet $ENV_VAR
6522 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6523 }
6524 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6525 {
6526 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006527
Bram Moolenaar752fc692021-01-04 21:57:11 +01006528 // This is similar to assigning: lookup the list/dict, compile the
6529 // idx/key. Then instead of storing the value unlet the item.
6530 // unlet {list}[idx]
6531 // unlet {dict}[key] dict.key
6532 //
6533 // Figure out the LHS type and other properties.
6534 //
6535 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6536
6537 // : unlet an indexed item
6538 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006539 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 iemsg("called compile_lhs() without an index");
6541 ret = FAIL;
6542 }
6543 else
6544 {
6545 // Use the info in "lhs" to unlet the item at the index in the
6546 // list or dict.
6547 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006548 }
6549
Bram Moolenaar752fc692021-01-04 21:57:11 +01006550 vim_free(lhs.lhs_name);
6551 }
6552 else if (check_vim9_unlet(p) == FAIL)
6553 {
6554 ret = FAIL;
6555 }
6556 else
6557 {
6558 // Normal name. Only supports g:, w:, t: and b: namespaces.
6559 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006560 }
6561
Bram Moolenaar752fc692021-01-04 21:57:11 +01006562 *name_end = cc;
6563 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006564}
6565
6566/*
6567 * compile "unlet var", "lock var" and "unlock var"
6568 * "arg" points to "var".
6569 */
6570 static char_u *
6571compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6572{
6573 char_u *p = arg;
6574
6575 if (eap->cmdidx != CMD_unlet)
6576 {
6577 emsg("Sorry, :lock and unlock not implemented yet");
6578 return NULL;
6579 }
6580
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006581 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6582 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006583 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6584}
6585
6586/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 * Compile an :import command.
6588 */
6589 static char_u *
6590compile_import(char_u *arg, cctx_T *cctx)
6591{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006592 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006593}
6594
6595/*
6596 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6597 */
6598 static int
6599compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6600{
6601 garray_T *instr = &cctx->ctx_instr;
6602 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6603
6604 if (endlabel == NULL)
6605 return FAIL;
6606 endlabel->el_next = *el;
6607 *el = endlabel;
6608 endlabel->el_end_label = instr->ga_len;
6609
6610 generate_JUMP(cctx, when, 0);
6611 return OK;
6612}
6613
6614 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006615compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616{
6617 garray_T *instr = &cctx->ctx_instr;
6618
6619 while (*el != NULL)
6620 {
6621 endlabel_T *cur = (*el);
6622 isn_T *isn;
6623
6624 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006625 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006626 *el = cur->el_next;
6627 vim_free(cur);
6628 }
6629}
6630
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006631 static void
6632compile_free_jump_to_end(endlabel_T **el)
6633{
6634 while (*el != NULL)
6635 {
6636 endlabel_T *cur = (*el);
6637
6638 *el = cur->el_next;
6639 vim_free(cur);
6640 }
6641}
6642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006643/*
6644 * Create a new scope and set up the generic items.
6645 */
6646 static scope_T *
6647new_scope(cctx_T *cctx, scopetype_T type)
6648{
6649 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6650
6651 if (scope == NULL)
6652 return NULL;
6653 scope->se_outer = cctx->ctx_scope;
6654 cctx->ctx_scope = scope;
6655 scope->se_type = type;
6656 scope->se_local_count = cctx->ctx_locals.ga_len;
6657 return scope;
6658}
6659
6660/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006661 * Free the current scope and go back to the outer scope.
6662 */
6663 static void
6664drop_scope(cctx_T *cctx)
6665{
6666 scope_T *scope = cctx->ctx_scope;
6667
6668 if (scope == NULL)
6669 {
6670 iemsg("calling drop_scope() without a scope");
6671 return;
6672 }
6673 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006674 switch (scope->se_type)
6675 {
6676 case IF_SCOPE:
6677 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6678 case FOR_SCOPE:
6679 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6680 case WHILE_SCOPE:
6681 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6682 case TRY_SCOPE:
6683 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6684 case NO_SCOPE:
6685 case BLOCK_SCOPE:
6686 break;
6687 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006688 vim_free(scope);
6689}
6690
6691/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006692 * compile "if expr"
6693 *
6694 * "if expr" Produces instructions:
6695 * EVAL expr Push result of "expr"
6696 * JUMP_IF_FALSE end
6697 * ... body ...
6698 * end:
6699 *
6700 * "if expr | else" Produces instructions:
6701 * EVAL expr Push result of "expr"
6702 * JUMP_IF_FALSE else
6703 * ... body ...
6704 * JUMP_ALWAYS end
6705 * else:
6706 * ... body ...
6707 * end:
6708 *
6709 * "if expr1 | elseif expr2 | else" Produces instructions:
6710 * EVAL expr Push result of "expr"
6711 * JUMP_IF_FALSE elseif
6712 * ... body ...
6713 * JUMP_ALWAYS end
6714 * elseif:
6715 * EVAL expr Push result of "expr"
6716 * JUMP_IF_FALSE else
6717 * ... body ...
6718 * JUMP_ALWAYS end
6719 * else:
6720 * ... body ...
6721 * end:
6722 */
6723 static char_u *
6724compile_if(char_u *arg, cctx_T *cctx)
6725{
6726 char_u *p = arg;
6727 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006728 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006729 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006730 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006731 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006732
Bram Moolenaara5565e42020-05-09 15:44:01 +02006733 CLEAR_FIELD(ppconst);
6734 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006735 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006736 clear_ppconst(&ppconst);
6737 return NULL;
6738 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006739 if (!ends_excmd2(arg, skipwhite(p)))
6740 {
6741 semsg(_(e_trailing_arg), p);
6742 return NULL;
6743 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006744 if (cctx->ctx_skip == SKIP_YES)
6745 clear_ppconst(&ppconst);
6746 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006747 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006748 int error = FALSE;
6749 int v;
6750
Bram Moolenaara5565e42020-05-09 15:44:01 +02006751 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006752 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006753 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006754 if (error)
6755 return NULL;
6756 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006757 }
6758 else
6759 {
6760 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006761 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006762 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006763 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006764 if (bool_on_stack(cctx) == FAIL)
6765 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006767
6768 scope = new_scope(cctx, IF_SCOPE);
6769 if (scope == NULL)
6770 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006771 scope->se_skip_save = skip_save;
6772 // "is_had_return" will be reset if any block does not end in :return
6773 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006775 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006776 {
6777 // "where" is set when ":elseif", "else" or ":endif" is found
6778 scope->se_u.se_if.is_if_label = instr->ga_len;
6779 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6780 }
6781 else
6782 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006783
Bram Moolenaarced68a02021-01-24 17:53:47 +01006784#ifdef FEAT_PROFILE
6785 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6786 && skip_save != SKIP_YES)
6787 {
6788 // generated a profile start, need to generate a profile end, since it
6789 // won't be done after returning
6790 cctx->ctx_skip = SKIP_NOT;
6791 generate_instr(cctx, ISN_PROF_END);
6792 cctx->ctx_skip = SKIP_YES;
6793 }
6794#endif
6795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006796 return p;
6797}
6798
6799 static char_u *
6800compile_elseif(char_u *arg, cctx_T *cctx)
6801{
6802 char_u *p = arg;
6803 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006804 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805 isn_T *isn;
6806 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006807 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006808 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006809
6810 if (scope == NULL || scope->se_type != IF_SCOPE)
6811 {
6812 emsg(_(e_elseif_without_if));
6813 return NULL;
6814 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006815 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006816 if (!cctx->ctx_had_return)
6817 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006818
Bram Moolenaarced68a02021-01-24 17:53:47 +01006819 if (cctx->ctx_skip == SKIP_NOT)
6820 {
6821 // previous block was executed, this one and following will not
6822 cctx->ctx_skip = SKIP_YES;
6823 scope->se_u.se_if.is_seen_skip_not = TRUE;
6824 }
6825 if (scope->se_u.se_if.is_seen_skip_not)
6826 {
6827 // A previous block was executed, skip over expression and bail out.
6828 // Do not count the "elseif" for profiling.
6829#ifdef FEAT_PROFILE
6830 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6831 .isn_type == ISN_PROF_START)
6832 --instr->ga_len;
6833#endif
6834 skip_expr_cctx(&p, cctx);
6835 return p;
6836 }
6837
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006838 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006839 {
6840 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006841 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006842 return NULL;
6843 // previous "if" or "elseif" jumps here
6844 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6845 isn->isn_arg.jump.jump_where = instr->ga_len;
6846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006847
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006848 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006849 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006850 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006851 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006852 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006853#ifdef FEAT_PROFILE
6854 if (cctx->ctx_profiling)
6855 {
6856 // the previous block was skipped, need to profile this line
6857 generate_instr(cctx, ISN_PROF_START);
6858 instr_count = instr->ga_len;
6859 }
6860#endif
6861 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006862 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006863 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006864 clear_ppconst(&ppconst);
6865 return NULL;
6866 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006867 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006868 if (!ends_excmd2(arg, skipwhite(p)))
6869 {
6870 semsg(_(e_trailing_arg), p);
6871 return NULL;
6872 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006873 if (scope->se_skip_save == SKIP_YES)
6874 clear_ppconst(&ppconst);
6875 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006876 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006877 int error = FALSE;
6878 int v;
6879
Bram Moolenaar7f141552020-05-09 17:35:53 +02006880 // The expression results in a constant.
6881 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006882 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6883 if (error)
6884 return NULL;
6885 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006886 clear_ppconst(&ppconst);
6887 scope->se_u.se_if.is_if_label = -1;
6888 }
6889 else
6890 {
6891 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006892 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006893 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006894 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006895 if (bool_on_stack(cctx) == FAIL)
6896 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006898 // "where" is set when ":elseif", "else" or ":endif" is found
6899 scope->se_u.se_if.is_if_label = instr->ga_len;
6900 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006902
6903 return p;
6904}
6905
6906 static char_u *
6907compile_else(char_u *arg, cctx_T *cctx)
6908{
6909 char_u *p = arg;
6910 garray_T *instr = &cctx->ctx_instr;
6911 isn_T *isn;
6912 scope_T *scope = cctx->ctx_scope;
6913
6914 if (scope == NULL || scope->se_type != IF_SCOPE)
6915 {
6916 emsg(_(e_else_without_if));
6917 return NULL;
6918 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006919 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006920 if (!cctx->ctx_had_return)
6921 scope->se_u.se_if.is_had_return = FALSE;
6922 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923
Bram Moolenaarced68a02021-01-24 17:53:47 +01006924#ifdef FEAT_PROFILE
6925 if (cctx->ctx_profiling)
6926 {
6927 if (cctx->ctx_skip == SKIP_NOT
6928 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6929 .isn_type == ISN_PROF_START)
6930 // the previous block was executed, do not count "else" for profiling
6931 --instr->ga_len;
6932 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
6933 {
6934 // the previous block was not executed, this one will, do count the
6935 // "else" for profiling
6936 cctx->ctx_skip = SKIP_NOT;
6937 generate_instr(cctx, ISN_PROF_END);
6938 generate_instr(cctx, ISN_PROF_START);
6939 cctx->ctx_skip = SKIP_YES;
6940 }
6941 }
6942#endif
6943
6944 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006945 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006946 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006947 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006948 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006949 if (!cctx->ctx_had_return
6950 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6951 JUMP_ALWAYS, cctx) == FAIL)
6952 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006953 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006954
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006955 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006956 {
6957 if (scope->se_u.se_if.is_if_label >= 0)
6958 {
6959 // previous "if" or "elseif" jumps here
6960 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6961 isn->isn_arg.jump.jump_where = instr->ga_len;
6962 scope->se_u.se_if.is_if_label = -1;
6963 }
6964 }
6965
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006966 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006967 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969
6970 return p;
6971}
6972
6973 static char_u *
6974compile_endif(char_u *arg, cctx_T *cctx)
6975{
6976 scope_T *scope = cctx->ctx_scope;
6977 ifscope_T *ifscope;
6978 garray_T *instr = &cctx->ctx_instr;
6979 isn_T *isn;
6980
6981 if (scope == NULL || scope->se_type != IF_SCOPE)
6982 {
6983 emsg(_(e_endif_without_if));
6984 return NULL;
6985 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006986 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006987 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006988 if (!cctx->ctx_had_return)
6989 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006991 if (scope->se_u.se_if.is_if_label >= 0)
6992 {
6993 // previous "if" or "elseif" jumps here
6994 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6995 isn->isn_arg.jump.jump_where = instr->ga_len;
6996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006998 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01006999
7000#ifdef FEAT_PROFILE
7001 // even when skipping we count the endif as executed, unless the block it's
7002 // in is skipped
7003 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
7004 && scope->se_skip_save != SKIP_YES)
7005 {
7006 cctx->ctx_skip = SKIP_NOT;
7007 generate_instr(cctx, ISN_PROF_START);
7008 }
7009#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007010 cctx->ctx_skip = scope->se_skip_save;
7011
7012 // If all the blocks end in :return and there is an :else then the
7013 // had_return flag is set.
7014 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007016 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 return arg;
7018}
7019
7020/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007021 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 *
7023 * Produces instructions:
7024 * PUSHNR -1
7025 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007026 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7028 * - if beyond end, jump to "end"
7029 * - otherwise get item from list and push it
7030 * STORE var Store item in "var"
7031 * ... body ...
7032 * JUMP top Jump back to repeat
7033 * end: DROP Drop the result of "expr"
7034 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007035 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7036 * UNPACK 2 Split item in 2
7037 * STORE var1 Store item in "var1"
7038 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007039 */
7040 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007041compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007042{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007043 char_u *arg;
7044 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007045 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007047 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007048 int var_count = 0;
7049 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 size_t varlen;
7051 garray_T *instr = &cctx->ctx_instr;
7052 garray_T *stack = &cctx->ctx_type_stack;
7053 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007054 lvar_T *loop_lvar; // loop iteration variable
7055 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007057 type_T *item_type = &t_any;
7058 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059
Bram Moolenaar792f7862020-11-23 08:31:18 +01007060 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007061 if (p == NULL)
7062 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007063 if (var_count == 0)
7064 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007065
7066 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007067 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007068 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7069 return NULL;
7070 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 {
7072 emsg(_(e_missing_in));
7073 return NULL;
7074 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007075 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007076 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7077 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007079 scope = new_scope(cctx, FOR_SCOPE);
7080 if (scope == NULL)
7081 return NULL;
7082
Bram Moolenaar792f7862020-11-23 08:31:18 +01007083 // Reserve a variable to store the loop iteration counter and initialize it
7084 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007085 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7086 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007087 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007088 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007089 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007090 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007091 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007092 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093
7094 // compile "expr", it remains on the stack until "endfor"
7095 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007096 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007097 {
7098 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007100 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007101 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007103 // Now that we know the type of "var", check that it is a list, now or at
7104 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007105 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007106 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007108 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109 return NULL;
7110 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007111
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007112 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007113 {
7114 if (var_count == 1)
7115 item_type = vartype->tt_member;
7116 else if (vartype->tt_member->tt_type == VAR_LIST
7117 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7118 item_type = vartype->tt_member->tt_member;
7119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120
7121 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007122 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007123 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007124
Bram Moolenaar792f7862020-11-23 08:31:18 +01007125 arg = arg_start;
7126 if (var_count > 1)
7127 {
7128 generate_UNPACK(cctx, var_count, semicolon);
7129 arg = skipwhite(arg + 1); // skip white after '['
7130
7131 // the list item is replaced by a number of items
7132 if (ga_grow(stack, var_count - 1) == FAIL)
7133 {
7134 drop_scope(cctx);
7135 return NULL;
7136 }
7137 --stack->ga_len;
7138 for (idx = 0; idx < var_count; ++idx)
7139 {
7140 ((type_T **)stack->ga_data)[stack->ga_len] =
7141 (semicolon && idx == 0) ? vartype : item_type;
7142 ++stack->ga_len;
7143 }
7144 }
7145
7146 for (idx = 0; idx < var_count; ++idx)
7147 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007148 assign_dest_T dest = dest_local;
7149 int opt_flags = 0;
7150 int vimvaridx = -1;
7151 type_T *type = &t_any;
7152
7153 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007154 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007155 name = vim_strnsave(arg, varlen);
7156 if (name == NULL)
7157 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007158
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007159 // TODO: script var not supported?
7160 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7161 &vimvaridx, &type, cctx) == FAIL)
7162 goto failed;
7163 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007164 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007165 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7166 0, 0, type, name) == FAIL)
7167 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007168 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007169 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007170 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007171 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007172 {
7173 semsg(_(e_variable_already_declared), arg);
7174 goto failed;
7175 }
7176
Bram Moolenaarea870692020-12-02 14:24:30 +01007177 if (STRNCMP(name, "s:", 2) == 0)
7178 {
7179 semsg(_(e_cannot_declare_script_variable_in_function), name);
7180 goto failed;
7181 }
7182
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007183 // Reserve a variable to store "var".
7184 // TODO: check for type
7185 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7186 if (var_lvar == NULL)
7187 // out of memory or used as an argument
7188 goto failed;
7189
7190 if (semicolon && idx == var_count - 1)
7191 var_lvar->lv_type = vartype;
7192 else
7193 var_lvar->lv_type = item_type;
7194 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7195 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007196
Bram Moolenaar036d0712021-01-17 20:23:38 +01007197 if (*p == ':')
7198 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007199 if (*p == ',' || *p == ';')
7200 ++p;
7201 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007202 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007203 }
7204
7205 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007206
7207failed:
7208 vim_free(name);
7209 drop_scope(cctx);
7210 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211}
7212
7213/*
7214 * compile "endfor"
7215 */
7216 static char_u *
7217compile_endfor(char_u *arg, cctx_T *cctx)
7218{
7219 garray_T *instr = &cctx->ctx_instr;
7220 scope_T *scope = cctx->ctx_scope;
7221 forscope_T *forscope;
7222 isn_T *isn;
7223
7224 if (scope == NULL || scope->se_type != FOR_SCOPE)
7225 {
7226 emsg(_(e_for));
7227 return NULL;
7228 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007229 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007230 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007231 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
7233 // At end of ":for" scope jump back to the FOR instruction.
7234 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7235
7236 // Fill in the "end" label in the FOR statement so it can jump here
7237 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7238 isn->isn_arg.forloop.for_end = instr->ga_len;
7239
7240 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007241 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007242
7243 // Below the ":for" scope drop the "expr" list from the stack.
7244 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7245 return NULL;
7246
7247 vim_free(scope);
7248
7249 return arg;
7250}
7251
7252/*
7253 * compile "while expr"
7254 *
7255 * Produces instructions:
7256 * top: EVAL expr Push result of "expr"
7257 * JUMP_IF_FALSE end jump if false
7258 * ... body ...
7259 * JUMP top Jump back to repeat
7260 * end:
7261 *
7262 */
7263 static char_u *
7264compile_while(char_u *arg, cctx_T *cctx)
7265{
7266 char_u *p = arg;
7267 garray_T *instr = &cctx->ctx_instr;
7268 scope_T *scope;
7269
7270 scope = new_scope(cctx, WHILE_SCOPE);
7271 if (scope == NULL)
7272 return NULL;
7273
Bram Moolenaarb2049902021-01-24 12:53:53 +01007274 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007275 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007276#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007277 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7278 .isn_type == ISN_PROF_START)
7279 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007280#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007281
7282 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007283 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007285 if (!ends_excmd2(arg, skipwhite(p)))
7286 {
7287 semsg(_(e_trailing_arg), p);
7288 return NULL;
7289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007290
Bram Moolenaar13106602020-10-04 16:06:05 +02007291 if (bool_on_stack(cctx) == FAIL)
7292 return FAIL;
7293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007294 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007295 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 JUMP_IF_FALSE, cctx) == FAIL)
7297 return FAIL;
7298
7299 return p;
7300}
7301
7302/*
7303 * compile "endwhile"
7304 */
7305 static char_u *
7306compile_endwhile(char_u *arg, cctx_T *cctx)
7307{
7308 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007309 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007310
7311 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7312 {
7313 emsg(_(e_while));
7314 return NULL;
7315 }
7316 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007317 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007318
Bram Moolenaarf002a412021-01-24 13:34:18 +01007319#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007320 // count the endwhile before jumping
7321 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007322#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007323
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007324 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007325 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007326
7327 // Fill in the "end" label in the WHILE statement so it can jump here.
7328 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007329 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7330 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331
7332 vim_free(scope);
7333
7334 return arg;
7335}
7336
7337/*
7338 * compile "continue"
7339 */
7340 static char_u *
7341compile_continue(char_u *arg, cctx_T *cctx)
7342{
7343 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007344 int try_scopes = 0;
7345 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346
7347 for (;;)
7348 {
7349 if (scope == NULL)
7350 {
7351 emsg(_(e_continue));
7352 return NULL;
7353 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007354 if (scope->se_type == FOR_SCOPE)
7355 {
7356 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007358 }
7359 if (scope->se_type == WHILE_SCOPE)
7360 {
7361 loop_label = scope->se_u.se_while.ws_top_label;
7362 break;
7363 }
7364 if (scope->se_type == TRY_SCOPE)
7365 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 scope = scope->se_outer;
7367 }
7368
Bram Moolenaarc150c092021-02-13 15:02:46 +01007369 if (try_scopes > 0)
7370 // Inside one or more try/catch blocks we first need to jump to the
7371 // "finally" or "endtry" to cleanup.
7372 generate_TRYCONT(cctx, try_scopes, loop_label);
7373 else
7374 // Jump back to the FOR or WHILE instruction.
7375 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 return arg;
7378}
7379
7380/*
7381 * compile "break"
7382 */
7383 static char_u *
7384compile_break(char_u *arg, cctx_T *cctx)
7385{
7386 scope_T *scope = cctx->ctx_scope;
7387 endlabel_T **el;
7388
7389 for (;;)
7390 {
7391 if (scope == NULL)
7392 {
7393 emsg(_(e_break));
7394 return NULL;
7395 }
7396 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7397 break;
7398 scope = scope->se_outer;
7399 }
7400
7401 // Jump to the end of the FOR or WHILE loop.
7402 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007403 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007405 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7407 return FAIL;
7408
7409 return arg;
7410}
7411
7412/*
7413 * compile "{" start of block
7414 */
7415 static char_u *
7416compile_block(char_u *arg, cctx_T *cctx)
7417{
7418 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7419 return NULL;
7420 return skipwhite(arg + 1);
7421}
7422
7423/*
7424 * compile end of block: drop one scope
7425 */
7426 static void
7427compile_endblock(cctx_T *cctx)
7428{
7429 scope_T *scope = cctx->ctx_scope;
7430
7431 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007432 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 vim_free(scope);
7434}
7435
7436/*
7437 * compile "try"
7438 * Creates a new scope for the try-endtry, pointing to the first catch and
7439 * finally.
7440 * Creates another scope for the "try" block itself.
7441 * TRY instruction sets up exception handling at runtime.
7442 *
7443 * "try"
7444 * TRY -> catch1, -> finally push trystack entry
7445 * ... try block
7446 * "throw {exception}"
7447 * EVAL {exception}
7448 * THROW create exception
7449 * ... try block
7450 * " catch {expr}"
7451 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007452 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 * EVAL {expr}
7454 * MATCH
7455 * JUMP nomatch -> catch2
7456 * CATCH remove exception
7457 * ... catch block
7458 * " catch"
7459 * JUMP -> finally
7460 * catch2: CATCH remove exception
7461 * ... catch block
7462 * " finally"
7463 * finally:
7464 * ... finally block
7465 * " endtry"
7466 * ENDTRY pop trystack entry, may rethrow
7467 */
7468 static char_u *
7469compile_try(char_u *arg, cctx_T *cctx)
7470{
7471 garray_T *instr = &cctx->ctx_instr;
7472 scope_T *try_scope;
7473 scope_T *scope;
7474
7475 // scope that holds the jumps that go to catch/finally/endtry
7476 try_scope = new_scope(cctx, TRY_SCOPE);
7477 if (try_scope == NULL)
7478 return NULL;
7479
Bram Moolenaar69f70502021-01-01 16:10:46 +01007480 if (cctx->ctx_skip != SKIP_YES)
7481 {
7482 // "catch" is set when the first ":catch" is found.
7483 // "finally" is set when ":finally" or ":endtry" is found
7484 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7485 if (generate_instr(cctx, ISN_TRY) == NULL)
7486 return NULL;
7487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488
7489 // scope for the try block itself
7490 scope = new_scope(cctx, BLOCK_SCOPE);
7491 if (scope == NULL)
7492 return NULL;
7493
7494 return arg;
7495}
7496
7497/*
7498 * compile "catch {expr}"
7499 */
7500 static char_u *
7501compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7502{
7503 scope_T *scope = cctx->ctx_scope;
7504 garray_T *instr = &cctx->ctx_instr;
7505 char_u *p;
7506 isn_T *isn;
7507
7508 // end block scope from :try or :catch
7509 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7510 compile_endblock(cctx);
7511 scope = cctx->ctx_scope;
7512
7513 // Error if not in a :try scope
7514 if (scope == NULL || scope->se_type != TRY_SCOPE)
7515 {
7516 emsg(_(e_catch));
7517 return NULL;
7518 }
7519
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007520 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007522 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523 return NULL;
7524 }
7525
Bram Moolenaar69f70502021-01-01 16:10:46 +01007526 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007527 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007528#ifdef FEAT_PROFILE
7529 // the profile-start should be after the jump
7530 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7531 .isn_type == ISN_PROF_START)
7532 --instr->ga_len;
7533#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007534 // Jump from end of previous block to :finally or :endtry
7535 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7536 JUMP_ALWAYS, cctx) == FAIL)
7537 return NULL;
7538
7539 // End :try or :catch scope: set value in ISN_TRY instruction
7540 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7541 if (isn->isn_arg.try.try_catch == 0)
7542 isn->isn_arg.try.try_catch = instr->ga_len;
7543 if (scope->se_u.se_try.ts_catch_label != 0)
7544 {
7545 // Previous catch without match jumps here
7546 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7547 isn->isn_arg.jump.jump_where = instr->ga_len;
7548 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007549#ifdef FEAT_PROFILE
7550 if (cctx->ctx_profiling)
7551 {
7552 // a "throw" that jumps here needs to be counted
7553 generate_instr(cctx, ISN_PROF_END);
7554 // the "catch" is also counted
7555 generate_instr(cctx, ISN_PROF_START);
7556 }
7557#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007558 }
7559
7560 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007561 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007563 scope->se_u.se_try.ts_caught_all = TRUE;
7564 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 }
7566 else
7567 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007568 char_u *end;
7569 char_u *pat;
7570 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007571 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007572 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007574 // Push v:exception, push {expr} and MATCH
7575 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7576
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007577 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007578 if (*end != *p)
7579 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007580 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007581 vim_free(tofree);
7582 return FAIL;
7583 }
7584 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007585 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007586 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007587 len = (int)(end - tofree);
7588 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007589 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007590 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007591 if (pat == NULL)
7592 return FAIL;
7593 if (generate_PUSHS(cctx, pat) == FAIL)
7594 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007596 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7597 return NULL;
7598
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007599 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7601 return NULL;
7602 }
7603
Bram Moolenaar69f70502021-01-01 16:10:46 +01007604 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007605 return NULL;
7606
7607 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7608 return NULL;
7609 return p;
7610}
7611
7612 static char_u *
7613compile_finally(char_u *arg, cctx_T *cctx)
7614{
7615 scope_T *scope = cctx->ctx_scope;
7616 garray_T *instr = &cctx->ctx_instr;
7617 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007618 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007619
7620 // end block scope from :try or :catch
7621 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7622 compile_endblock(cctx);
7623 scope = cctx->ctx_scope;
7624
7625 // Error if not in a :try scope
7626 if (scope == NULL || scope->se_type != TRY_SCOPE)
7627 {
7628 emsg(_(e_finally));
7629 return NULL;
7630 }
7631
7632 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007633 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007634 if (isn->isn_arg.try.try_finally != 0)
7635 {
7636 emsg(_(e_finally_dup));
7637 return NULL;
7638 }
7639
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007640 this_instr = instr->ga_len;
7641#ifdef FEAT_PROFILE
7642 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7643 .isn_type == ISN_PROF_START)
7644 // jump to the profile start of the "finally"
7645 --this_instr;
7646#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007647
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007648 // Fill in the "end" label in jumps at the end of the blocks.
7649 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7650 this_instr, cctx);
7651
7652 isn->isn_arg.try.try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007653 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007654 {
7655 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007656 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007657 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007658 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007659 }
7660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007661 // TODO: set index in ts_finally_label jumps
7662
7663 return arg;
7664}
7665
7666 static char_u *
7667compile_endtry(char_u *arg, cctx_T *cctx)
7668{
7669 scope_T *scope = cctx->ctx_scope;
7670 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007671 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007672
7673 // end block scope from :catch or :finally
7674 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7675 compile_endblock(cctx);
7676 scope = cctx->ctx_scope;
7677
7678 // Error if not in a :try scope
7679 if (scope == NULL || scope->se_type != TRY_SCOPE)
7680 {
7681 if (scope == NULL)
7682 emsg(_(e_no_endtry));
7683 else if (scope->se_type == WHILE_SCOPE)
7684 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007685 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 emsg(_(e_endfor));
7687 else
7688 emsg(_(e_endif));
7689 return NULL;
7690 }
7691
Bram Moolenaarc150c092021-02-13 15:02:46 +01007692 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007693 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 {
Bram Moolenaarc150c092021-02-13 15:02:46 +01007695 if (try_isn->isn_arg.try.try_catch == 0
7696 && try_isn->isn_arg.try.try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007697 {
7698 emsg(_(e_missing_catch_or_finally));
7699 return NULL;
7700 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007701
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007702#ifdef FEAT_PROFILE
7703 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7704 .isn_type == ISN_PROF_START)
7705 // move the profile start after "endtry" so that it's not counted when
7706 // the exception is rethrown.
7707 --instr->ga_len;
7708#endif
7709
Bram Moolenaar69f70502021-01-01 16:10:46 +01007710 // Fill in the "end" label in jumps at the end of the blocks, if not
7711 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007712 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7713 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007714
Bram Moolenaar69f70502021-01-01 16:10:46 +01007715 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaarc150c092021-02-13 15:02:46 +01007716 if (try_isn->isn_arg.try.try_catch == 0)
7717 try_isn->isn_arg.try.try_catch = instr->ga_len;
7718 if (try_isn->isn_arg.try.try_finally == 0)
7719 try_isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007720
Bram Moolenaar69f70502021-01-01 16:10:46 +01007721 if (scope->se_u.se_try.ts_catch_label != 0)
7722 {
7723 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007724 isn_T *isn = ((isn_T *)instr->ga_data)
7725 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007726 isn->isn_arg.jump.jump_where = instr->ga_len;
7727 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007728 }
7729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007730 compile_endblock(cctx);
7731
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007732 if (cctx->ctx_skip != SKIP_YES)
7733 {
7734 if (try_isn->isn_arg.try.try_finally == 0)
7735 // No :finally encountered, use the try_finaly field to point to
7736 // ENDTRY, so that TRYCONT can jump there.
7737 try_isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007738
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007739 if (cctx->ctx_skip != SKIP_YES
7740 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7741 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007742#ifdef FEAT_PROFILE
7743 if (cctx->ctx_profiling)
7744 generate_instr(cctx, ISN_PROF_START);
7745#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007746 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007747 return arg;
7748}
7749
7750/*
7751 * compile "throw {expr}"
7752 */
7753 static char_u *
7754compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7755{
7756 char_u *p = skipwhite(arg);
7757
Bram Moolenaara5565e42020-05-09 15:44:01 +02007758 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007759 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007760 if (cctx->ctx_skip == SKIP_YES)
7761 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 if (may_generate_2STRING(-1, cctx) == FAIL)
7763 return NULL;
7764 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7765 return NULL;
7766
7767 return p;
7768}
7769
7770/*
7771 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007772 * compile "echomsg expr"
7773 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007774 * compile "execute expr"
7775 */
7776 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007777compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007778{
7779 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007780 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007781 int count = 0;
7782
7783 for (;;)
7784 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007785 if (ends_excmd2(prev, p))
7786 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007787 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007788 return NULL;
7789 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007790 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007791 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007792 }
7793
Bram Moolenaare4984292020-12-13 14:19:25 +01007794 if (count > 0)
7795 {
7796 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7797 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7798 else if (cmdidx == CMD_execute)
7799 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7800 else if (cmdidx == CMD_echomsg)
7801 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7802 else
7803 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007805 return p;
7806}
7807
7808/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007809 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007810 * instruction to compute it and return OK.
7811 * Otherwise return FAIL, the caller must deal with any range.
7812 */
7813 static int
7814compile_variable_range(exarg_T *eap, cctx_T *cctx)
7815{
7816 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7817 char_u *p = skipdigits(eap->cmd);
7818
7819 if (p == range_end)
7820 return FAIL;
7821 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7822}
7823
7824/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007825 * :put r
7826 * :put ={expr}
7827 */
7828 static char_u *
7829compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7830{
7831 char_u *line = arg;
7832 linenr_T lnum;
7833 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007834 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007835
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007836 eap->regname = *line;
7837
7838 if (eap->regname == '=')
7839 {
7840 char_u *p = line + 1;
7841
7842 if (compile_expr0(&p, cctx) == FAIL)
7843 return NULL;
7844 line = p;
7845 }
7846 else if (eap->regname != NUL)
7847 ++line;
7848
Bram Moolenaar08597872020-12-10 19:43:40 +01007849 if (compile_variable_range(eap, cctx) == OK)
7850 {
7851 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7852 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007853 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007854 {
7855 // Either no range or a number.
7856 // "errormsg" will not be set because the range is ADDR_LINES.
7857 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007858 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007859 return NULL;
7860 if (eap->addr_count == 0)
7861 lnum = -1;
7862 else
7863 lnum = eap->line2;
7864 if (above)
7865 --lnum;
7866 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007867
7868 generate_PUT(cctx, eap->regname, lnum);
7869 return line;
7870}
7871
7872/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007873 * A command that is not compiled, execute with legacy code.
7874 */
7875 static char_u *
7876compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7877{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007878 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007879 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007880 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007881
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007882 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007883 goto theend;
7884
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007885 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007886 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007887 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007888 int usefilter = FALSE;
7889
7890 has_expr = argt & (EX_XFILE | EX_EXPAND);
7891
7892 // If the command can be followed by a bar, find the bar and truncate
7893 // it, so that the following command can be compiled.
7894 // The '|' is overwritten with a NUL, it is put back below.
7895 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7896 && *eap->arg == '!')
7897 // :w !filter or :r !filter or :r! filter
7898 usefilter = TRUE;
7899 if ((argt & EX_TRLBAR) && !usefilter)
7900 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007901 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007902 separate_nextcmd(eap);
7903 if (eap->nextcmd != NULL)
7904 nextcmd = eap->nextcmd;
7905 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007906 else if (eap->cmdidx == CMD_wincmd)
7907 {
7908 p = eap->arg;
7909 if (*p != NUL)
7910 ++p;
7911 if (*p == 'g' || *p == Ctrl_G)
7912 ++p;
7913 p = skipwhite(p);
7914 if (*p == '|')
7915 {
7916 *p = NUL;
7917 nextcmd = p + 1;
7918 }
7919 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007920 }
7921
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007922 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7923 {
7924 // expand filename in "syntax include [@group] filename"
7925 has_expr = TRUE;
7926 eap->arg = skipwhite(eap->arg + 7);
7927 if (*eap->arg == '@')
7928 eap->arg = skiptowhite(eap->arg);
7929 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007930
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007931 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7932 && STRLEN(eap->arg) > 4)
7933 {
7934 int delim = *eap->arg;
7935
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007936 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007937 if (*p == delim)
7938 {
7939 eap->arg = p + 1;
7940 has_expr = TRUE;
7941 }
7942 }
7943
Bram Moolenaarecac5912021-01-05 19:23:28 +01007944 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7945 {
7946 // TODO: should only expand when appropriate for the command
7947 eap->arg = skiptowhite(eap->arg);
7948 has_expr = TRUE;
7949 }
7950
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007951 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007952 {
7953 int count = 0;
7954 char_u *start = skipwhite(line);
7955
7956 // :cmd xxx`=expr1`yyy`=expr2`zzz
7957 // PUSHS ":cmd xxx"
7958 // eval expr1
7959 // PUSHS "yyy"
7960 // eval expr2
7961 // PUSHS "zzz"
7962 // EXECCONCAT 5
7963 for (;;)
7964 {
7965 if (p > start)
7966 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007967 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007968 ++count;
7969 }
7970 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007971 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007972 return NULL;
7973 may_generate_2STRING(-1, cctx);
7974 ++count;
7975 p = skipwhite(p);
7976 if (*p != '`')
7977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007978 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007979 return NULL;
7980 }
7981 start = p + 1;
7982
7983 p = (char_u *)strstr((char *)start, "`=");
7984 if (p == NULL)
7985 {
7986 if (*skipwhite(start) != NUL)
7987 {
7988 generate_PUSHS(cctx, vim_strsave(start));
7989 ++count;
7990 }
7991 break;
7992 }
7993 }
7994 generate_EXECCONCAT(cctx, count);
7995 }
7996 else
7997 generate_EXEC(cctx, line);
7998
7999theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02008000 if (*nextcmd != NUL)
8001 {
8002 // the parser expects a pointer to the bar, put it back
8003 --nextcmd;
8004 *nextcmd = '|';
8005 }
8006
8007 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008008}
8009
8010/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008011 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008012 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008013 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008014 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008015add_def_function(ufunc_T *ufunc)
8016{
8017 dfunc_T *dfunc;
8018
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008019 if (def_functions.ga_len == 0)
8020 {
8021 // The first position is not used, so that a zero uf_dfunc_idx means it
8022 // wasn't set.
8023 if (ga_grow(&def_functions, 1) == FAIL)
8024 return FAIL;
8025 ++def_functions.ga_len;
8026 }
8027
Bram Moolenaar09689a02020-05-09 22:50:08 +02008028 // Add the function to "def_functions".
8029 if (ga_grow(&def_functions, 1) == FAIL)
8030 return FAIL;
8031 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8032 CLEAR_POINTER(dfunc);
8033 dfunc->df_idx = def_functions.ga_len;
8034 ufunc->uf_dfunc_idx = dfunc->df_idx;
8035 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008036 dfunc->df_name = vim_strsave(ufunc->uf_name);
8037 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008038 ++def_functions.ga_len;
8039 return OK;
8040}
8041
8042/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043 * After ex_function() has collected all the function lines: parse and compile
8044 * the lines into instructions.
8045 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008046 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8047 * the return statement (used for lambda). When uf_ret_type is already set
8048 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008049 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008050 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008051 * This can be used recursively through compile_lambda(), which may reallocate
8052 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008053 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008055 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008056compile_def_function(
8057 ufunc_T *ufunc,
8058 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008059 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008060 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008061{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008062 char_u *line = NULL;
8063 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008064 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008065 cctx_T cctx;
8066 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008067 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068 int ret = FAIL;
8069 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008070 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008071 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008072 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008073#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008074 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008075#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008077 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008078 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008079 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008080 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008081 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8082 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008083 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008084 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008085 else
8086 {
8087 if (add_def_function(ufunc) == FAIL)
8088 return FAIL;
8089 new_def_function = TRUE;
8090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008091
Bram Moolenaar985116a2020-07-12 17:31:09 +02008092 ufunc->uf_def_status = UF_COMPILING;
8093
Bram Moolenaara80faa82020-04-12 19:37:17 +02008094 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008095
Bram Moolenaarf002a412021-01-24 13:34:18 +01008096#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008097 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008098#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008099 cctx.ctx_ufunc = ufunc;
8100 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008101 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008102 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8103 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8104 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8105 cctx.ctx_type_list = &ufunc->uf_type_list;
8106 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8107 instr = &cctx.ctx_instr;
8108
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008109 // Set the context to the function, it may be compiled when called from
8110 // another script. Set the script version to the most modern one.
8111 // The line number will be set in next_line_from_context().
8112 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008113 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8114
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008115 // Make sure error messages are OK.
8116 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8117 if (do_estack_push)
8118 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008119 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008120
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008121 if (ufunc->uf_def_args.ga_len > 0)
8122 {
8123 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008124 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008125 int i;
8126 char_u *arg;
8127 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008128 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008129
8130 // Produce instructions for the default values of optional arguments.
8131 // Store the instruction index in uf_def_arg_idx[] so that we know
8132 // where to start when the function is called, depending on the number
8133 // of arguments.
8134 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8135 if (ufunc->uf_def_arg_idx == NULL)
8136 goto erret;
8137 for (i = 0; i < count; ++i)
8138 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008139 garray_T *stack = &cctx.ctx_type_stack;
8140 type_T *val_type;
8141 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008142 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008143
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008144 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8145 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008146 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008147 goto erret;
8148
8149 // If no type specified use the type of the default value.
8150 // Otherwise check that the default value type matches the
8151 // specified type.
8152 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008153 where.wt_index = arg_idx + 1;
8154 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008155 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008156 {
8157 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008158 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008159 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008160 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008161 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008162 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008163
8164 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008165 goto erret;
8166 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008167 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008168
8169 if (did_set_arg_type)
8170 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008171 }
8172
8173 /*
8174 * Loop over all the lines of the function and generate instructions.
8175 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008176 for (;;)
8177 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008178 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008179 int starts_with_colon = FALSE;
8180 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008181 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008182
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008183 // Bail out on the first error to avoid a flood of errors and report
8184 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008185 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008186 goto erret;
8187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 if (line != NULL && *line == '|')
8189 // the line continues after a '|'
8190 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008191 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008192 && !(*line == '#' && (line == cctx.ctx_line_start
8193 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008194 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008195 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008196 goto erret;
8197 }
8198 else
8199 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008200 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008201 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008202 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008203 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008204#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008205 if (cctx.ctx_skip != SKIP_YES)
8206 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008207#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 }
8211
Bram Moolenaara80faa82020-04-12 19:37:17 +02008212 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008213 ea.cmdlinep = &line;
8214 ea.cmd = skipwhite(line);
8215
Bram Moolenaarb2049902021-01-24 12:53:53 +01008216 if (*ea.cmd == '#')
8217 {
8218 // "#" starts a comment
8219 line = (char_u *)"";
8220 continue;
8221 }
8222
Bram Moolenaarf002a412021-01-24 13:34:18 +01008223#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008224 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8225 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008226 {
8227 may_generate_prof_end(&cctx, prof_lnum);
8228
8229 prof_lnum = cctx.ctx_lnum;
8230 generate_instr(&cctx, ISN_PROF_START);
8231 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008232#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008233
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008234 // Some things can be recognized by the first character.
8235 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008237 case '}':
8238 {
8239 // "}" ends a block scope
8240 scopetype_T stype = cctx.ctx_scope == NULL
8241 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008243 if (stype == BLOCK_SCOPE)
8244 {
8245 compile_endblock(&cctx);
8246 line = ea.cmd;
8247 }
8248 else
8249 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008250 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008251 goto erret;
8252 }
8253 if (line != NULL)
8254 line = skipwhite(ea.cmd + 1);
8255 continue;
8256 }
8257
8258 case '{':
8259 // "{" starts a block scope
8260 // "{'a': 1}->func() is something else
8261 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8262 {
8263 line = compile_block(ea.cmd, &cctx);
8264 continue;
8265 }
8266 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008267 }
8268
8269 /*
8270 * COMMAND MODIFIERS
8271 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008272 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008273 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8274 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008275 {
8276 if (errormsg != NULL)
8277 goto erret;
8278 // empty line or comment
8279 line = (char_u *)"";
8280 continue;
8281 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008282 generate_cmdmods(&cctx, &local_cmdmod);
8283 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008284
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008285 // Check if there was a colon after the last command modifier or before
8286 // the current position.
8287 for (p = ea.cmd; p >= line; --p)
8288 {
8289 if (*p == ':')
8290 starts_with_colon = TRUE;
8291 if (p < ea.cmd && !VIM_ISWHITE(*p))
8292 break;
8293 }
8294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008296 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008298 {
8299 if (*ea.cmd == '(')
8300 // not for "call()"
8301 ea.cmd = p;
8302 else
8303 ea.cmd = skipwhite(ea.cmd);
8304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008305
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008306 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008308 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008309
Bram Moolenaar17126b12021-01-07 22:03:02 +01008310 // Check for assignment after command modifiers.
8311 assign = may_compile_assignment(&ea, &line, &cctx);
8312 if (assign == OK)
8313 goto nextline;
8314 if (assign == FAIL)
8315 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008316 }
8317
8318 /*
8319 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008320 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008321 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008322 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008323 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008324 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008325 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008326 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008327 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008328 if (!starts_with_colon)
8329 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008330 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008331 goto erret;
8332 }
8333 if (ends_excmd2(line, ea.cmd))
8334 {
8335 // A range without a command: jump to the line.
8336 // TODO: compile to a more efficient command, possibly
8337 // calling parse_cmd_address().
8338 ea.cmdidx = CMD_SIZE;
8339 line = compile_exec(line, &ea, &cctx);
8340 goto nextline;
8341 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008342 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008343 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008344 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaare0890d62021-02-17 14:52:14 +01008345 : (int (*)(char_u *, size_t, cctx_T *))variable_exists,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008346 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008348 if (p == NULL)
8349 {
8350 if (cctx.ctx_skip != SKIP_YES)
8351 emsg(_(e_ambiguous_use_of_user_defined_command));
8352 goto erret;
8353 }
8354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8356 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008357 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008358 {
8359 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008360 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008361 }
8362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008364 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008365 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008366 // CMD_var cannot happen, compile_assignment() above would be
8367 // used. Most likely an assignment to a non-existing variable.
8368 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008369 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008371 }
8372
Bram Moolenaar3988f642020-08-27 22:43:03 +02008373 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008374 && ea.cmdidx != CMD_elseif
8375 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008376 && ea.cmdidx != CMD_endif
8377 && ea.cmdidx != CMD_endfor
8378 && ea.cmdidx != CMD_endwhile
8379 && ea.cmdidx != CMD_catch
8380 && ea.cmdidx != CMD_finally
8381 && ea.cmdidx != CMD_endtry)
8382 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008383 emsg(_(e_unreachable_code_after_return));
8384 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008385 }
8386
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008387 p = skipwhite(p);
8388 if (ea.cmdidx != CMD_SIZE
8389 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8390 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008391 if (ea.cmdidx >= 0)
8392 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008393 if ((ea.argt & EX_BANG) && *p == '!')
8394 {
8395 ea.forceit = TRUE;
8396 p = skipwhite(p + 1);
8397 }
8398 }
8399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008400 switch (ea.cmdidx)
8401 {
8402 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008403 ea.arg = p;
8404 line = compile_nested_function(&ea, &cctx);
8405 break;
8406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008408 // TODO: should we allow this, e.g. to declare a global
8409 // function?
8410 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008411 goto erret;
8412
8413 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008414 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008415 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416 break;
8417
8418 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008419 emsg(_(e_cannot_use_let_in_vim9_script));
8420 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008421 case CMD_var:
8422 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008423 case CMD_const:
8424 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008425 if (line == p)
8426 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008427 break;
8428
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008429 case CMD_unlet:
8430 case CMD_unlockvar:
8431 case CMD_lockvar:
8432 line = compile_unletlock(p, &ea, &cctx);
8433 break;
8434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435 case CMD_import:
8436 line = compile_import(p, &cctx);
8437 break;
8438
8439 case CMD_if:
8440 line = compile_if(p, &cctx);
8441 break;
8442 case CMD_elseif:
8443 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008444 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008445 break;
8446 case CMD_else:
8447 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008448 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008449 break;
8450 case CMD_endif:
8451 line = compile_endif(p, &cctx);
8452 break;
8453
8454 case CMD_while:
8455 line = compile_while(p, &cctx);
8456 break;
8457 case CMD_endwhile:
8458 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008459 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 break;
8461
8462 case CMD_for:
8463 line = compile_for(p, &cctx);
8464 break;
8465 case CMD_endfor:
8466 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008467 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008468 break;
8469 case CMD_continue:
8470 line = compile_continue(p, &cctx);
8471 break;
8472 case CMD_break:
8473 line = compile_break(p, &cctx);
8474 break;
8475
8476 case CMD_try:
8477 line = compile_try(p, &cctx);
8478 break;
8479 case CMD_catch:
8480 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008481 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008482 break;
8483 case CMD_finally:
8484 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008485 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008486 break;
8487 case CMD_endtry:
8488 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008489 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490 break;
8491 case CMD_throw:
8492 line = compile_throw(p, &cctx);
8493 break;
8494
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008495 case CMD_eval:
8496 if (compile_expr0(&p, &cctx) == FAIL)
8497 goto erret;
8498
Bram Moolenaar3988f642020-08-27 22:43:03 +02008499 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008500 generate_instr_drop(&cctx, ISN_DROP, 1);
8501
8502 line = skipwhite(p);
8503 break;
8504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008505 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008506 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008507 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008508 case CMD_echomsg:
8509 case CMD_echoerr:
8510 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008511 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008512
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008513 case CMD_put:
8514 ea.cmd = cmd;
8515 line = compile_put(p, &ea, &cctx);
8516 break;
8517
Bram Moolenaar3988f642020-08-27 22:43:03 +02008518 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008519
Bram Moolenaarae616492020-07-28 20:07:27 +02008520 case CMD_append:
8521 case CMD_change:
8522 case CMD_insert:
Bram Moolenaar10b94212021-02-19 21:42:57 +01008523 case CMD_k:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008524 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008525 case CMD_xit:
8526 not_in_vim9(&ea);
8527 goto erret;
8528
Bram Moolenaar002262f2020-07-08 17:47:57 +02008529 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008530 if (cctx.ctx_skip != SKIP_YES)
8531 {
8532 semsg(_(e_invalid_command_str), ea.cmd);
8533 goto erret;
8534 }
8535 // We don't check for a next command here.
8536 line = (char_u *)"";
8537 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008540 if (cctx.ctx_skip == SKIP_YES)
8541 {
8542 // We don't check for a next command here.
8543 line = (char_u *)"";
8544 }
8545 else
8546 {
8547 // Not recognized, execute with do_cmdline_cmd().
8548 ea.arg = p;
8549 line = compile_exec(line, &ea, &cctx);
8550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008551 break;
8552 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008553nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008554 if (line == NULL)
8555 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008556 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008557
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008558 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008559 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008561 if (cctx.ctx_type_stack.ga_len < 0)
8562 {
8563 iemsg("Type stack underflow");
8564 goto erret;
8565 }
8566 }
8567
8568 if (cctx.ctx_scope != NULL)
8569 {
8570 if (cctx.ctx_scope->se_type == IF_SCOPE)
8571 emsg(_(e_endif));
8572 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8573 emsg(_(e_endwhile));
8574 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8575 emsg(_(e_endfor));
8576 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008577 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008578 goto erret;
8579 }
8580
Bram Moolenaarefd88552020-06-18 20:50:10 +02008581 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008582 {
8583 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008585 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008586 goto erret;
8587 }
8588
8589 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008590 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008591 }
8592
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008593 {
8594 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8595 + ufunc->uf_dfunc_idx;
8596 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008597 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008598#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008599 if (cctx.ctx_profiling)
8600 {
8601 dfunc->df_instr_prof = instr->ga_data;
8602 dfunc->df_instr_prof_count = instr->ga_len;
8603 }
8604 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008605#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008606 {
8607 dfunc->df_instr = instr->ga_data;
8608 dfunc->df_instr_count = instr->ga_len;
8609 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008610 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008611 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008612 if (cctx.ctx_outer_used)
8613 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008614 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616
8617 ret = OK;
8618
8619erret:
8620 if (ret == FAIL)
8621 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008622 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008623 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8624 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008625
8626 for (idx = 0; idx < instr->ga_len; ++idx)
8627 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008628 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008629 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008630
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008631 // If using the last entry in the table and it was added above, we
8632 // might as well remove it.
8633 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008634 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008635 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008636 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008637 ufunc->uf_dfunc_idx = 0;
8638 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008639 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008640
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008641 while (cctx.ctx_scope != NULL)
8642 drop_scope(&cctx);
8643
Bram Moolenaar20431c92020-03-20 18:39:46 +01008644 // Don't execute this function body.
8645 ga_clear_strings(&ufunc->uf_lines);
8646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647 if (errormsg != NULL)
8648 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008649 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008650 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 }
8652
8653 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008654 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008655 if (do_estack_push)
8656 estack_pop();
8657
Bram Moolenaar20431c92020-03-20 18:39:46 +01008658 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008659 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008660 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008661 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662}
8663
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008664 void
8665set_function_type(ufunc_T *ufunc)
8666{
8667 int varargs = ufunc->uf_va_name != NULL;
8668 int argcount = ufunc->uf_args.ga_len;
8669
8670 // Create a type for the function, with the return type and any
8671 // argument types.
8672 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8673 // The type is included in "tt_args".
8674 if (argcount > 0 || varargs)
8675 {
8676 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8677 argcount, &ufunc->uf_type_list);
8678 // Add argument types to the function type.
8679 if (func_type_add_arg_types(ufunc->uf_func_type,
8680 argcount + varargs,
8681 &ufunc->uf_type_list) == FAIL)
8682 return;
8683 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8684 ufunc->uf_func_type->tt_min_argcount =
8685 argcount - ufunc->uf_def_args.ga_len;
8686 if (ufunc->uf_arg_types == NULL)
8687 {
8688 int i;
8689
8690 // lambda does not have argument types.
8691 for (i = 0; i < argcount; ++i)
8692 ufunc->uf_func_type->tt_args[i] = &t_any;
8693 }
8694 else
8695 mch_memmove(ufunc->uf_func_type->tt_args,
8696 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8697 if (varargs)
8698 {
8699 ufunc->uf_func_type->tt_args[argcount] =
8700 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8701 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8702 }
8703 }
8704 else
8705 // No arguments, can use a predefined type.
8706 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8707 argcount, &ufunc->uf_type_list);
8708}
8709
8710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008711/*
8712 * Delete an instruction, free what it contains.
8713 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008714 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008715delete_instr(isn_T *isn)
8716{
8717 switch (isn->isn_type)
8718 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008719 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008720 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008721 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008722 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008723 case ISN_LOADENV:
8724 case ISN_LOADG:
8725 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008726 case ISN_LOADT:
8727 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008729 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008730 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008731 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008732 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008733 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008734 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008735 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008736 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008737 case ISN_STOREW:
8738 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008739 vim_free(isn->isn_arg.string);
8740 break;
8741
8742 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008743 case ISN_STORES:
8744 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008745 break;
8746
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008747 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008748 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008749 vim_free(isn->isn_arg.unlet.ul_name);
8750 break;
8751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008752 case ISN_STOREOPT:
8753 vim_free(isn->isn_arg.storeopt.so_name);
8754 break;
8755
8756 case ISN_PUSHBLOB: // push blob isn_arg.blob
8757 blob_unref(isn->isn_arg.blob);
8758 break;
8759
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008760 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008761#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008762 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008763#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008764 break;
8765
8766 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008767#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008768 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008769#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008770 break;
8771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008772 case ISN_UCALL:
8773 vim_free(isn->isn_arg.ufunc.cuf_name);
8774 break;
8775
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008776 case ISN_FUNCREF:
8777 {
8778 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8779 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008780 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008781
Bram Moolenaar077a4232020-12-22 18:33:27 +01008782 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8783 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008784 }
8785 break;
8786
8787 case ISN_DCALL:
8788 {
8789 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8790 + isn->isn_arg.dfunc.cdf_idx;
8791
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008792 if (dfunc->df_ufunc != NULL
8793 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008794 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008795 }
8796 break;
8797
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008798 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008799 {
8800 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8801 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8802
8803 if (ufunc != NULL)
8804 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008805 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008806 func_ptr_unref(ufunc);
8807 }
8808
8809 vim_free(lambda);
8810 vim_free(isn->isn_arg.newfunc.nf_global);
8811 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008812 break;
8813
Bram Moolenaar5e654232020-09-16 15:22:00 +02008814 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008815 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008816 free_type(isn->isn_arg.type.ct_type);
8817 break;
8818
Bram Moolenaar02194d22020-10-24 23:08:38 +02008819 case ISN_CMDMOD:
8820 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8821 ->cmod_filter_regmatch.regprog);
8822 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8823 break;
8824
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008825 case ISN_LOADSCRIPT:
8826 case ISN_STORESCRIPT:
8827 vim_free(isn->isn_arg.script.scriptref);
8828 break;
8829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 case ISN_2BOOL:
8831 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008832 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008833 case ISN_ADDBLOB:
8834 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008835 case ISN_ANYINDEX:
8836 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008837 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008838 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008839 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008840 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008841 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008842 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008843 case ISN_COMPAREANY:
8844 case ISN_COMPAREBLOB:
8845 case ISN_COMPAREBOOL:
8846 case ISN_COMPAREDICT:
8847 case ISN_COMPAREFLOAT:
8848 case ISN_COMPAREFUNC:
8849 case ISN_COMPARELIST:
8850 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 case ISN_COMPARESPECIAL:
8852 case ISN_COMPARESTRING:
8853 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008854 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008855 case ISN_DROP:
8856 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008857 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008858 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008859 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008860 case ISN_EXECCONCAT:
8861 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008862 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008863 case ISN_GETITEM:
8864 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008865 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008866 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008867 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008868 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008869 case ISN_LOADBDICT:
8870 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008871 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008872 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008873 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008874 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008875 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008876 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008877 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 case ISN_NEGATENR:
8879 case ISN_NEWDICT:
8880 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008881 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008882 case ISN_OPFLOAT:
8883 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008884 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008885 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008886 case ISN_PROF_END:
8887 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008888 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008889 case ISN_PUSHF:
8890 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008891 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008892 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008893 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008894 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008895 case ISN_SHUFFLE:
8896 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008897 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008898 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008899 case ISN_STORENR:
8900 case ISN_STOREOUTER:
8901 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008902 case ISN_STOREV:
8903 case ISN_STRINDEX:
8904 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905 case ISN_THROW:
8906 case ISN_TRY:
Bram Moolenaarc150c092021-02-13 15:02:46 +01008907 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008908 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008909 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008910 // nothing allocated
8911 break;
8912 }
8913}
8914
8915/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008916 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008917 */
8918 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008919delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008920{
8921 int idx;
8922
8923 ga_clear(&dfunc->df_def_args_isn);
8924
8925 if (dfunc->df_instr != NULL)
8926 {
8927 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8928 delete_instr(dfunc->df_instr + idx);
8929 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008930 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008931 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01008932#ifdef FEAT_PROFILE
8933 if (dfunc->df_instr_prof != NULL)
8934 {
8935 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
8936 delete_instr(dfunc->df_instr_prof + idx);
8937 VIM_CLEAR(dfunc->df_instr_prof);
8938 dfunc->df_instr_prof = NULL;
8939 }
8940#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01008941
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008942 if (mark_deleted)
8943 dfunc->df_deleted = TRUE;
8944 if (dfunc->df_ufunc != NULL)
8945 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008946}
8947
8948/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008949 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008950 * function, unless another user function still uses it.
8951 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008952 */
8953 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008954unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008955{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008956 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008957 {
8958 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8959 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008960
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008961 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008962 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008963 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008964 ufunc->uf_dfunc_idx = 0;
8965 if (dfunc->df_ufunc == ufunc)
8966 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008967 }
8968}
8969
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008970/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008971 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008972 */
8973 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008974link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008975{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008976 if (ufunc->uf_dfunc_idx > 0)
8977 {
8978 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8979 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008980
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008981 ++dfunc->df_refcount;
8982 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008983}
8984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008985#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008986/*
8987 * Free all functions defined with ":def".
8988 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008989 void
8990free_def_functions(void)
8991{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008992 int idx;
8993
8994 for (idx = 0; idx < def_functions.ga_len; ++idx)
8995 {
8996 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8997
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008998 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008999 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01009000 }
9001
9002 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009003}
9004#endif
9005
9006
9007#endif // FEAT_EVAL