blob: b4b9c28c1d8f0f0930ebb9c631cae15e1b699df8 [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/*
376 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200377 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200378 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379 * Return FAIL and give an error if it defined.
380 */
381 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200382check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100383{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200384 int c = p[len];
385 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200386
387 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200388 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100389 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100390 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200391 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200392 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200393 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100394 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200395 // A local or script-local function can shadow a global function.
396 if (ufunc == NULL || !func_is_global(ufunc)
397 || (p[0] == 'g' && p[1] == ':'))
398 {
399 p[len] = c;
400 semsg(_(e_name_already_defined_str), p);
401 return FAIL;
402 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100403 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200404 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100405 return OK;
406}
407
Bram Moolenaar65b95452020-07-19 14:03:09 +0200408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409/////////////////////////////////////////////////////////////////////
410// Following generate_ functions expect the caller to call ga_grow().
411
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200412#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
413#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415/*
416 * Generate an instruction without arguments.
417 * Returns a pointer to the new instruction, NULL if failed.
418 */
419 static isn_T *
420generate_instr(cctx_T *cctx, isntype_T isn_type)
421{
422 garray_T *instr = &cctx->ctx_instr;
423 isn_T *isn;
424
Bram Moolenaar080457c2020-03-03 21:53:32 +0100425 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 if (ga_grow(instr, 1) == FAIL)
427 return NULL;
428 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
429 isn->isn_type = isn_type;
430 isn->isn_lnum = cctx->ctx_lnum + 1;
431 ++instr->ga_len;
432
433 return isn;
434}
435
436/*
437 * Generate an instruction without arguments.
438 * "drop" will be removed from the stack.
439 * Returns a pointer to the new instruction, NULL if failed.
440 */
441 static isn_T *
442generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
443{
444 garray_T *stack = &cctx->ctx_type_stack;
445
Bram Moolenaar080457c2020-03-03 21:53:32 +0100446 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447 stack->ga_len -= drop;
448 return generate_instr(cctx, isn_type);
449}
450
451/*
452 * Generate instruction "isn_type" and put "type" on the type stack.
453 */
454 static isn_T *
455generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
456{
457 isn_T *isn;
458 garray_T *stack = &cctx->ctx_type_stack;
459
460 if ((isn = generate_instr(cctx, isn_type)) == NULL)
461 return NULL;
462
463 if (ga_grow(stack, 1) == FAIL)
464 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200465 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466 ++stack->ga_len;
467
468 return isn;
469}
470
471/*
472 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200473 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474 */
475 static int
476may_generate_2STRING(int offset, cctx_T *cctx)
477{
478 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100481 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100483 RETURN_OK_IF_SKIP(cctx);
484 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200485 switch ((*type)->tt_type)
486 {
487 // nothing to be done
488 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200490 // conversion possible
491 case VAR_SPECIAL:
492 case VAR_BOOL:
493 case VAR_NUMBER:
494 case VAR_FLOAT:
495 break;
496
497 // conversion possible (with runtime check)
498 case VAR_ANY:
499 case VAR_UNKNOWN:
500 isntype = ISN_2STRING_ANY;
501 break;
502
503 // conversion not possible
504 case VAR_VOID:
505 case VAR_BLOB:
506 case VAR_FUNC:
507 case VAR_PARTIAL:
508 case VAR_LIST:
509 case VAR_DICT:
510 case VAR_JOB:
511 case VAR_CHANNEL:
512 to_string_error((*type)->tt_type);
513 return FAIL;
514 }
515
516 *type = &t_string;
517 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 return FAIL;
519 isn->isn_arg.number = offset;
520
521 return OK;
522}
523
524 static int
525check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
526{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200527 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200529 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 {
531 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200532 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200534 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 return FAIL;
536 }
537 return OK;
538}
539
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200540 static int
541generate_add_instr(
542 cctx_T *cctx,
543 vartype_T vartype,
544 type_T *type1,
545 type_T *type2)
546{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 garray_T *stack = &cctx->ctx_type_stack;
548 isn_T *isn = generate_instr_drop(cctx,
549 vartype == VAR_NUMBER ? ISN_OPNR
550 : vartype == VAR_LIST ? ISN_ADDLIST
551 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200552#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100553 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200554#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100555 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200556
557 if (vartype != VAR_LIST && vartype != VAR_BLOB
558 && type1->tt_type != VAR_ANY
559 && type2->tt_type != VAR_ANY
560 && check_number_or_float(
561 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
562 return FAIL;
563
564 if (isn != NULL)
565 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100566
567 // When concatenating two lists with different member types the member type
568 // becomes "any".
569 if (vartype == VAR_LIST
570 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
571 && type1->tt_member != type2->tt_member)
572 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
573
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200574 return isn == NULL ? FAIL : OK;
575}
576
577/*
578 * Get the type to use for an instruction for an operation on "type1" and
579 * "type2". If they are matching use a type-specific instruction. Otherwise
580 * fall back to runtime type checking.
581 */
582 static vartype_T
583operator_type(type_T *type1, type_T *type2)
584{
585 if (type1->tt_type == type2->tt_type
586 && (type1->tt_type == VAR_NUMBER
587 || type1->tt_type == VAR_LIST
588#ifdef FEAT_FLOAT
589 || type1->tt_type == VAR_FLOAT
590#endif
591 || type1->tt_type == VAR_BLOB))
592 return type1->tt_type;
593 return VAR_ANY;
594}
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596/*
597 * Generate an instruction with two arguments. The instruction depends on the
598 * type of the arguments.
599 */
600 static int
601generate_two_op(cctx_T *cctx, char_u *op)
602{
603 garray_T *stack = &cctx->ctx_type_stack;
604 type_T *type1;
605 type_T *type2;
606 vartype_T vartype;
607 isn_T *isn;
608
Bram Moolenaar080457c2020-03-03 21:53:32 +0100609 RETURN_OK_IF_SKIP(cctx);
610
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200611 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
613 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200614 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 switch (*op)
617 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200618 case '+':
619 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 break;
622
623 case '-':
624 case '*':
625 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
626 op) == FAIL)
627 return FAIL;
628 if (vartype == VAR_NUMBER)
629 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
630#ifdef FEAT_FLOAT
631 else if (vartype == VAR_FLOAT)
632 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
633#endif
634 else
635 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
636 if (isn != NULL)
637 isn->isn_arg.op.op_type = *op == '*'
638 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
639 break;
640
Bram Moolenaar4c683752020-04-05 21:38:23 +0200641 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200643 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 && type2->tt_type != VAR_NUMBER))
645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200646 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 return FAIL;
648 }
649 isn = generate_instr_drop(cctx,
650 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
651 if (isn != NULL)
652 isn->isn_arg.op.op_type = EXPR_REM;
653 break;
654 }
655
656 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200657 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 {
659 type_T *type = &t_any;
660
661#ifdef FEAT_FLOAT
662 // float+number and number+float results in float
663 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
664 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
665 type = &t_float;
666#endif
667 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
668 }
669
670 return OK;
671}
672
673/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200674 * Get the instruction to use for comparing "type1" with "type2"
675 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200677 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100678get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679{
680 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681
Bram Moolenaar4c683752020-04-05 21:38:23 +0200682 if (type1 == VAR_UNKNOWN)
683 type1 = VAR_ANY;
684 if (type2 == VAR_UNKNOWN)
685 type2 = VAR_ANY;
686
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 if (type1 == type2)
688 {
689 switch (type1)
690 {
691 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
692 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
693 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
694 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
695 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
696 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
697 case VAR_LIST: isntype = ISN_COMPARELIST; break;
698 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
699 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 default: isntype = ISN_COMPAREANY; break;
701 }
702 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200703 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
705 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
706 isntype = ISN_COMPAREANY;
707
Bram Moolenaar657137c2021-01-09 15:45:23 +0100708 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 && (isntype == ISN_COMPAREBOOL
710 || isntype == ISN_COMPARESPECIAL
711 || isntype == ISN_COMPARENR
712 || isntype == ISN_COMPAREFLOAT))
713 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200714 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100715 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200716 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 }
718 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100719 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
721 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100722 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
723 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 && (type1 == VAR_BLOB || type2 == VAR_BLOB
725 || type1 == VAR_LIST || type2 == VAR_LIST))))
726 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200727 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200729 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200731 return isntype;
732}
733
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200734 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100735check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200736{
737 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
738 return FAIL;
739 return OK;
740}
741
Bram Moolenaara5565e42020-05-09 15:44:01 +0200742/*
743 * Generate an ISN_COMPARE* instruction with a boolean result.
744 */
745 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100746generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200747{
748 isntype_T isntype;
749 isn_T *isn;
750 garray_T *stack = &cctx->ctx_type_stack;
751 vartype_T type1;
752 vartype_T type2;
753
754 RETURN_OK_IF_SKIP(cctx);
755
756 // Get the known type of the two items on the stack. If they are matching
757 // use a type-specific instruction. Otherwise fall back to runtime type
758 // checking.
759 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
760 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100761 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200762 if (isntype == ISN_DROP)
763 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
765 if ((isn = generate_instr(cctx, isntype)) == NULL)
766 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100767 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 isn->isn_arg.op.op_ic = ic;
769
770 // takes two arguments, puts one bool back
771 if (stack->ga_len >= 2)
772 {
773 --stack->ga_len;
774 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
775 }
776
777 return OK;
778}
779
780/*
781 * Generate an ISN_2BOOL instruction.
782 */
783 static int
784generate_2BOOL(cctx_T *cctx, int invert)
785{
786 isn_T *isn;
787 garray_T *stack = &cctx->ctx_type_stack;
788
Bram Moolenaar080457c2020-03-03 21:53:32 +0100789 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
791 return FAIL;
792 isn->isn_arg.number = invert;
793
794 // type becomes bool
795 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
796
797 return OK;
798}
799
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200800/*
801 * Generate an ISN_COND2BOOL instruction.
802 */
803 static int
804generate_COND2BOOL(cctx_T *cctx)
805{
806 isn_T *isn;
807 garray_T *stack = &cctx->ctx_type_stack;
808
809 RETURN_OK_IF_SKIP(cctx);
810 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
811 return FAIL;
812
813 // type becomes bool
814 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
815
816 return OK;
817}
818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200820generate_TYPECHECK(
821 cctx_T *cctx,
822 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100823 int offset,
824 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825{
826 isn_T *isn;
827 garray_T *stack = &cctx->ctx_type_stack;
828
Bram Moolenaar080457c2020-03-03 21:53:32 +0100829 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
831 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200832 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100833 isn->isn_arg.type.ct_off = (int8_T)offset;
834 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaar5e654232020-09-16 15:22:00 +0200836 // type becomes expected
837 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838
839 return OK;
840}
841
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100842 static int
843generate_SETTYPE(
844 cctx_T *cctx,
845 type_T *expected)
846{
847 isn_T *isn;
848
849 RETURN_OK_IF_SKIP(cctx);
850 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
851 return FAIL;
852 isn->isn_arg.type.ct_type = alloc_type(expected);
853 return OK;
854}
855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200857 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
858 * used. Return FALSE if the types will never match.
859 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100860 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200861use_typecheck(type_T *actual, type_T *expected)
862{
863 if (actual->tt_type == VAR_ANY
864 || actual->tt_type == VAR_UNKNOWN
865 || (actual->tt_type == VAR_FUNC
866 && (expected->tt_type == VAR_FUNC
867 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100868 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
869 && ((actual->tt_member == &t_void)
870 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200871 return TRUE;
872 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
873 && actual->tt_type == expected->tt_type)
874 // This takes care of a nested list or dict.
875 return use_typecheck(actual->tt_member, expected->tt_member);
876 return FALSE;
877}
878
879/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200881 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200882 * - "actual" is a type that can be "expected" type: add a runtime check; or
883 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200884 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
885 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200886 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100887 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200888need_type(
889 type_T *actual,
890 type_T *expected,
891 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100892 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200893 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200894 int silent,
895 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200896{
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100897 where_T where;
898
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200899 if (expected == &t_bool && actual != &t_bool
900 && (actual->tt_flags & TTFLAG_BOOL_OK))
901 {
902 // Using "0", "1" or the result of an expression with "&&" or "||" as a
903 // boolean is OK but requires a conversion.
904 generate_2BOOL(cctx, FALSE);
905 return OK;
906 }
907
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100908 where.wt_index = arg_idx;
909 where.wt_variable = FALSE;
910 if (check_type(expected, actual, FALSE, where) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200911 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200912
913 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200914 // If it's a constant a runtime check makes no sense.
915 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200916 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100917 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200918 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200919 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200920
921 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100922 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200923 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200924}
925
926/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100927 * Check that the top of the type stack has a type that can be used as a
928 * condition. Give an error and return FAIL if not.
929 */
930 static int
931bool_on_stack(cctx_T *cctx)
932{
933 garray_T *stack = &cctx->ctx_type_stack;
934 type_T *type;
935
936 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
937 if (type == &t_bool)
938 return OK;
939
940 if (type == &t_any || type == &t_number)
941 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
942 // This requires a runtime type check.
943 return generate_COND2BOOL(cctx);
944
Bram Moolenaar351ead02021-01-16 16:07:01 +0100945 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100946}
947
948/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 * Generate an ISN_PUSHNR instruction.
950 */
951 static int
952generate_PUSHNR(cctx_T *cctx, varnumber_T number)
953{
954 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200955 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
Bram Moolenaar080457c2020-03-03 21:53:32 +0100957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
959 return FAIL;
960 isn->isn_arg.number = number;
961
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200962 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200963 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100964 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 return OK;
966}
967
968/*
969 * Generate an ISN_PUSHBOOL instruction.
970 */
971 static int
972generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
973{
974 isn_T *isn;
975
Bram Moolenaar080457c2020-03-03 21:53:32 +0100976 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
978 return FAIL;
979 isn->isn_arg.number = number;
980
981 return OK;
982}
983
984/*
985 * Generate an ISN_PUSHSPEC instruction.
986 */
987 static int
988generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
989{
990 isn_T *isn;
991
Bram Moolenaar080457c2020-03-03 21:53:32 +0100992 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
994 return FAIL;
995 isn->isn_arg.number = number;
996
997 return OK;
998}
999
1000#ifdef FEAT_FLOAT
1001/*
1002 * Generate an ISN_PUSHF instruction.
1003 */
1004 static int
1005generate_PUSHF(cctx_T *cctx, float_T fnumber)
1006{
1007 isn_T *isn;
1008
Bram Moolenaar080457c2020-03-03 21:53:32 +01001009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1011 return FAIL;
1012 isn->isn_arg.fnumber = fnumber;
1013
1014 return OK;
1015}
1016#endif
1017
1018/*
1019 * Generate an ISN_PUSHS instruction.
1020 * Consumes "str".
1021 */
1022 static int
1023generate_PUSHS(cctx_T *cctx, char_u *str)
1024{
1025 isn_T *isn;
1026
Bram Moolenaar508b5612021-01-02 18:17:26 +01001027 if (cctx->ctx_skip == SKIP_YES)
1028 {
1029 vim_free(str);
1030 return OK;
1031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.string = str;
1035
1036 return OK;
1037}
1038
1039/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001040 * Generate an ISN_PUSHCHANNEL instruction.
1041 * Consumes "channel".
1042 */
1043 static int
1044generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1045{
1046 isn_T *isn;
1047
Bram Moolenaar080457c2020-03-03 21:53:32 +01001048 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001049 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1050 return FAIL;
1051 isn->isn_arg.channel = channel;
1052
1053 return OK;
1054}
1055
1056/*
1057 * Generate an ISN_PUSHJOB instruction.
1058 * Consumes "job".
1059 */
1060 static int
1061generate_PUSHJOB(cctx_T *cctx, job_T *job)
1062{
1063 isn_T *isn;
1064
Bram Moolenaar080457c2020-03-03 21:53:32 +01001065 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001066 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001067 return FAIL;
1068 isn->isn_arg.job = job;
1069
1070 return OK;
1071}
1072
1073/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 * Generate an ISN_PUSHBLOB instruction.
1075 * Consumes "blob".
1076 */
1077 static int
1078generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1079{
1080 isn_T *isn;
1081
Bram Moolenaar080457c2020-03-03 21:53:32 +01001082 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1084 return FAIL;
1085 isn->isn_arg.blob = blob;
1086
1087 return OK;
1088}
1089
1090/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001091 * Generate an ISN_PUSHFUNC instruction with name "name".
1092 * Consumes "name".
1093 */
1094 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001095generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001096{
1097 isn_T *isn;
1098
Bram Moolenaar080457c2020-03-03 21:53:32 +01001099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001100 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001101 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001102 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001103
1104 return OK;
1105}
1106
1107/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001108 * Generate an ISN_GETITEM instruction with "index".
1109 */
1110 static int
1111generate_GETITEM(cctx_T *cctx, int index)
1112{
1113 isn_T *isn;
1114 garray_T *stack = &cctx->ctx_type_stack;
1115 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1116 type_T *item_type = &t_any;
1117
1118 RETURN_OK_IF_SKIP(cctx);
1119
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001120 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001121 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001122 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001123 emsg(_(e_listreq));
1124 return FAIL;
1125 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001126 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001127 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1128 return FAIL;
1129 isn->isn_arg.number = index;
1130
1131 // add the item type to the type stack
1132 if (ga_grow(stack, 1) == FAIL)
1133 return FAIL;
1134 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1135 ++stack->ga_len;
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001140 * Generate an ISN_SLICE instruction with "count".
1141 */
1142 static int
1143generate_SLICE(cctx_T *cctx, int count)
1144{
1145 isn_T *isn;
1146
1147 RETURN_OK_IF_SKIP(cctx);
1148 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1149 return FAIL;
1150 isn->isn_arg.number = count;
1151 return OK;
1152}
1153
1154/*
1155 * Generate an ISN_CHECKLEN instruction with "min_len".
1156 */
1157 static int
1158generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1159{
1160 isn_T *isn;
1161
1162 RETURN_OK_IF_SKIP(cctx);
1163
1164 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1165 return FAIL;
1166 isn->isn_arg.checklen.cl_min_len = min_len;
1167 isn->isn_arg.checklen.cl_more_OK = more_OK;
1168
1169 return OK;
1170}
1171
1172/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 * Generate an ISN_STORE instruction.
1174 */
1175 static int
1176generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1177{
1178 isn_T *isn;
1179
Bram Moolenaar080457c2020-03-03 21:53:32 +01001180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1182 return FAIL;
1183 if (name != NULL)
1184 isn->isn_arg.string = vim_strsave(name);
1185 else
1186 isn->isn_arg.number = idx;
1187
1188 return OK;
1189}
1190
1191/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001192 * Generate an ISN_STOREOUTER instruction.
1193 */
1194 static int
1195generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1196{
1197 isn_T *isn;
1198
1199 RETURN_OK_IF_SKIP(cctx);
1200 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1201 return FAIL;
1202 isn->isn_arg.outer.outer_idx = idx;
1203 isn->isn_arg.outer.outer_depth = level;
1204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1210 */
1211 static int
1212generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1213{
1214 isn_T *isn;
1215
Bram Moolenaar080457c2020-03-03 21:53:32 +01001216 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1218 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001219 isn->isn_arg.storenr.stnr_idx = idx;
1220 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
1222 return OK;
1223}
1224
1225/*
1226 * Generate an ISN_STOREOPT instruction
1227 */
1228 static int
1229generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1230{
1231 isn_T *isn;
1232
Bram Moolenaar080457c2020-03-03 21:53:32 +01001233 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001234 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 return FAIL;
1236 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1237 isn->isn_arg.storeopt.so_flags = opt_flags;
1238
1239 return OK;
1240}
1241
1242/*
1243 * Generate an ISN_LOAD or similar instruction.
1244 */
1245 static int
1246generate_LOAD(
1247 cctx_T *cctx,
1248 isntype_T isn_type,
1249 int idx,
1250 char_u *name,
1251 type_T *type)
1252{
1253 isn_T *isn;
1254
Bram Moolenaar080457c2020-03-03 21:53:32 +01001255 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1257 return FAIL;
1258 if (name != NULL)
1259 isn->isn_arg.string = vim_strsave(name);
1260 else
1261 isn->isn_arg.number = idx;
1262
1263 return OK;
1264}
1265
1266/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001267 * Generate an ISN_LOADOUTER instruction
1268 */
1269 static int
1270generate_LOADOUTER(
1271 cctx_T *cctx,
1272 int idx,
1273 int nesting,
1274 type_T *type)
1275{
1276 isn_T *isn;
1277
1278 RETURN_OK_IF_SKIP(cctx);
1279 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1280 return FAIL;
1281 isn->isn_arg.outer.outer_idx = idx;
1282 isn->isn_arg.outer.outer_depth = nesting;
1283
1284 return OK;
1285}
1286
1287/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001288 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 */
1290 static int
1291generate_LOADV(
1292 cctx_T *cctx,
1293 char_u *name,
1294 int error)
1295{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001296 int di_flags;
1297 int vidx = find_vim_var(name, &di_flags);
1298 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299
Bram Moolenaar080457c2020-03-03 21:53:32 +01001300 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001301 if (vidx < 0)
1302 {
1303 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001304 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001305 return FAIL;
1306 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001307 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001308
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001309 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001310}
1311
1312/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001313 * Generate an ISN_UNLET instruction.
1314 */
1315 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001316generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001317{
1318 isn_T *isn;
1319
1320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001321 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001322 return FAIL;
1323 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1324 isn->isn_arg.unlet.ul_forceit = forceit;
1325
1326 return OK;
1327}
1328
1329/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001330 * Generate an ISN_LOCKCONST instruction.
1331 */
1332 static int
1333generate_LOCKCONST(cctx_T *cctx)
1334{
1335 isn_T *isn;
1336
1337 RETURN_OK_IF_SKIP(cctx);
1338 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1339 return FAIL;
1340 return OK;
1341}
1342
1343/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 * Generate an ISN_LOADS instruction.
1345 */
1346 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001347generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001349 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 int sid,
1352 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353{
1354 isn_T *isn;
1355
Bram Moolenaar080457c2020-03-03 21:53:32 +01001356 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 if (isn_type == ISN_LOADS)
1358 isn = generate_instr_type(cctx, isn_type, type);
1359 else
1360 isn = generate_instr_drop(cctx, isn_type, 1);
1361 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1364 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1371 */
1372 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001373generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 cctx_T *cctx,
1375 isntype_T isn_type,
1376 int sid,
1377 int idx,
1378 type_T *type)
1379{
1380 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001381 scriptref_T *sref;
1382 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383
Bram Moolenaar080457c2020-03-03 21:53:32 +01001384 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 if (isn_type == ISN_LOADSCRIPT)
1386 isn = generate_instr_type(cctx, isn_type, type);
1387 else
1388 isn = generate_instr_drop(cctx, isn_type, 1);
1389 if (isn == NULL)
1390 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001391
1392 // This requires three arguments, which doesn't fit in an instruction, thus
1393 // we need to allocate a struct for this.
1394 sref = ALLOC_ONE(scriptref_T);
1395 if (sref == NULL)
1396 return FAIL;
1397 isn->isn_arg.script.scriptref = sref;
1398 sref->sref_sid = sid;
1399 sref->sref_idx = idx;
1400 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001401 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 return OK;
1403}
1404
1405/*
1406 * Generate an ISN_NEWLIST instruction.
1407 */
1408 static int
1409generate_NEWLIST(cctx_T *cctx, int count)
1410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 type_T *type;
1414 type_T *member;
1415
Bram Moolenaar080457c2020-03-03 21:53:32 +01001416 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1418 return FAIL;
1419 isn->isn_arg.number = count;
1420
Bram Moolenaar127542b2020-08-09 17:22:04 +02001421 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001422 if (count == 0)
1423 member = &t_void;
1424 else
1425 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001426 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1427 cctx->ctx_type_list);
1428 type = get_list_type(member, cctx->ctx_type_list);
1429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 // drop the value types
1431 stack->ga_len -= count;
1432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 // add the list type to the type stack
1434 if (ga_grow(stack, 1) == FAIL)
1435 return FAIL;
1436 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1437 ++stack->ga_len;
1438
1439 return OK;
1440}
1441
1442/*
1443 * Generate an ISN_NEWDICT instruction.
1444 */
1445 static int
1446generate_NEWDICT(cctx_T *cctx, int count)
1447{
1448 isn_T *isn;
1449 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 type_T *type;
1451 type_T *member;
1452
Bram Moolenaar080457c2020-03-03 21:53:32 +01001453 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1455 return FAIL;
1456 isn->isn_arg.number = count;
1457
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001458 if (count == 0)
1459 member = &t_void;
1460 else
1461 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001462 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1463 cctx->ctx_type_list);
1464 type = get_dict_type(member, cctx->ctx_type_list);
1465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 // drop the key and value types
1467 stack->ga_len -= 2 * count;
1468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 // add the dict type to the type stack
1470 if (ga_grow(stack, 1) == FAIL)
1471 return FAIL;
1472 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1473 ++stack->ga_len;
1474
1475 return OK;
1476}
1477
1478/*
1479 * Generate an ISN_FUNCREF instruction.
1480 */
1481 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001482generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1489 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001490 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001491 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492
Bram Moolenaarab360522021-01-10 14:02:28 +01001493 // if the referenced function is a closure, it may use items further up in
1494 // the nested context, including this one.
1495 if (ufunc->uf_flags & FC_CLOSURE)
1496 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 if (ga_grow(stack, 1) == FAIL)
1499 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001500 ((type_T **)stack->ga_data)[stack->ga_len] =
1501 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 ++stack->ga_len;
1503
1504 return OK;
1505}
1506
1507/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001508 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001509 * "lambda_name" and "func_name" must be in allocated memory and will be
1510 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001511 */
1512 static int
1513generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1514{
1515 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001516
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001517 if (cctx->ctx_skip == SKIP_YES)
1518 {
1519 vim_free(lambda_name);
1520 vim_free(func_name);
1521 return OK;
1522 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001523 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001524 {
1525 vim_free(lambda_name);
1526 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001527 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001528 }
1529 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001530 isn->isn_arg.newfunc.nf_global = func_name;
1531
1532 return OK;
1533}
1534
1535/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001536 * Generate an ISN_DEF instruction: list functions
1537 */
1538 static int
1539generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1540{
1541 isn_T *isn;
1542
1543 RETURN_OK_IF_SKIP(cctx);
1544 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1545 return FAIL;
1546 if (len > 0)
1547 {
1548 isn->isn_arg.string = vim_strnsave(name, len);
1549 if (isn->isn_arg.string == NULL)
1550 return FAIL;
1551 }
1552 return OK;
1553}
1554
1555/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 * Generate an ISN_JUMP instruction.
1557 */
1558 static int
1559generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1560{
1561 isn_T *isn;
1562 garray_T *stack = &cctx->ctx_type_stack;
1563
Bram Moolenaar080457c2020-03-03 21:53:32 +01001564 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1566 return FAIL;
1567 isn->isn_arg.jump.jump_when = when;
1568 isn->isn_arg.jump.jump_where = where;
1569
1570 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1571 --stack->ga_len;
1572
1573 return OK;
1574}
1575
1576 static int
1577generate_FOR(cctx_T *cctx, int loop_idx)
1578{
1579 isn_T *isn;
1580 garray_T *stack = &cctx->ctx_type_stack;
1581
Bram Moolenaar080457c2020-03-03 21:53:32 +01001582 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1584 return FAIL;
1585 isn->isn_arg.forloop.for_idx = loop_idx;
1586
1587 if (ga_grow(stack, 1) == FAIL)
1588 return FAIL;
1589 // type doesn't matter, will be stored next
1590 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1591 ++stack->ga_len;
1592
1593 return OK;
1594}
1595
1596/*
1597 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001598 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 * Return FAIL if the number of arguments is wrong.
1600 */
1601 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001602generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603{
1604 isn_T *isn;
1605 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001606 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001607 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001608 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609
Bram Moolenaar080457c2020-03-03 21:53:32 +01001610 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001611 argoff = check_internal_func(func_idx, argcount);
1612 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 return FAIL;
1614
Bram Moolenaar389df252020-07-09 21:20:47 +02001615 if (method_call && argoff > 1)
1616 {
1617 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1618 return FAIL;
1619 isn->isn_arg.shuffle.shfl_item = argcount;
1620 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1621 }
1622
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001623 if (argcount > 0)
1624 {
1625 // Check the types of the arguments.
1626 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001627 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1628 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001629 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001630 if (internal_func_is_map(func_idx))
1631 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001632 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.bfunc.cbf_idx = func_idx;
1637 isn->isn_arg.bfunc.cbf_argcount = argcount;
1638
Bram Moolenaar94738d82020-10-21 14:25:07 +02001639 // Drop the argument types and push the return type.
1640 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 if (ga_grow(stack, 1) == FAIL)
1642 return FAIL;
1643 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001644 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001645 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001647 if (maptype != NULL && maptype->tt_member != NULL
1648 && maptype->tt_member != &t_any)
1649 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001650 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 return OK;
1653}
1654
1655/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001656 * Generate an ISN_LISTAPPEND instruction. Works like add().
1657 * Argument count is already checked.
1658 */
1659 static int
1660generate_LISTAPPEND(cctx_T *cctx)
1661{
1662 garray_T *stack = &cctx->ctx_type_stack;
1663 type_T *list_type;
1664 type_T *item_type;
1665 type_T *expected;
1666
1667 // Caller already checked that list_type is a list.
1668 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1669 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1670 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001671 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001672 return FAIL;
1673
1674 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1675 return FAIL;
1676
1677 --stack->ga_len; // drop the argument
1678 return OK;
1679}
1680
1681/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001682 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1683 * Argument count is already checked.
1684 */
1685 static int
1686generate_BLOBAPPEND(cctx_T *cctx)
1687{
1688 garray_T *stack = &cctx->ctx_type_stack;
1689 type_T *item_type;
1690
1691 // Caller already checked that blob_type is a blob.
1692 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001693 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001694 return FAIL;
1695
1696 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1697 return FAIL;
1698
1699 --stack->ga_len; // drop the argument
1700 return OK;
1701}
1702
1703/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001704 * Return TRUE if "ufunc" should be compiled, taking into account whether
1705 * "profile" indicates profiling is to be done.
1706 */
1707 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001708func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001709{
1710 switch (ufunc->uf_def_status)
1711 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001712 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001713 case UF_TO_BE_COMPILED: return TRUE;
1714 case UF_COMPILED:
1715 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001716#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001717 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1718 + ufunc->uf_dfunc_idx;
1719
1720 return profile ? dfunc->df_instr_prof == NULL
1721 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001722#else
1723 break;
1724#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001725 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001726 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001727 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001728 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001729}
1730
1731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 * Generate an ISN_DCALL or ISN_UCALL instruction.
1733 * Return FAIL if the number of arguments is wrong.
1734 */
1735 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001736generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737{
1738 isn_T *isn;
1739 garray_T *stack = &cctx->ctx_type_stack;
1740 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001741 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
Bram Moolenaar080457c2020-03-03 21:53:32 +01001743 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 if (argcount > regular_args && !has_varargs(ufunc))
1745 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001746 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 return FAIL;
1748 }
1749 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1750 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001751 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 return FAIL;
1753 }
1754
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001755 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001756 {
1757 int i;
1758
1759 for (i = 0; i < argcount; ++i)
1760 {
1761 type_T *expected;
1762 type_T *actual;
1763
1764 if (i < regular_args)
1765 {
1766 if (ufunc->uf_arg_types == NULL)
1767 continue;
1768 expected = ufunc->uf_arg_types[i];
1769 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001770 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1771 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001772 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001773 else
1774 expected = ufunc->uf_va_type->tt_member;
1775 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001776 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001777 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001778 {
1779 arg_type_mismatch(expected, actual, i + 1);
1780 return FAIL;
1781 }
1782 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001783 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001784 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001785 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001786 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001787 }
1788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001790 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001791 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001793 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 {
1795 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1796 isn->isn_arg.dfunc.cdf_argcount = argcount;
1797 }
1798 else
1799 {
1800 // A user function may be deleted and redefined later, can't use the
1801 // ufunc pointer, need to look it up again at runtime.
1802 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1803 isn->isn_arg.ufunc.cuf_argcount = argcount;
1804 }
1805
1806 stack->ga_len -= argcount; // drop the arguments
1807 if (ga_grow(stack, 1) == FAIL)
1808 return FAIL;
1809 // add return value
1810 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1811 ++stack->ga_len;
1812
1813 return OK;
1814}
1815
1816/*
1817 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1818 */
1819 static int
1820generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1821{
1822 isn_T *isn;
1823 garray_T *stack = &cctx->ctx_type_stack;
1824
Bram Moolenaar080457c2020-03-03 21:53:32 +01001825 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1827 return FAIL;
1828 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1829 isn->isn_arg.ufunc.cuf_argcount = argcount;
1830
1831 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001832 if (ga_grow(stack, 1) == FAIL)
1833 return FAIL;
1834 // add return value
1835 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1836 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837
1838 return OK;
1839}
1840
1841/*
1842 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001843 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 */
1845 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001846generate_PCALL(
1847 cctx_T *cctx,
1848 int argcount,
1849 char_u *name,
1850 type_T *type,
1851 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852{
1853 isn_T *isn;
1854 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001855 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856
Bram Moolenaar080457c2020-03-03 21:53:32 +01001857 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001858
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001859 if (type->tt_type == VAR_ANY)
1860 ret_type = &t_any;
1861 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001862 {
1863 if (type->tt_argcount != -1)
1864 {
1865 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1866
1867 if (argcount < type->tt_min_argcount - varargs)
1868 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001869 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001870 return FAIL;
1871 }
1872 if (!varargs && argcount > type->tt_argcount)
1873 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001874 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001875 return FAIL;
1876 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001877 if (type->tt_args != NULL)
1878 {
1879 int i;
1880
1881 for (i = 0; i < argcount; ++i)
1882 {
1883 int offset = -argcount + i - 1;
1884 type_T *actual = ((type_T **)stack->ga_data)[
1885 stack->ga_len + offset];
1886 type_T *expected;
1887
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001888 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001889 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001890 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001891 else
1892 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001893 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001894 cctx, TRUE, FALSE) == FAIL)
1895 {
1896 arg_type_mismatch(expected, actual, i + 1);
1897 return FAIL;
1898 }
1899 }
1900 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001901 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001902 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001903 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001904 else
1905 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001906 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001907 return FAIL;
1908 }
1909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1911 return FAIL;
1912 isn->isn_arg.pfunc.cpf_top = at_top;
1913 isn->isn_arg.pfunc.cpf_argcount = argcount;
1914
1915 stack->ga_len -= argcount; // drop the arguments
1916
1917 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001918 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001920 // If partial is above the arguments it must be cleared and replaced with
1921 // the return value.
1922 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1923 return FAIL;
1924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 return OK;
1926}
1927
1928/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001929 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 */
1931 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001932generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933{
1934 isn_T *isn;
1935 garray_T *stack = &cctx->ctx_type_stack;
1936 type_T *type;
1937
Bram Moolenaar080457c2020-03-03 21:53:32 +01001938 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001939 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001941 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001943 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001945 if (type->tt_type != VAR_DICT && type != &t_any)
1946 {
1947 emsg(_(e_dictreq));
1948 return FAIL;
1949 }
1950 // change dict type to dict member type
1951 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001952 {
1953 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1954 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956
1957 return OK;
1958}
1959
1960/*
1961 * Generate an ISN_ECHO instruction.
1962 */
1963 static int
1964generate_ECHO(cctx_T *cctx, int with_white, int count)
1965{
1966 isn_T *isn;
1967
Bram Moolenaar080457c2020-03-03 21:53:32 +01001968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1970 return FAIL;
1971 isn->isn_arg.echo.echo_with_white = with_white;
1972 isn->isn_arg.echo.echo_count = count;
1973
1974 return OK;
1975}
1976
Bram Moolenaarad39c092020-02-26 18:23:43 +01001977/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001978 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001979 */
1980 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001981generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001982{
1983 isn_T *isn;
1984
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001985 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001986 return FAIL;
1987 isn->isn_arg.number = count;
1988
1989 return OK;
1990}
1991
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001992/*
1993 * Generate an ISN_PUT instruction.
1994 */
1995 static int
1996generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1997{
1998 isn_T *isn;
1999
2000 RETURN_OK_IF_SKIP(cctx);
2001 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2002 return FAIL;
2003 isn->isn_arg.put.put_regname = regname;
2004 isn->isn_arg.put.put_lnum = lnum;
2005 return OK;
2006}
2007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 static int
2009generate_EXEC(cctx_T *cctx, char_u *line)
2010{
2011 isn_T *isn;
2012
Bram Moolenaar080457c2020-03-03 21:53:32 +01002013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2015 return FAIL;
2016 isn->isn_arg.string = vim_strsave(line);
2017 return OK;
2018}
2019
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002020 static int
2021generate_EXECCONCAT(cctx_T *cctx, int count)
2022{
2023 isn_T *isn;
2024
2025 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2026 return FAIL;
2027 isn->isn_arg.number = count;
2028 return OK;
2029}
2030
Bram Moolenaar08597872020-12-10 19:43:40 +01002031/*
2032 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2033 */
2034 static int
2035generate_RANGE(cctx_T *cctx, char_u *range)
2036{
2037 isn_T *isn;
2038 garray_T *stack = &cctx->ctx_type_stack;
2039
2040 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2041 return FAIL;
2042 isn->isn_arg.string = range;
2043
2044 if (ga_grow(stack, 1) == FAIL)
2045 return FAIL;
2046 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2047 ++stack->ga_len;
2048 return OK;
2049}
2050
Bram Moolenaar792f7862020-11-23 08:31:18 +01002051 static int
2052generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2053{
2054 isn_T *isn;
2055
2056 RETURN_OK_IF_SKIP(cctx);
2057 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2058 return FAIL;
2059 isn->isn_arg.unpack.unp_count = var_count;
2060 isn->isn_arg.unpack.unp_semicolon = semicolon;
2061 return OK;
2062}
2063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002065 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002066 */
2067 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002068generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002069{
2070 isn_T *isn;
2071
Bram Moolenaar02194d22020-10-24 23:08:38 +02002072 if (cmod->cmod_flags != 0
2073 || cmod->cmod_split != 0
2074 || cmod->cmod_verbose != 0
2075 || cmod->cmod_tab != 0
2076 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002077 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002078 cctx->ctx_has_cmdmod = TRUE;
2079
2080 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002081 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002082 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2083 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2084 return FAIL;
2085 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002086 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002087 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002088 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002089
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002090 return OK;
2091}
2092
2093 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002094generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002095{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002096 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2097 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002098 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002099 return OK;
2100}
2101
Bram Moolenaarf002a412021-01-24 13:34:18 +01002102#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002103 static void
2104may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2105{
2106 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002107 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002108}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002109#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002110
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002113 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002115 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002116reserve_local(
2117 cctx_T *cctx,
2118 char_u *name,
2119 size_t len,
2120 int isConst,
2121 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 lvar_T *lvar;
2124
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002125 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002127 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002128 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 }
2130
2131 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002132 return NULL;
2133 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002134 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002136 // Every local variable uses the next entry on the stack. We could re-use
2137 // the last ones when leaving a scope, but then variables used in a closure
2138 // might get overwritten. To keep things simple do not re-use stack
2139 // entries. This is less efficient, but memory is cheap these days.
2140 lvar->lv_idx = cctx->ctx_locals_count++;
2141
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002142 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 lvar->lv_const = isConst;
2144 lvar->lv_type = type;
2145
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002146 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147}
2148
2149/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002150 * Remove local variables above "new_top".
2151 */
2152 static void
2153unwind_locals(cctx_T *cctx, int new_top)
2154{
2155 if (cctx->ctx_locals.ga_len > new_top)
2156 {
2157 int idx;
2158 lvar_T *lvar;
2159
2160 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2161 {
2162 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2163 vim_free(lvar->lv_name);
2164 }
2165 }
2166 cctx->ctx_locals.ga_len = new_top;
2167}
2168
2169/*
2170 * Free all local variables.
2171 */
2172 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002173free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002174{
2175 unwind_locals(cctx, 0);
2176 ga_clear(&cctx->ctx_locals);
2177}
2178
2179/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002180 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2181 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2182 * error if the variable was defined with :const.
2183 */
2184 static int
2185check_item_writable(svar_T *sv, int check_writable, char_u *name)
2186{
2187 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2188 || (check_writable == ASSIGN_FINAL
2189 && sv->sv_const == ASSIGN_CONST))
2190 {
2191 semsg(_(e_readonlyvar), name);
2192 return FAIL;
2193 }
2194 return OK;
2195}
2196
2197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002199 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 * Returns the index in "sn_var_vals" if found.
2201 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002202 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 */
2204 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002205get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206{
2207 hashtab_T *ht;
2208 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002209 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002210 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 int idx;
2212
Bram Moolenaare3d46852020-08-29 13:39:17 +02002213 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002215 if (sid == current_sctx.sc_sid)
2216 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002217 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002218
2219 if (sav == NULL)
2220 return -2;
2221 idx = sav->sav_var_vals_idx;
2222 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002223 if (check_item_writable(sv, check_writable, name) == FAIL)
2224 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002225 return idx;
2226 }
2227
2228 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 ht = &SCRIPT_VARS(sid);
2230 di = find_var_in_ht(ht, 0, name, TRUE);
2231 if (di == NULL)
2232 return -2;
2233
2234 // Now find the svar_T index in sn_var_vals.
2235 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2236 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002237 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 if (sv->sv_tv == &di->di_tv)
2239 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002240 if (check_item_writable(sv, check_writable, name) == FAIL)
2241 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 return idx;
2243 }
2244 }
2245 return -1;
2246}
2247
2248/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002249 * Find "name" in imported items of the current script or in "cctx" if not
2250 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 */
2252 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002253find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 int idx;
2256
Bram Moolenaare3d46852020-08-29 13:39:17 +02002257 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002258 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 if (cctx != NULL)
2260 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2261 {
2262 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2263 + idx;
2264
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002265 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2266 : STRLEN(import->imp_name) == len
2267 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 return import;
2269 }
2270
Bram Moolenaarefa94442020-08-08 22:16:00 +02002271 return find_imported_in_script(name, len, current_sctx.sc_sid);
2272}
2273
2274 imported_T *
2275find_imported_in_script(char_u *name, size_t len, int sid)
2276{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002277 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002278 int idx;
2279
Bram Moolenaare3d46852020-08-29 13:39:17 +02002280 if (!SCRIPT_ID_VALID(sid))
2281 return NULL;
2282 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2284 {
2285 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2286
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002287 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2288 : STRLEN(import->imp_name) == len
2289 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 return import;
2291 }
2292 return NULL;
2293}
2294
2295/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002296 * Free all imported variables.
2297 */
2298 static void
2299free_imported(cctx_T *cctx)
2300{
2301 int idx;
2302
2303 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2304 {
2305 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2306
2307 vim_free(import->imp_name);
2308 }
2309 ga_clear(&cctx->ctx_imports);
2310}
2311
2312/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002313 * Return a pointer to the next line that isn't empty or only contains a
2314 * comment. Skips over white space.
2315 * Returns NULL if there is none.
2316 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002317 char_u *
2318peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002319{
2320 int lnum = cctx->ctx_lnum;
2321
2322 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2323 {
2324 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002325 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002326
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002327 // ignore NULLs inserted for continuation lines
2328 if (line != NULL)
2329 {
2330 p = skipwhite(line);
2331 if (*p != NUL && !vim9_comment_start(p))
2332 return p;
2333 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002334 }
2335 return NULL;
2336}
2337
2338/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002339 * Called when checking for a following operator at "arg". When the rest of
2340 * the line is empty or only a comment, peek the next line. If there is a next
2341 * line return a pointer to it and set "nextp".
2342 * Otherwise skip over white space.
2343 */
2344 static char_u *
2345may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2346{
2347 char_u *p = skipwhite(arg);
2348
2349 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002350 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002351 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002352 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002353 if (*nextp != NULL)
2354 return *nextp;
2355 }
2356 return p;
2357}
2358
2359/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002360 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002361 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002362 * Returns NULL when at the end.
2363 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002364 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002365next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002366{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002367 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002368
2369 do
2370 {
2371 ++cctx->ctx_lnum;
2372 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002373 {
2374 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002375 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002376 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002377 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002378 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002379 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002380 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002381 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002382 return line;
2383}
2384
2385/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002386 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002387 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002388 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002389 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2390 */
2391 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002392may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002393{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002394 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002395 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002396 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002397 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002398
2399 if (next == NULL)
2400 return FAIL;
2401 *arg = skipwhite(next);
2402 }
2403 return OK;
2404}
2405
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002406/*
2407 * Idem, and give an error when failed.
2408 */
2409 static int
2410may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2411{
2412 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2413 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002414 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002415 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002416 return FAIL;
2417 }
2418 return OK;
2419}
2420
2421
Bram Moolenaara5565e42020-05-09 15:44:01 +02002422// Structure passed between the compile_expr* functions to keep track of
2423// constants that have been parsed but for which no code was produced yet. If
2424// possible expressions on these constants are applied at compile time. If
2425// that is not possible, the code to push the constants needs to be generated
2426// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002427// Using 50 should be more than enough of 5 levels of ().
2428#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002429typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002430 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002432 int pp_is_const; // all generated code was constants, used for a
2433 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002434} ppconst_T;
2435
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002436static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002437static int compile_expr0(char_u **arg, cctx_T *cctx);
2438static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2439
Bram Moolenaara5565e42020-05-09 15:44:01 +02002440/*
2441 * Generate a PUSH instruction for "tv".
2442 * "tv" will be consumed or cleared.
2443 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2444 */
2445 static int
2446generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2447{
2448 if (tv != NULL)
2449 {
2450 switch (tv->v_type)
2451 {
2452 case VAR_UNKNOWN:
2453 break;
2454 case VAR_BOOL:
2455 generate_PUSHBOOL(cctx, tv->vval.v_number);
2456 break;
2457 case VAR_SPECIAL:
2458 generate_PUSHSPEC(cctx, tv->vval.v_number);
2459 break;
2460 case VAR_NUMBER:
2461 generate_PUSHNR(cctx, tv->vval.v_number);
2462 break;
2463#ifdef FEAT_FLOAT
2464 case VAR_FLOAT:
2465 generate_PUSHF(cctx, tv->vval.v_float);
2466 break;
2467#endif
2468 case VAR_BLOB:
2469 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2470 tv->vval.v_blob = NULL;
2471 break;
2472 case VAR_STRING:
2473 generate_PUSHS(cctx, tv->vval.v_string);
2474 tv->vval.v_string = NULL;
2475 break;
2476 default:
2477 iemsg("constant type not supported");
2478 clear_tv(tv);
2479 return FAIL;
2480 }
2481 tv->v_type = VAR_UNKNOWN;
2482 }
2483 return OK;
2484}
2485
2486/*
2487 * Generate code for any ppconst entries.
2488 */
2489 static int
2490generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2491{
2492 int i;
2493 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002494 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002495
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002496 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497 for (i = 0; i < ppconst->pp_used; ++i)
2498 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2499 ret = FAIL;
2500 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002501 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002502 return ret;
2503}
2504
2505/*
2506 * Clear ppconst constants. Used when failing.
2507 */
2508 static void
2509clear_ppconst(ppconst_T *ppconst)
2510{
2511 int i;
2512
2513 for (i = 0; i < ppconst->pp_used; ++i)
2514 clear_tv(&ppconst->pp_tv[i]);
2515 ppconst->pp_used = 0;
2516}
2517
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002518/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002519 * Generate an instruction to load script-local variable "name", without the
2520 * leading "s:".
2521 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 */
2523 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002524compile_load_scriptvar(
2525 cctx_T *cctx,
2526 char_u *name, // variable NUL terminated
2527 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002528 char_u **end, // end of variable
2529 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002531 scriptitem_T *si;
2532 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 imported_T *import;
2534
Bram Moolenaare3d46852020-08-29 13:39:17 +02002535 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2536 return FAIL;
2537 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002538 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002539 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002541 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002542 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2543 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
2545 if (idx >= 0)
2546 {
2547 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2548
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002549 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 current_sctx.sc_sid, idx, sv->sv_type);
2551 return OK;
2552 }
2553
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002554 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 if (import != NULL)
2556 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002557 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002558 {
2559 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002560 char_u *exp_name;
2561 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002562 ufunc_T *ufunc;
2563 type_T *type;
2564
2565 // Used "import * as Name", need to lookup the member.
2566 if (*p != '.')
2567 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002568 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002569 return FAIL;
2570 }
2571 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002572 if (VIM_ISWHITE(*p))
2573 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002574 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002575 return FAIL;
2576 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002577
Bram Moolenaar1c991142020-07-04 13:15:31 +02002578 // isolate one name
2579 exp_name = p;
2580 while (eval_isnamec(*p))
2581 ++p;
2582 cc = *p;
2583 *p = NUL;
2584
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002585 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002586 *p = cc;
2587 p = skipwhite(p);
2588
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002589 // TODO: what if it is a function?
2590 if (idx < 0)
2591 return FAIL;
2592 *end = p;
2593
2594 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2595 import->imp_sid,
2596 idx,
2597 type);
2598 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002599 else if (import->imp_funcname != NULL)
2600 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002601 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002602 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2603 import->imp_sid,
2604 import->imp_var_vals_idx,
2605 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 return OK;
2607 }
2608
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002609 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002610 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return FAIL;
2612}
2613
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002614 static int
2615generate_funcref(cctx_T *cctx, char_u *name)
2616{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002617 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002618
2619 if (ufunc == NULL)
2620 return FAIL;
2621
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002622 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002623 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2624 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002625 == FAIL)
2626 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002627 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002628}
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630/*
2631 * Compile a variable name into a load instruction.
2632 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002633 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 * When "error" is FALSE do not give an error when not found.
2635 */
2636 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002637compile_load(
2638 char_u **arg,
2639 char_u *end_arg,
2640 cctx_T *cctx,
2641 int is_expr,
2642 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643{
2644 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002645 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002646 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002648 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 if (*(*arg + 1) == ':')
2651 {
2652 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002653 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002654 {
2655 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002657 switch (**arg)
2658 {
2659 case 'g': isn_type = ISN_LOADGDICT; break;
2660 case 'w': isn_type = ISN_LOADWDICT; break;
2661 case 't': isn_type = ISN_LOADTDICT; break;
2662 case 'b': isn_type = ISN_LOADBDICT; break;
2663 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002664 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002665 goto theend;
2666 }
2667 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2668 goto theend;
2669 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 else
2672 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002673 isntype_T isn_type = ISN_DROP;
2674
2675 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2676 if (name == NULL)
2677 return FAIL;
2678
2679 switch (**arg)
2680 {
2681 case 'v': res = generate_LOADV(cctx, name, error);
2682 break;
2683 case 's': res = compile_load_scriptvar(cctx, name,
2684 NULL, NULL, error);
2685 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002686 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2687 isn_type = ISN_LOADG;
2688 else
2689 {
2690 isn_type = ISN_LOADAUTO;
2691 vim_free(name);
2692 name = vim_strnsave(*arg, end - *arg);
2693 if (name == NULL)
2694 return FAIL;
2695 }
2696 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002697 case 'w': isn_type = ISN_LOADW; break;
2698 case 't': isn_type = ISN_LOADT; break;
2699 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002700 default: // cannot happen, just in case
2701 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002702 goto theend;
2703 }
2704 if (isn_type != ISN_DROP)
2705 {
2706 // Global, Buffer-local, Window-local and Tabpage-local
2707 // variables can be defined later, thus we don't check if it
2708 // exists, give error at runtime.
2709 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
2712 }
2713 else
2714 {
2715 size_t len = end - *arg;
2716 int idx;
2717 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002718 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719
2720 name = vim_strnsave(*arg, end - *arg);
2721 if (name == NULL)
2722 return FAIL;
2723
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002724 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002726 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002727 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 else
2730 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002731 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002732
Bram Moolenaar709664c2020-12-12 14:33:41 +01002733 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002735 type = lvar.lv_type;
2736 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002737 if (lvar.lv_from_outer != 0)
2738 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002739 else
2740 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 }
2742 else
2743 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002745 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002746 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002747 || find_imported(name, 0, cctx) != NULL)
2748 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002749
Bram Moolenaar0f769812020-09-12 18:32:34 +02002750 // When evaluating an expression and the name starts with an
2751 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002752 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002753 if (res == FAIL && is_expr
2754 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002755 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 }
2757 }
2758 if (gen_load)
2759 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002760 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002761 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002762 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002763 cctx->ctx_outer_used = TRUE;
2764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 }
2766
2767 *arg = end;
2768
2769theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002770 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002771 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 vim_free(name);
2773 return res;
2774}
2775
2776/*
2777 * Compile the argument expressions.
2778 * "arg" points to just after the "(" and is advanced to after the ")"
2779 */
2780 static int
2781compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2782{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783 char_u *p = *arg;
2784 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002785 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
Bram Moolenaare6085c52020-04-12 20:19:16 +02002787 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002789 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2790 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002791 if (*p == ')')
2792 {
2793 *arg = p + 1;
2794 return OK;
2795 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002796 if (must_end)
2797 {
2798 semsg(_(e_missing_comma_before_argument_str), p);
2799 return FAIL;
2800 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002801
Bram Moolenaara5565e42020-05-09 15:44:01 +02002802 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 return FAIL;
2804 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002805
2806 if (*p != ',' && *skipwhite(p) == ',')
2807 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002808 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002809 p = skipwhite(p);
2810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002812 {
2813 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002814 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002815 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002816 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002817 else
2818 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002819 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002820 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002822failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002823 emsg(_(e_missing_close));
2824 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825}
2826
2827/*
2828 * Compile a function call: name(arg1, arg2)
2829 * "arg" points to "name", "arg + varlen" to the "(".
2830 * "argcount_init" is 1 for "value->method()"
2831 * Instructions:
2832 * EVAL arg1
2833 * EVAL arg2
2834 * BCALL / DCALL / UCALL
2835 */
2836 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002837compile_call(
2838 char_u **arg,
2839 size_t varlen,
2840 cctx_T *cctx,
2841 ppconst_T *ppconst,
2842 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843{
2844 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002845 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 int argcount = argcount_init;
2847 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002848 char_u fname_buf[FLEN_FIXED + 1];
2849 char_u *tofree = NULL;
2850 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002851 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002852 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002853 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854
Bram Moolenaara5565e42020-05-09 15:44:01 +02002855 // we can evaluate "has('name')" at compile time
2856 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2857 {
2858 char_u *s = skipwhite(*arg + varlen + 1);
2859 typval_T argvars[2];
2860
2861 argvars[0].v_type = VAR_UNKNOWN;
2862 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002863 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002864 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002865 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002867 if (*s == ')' && argvars[0].v_type == VAR_STRING
2868 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002869 {
2870 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2871
2872 *arg = s + 1;
2873 argvars[1].v_type = VAR_UNKNOWN;
2874 tv->v_type = VAR_NUMBER;
2875 tv->vval.v_number = 0;
2876 f_has(argvars, tv);
2877 clear_tv(&argvars[0]);
2878 ++ppconst->pp_used;
2879 return OK;
2880 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002881 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002882 }
2883
2884 if (generate_ppconst(cctx, ppconst) == FAIL)
2885 return FAIL;
2886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (varlen >= sizeof(namebuf))
2888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002889 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 return FAIL;
2891 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002892 vim_strncpy(namebuf, *arg, varlen);
2893 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
2895 *arg = skipwhite(*arg + varlen + 1);
2896 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002897 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
Bram Moolenaar03290b82020-12-19 16:30:44 +01002899 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002900 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 {
2902 int idx;
2903
2904 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002905 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002907 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002908 if (STRCMP(name, "flatten") == 0)
2909 {
2910 emsg(_(e_cannot_use_flatten_in_vim9_script));
2911 goto theend;
2912 }
2913
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002914 if (STRCMP(name, "add") == 0 && argcount == 2)
2915 {
2916 garray_T *stack = &cctx->ctx_type_stack;
2917 type_T *type = ((type_T **)stack->ga_data)[
2918 stack->ga_len - 2];
2919
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002920 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002921 if (type->tt_type == VAR_LIST)
2922 {
2923 // inline "add(list, item)" so that the type can be checked
2924 res = generate_LISTAPPEND(cctx);
2925 idx = -1;
2926 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002927 else if (type->tt_type == VAR_BLOB)
2928 {
2929 // inline "add(blob, nr)" so that the type can be checked
2930 res = generate_BLOBAPPEND(cctx);
2931 idx = -1;
2932 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002933 }
2934
2935 if (idx >= 0)
2936 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2937 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002938 else
2939 semsg(_(e_unknownfunc), namebuf);
2940 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
2942
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002943 // An argument or local variable can be a function reference, this
2944 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002945 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002946 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002947 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002948 // If we can find the function by name generate the right call.
2949 // Skip global functions here, a local funcref takes precedence.
2950 ufunc = find_func(name, FALSE, cctx);
2951 if (ufunc != NULL && !func_is_global(ufunc))
2952 {
2953 res = generate_CALL(cctx, ufunc, argcount);
2954 goto theend;
2955 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002956 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957
2958 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002959 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002960 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002962 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002963 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002964 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002965 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002966 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002967
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002968 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002969 goto theend;
2970 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971
Bram Moolenaar0f769812020-09-12 18:32:34 +02002972 // If we can find a global function by name generate the right call.
2973 if (ufunc != NULL)
2974 {
2975 res = generate_CALL(cctx, ufunc, argcount);
2976 goto theend;
2977 }
2978
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002979 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002980 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002981 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002982 res = generate_UCALL(cctx, name, argcount);
2983 else
2984 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002985
2986theend:
2987 vim_free(tofree);
2988 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989}
2990
2991// like NAMESPACE_CHAR but with 'a' and 'l'.
2992#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2993
2994/*
2995 * Find the end of a variable or function name. Unlike find_name_end() this
2996 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002997 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 * Return a pointer to just after the name. Equal to "arg" if there is no
2999 * valid name.
3000 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003001 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003002to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003{
3004 char_u *p;
3005
3006 // Quick check for valid starting character.
3007 if (!eval_isnamec1(*arg))
3008 return arg;
3009
3010 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3011 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3012 // and can be used in slice "[n:]".
3013 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003014 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3016 break;
3017 return p;
3018}
3019
3020/*
3021 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003022 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 */
3024 char_u *
3025to_name_const_end(char_u *arg)
3026{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003027 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 typval_T rettv;
3029
3030 if (p == arg && *arg == '[')
3031 {
3032
3033 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003034 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 p = arg;
3036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 return p;
3038}
3039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040/*
3041 * parse a list: [expr, expr]
3042 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003043 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 */
3045 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003046compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047{
3048 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003049 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003051 int is_const;
3052 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003054 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003056 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003057 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003058 semsg(_(e_list_end), *arg);
3059 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003061 if (*p == ',')
3062 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003063 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003064 return FAIL;
3065 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003066 if (*p == ']')
3067 {
3068 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003069 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003070 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003071 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003072 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003073 if (!is_const)
3074 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 ++count;
3076 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003077 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003079 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3080 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003081 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003082 return FAIL;
3083 }
3084 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003085 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 p = skipwhite(p);
3087 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003088 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003090 ppconst->pp_is_const = is_all_const;
3091 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092}
3093
3094/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003095 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003097 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 */
3099 static int
3100compile_lambda(char_u **arg, cctx_T *cctx)
3101{
Bram Moolenaare462f522020-12-27 14:43:30 +01003102 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 typval_T rettv;
3104 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003105 evalarg_T evalarg;
3106
3107 CLEAR_FIELD(evalarg);
3108 evalarg.eval_flags = EVAL_EVALUATE;
3109 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110
3111 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003112 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3113 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003114 {
3115 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003116 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003117 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003118
Bram Moolenaar65c44152020-12-24 15:14:01 +01003119 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003121 ++ufunc->uf_refcount;
3122 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
Bram Moolenaar65c44152020-12-24 15:14:01 +01003124 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003125 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003127 clear_evalarg(&evalarg, NULL);
3128
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003129 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003130 {
3131 // The return type will now be known.
3132 set_function_type(ufunc);
3133
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003134 // The function reference count will be 1. When the ISN_FUNCREF
3135 // instruction is deleted the reference count is decremented and the
3136 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003137 return generate_FUNCREF(cctx, ufunc);
3138 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003139
3140 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 return FAIL;
3142}
3143
3144/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003145 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003147 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 */
3149 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003150compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151{
3152 garray_T *instr = &cctx->ctx_instr;
3153 int count = 0;
3154 dict_T *d = dict_alloc();
3155 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003156 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003157 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003158 int is_const;
3159 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160
3161 if (d == NULL)
3162 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003163 if (generate_ppconst(cctx, ppconst) == FAIL)
3164 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003167 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168
Bram Moolenaar23c55272020-06-21 16:58:13 +02003169 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003170 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003171 *arg = NULL;
3172 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003173 }
3174
3175 if (**arg == '}')
3176 break;
3177
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003178 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003180 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003182 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003183 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003184 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003187 if (isn->isn_type == ISN_PUSHNR)
3188 {
3189 char buf[NUMBUFLEN];
3190
3191 // Convert to string at compile time.
3192 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3193 isn->isn_type = ISN_PUSHS;
3194 isn->isn_arg.string = vim_strsave((char_u *)buf);
3195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 if (isn->isn_type == ISN_PUSHS)
3197 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003198 else if (may_generate_2STRING(-1, cctx) == FAIL)
3199 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003200 *arg = skipwhite(*arg);
3201 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003202 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003203 emsg(_(e_missing_matching_bracket_after_dict_key));
3204 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003205 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003206 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003208 else
3209 {
3210 // {"name": value},
3211 // {'name': value},
3212 // {name: value} use "name" as a literal key
3213 key = get_literal_key(arg);
3214 if (key == NULL)
3215 return FAIL;
3216 if (generate_PUSHS(cctx, key) == FAIL)
3217 return FAIL;
3218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219
3220 // Check for duplicate keys, if using string keys.
3221 if (key != NULL)
3222 {
3223 item = dict_find(d, key, -1);
3224 if (item != NULL)
3225 {
3226 semsg(_(e_duplicate_key), key);
3227 goto failret;
3228 }
3229 item = dictitem_alloc(key);
3230 if (item != NULL)
3231 {
3232 item->di_tv.v_type = VAR_UNKNOWN;
3233 item->di_tv.v_lock = 0;
3234 if (dict_add(d, item) == FAIL)
3235 dictitem_free(item);
3236 }
3237 }
3238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 if (**arg != ':')
3240 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003241 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003242 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003243 else
3244 semsg(_(e_missing_dict_colon), *arg);
3245 return FAIL;
3246 }
3247 whitep = *arg + 1;
3248 if (!IS_WHITE_OR_NUL(*whitep))
3249 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003250 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 return FAIL;
3252 }
3253
Bram Moolenaar23c55272020-06-21 16:58:13 +02003254 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003255 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003256 *arg = NULL;
3257 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003258 }
3259
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003260 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003262 if (!is_const)
3263 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 ++count;
3265
Bram Moolenaar2c330432020-04-13 14:41:35 +02003266 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003267 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003268 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003269 *arg = NULL;
3270 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 if (**arg == '}')
3273 break;
3274 if (**arg != ',')
3275 {
3276 semsg(_(e_missing_dict_comma), *arg);
3277 goto failret;
3278 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003279 if (IS_WHITE_OR_NUL(*whitep))
3280 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003281 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003282 return FAIL;
3283 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003284 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003285 if (!IS_WHITE_OR_NUL(*whitep))
3286 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003287 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003288 return FAIL;
3289 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003290 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 }
3292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 *arg = *arg + 1;
3294
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003295 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003296 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003297 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003298 *arg += STRLEN(*arg);
3299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003301 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 return generate_NEWDICT(cctx, count);
3303
3304failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003305 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003306 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003307 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003308 *arg = (char_u *)"";
3309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 dict_unref(d);
3311 return FAIL;
3312}
3313
3314/*
3315 * Compile "&option".
3316 */
3317 static int
3318compile_get_option(char_u **arg, cctx_T *cctx)
3319{
3320 typval_T rettv;
3321 char_u *start = *arg;
3322 int ret;
3323
3324 // parse the option and get the current value to get the type.
3325 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003326 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 if (ret == OK)
3328 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003329 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003330 char_u *name = vim_strnsave(start, *arg - start);
3331 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3332 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333
3334 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3335 vim_free(name);
3336 }
3337 clear_tv(&rettv);
3338
3339 return ret;
3340}
3341
3342/*
3343 * Compile "$VAR".
3344 */
3345 static int
3346compile_get_env(char_u **arg, cctx_T *cctx)
3347{
3348 char_u *start = *arg;
3349 int len;
3350 int ret;
3351 char_u *name;
3352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 ++*arg;
3354 len = get_env_len(arg);
3355 if (len == 0)
3356 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003357 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 return FAIL;
3359 }
3360
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003361 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 name = vim_strnsave(start, len + 1);
3363 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3364 vim_free(name);
3365 return ret;
3366}
3367
3368/*
3369 * Compile "@r".
3370 */
3371 static int
3372compile_get_register(char_u **arg, cctx_T *cctx)
3373{
3374 int ret;
3375
3376 ++*arg;
3377 if (**arg == NUL)
3378 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003379 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 return FAIL;
3381 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003382 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 {
3384 emsg_invreg(**arg);
3385 return FAIL;
3386 }
3387 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3388 ++*arg;
3389 return ret;
3390}
3391
3392/*
3393 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003394 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 */
3396 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003397apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003399 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400
3401 // this works from end to start
3402 while (p > start)
3403 {
3404 --p;
3405 if (*p == '-' || *p == '+')
3406 {
3407 // only '-' has an effect, for '+' we only check the type
3408#ifdef FEAT_FLOAT
3409 if (rettv->v_type == VAR_FLOAT)
3410 {
3411 if (*p == '-')
3412 rettv->vval.v_float = -rettv->vval.v_float;
3413 }
3414 else
3415#endif
3416 {
3417 varnumber_T val;
3418 int error = FALSE;
3419
3420 // tv_get_number_chk() accepts a string, but we don't want that
3421 // here
3422 if (check_not_string(rettv) == FAIL)
3423 return FAIL;
3424 val = tv_get_number_chk(rettv, &error);
3425 clear_tv(rettv);
3426 if (error)
3427 return FAIL;
3428 if (*p == '-')
3429 val = -val;
3430 rettv->v_type = VAR_NUMBER;
3431 rettv->vval.v_number = val;
3432 }
3433 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003434 else if (numeric_only)
3435 {
3436 ++p;
3437 break;
3438 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003439 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
3441 int v = tv2bool(rettv);
3442
3443 // '!' is permissive in the type.
3444 clear_tv(rettv);
3445 rettv->v_type = VAR_BOOL;
3446 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3447 }
3448 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003449 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 return OK;
3451}
3452
3453/*
3454 * Recognize v: variables that are constants and set "rettv".
3455 */
3456 static void
3457get_vim_constant(char_u **arg, typval_T *rettv)
3458{
3459 if (STRNCMP(*arg, "v:true", 6) == 0)
3460 {
3461 rettv->v_type = VAR_BOOL;
3462 rettv->vval.v_number = VVAL_TRUE;
3463 *arg += 6;
3464 }
3465 else if (STRNCMP(*arg, "v:false", 7) == 0)
3466 {
3467 rettv->v_type = VAR_BOOL;
3468 rettv->vval.v_number = VVAL_FALSE;
3469 *arg += 7;
3470 }
3471 else if (STRNCMP(*arg, "v:null", 6) == 0)
3472 {
3473 rettv->v_type = VAR_SPECIAL;
3474 rettv->vval.v_number = VVAL_NULL;
3475 *arg += 6;
3476 }
3477 else if (STRNCMP(*arg, "v:none", 6) == 0)
3478 {
3479 rettv->v_type = VAR_SPECIAL;
3480 rettv->vval.v_number = VVAL_NONE;
3481 *arg += 6;
3482 }
3483}
3484
Bram Moolenaar657137c2021-01-09 15:45:23 +01003485 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003486get_compare_type(char_u *p, int *len, int *type_is)
3487{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003488 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003489 int i;
3490
3491 switch (p[0])
3492 {
3493 case '=': if (p[1] == '=')
3494 type = EXPR_EQUAL;
3495 else if (p[1] == '~')
3496 type = EXPR_MATCH;
3497 break;
3498 case '!': if (p[1] == '=')
3499 type = EXPR_NEQUAL;
3500 else if (p[1] == '~')
3501 type = EXPR_NOMATCH;
3502 break;
3503 case '>': if (p[1] != '=')
3504 {
3505 type = EXPR_GREATER;
3506 *len = 1;
3507 }
3508 else
3509 type = EXPR_GEQUAL;
3510 break;
3511 case '<': if (p[1] != '=')
3512 {
3513 type = EXPR_SMALLER;
3514 *len = 1;
3515 }
3516 else
3517 type = EXPR_SEQUAL;
3518 break;
3519 case 'i': if (p[1] == 's')
3520 {
3521 // "is" and "isnot"; but not a prefix of a name
3522 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3523 *len = 5;
3524 i = p[*len];
3525 if (!isalnum(i) && i != '_')
3526 {
3527 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3528 *type_is = TRUE;
3529 }
3530 }
3531 break;
3532 }
3533 return type;
3534}
3535
3536/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003537 * Skip over an expression, ignoring most errors.
3538 */
3539 static void
3540skip_expr_cctx(char_u **arg, cctx_T *cctx)
3541{
3542 evalarg_T evalarg;
3543
3544 CLEAR_FIELD(evalarg);
3545 evalarg.eval_cctx = cctx;
3546 skip_expr(arg, &evalarg);
3547}
3548
3549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003551 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 */
3553 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003554compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003556 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557
3558 // this works from end to start
3559 while (p > start)
3560 {
3561 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003562 while (VIM_ISWHITE(*p))
3563 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 if (*p == '-' || *p == '+')
3565 {
3566 int negate = *p == '-';
3567 isn_T *isn;
3568
3569 // TODO: check type
3570 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3571 {
3572 --p;
3573 if (*p == '-')
3574 negate = !negate;
3575 }
3576 // only '-' has an effect, for '+' we only check the type
3577 if (negate)
3578 isn = generate_instr(cctx, ISN_NEGATENR);
3579 else
3580 isn = generate_instr(cctx, ISN_CHECKNR);
3581 if (isn == NULL)
3582 return FAIL;
3583 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003584 else if (numeric_only)
3585 {
3586 ++p;
3587 break;
3588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 else
3590 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003591 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003593 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003595 if (p[-1] == '!')
3596 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 }
3599 if (generate_2BOOL(cctx, invert) == FAIL)
3600 return FAIL;
3601 }
3602 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003603 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 return OK;
3605}
3606
3607/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003608 * Compile "(expression)": recursive!
3609 * Return FAIL/OK.
3610 */
3611 static int
3612compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3613{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003614 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003615 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003616
Bram Moolenaar24156692021-01-14 20:35:49 +01003617 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3618 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003619 if (ppconst->pp_used <= PPSIZE - 10)
3620 {
3621 ret = compile_expr1(arg, cctx, ppconst);
3622 }
3623 else
3624 {
3625 // Not enough space in ppconst, flush constants.
3626 if (generate_ppconst(cctx, ppconst) == FAIL)
3627 return FAIL;
3628 ret = compile_expr0(arg, cctx);
3629 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003630 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3631 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003632 if (**arg == ')')
3633 ++*arg;
3634 else if (ret == OK)
3635 {
3636 emsg(_(e_missing_close));
3637 ret = FAIL;
3638 }
3639 return ret;
3640}
3641
3642/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003644 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 */
3646 static int
3647compile_subscript(
3648 char_u **arg,
3649 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003650 char_u *start_leader,
3651 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003652 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003654 char_u *name_start = *end_leader;
3655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 for (;;)
3657 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003658 char_u *p = skipwhite(*arg);
3659
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003660 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003661 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003662 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003663
3664 // If a following line starts with "->{" or "->X" advance to that
3665 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003666 // Also if a following line starts with ".x".
3667 if (next != NULL &&
3668 ((next[0] == '-' && next[1] == '>'
3669 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003670 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003671 {
3672 next = next_line_from_context(cctx, TRUE);
3673 if (next == NULL)
3674 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003675 *arg = next;
3676 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003677 }
3678 }
3679
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003680 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003681 // not a function call.
3682 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003684 garray_T *stack = &cctx->ctx_type_stack;
3685 type_T *type;
3686 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003688 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003689 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003690 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003693 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3694
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003695 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3697 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003698 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 return FAIL;
3700 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003701 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003703 char_u *pstart = p;
3704
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003705 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003706 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003707 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 // something->method()
3710 // Apply the '!', '-' and '+' first:
3711 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003712 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003715 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003716 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003717 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003718 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003719 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003720 int argcount = 1;
3721 char_u *expr;
3722 garray_T *stack;
3723 type_T *type;
3724
3725 // Funcref call: list->(Refs[2])(arg)
3726 // or lambda: list->((arg) => expr)(arg)
3727 // Fist compile the arguments.
3728 expr = *arg;
3729 *arg = skipwhite(*arg + 1);
3730 skip_expr_cctx(arg, cctx);
3731 *arg = skipwhite(*arg);
3732 if (**arg != ')')
3733 {
3734 semsg(_(e_missing_paren), *arg);
3735 return FAIL;
3736 }
3737 ++*arg;
3738 if (**arg != '(')
3739 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003740 if (*skipwhite(*arg) == '(')
3741 emsg(_(e_nowhitespace));
3742 else
3743 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003744 return FAIL;
3745 }
3746
3747 *arg = skipwhite(*arg + 1);
3748 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3749 return FAIL;
3750
3751 // Compile the function expression.
3752 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3753 return FAIL;
3754
3755 stack = &cctx->ctx_type_stack;
3756 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3757 if (generate_PCALL(cctx, argcount,
3758 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 return FAIL;
3760 }
3761 else
3762 {
3763 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003764 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003765 if (!eval_isnamec1(*p))
3766 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003767 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003768 return FAIL;
3769 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003770 if (ASCII_ISALPHA(*p) && p[1] == ':')
3771 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003772 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 ;
3774 if (*p != '(')
3775 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003776 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 return FAIL;
3778 }
3779 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003780 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 return FAIL;
3782 }
3783 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003784 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003786 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003787 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003788 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003789 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003790 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003791
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003792 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003793 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003794 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003795 // TODO: blob index
3796 // TODO: more arguments
3797 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003798 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003799 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003800 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003801
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003802 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003803 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003804 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003805 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003806 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003807 // missing first index is equal to zero
3808 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003809 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003810 else
3811 {
3812 if (compile_expr0(arg, cctx) == FAIL)
3813 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003814 if (**arg == ':')
3815 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003816 semsg(_(e_white_space_required_before_and_after_str_at_str),
3817 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003818 return FAIL;
3819 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003820 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003821 return FAIL;
3822 *arg = skipwhite(*arg);
3823 }
3824 if (**arg == ':')
3825 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003826 is_slice = TRUE;
3827 ++*arg;
3828 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3829 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003830 semsg(_(e_white_space_required_before_and_after_str_at_str),
3831 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003832 return FAIL;
3833 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003834 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003835 return FAIL;
3836 if (**arg == ']')
3837 // missing second index is equal to end of string
3838 generate_PUSHNR(cctx, -1);
3839 else
3840 {
3841 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003842 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003843 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003844 return FAIL;
3845 *arg = skipwhite(*arg);
3846 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
3849 if (**arg != ']')
3850 {
3851 emsg(_(e_missbrac));
3852 return FAIL;
3853 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003854 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003856 // We can index a list and a dict. If we don't know the type
3857 // we can use the index value type.
3858 // TODO: If we don't know use an instruction to figure it out at
3859 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003860 typep = ((type_T **)stack->ga_data) + stack->ga_len
3861 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003862 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003863 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3864 // If the index is a string, the variable must be a Dict.
3865 if (*typep == &t_any && valtype == &t_string)
3866 vtype = VAR_DICT;
3867 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003868 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003869 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003870 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003871 return FAIL;
3872 if (is_slice)
3873 {
3874 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003875 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003876 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003877 return FAIL;
3878 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003879 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003880
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003881 if (vtype == VAR_DICT)
3882 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003883 if (is_slice)
3884 {
3885 emsg(_(e_cannot_slice_dictionary));
3886 return FAIL;
3887 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003888 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003889 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003890 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003891 if (*typep == &t_unknown)
3892 // empty dict was used
3893 *typep = &t_any;
3894 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003895 else
3896 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003897 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003898 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003899 return FAIL;
3900 *typep = &t_any;
3901 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003902 if (may_generate_2STRING(-1, cctx) == FAIL)
3903 return FAIL;
3904 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3905 return FAIL;
3906 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003907 else if (vtype == VAR_STRING)
3908 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003909 *typep = &t_string;
3910 if ((is_slice
3911 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3912 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003913 return FAIL;
3914 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003915 else if (vtype == VAR_BLOB)
3916 {
3917 emsg("Sorry, blob index and slice not implemented yet");
3918 return FAIL;
3919 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003920 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003921 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003922 if (is_slice)
3923 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003924 if (generate_instr_drop(cctx,
3925 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3926 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003927 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003928 }
Bram Moolenaared591872020-08-15 22:14:53 +02003929 else
3930 {
3931 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003932 {
Bram Moolenaared591872020-08-15 22:14:53 +02003933 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003934 if (*typep == &t_unknown)
3935 // empty list was used
3936 *typep = &t_any;
3937 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003938 if (generate_instr_drop(cctx,
3939 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3940 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003941 return FAIL;
3942 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003943 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003944 else
3945 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003946 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003947 return FAIL;
3948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003950 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003952 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003953 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003954 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003955 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003956
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003957 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003958 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003959 {
3960 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003961 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003962 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003963 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003964 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 while (eval_isnamec(*p))
3966 MB_PTR_ADV(p);
3967 if (p == *arg)
3968 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003969 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 return FAIL;
3971 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003972 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 return FAIL;
3974 *arg = p;
3975 }
3976 else
3977 break;
3978 }
3979
3980 // TODO - see handle_subscript():
3981 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3982 // Don't do this when "Func" is already a partial that was bound
3983 // explicitly (pt_auto is FALSE).
3984
3985 return OK;
3986}
3987
3988/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003989 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3990 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003992 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003993 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003994 *
3995 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 */
3997
3998/*
3999 * number number constant
4000 * 0zFFFFFFFF Blob constant
4001 * "string" string constant
4002 * 'string' literal string constant
4003 * &option-name option value
4004 * @r register contents
4005 * identifier variable value
4006 * function() function call
4007 * $VAR environment variable
4008 * (expression) nested expression
4009 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004010 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 *
4012 * Also handle:
4013 * ! in front logical NOT
4014 * - in front unary minus
4015 * + in front unary plus (ignored)
4016 * trailing (arg) funcref/partial call
4017 * trailing [] subscript in String or List
4018 * trailing .name entry in Dictionary
4019 * trailing ->name() method call
4020 */
4021 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004022compile_expr7(
4023 char_u **arg,
4024 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004025 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 char_u *start_leader, *end_leader;
4028 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004029 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004030 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004032 ppconst->pp_is_const = FALSE;
4033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 /*
4035 * Skip '!', '-' and '+' characters. They are handled later.
4036 */
4037 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004038 if (eval_leader(arg, TRUE) == FAIL)
4039 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 end_leader = *arg;
4041
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004042 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 switch (**arg)
4044 {
4045 /*
4046 * Number constant.
4047 */
4048 case '0': // also for blob starting with 0z
4049 case '1':
4050 case '2':
4051 case '3':
4052 case '4':
4053 case '5':
4054 case '6':
4055 case '7':
4056 case '8':
4057 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004058 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004060 // Apply "-" and "+" just before the number now, right to
4061 // left. Matters especially when "->" follows. Stops at
4062 // '!'.
4063 if (apply_leader(rettv, TRUE,
4064 start_leader, &end_leader) == FAIL)
4065 {
4066 clear_tv(rettv);
4067 return FAIL;
4068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 break;
4070
4071 /*
4072 * String constant: "string".
4073 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004074 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 return FAIL;
4076 break;
4077
4078 /*
4079 * Literal string constant: 'str''ing'.
4080 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004081 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 return FAIL;
4083 break;
4084
4085 /*
4086 * Constant Vim variable.
4087 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004088 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 ret = NOTDONE;
4090 break;
4091
4092 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004093 * "true" constant
4094 */
4095 case 't': if (STRNCMP(*arg, "true", 4) == 0
4096 && !eval_isnamec((*arg)[4]))
4097 {
4098 *arg += 4;
4099 rettv->v_type = VAR_BOOL;
4100 rettv->vval.v_number = VVAL_TRUE;
4101 }
4102 else
4103 ret = NOTDONE;
4104 break;
4105
4106 /*
4107 * "false" constant
4108 */
4109 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4110 && !eval_isnamec((*arg)[5]))
4111 {
4112 *arg += 5;
4113 rettv->v_type = VAR_BOOL;
4114 rettv->vval.v_number = VVAL_FALSE;
4115 }
4116 else
4117 ret = NOTDONE;
4118 break;
4119
4120 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004121 * "null" constant
4122 */
4123 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4124 && !eval_isnamec((*arg)[5]))
4125 {
4126 *arg += 4;
4127 rettv->v_type = VAR_SPECIAL;
4128 rettv->vval.v_number = VVAL_NULL;
4129 }
4130 else
4131 ret = NOTDONE;
4132 break;
4133
4134 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 * List: [expr, expr]
4136 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004137 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4138 return FAIL;
4139 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 break;
4141
4142 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 * Dictionary: {'key': val, 'key': val}
4144 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004145 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4146 return FAIL;
4147 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 break;
4149
4150 /*
4151 * Option value: &name
4152 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004153 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4154 return FAIL;
4155 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 break;
4157
4158 /*
4159 * Environment variable: $VAR.
4160 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004161 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4162 return FAIL;
4163 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 break;
4165
4166 /*
4167 * Register contents: @r.
4168 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004169 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4170 return FAIL;
4171 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 break;
4173 /*
4174 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004175 * lambda: (arg, arg) => expr
4176 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004178 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4179 ret = compile_lambda(arg, cctx);
4180 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004181 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 break;
4183
4184 default: ret = NOTDONE;
4185 break;
4186 }
4187 if (ret == FAIL)
4188 return FAIL;
4189
Bram Moolenaar1c747212020-05-09 18:28:34 +02004190 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004192 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004193 clear_tv(rettv);
4194 else
4195 // A constant expression can possibly be handled compile time,
4196 // return the value instead of generating code.
4197 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 }
4199 else if (ret == NOTDONE)
4200 {
4201 char_u *p;
4202 int r;
4203
4204 if (!eval_isnamec1(**arg))
4205 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004206 if (ends_excmd(*skipwhite(*arg)))
4207 semsg(_(e_empty_expression_str), *arg);
4208 else
4209 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 return FAIL;
4211 }
4212
4213 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004214 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004216 {
4217 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 {
4221 if (generate_ppconst(cctx, ppconst) == FAIL)
4222 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004223 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004224 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 if (r == FAIL)
4226 return FAIL;
4227 }
4228
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004229 // Handle following "[]", ".member", etc.
4230 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004231 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004232 ppconst) == FAIL)
4233 return FAIL;
4234 if (ppconst->pp_used > 0)
4235 {
4236 // apply the '!', '-' and '+' before the constant
4237 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004238 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004239 return FAIL;
4240 return OK;
4241 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004242 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004244 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245}
4246
4247/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004248 * Give the "white on both sides" error, taking the operator from "p[len]".
4249 */
4250 void
4251error_white_both(char_u *op, int len)
4252{
4253 char_u buf[10];
4254
4255 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004256 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004257}
4258
4259/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004260 * <type>expr7: runtime type check / conversion
4261 */
4262 static int
4263compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4264{
4265 type_T *want_type = NULL;
4266
4267 // Recognize <type>
4268 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4269 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004270 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004271 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4272 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004273 return FAIL;
4274
4275 if (**arg != '>')
4276 {
4277 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004278 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004279 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004280 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004281 return FAIL;
4282 }
4283 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004284 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004285 return FAIL;
4286 }
4287
4288 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4289 return FAIL;
4290
4291 if (want_type != NULL)
4292 {
4293 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004294 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004295 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004296
Bram Moolenaard1103582020-08-14 22:44:25 +02004297 generate_ppconst(cctx, ppconst);
4298 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004299 where.wt_index = 0;
4300 where.wt_variable = FALSE;
4301 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004302 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004303 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004304 return FAIL;
4305 }
4306 }
4307
4308 return OK;
4309}
4310
4311/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 * * number multiplication
4313 * / number division
4314 * % number modulo
4315 */
4316 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004317compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318{
4319 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004320 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004321 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004323 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004324 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 return FAIL;
4326
4327 /*
4328 * Repeat computing, until no "*", "/" or "%" is following.
4329 */
4330 for (;;)
4331 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004332 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 if (*op != '*' && *op != '/' && *op != '%')
4334 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004335 if (next != NULL)
4336 {
4337 *arg = next_line_from_context(cctx, TRUE);
4338 op = skipwhite(*arg);
4339 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004341 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004343 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004344 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004346 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004347 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004349 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004350 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004352
4353 if (ppconst->pp_used == ppconst_used + 2
4354 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4355 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004357 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4358 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4359 varnumber_T res = 0;
4360 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004361
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004362 // both are numbers: compute the result
4363 switch (*op)
4364 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004365 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004366 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004367 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004368 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004369 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004370 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004371 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004372 break;
4373 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004374 if (failed)
4375 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004376 tv1->vval.v_number = res;
4377 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004378 }
4379 else
4380 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004381 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004382 generate_two_op(cctx, op);
4383 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
4385
4386 return OK;
4387}
4388
4389/*
4390 * + number addition
4391 * - number subtraction
4392 * .. string concatenation
4393 */
4394 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004395compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396{
4397 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004398 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004400 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
4402 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004403 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004404 return FAIL;
4405
4406 /*
4407 * Repeat computing, until no "+", "-" or ".." is following.
4408 */
4409 for (;;)
4410 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004411 op = may_peek_next_line(cctx, *arg, &next);
4412 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 break;
4414 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004415 if (next != NULL)
4416 {
4417 *arg = next_line_from_context(cctx, TRUE);
4418 op = skipwhite(*arg);
4419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004421 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004423 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004424 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 }
4426
Bram Moolenaare0de1712020-12-02 17:36:54 +01004427 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004428 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004430 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004431 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 return FAIL;
4433
Bram Moolenaara5565e42020-05-09 15:44:01 +02004434 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004435 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004436 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4437 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4438 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4439 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004441 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4442 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004443
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004444 // concat/subtract/add constant numbers
4445 if (*op == '+')
4446 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4447 else if (*op == '-')
4448 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4449 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004450 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004451 // concatenate constant strings
4452 char_u *s1 = tv1->vval.v_string;
4453 char_u *s2 = tv2->vval.v_string;
4454 size_t len1 = STRLEN(s1);
4455
4456 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4457 if (tv1->vval.v_string == NULL)
4458 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004459 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004460 return FAIL;
4461 }
4462 mch_memmove(tv1->vval.v_string, s1, len1);
4463 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004464 vim_free(s1);
4465 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004466 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004467 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 }
4469 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004470 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004472 if (*op == '.')
4473 {
4474 if (may_generate_2STRING(-2, cctx) == FAIL
4475 || may_generate_2STRING(-1, cctx) == FAIL)
4476 return FAIL;
4477 generate_instr_drop(cctx, ISN_CONCAT, 1);
4478 }
4479 else
4480 generate_two_op(cctx, op);
4481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004482 }
4483
4484 return OK;
4485}
4486
4487/*
4488 * expr5a == expr5b
4489 * expr5a =~ expr5b
4490 * expr5a != expr5b
4491 * expr5a !~ expr5b
4492 * expr5a > expr5b
4493 * expr5a >= expr5b
4494 * expr5a < expr5b
4495 * expr5a <= expr5b
4496 * expr5a is expr5b
4497 * expr5a isnot expr5b
4498 *
4499 * Produces instructions:
4500 * EVAL expr5a Push result of "expr5a"
4501 * EVAL expr5b Push result of "expr5b"
4502 * COMPARE one of the compare instructions
4503 */
4504 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004507 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004509 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004512 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004513
4514 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004515 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 return FAIL;
4517
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004518 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004519 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520
4521 /*
4522 * If there is a comparative operator, use it.
4523 */
4524 if (type != EXPR_UNKNOWN)
4525 {
4526 int ic = FALSE; // Default: do not ignore case
4527
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004528 if (next != NULL)
4529 {
4530 *arg = next_line_from_context(cctx, TRUE);
4531 p = skipwhite(*arg);
4532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 if (type_is && (p[len] == '?' || p[len] == '#'))
4534 {
4535 semsg(_(e_invexpr2), *arg);
4536 return FAIL;
4537 }
4538 // extra question mark appended: ignore case
4539 if (p[len] == '?')
4540 {
4541 ic = TRUE;
4542 ++len;
4543 }
4544 // extra '#' appended: match case (ignored)
4545 else if (p[len] == '#')
4546 ++len;
4547 // nothing appended: match case
4548
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004549 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004551 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004552 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 }
4554
4555 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004556 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004557 return FAIL;
4558
Bram Moolenaara5565e42020-05-09 15:44:01 +02004559 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 return FAIL;
4561
Bram Moolenaara5565e42020-05-09 15:44:01 +02004562 if (ppconst->pp_used == ppconst_used + 2)
4563 {
4564 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4565 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4566 int ret;
4567
4568 // Both sides are a constant, compute the result now.
4569 // First check for a valid combination of types, this is more
4570 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004571 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572 ret = FAIL;
4573 else
4574 {
4575 ret = typval_compare(tv1, tv2, type, ic);
4576 tv1->v_type = VAR_BOOL;
4577 tv1->vval.v_number = tv1->vval.v_number
4578 ? VVAL_TRUE : VVAL_FALSE;
4579 clear_tv(tv2);
4580 --ppconst->pp_used;
4581 }
4582 return ret;
4583 }
4584
4585 generate_ppconst(cctx, ppconst);
4586 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 }
4588
4589 return OK;
4590}
4591
Bram Moolenaar7f141552020-05-09 17:35:53 +02004592static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594/*
4595 * Compile || or &&.
4596 */
4597 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004598compile_and_or(
4599 char_u **arg,
4600 cctx_T *cctx,
4601 char *op,
4602 ppconst_T *ppconst,
4603 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605 char_u *next;
4606 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 int opchar = *op;
4608
4609 if (p[0] == opchar && p[1] == opchar)
4610 {
4611 garray_T *instr = &cctx->ctx_instr;
4612 garray_T end_ga;
4613
4614 /*
4615 * Repeat until there is no following "||" or "&&"
4616 */
4617 ga_init2(&end_ga, sizeof(int), 10);
4618 while (p[0] == opchar && p[1] == opchar)
4619 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004620 if (next != NULL)
4621 {
4622 *arg = next_line_from_context(cctx, TRUE);
4623 p = skipwhite(*arg);
4624 }
4625
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004626 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4627 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004628 semsg(_(e_white_space_required_before_and_after_str_at_str),
4629 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004630 return FAIL;
4631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004633 // TODO: use ppconst if the value is a constant and check
4634 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004635 generate_ppconst(cctx, ppconst);
4636
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004637 // Every part must evaluate to a bool.
4638 if (bool_on_stack(cctx) == FAIL)
4639 {
4640 ga_clear(&end_ga);
4641 return FAIL;
4642 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 if (ga_grow(&end_ga, 1) == FAIL)
4645 {
4646 ga_clear(&end_ga);
4647 return FAIL;
4648 }
4649 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4650 ++end_ga.ga_len;
4651 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004652 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653
4654 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004655 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004656 {
4657 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004658 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004659 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004660
Bram Moolenaara5565e42020-05-09 15:44:01 +02004661 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4662 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663 {
4664 ga_clear(&end_ga);
4665 return FAIL;
4666 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004667
4668 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004670 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004672 // Every part must evaluate to a bool.
4673 if (bool_on_stack(cctx) == FAIL)
4674 {
4675 ga_clear(&end_ga);
4676 return FAIL;
4677 }
4678
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 // Fill in the end label in all jumps.
4680 while (end_ga.ga_len > 0)
4681 {
4682 isn_T *isn;
4683
4684 --end_ga.ga_len;
4685 isn = ((isn_T *)instr->ga_data)
4686 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4687 isn->isn_arg.jump.jump_where = instr->ga_len;
4688 }
4689 ga_clear(&end_ga);
4690 }
4691
4692 return OK;
4693}
4694
4695/*
4696 * expr4a && expr4a && expr4a logical AND
4697 *
4698 * Produces instructions:
4699 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004700 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004701 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004703 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 * EVAL expr4c Push result of "expr4c"
4705 * end:
4706 */
4707 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004708compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004710 int ppconst_used = ppconst->pp_used;
4711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 return FAIL;
4715
4716 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718}
4719
4720/*
4721 * expr3a || expr3b || expr3c logical OR
4722 *
4723 * Produces instructions:
4724 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004725 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004726 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004728 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 * EVAL expr3c Push result of "expr3c"
4730 * end:
4731 */
4732 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004733compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004735 int ppconst_used = ppconst->pp_used;
4736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004738 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 return FAIL;
4740
4741 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004742 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743}
4744
4745/*
4746 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004748 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 * JUMP_IF_FALSE alt jump if false
4750 * EVAL expr1a
4751 * JUMP_ALWAYS end
4752 * alt: EVAL expr1b
4753 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004754 *
4755 * Toplevel expression: expr2 ?? expr1
4756 * Produces instructions:
4757 * EVAL expr2 Push result of "expr2"
4758 * JUMP_AND_KEEP_IF_TRUE end jump if true
4759 * EVAL expr1
4760 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 */
4762 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004763compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764{
4765 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004767 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768
Bram Moolenaar3988f642020-08-27 22:43:03 +02004769 // Ignore all kinds of errors when not producing code.
4770 if (cctx->ctx_skip == SKIP_YES)
4771 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004772 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004773 return OK;
4774 }
4775
Bram Moolenaar61a89812020-05-07 16:58:17 +02004776 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004777 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 return FAIL;
4779
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004780 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 if (*p == '?')
4782 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004783 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 garray_T *instr = &cctx->ctx_instr;
4785 garray_T *stack = &cctx->ctx_type_stack;
4786 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004787 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004789 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004790 int has_const_expr = FALSE;
4791 int const_value = FALSE;
4792 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004794 if (next != NULL)
4795 {
4796 *arg = next_line_from_context(cctx, TRUE);
4797 p = skipwhite(*arg);
4798 }
4799
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004800 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004801 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004802 semsg(_(e_white_space_required_before_and_after_str_at_str),
4803 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004804 return FAIL;
4805 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806
Bram Moolenaara5565e42020-05-09 15:44:01 +02004807 if (ppconst->pp_used == ppconst_used + 1)
4808 {
4809 // the condition is a constant, we know whether the ? or the :
4810 // expression is to be evaluated.
4811 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004812 if (op_falsy)
4813 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4814 else
4815 {
4816 int error = FALSE;
4817
4818 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4819 &error);
4820 if (error)
4821 return FAIL;
4822 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004823 cctx->ctx_skip = save_skip == SKIP_YES ||
4824 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4825
4826 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4827 // "left ?? right" and "left" is truthy: produce "left"
4828 generate_ppconst(cctx, ppconst);
4829 else
4830 {
4831 clear_tv(&ppconst->pp_tv[ppconst_used]);
4832 --ppconst->pp_used;
4833 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 }
4835 else
4836 {
4837 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004838 if (op_falsy)
4839 end_idx = instr->ga_len;
4840 generate_JUMP(cctx, op_falsy
4841 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4842 if (op_falsy)
4843 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845
4846 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004847 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004848 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004850 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851
Bram Moolenaara5565e42020-05-09 15:44:01 +02004852 if (!has_const_expr)
4853 {
4854 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004856 if (!op_falsy)
4857 {
4858 // remember the type and drop it
4859 --stack->ga_len;
4860 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004862 end_idx = instr->ga_len;
4863 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004864
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004865 // jump here from JUMP_IF_FALSE
4866 isn = ((isn_T *)instr->ga_data) + alt_idx;
4867 isn->isn_arg.jump.jump_where = instr->ga_len;
4868 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004871 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004873 // Check for the ":".
4874 p = may_peek_next_line(cctx, *arg, &next);
4875 if (*p != ':')
4876 {
4877 emsg(_(e_missing_colon));
4878 return FAIL;
4879 }
4880 if (next != NULL)
4881 {
4882 *arg = next_line_from_context(cctx, TRUE);
4883 p = skipwhite(*arg);
4884 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004885
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004886 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4887 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004888 semsg(_(e_white_space_required_before_and_after_str_at_str),
4889 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004890 return FAIL;
4891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004893 // evaluate the third expression
4894 if (has_const_expr)
4895 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004896 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004897 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004898 return FAIL;
4899 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4900 return FAIL;
4901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902
Bram Moolenaara5565e42020-05-09 15:44:01 +02004903 if (!has_const_expr)
4904 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004905 type_T **typep;
4906
Bram Moolenaara5565e42020-05-09 15:44:01 +02004907 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004910 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4911 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004913 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004914 isn = ((isn_T *)instr->ga_data) + end_idx;
4915 isn->isn_arg.jump.jump_where = instr->ga_len;
4916 }
4917
4918 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919 }
4920 return OK;
4921}
4922
4923/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004925 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4926 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004927 */
4928 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004929compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004930{
4931 ppconst_T ppconst;
4932
4933 CLEAR_FIELD(ppconst);
4934 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4935 {
4936 clear_ppconst(&ppconst);
4937 return FAIL;
4938 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004939 if (is_const != NULL)
4940 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 if (generate_ppconst(cctx, &ppconst) == FAIL)
4942 return FAIL;
4943 return OK;
4944}
4945
4946/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004947 * Toplevel expression.
4948 */
4949 static int
4950compile_expr0(char_u **arg, cctx_T *cctx)
4951{
4952 return compile_expr0_ext(arg, cctx, NULL);
4953}
4954
4955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 * compile "return [expr]"
4957 */
4958 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004959compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960{
4961 char_u *p = arg;
4962 garray_T *stack = &cctx->ctx_type_stack;
4963 type_T *stack_type;
4964
4965 if (*p != NUL && *p != '|' && *p != '\n')
4966 {
4967 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004968 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 return NULL;
4970
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004971 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004972 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004973 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004974 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004975 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4976 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004977 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004978 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004979 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004980 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004981 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004982 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4983 && stack_type->tt_type != VAR_VOID
4984 && stack_type->tt_type != VAR_UNKNOWN)
4985 {
4986 emsg(_(e_returning_value_in_function_without_return_type));
4987 return NULL;
4988 }
4989 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004990 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004991 return NULL;
4992 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 }
4995 else
4996 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004997 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004998 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004999 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5000 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005002 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 return NULL;
5004 }
5005
5006 // No argument, return zero.
5007 generate_PUSHNR(cctx, 0);
5008 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005009
5010 // Undo any command modifiers.
5011 generate_undo_cmdmods(cctx);
5012
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005013 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 return NULL;
5015
5016 // "return val | endif" is possible
5017 return skipwhite(p);
5018}
5019
5020/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005021 * Get a line from the compilation context, compatible with exarg_T getline().
5022 * Return a pointer to the line in allocated memory.
5023 * Return NULL for end-of-file or some error.
5024 */
5025 static char_u *
5026exarg_getline(
5027 int c UNUSED,
5028 void *cookie,
5029 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005030 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005031{
5032 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005033 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005034
Bram Moolenaar66250c92020-08-20 15:02:42 +02005035 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005036 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005037 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005038 return NULL;
5039 ++cctx->ctx_lnum;
5040 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5041 // Comment lines result in NULL pointers, skip them.
5042 if (p != NULL)
5043 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005044 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005045}
5046
5047/*
5048 * Compile a nested :def command.
5049 */
5050 static char_u *
5051compile_nested_function(exarg_T *eap, cctx_T *cctx)
5052{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005053 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005054 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005055 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005056 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005057 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005058 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005059
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005060 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005061 {
5062 emsg(_(e_cannot_use_bang_with_nested_def));
5063 return NULL;
5064 }
5065
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005066 if (*name_start == '/')
5067 {
5068 name_end = skip_regexp(name_start + 1, '/', TRUE);
5069 if (*name_end == '/')
5070 ++name_end;
5071 eap->nextcmd = check_nextcmd(name_end);
5072 }
5073 if (name_end == name_start || *skipwhite(name_end) != '(')
5074 {
5075 if (!ends_excmd2(name_start, name_end))
5076 {
5077 semsg(_(e_invalid_command_str), eap->cmd);
5078 return NULL;
5079 }
5080
5081 // "def" or "def Name": list functions
5082 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5083 return NULL;
5084 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5085 }
5086
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005087 // Only g:Func() can use a namespace.
5088 if (name_start[1] == ':' && !is_global)
5089 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005090 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005091 return NULL;
5092 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005093 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5094 return NULL;
5095
Bram Moolenaar04b12692020-05-04 23:24:44 +02005096 eap->arg = name_end;
5097 eap->getline = exarg_getline;
5098 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005099 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005100 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005101 lambda_name = vim_strsave(get_lambda_name());
5102 if (lambda_name == NULL)
5103 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005104 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005105
Bram Moolenaar822ba242020-05-24 23:00:18 +02005106 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005107 {
5108 r = eap->skip ? OK : FAIL;
5109 goto theend;
5110 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005111 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5112 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005113 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005114 {
5115 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005116 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005117 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005118
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005119 if (is_global)
5120 {
5121 char_u *func_name = vim_strnsave(name_start + 2,
5122 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005123
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005124 if (func_name == NULL)
5125 r = FAIL;
5126 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005127 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005128 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005129 lambda_name = NULL;
5130 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005131 }
5132 else
5133 {
5134 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005135 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005136 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005137 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005138
Bram Moolenaareef21022020-08-01 22:16:43 +02005139 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005140 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005141 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005142 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005143 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005144
5145 // copy over the block scope IDs
5146 if (block_depth > 0)
5147 {
5148 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5149 if (ufunc->uf_block_ids != NULL)
5150 {
5151 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5152 sizeof(int) * block_depth);
5153 ufunc->uf_block_depth = block_depth;
5154 }
5155 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005156 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005157 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005158
5159theend:
5160 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005161 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005162}
5163
5164/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165 * Return the length of an assignment operator, or zero if there isn't one.
5166 */
5167 int
5168assignment_len(char_u *p, int *heredoc)
5169{
5170 if (*p == '=')
5171 {
5172 if (p[1] == '<' && p[2] == '<')
5173 {
5174 *heredoc = TRUE;
5175 return 3;
5176 }
5177 return 1;
5178 }
5179 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5180 return 2;
5181 if (STRNCMP(p, "..=", 3) == 0)
5182 return 3;
5183 return 0;
5184}
5185
5186// words that cannot be used as a variable
5187static char *reserved[] = {
5188 "true",
5189 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005190 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 NULL
5192};
5193
Bram Moolenaar752fc692021-01-04 21:57:11 +01005194// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005195typedef enum {
5196 dest_local,
5197 dest_option,
5198 dest_env,
5199 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005200 dest_buffer,
5201 dest_window,
5202 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005203 dest_vimvar,
5204 dest_script,
5205 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005206 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005207} assign_dest_T;
5208
Bram Moolenaar752fc692021-01-04 21:57:11 +01005209// Used by compile_lhs() to store information about the LHS of an assignment
5210// and one argument of ":unlet" with an index.
5211typedef struct {
5212 assign_dest_T lhs_dest; // type of destination
5213
5214 char_u *lhs_name; // allocated name including
5215 // "[expr]" or ".name".
5216 size_t lhs_varlen; // length of the variable without
5217 // "[expr]" or ".name"
5218 char_u *lhs_dest_end; // end of the destination, including
5219 // "[expr]" or ".name".
5220
5221 int lhs_has_index; // has "[expr]" or ".name"
5222
5223 int lhs_new_local; // create new local variable
5224 int lhs_opt_flags; // for when destination is an option
5225 int lhs_vimvaridx; // for when destination is a v:var
5226
5227 lvar_T lhs_local_lvar; // used for existing local destination
5228 lvar_T lhs_arg_lvar; // used for argument destination
5229 lvar_T *lhs_lvar; // points to destination lvar
5230 int lhs_scriptvar_sid;
5231 int lhs_scriptvar_idx;
5232
5233 int lhs_has_type; // type was specified
5234 type_T *lhs_type;
5235 type_T *lhs_member_type;
5236} lhs_T;
5237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005239 * Generate the load instruction for "name".
5240 */
5241 static void
5242generate_loadvar(
5243 cctx_T *cctx,
5244 assign_dest_T dest,
5245 char_u *name,
5246 lvar_T *lvar,
5247 type_T *type)
5248{
5249 switch (dest)
5250 {
5251 case dest_option:
5252 // TODO: check the option exists
5253 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5254 break;
5255 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005256 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5257 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5258 else
5259 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005260 break;
5261 case dest_buffer:
5262 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5263 break;
5264 case dest_window:
5265 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5266 break;
5267 case dest_tab:
5268 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5269 break;
5270 case dest_script:
5271 compile_load_scriptvar(cctx,
5272 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5273 break;
5274 case dest_env:
5275 // Include $ in the name here
5276 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5277 break;
5278 case dest_reg:
5279 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5280 break;
5281 case dest_vimvar:
5282 generate_LOADV(cctx, name + 2, TRUE);
5283 break;
5284 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005285 if (lvar->lv_from_outer > 0)
5286 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5287 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005288 else
5289 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5290 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005291 case dest_expr:
5292 // list or dict value should already be on the stack.
5293 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005294 }
5295}
5296
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005297/*
5298 * Skip over "[expr]" or ".member".
5299 * Does not check for any errors.
5300 */
5301 static char_u *
5302skip_index(char_u *start)
5303{
5304 char_u *p = start;
5305
5306 if (*p == '[')
5307 {
5308 p = skipwhite(p + 1);
5309 (void)skip_expr(&p, NULL);
5310 p = skipwhite(p);
5311 if (*p == ']')
5312 return p + 1;
5313 return p;
5314 }
5315 // if (*p == '.')
5316 return to_name_end(p + 1, TRUE);
5317}
5318
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005319 void
5320vim9_declare_error(char_u *name)
5321{
5322 char *scope = "";
5323
5324 switch (*name)
5325 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005326 case 'g': scope = _("global"); break;
5327 case 'b': scope = _("buffer"); break;
5328 case 'w': scope = _("window"); break;
5329 case 't': scope = _("tab"); break;
5330 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005331 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005332 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005333 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005334 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005335 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005336 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005337 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005338 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005339 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005340}
5341
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005342/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005343 * For one assignment figure out the type of destination. Return it in "dest".
5344 * When not recognized "dest" is not set.
5345 * For an option "opt_flags" is set.
5346 * For a v:var "vimvaridx" is set.
5347 * "type" is set to the destination type if known, unchanted otherwise.
5348 * Return FAIL if an error message was given.
5349 */
5350 static int
5351get_var_dest(
5352 char_u *name,
5353 assign_dest_T *dest,
5354 int cmdidx,
5355 int *opt_flags,
5356 int *vimvaridx,
5357 type_T **type,
5358 cctx_T *cctx)
5359{
5360 char_u *p;
5361
5362 if (*name == '&')
5363 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005364 int cc;
5365 long numval;
5366 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005367
5368 *dest = dest_option;
5369 if (cmdidx == CMD_final || cmdidx == CMD_const)
5370 {
5371 emsg(_(e_const_option));
5372 return FAIL;
5373 }
5374 p = name;
5375 p = find_option_end(&p, opt_flags);
5376 if (p == NULL)
5377 {
5378 // cannot happen?
5379 emsg(_(e_letunexp));
5380 return FAIL;
5381 }
5382 cc = *p;
5383 *p = NUL;
5384 opt_type = get_option_value(skip_option_env_lead(name),
5385 &numval, NULL, *opt_flags);
5386 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005387 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005388 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005389 case gov_unknown:
5390 semsg(_(e_unknown_option), name);
5391 return FAIL;
5392 case gov_string:
5393 case gov_hidden_string:
5394 *type = &t_string;
5395 break;
5396 case gov_bool:
5397 case gov_hidden_bool:
5398 *type = &t_bool;
5399 break;
5400 case gov_number:
5401 case gov_hidden_number:
5402 *type = &t_number;
5403 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005404 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005405 }
5406 else if (*name == '$')
5407 {
5408 *dest = dest_env;
5409 *type = &t_string;
5410 }
5411 else if (*name == '@')
5412 {
5413 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5414 {
5415 emsg_invreg(name[1]);
5416 return FAIL;
5417 }
5418 *dest = dest_reg;
5419 *type = &t_string;
5420 }
5421 else if (STRNCMP(name, "g:", 2) == 0)
5422 {
5423 *dest = dest_global;
5424 }
5425 else if (STRNCMP(name, "b:", 2) == 0)
5426 {
5427 *dest = dest_buffer;
5428 }
5429 else if (STRNCMP(name, "w:", 2) == 0)
5430 {
5431 *dest = dest_window;
5432 }
5433 else if (STRNCMP(name, "t:", 2) == 0)
5434 {
5435 *dest = dest_tab;
5436 }
5437 else if (STRNCMP(name, "v:", 2) == 0)
5438 {
5439 typval_T *vtv;
5440 int di_flags;
5441
5442 *vimvaridx = find_vim_var(name + 2, &di_flags);
5443 if (*vimvaridx < 0)
5444 {
5445 semsg(_(e_variable_not_found_str), name);
5446 return FAIL;
5447 }
5448 // We use the current value of "sandbox" here, is that OK?
5449 if (var_check_ro(di_flags, name, FALSE))
5450 return FAIL;
5451 *dest = dest_vimvar;
5452 vtv = get_vim_var_tv(*vimvaridx);
5453 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5454 }
5455 return OK;
5456}
5457
5458/*
5459 * Generate a STORE instruction for "dest", not being "dest_local".
5460 * Return FAIL when out of memory.
5461 */
5462 static int
5463generate_store_var(
5464 cctx_T *cctx,
5465 assign_dest_T dest,
5466 int opt_flags,
5467 int vimvaridx,
5468 int scriptvar_idx,
5469 int scriptvar_sid,
5470 type_T *type,
5471 char_u *name)
5472{
5473 switch (dest)
5474 {
5475 case dest_option:
5476 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5477 opt_flags);
5478 case dest_global:
5479 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005480 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5481 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005482 case dest_buffer:
5483 // include b: with the name, easier to execute that way
5484 return generate_STORE(cctx, ISN_STOREB, 0, name);
5485 case dest_window:
5486 // include w: with the name, easier to execute that way
5487 return generate_STORE(cctx, ISN_STOREW, 0, name);
5488 case dest_tab:
5489 // include t: with the name, easier to execute that way
5490 return generate_STORE(cctx, ISN_STORET, 0, name);
5491 case dest_env:
5492 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5493 case dest_reg:
5494 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5495 case dest_vimvar:
5496 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5497 case dest_script:
5498 if (scriptvar_idx < 0)
5499 {
5500 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005501 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005502
Bram Moolenaar41d61962020-12-06 20:12:43 +01005503 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005504 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5505 scriptvar_sid, type);
5506 if (name_s != name)
5507 vim_free(name_s);
5508 return r;
5509 }
5510 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5511 scriptvar_sid, scriptvar_idx, type);
5512 case dest_local:
5513 case dest_expr:
5514 // cannot happen
5515 break;
5516 }
5517 return FAIL;
5518}
5519
Bram Moolenaar752fc692021-01-04 21:57:11 +01005520 static int
5521is_decl_command(int cmdidx)
5522{
5523 return cmdidx == CMD_let || cmdidx == CMD_var
5524 || cmdidx == CMD_final || cmdidx == CMD_const;
5525}
5526
5527/*
5528 * Figure out the LHS type and other properties for an assignment or one item
5529 * of ":unlet" with an index.
5530 * Returns OK or FAIL.
5531 */
5532 static int
5533compile_lhs(
5534 char_u *var_start,
5535 lhs_T *lhs,
5536 int cmdidx,
5537 int heredoc,
5538 int oplen,
5539 cctx_T *cctx)
5540{
5541 char_u *var_end;
5542 int is_decl = is_decl_command(cmdidx);
5543
5544 CLEAR_POINTER(lhs);
5545 lhs->lhs_dest = dest_local;
5546 lhs->lhs_vimvaridx = -1;
5547 lhs->lhs_scriptvar_idx = -1;
5548
5549 // "dest_end" is the end of the destination, including "[expr]" or
5550 // ".name".
5551 // "var_end" is the end of the variable/option/etc. name.
5552 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5553 if (*var_start == '@')
5554 var_end = var_start + 2;
5555 else
5556 {
5557 // skip over the leading "&", "&l:", "&g:" and "$"
5558 var_end = skip_option_env_lead(var_start);
5559 var_end = to_name_end(var_end, TRUE);
5560 }
5561
5562 // "a: type" is declaring variable "a" with a type, not dict "a:".
5563 if (is_decl && lhs->lhs_dest_end == var_start + 2
5564 && lhs->lhs_dest_end[-1] == ':')
5565 --lhs->lhs_dest_end;
5566 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5567 --var_end;
5568
5569 // compute the length of the destination without "[expr]" or ".name"
5570 lhs->lhs_varlen = var_end - var_start;
5571 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5572 if (lhs->lhs_name == NULL)
5573 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005574
5575 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5576 // Something follows after the variable: "var[idx]" or "var.key".
5577 lhs->lhs_has_index = TRUE;
5578
Bram Moolenaar752fc692021-01-04 21:57:11 +01005579 if (heredoc)
5580 lhs->lhs_type = &t_list_string;
5581 else
5582 lhs->lhs_type = &t_any;
5583
5584 if (cctx->ctx_skip != SKIP_YES)
5585 {
5586 int declare_error = FALSE;
5587
5588 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5589 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5590 &lhs->lhs_type, cctx) == FAIL)
5591 return FAIL;
5592 if (lhs->lhs_dest != dest_local)
5593 {
5594 // Specific kind of variable recognized.
5595 declare_error = is_decl;
5596 }
5597 else
5598 {
5599 int idx;
5600
5601 // No specific kind of variable recognized, just a name.
5602 for (idx = 0; reserved[idx] != NULL; ++idx)
5603 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5604 {
5605 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5606 return FAIL;
5607 }
5608
5609
5610 if (lookup_local(var_start, lhs->lhs_varlen,
5611 &lhs->lhs_local_lvar, cctx) == OK)
5612 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5613 else
5614 {
5615 CLEAR_FIELD(lhs->lhs_arg_lvar);
5616 if (arg_exists(var_start, lhs->lhs_varlen,
5617 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5618 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5619 {
5620 if (is_decl)
5621 {
5622 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5623 return FAIL;
5624 }
5625 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5626 }
5627 }
5628 if (lhs->lhs_lvar != NULL)
5629 {
5630 if (is_decl)
5631 {
5632 semsg(_(e_variable_already_declared), lhs->lhs_name);
5633 return FAIL;
5634 }
5635 }
5636 else
5637 {
5638 int script_namespace = lhs->lhs_varlen > 1
5639 && STRNCMP(var_start, "s:", 2) == 0;
5640 int script_var = (script_namespace
5641 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5642 FALSE, cctx)
5643 : script_var_exists(var_start, lhs->lhs_varlen,
5644 TRUE, cctx)) == OK;
5645 imported_T *import =
5646 find_imported(var_start, lhs->lhs_varlen, cctx);
5647
5648 if (script_namespace || script_var || import != NULL)
5649 {
5650 char_u *rawname = lhs->lhs_name
5651 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5652
5653 if (is_decl)
5654 {
5655 if (script_namespace)
5656 semsg(_(e_cannot_declare_script_variable_in_function),
5657 lhs->lhs_name);
5658 else
5659 semsg(_(e_variable_already_declared_in_script),
5660 lhs->lhs_name);
5661 return FAIL;
5662 }
5663 else if (cctx->ctx_ufunc->uf_script_ctx_version
5664 == SCRIPT_VERSION_VIM9
5665 && script_namespace
5666 && !script_var && import == NULL)
5667 {
5668 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5669 return FAIL;
5670 }
5671
5672 lhs->lhs_dest = dest_script;
5673
5674 // existing script-local variables should have a type
5675 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5676 if (import != NULL)
5677 lhs->lhs_scriptvar_sid = import->imp_sid;
5678 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5679 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005680 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005681 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005682 lhs->lhs_scriptvar_sid, rawname,
5683 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5684 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005685 if (lhs->lhs_scriptvar_idx >= 0)
5686 {
5687 scriptitem_T *si = SCRIPT_ITEM(
5688 lhs->lhs_scriptvar_sid);
5689 svar_T *sv =
5690 ((svar_T *)si->sn_var_vals.ga_data)
5691 + lhs->lhs_scriptvar_idx;
5692 lhs->lhs_type = sv->sv_type;
5693 }
5694 }
5695 }
5696 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5697 == FAIL)
5698 return FAIL;
5699 }
5700 }
5701
5702 if (declare_error)
5703 {
5704 vim9_declare_error(lhs->lhs_name);
5705 return FAIL;
5706 }
5707 }
5708
5709 // handle "a:name" as a name, not index "name" on "a"
5710 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5711 var_end = lhs->lhs_dest_end;
5712
5713 if (lhs->lhs_dest != dest_option)
5714 {
5715 if (is_decl && *var_end == ':')
5716 {
5717 char_u *p;
5718
5719 // parse optional type: "let var: type = expr"
5720 if (!VIM_ISWHITE(var_end[1]))
5721 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005722 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005723 return FAIL;
5724 }
5725 p = skipwhite(var_end + 1);
5726 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5727 if (lhs->lhs_type == NULL)
5728 return FAIL;
5729 lhs->lhs_has_type = TRUE;
5730 }
5731 else if (lhs->lhs_lvar != NULL)
5732 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5733 }
5734
5735 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5736 && lhs->lhs_type->tt_type != VAR_STRING
5737 && lhs->lhs_type->tt_type != VAR_ANY)
5738 {
5739 emsg(_(e_can_only_concatenate_to_string));
5740 return FAIL;
5741 }
5742
5743 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5744 && cctx->ctx_skip != SKIP_YES)
5745 {
5746 if (oplen > 1 && !heredoc)
5747 {
5748 // +=, /=, etc. require an existing variable
5749 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5750 return FAIL;
5751 }
5752 if (!is_decl)
5753 {
5754 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5755 return FAIL;
5756 }
5757
5758 // new local variable
5759 if ((lhs->lhs_type->tt_type == VAR_FUNC
5760 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5761 && var_wrong_func_name(lhs->lhs_name, TRUE))
5762 return FAIL;
5763 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5764 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5765 if (lhs->lhs_lvar == NULL)
5766 return FAIL;
5767 lhs->lhs_new_local = TRUE;
5768 }
5769
5770 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005771 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005772 {
5773 // Something follows after the variable: "var[idx]" or "var.key".
5774 // TODO: should we also handle "->func()" here?
5775 if (is_decl)
5776 {
5777 emsg(_(e_cannot_use_index_when_declaring_variable));
5778 return FAIL;
5779 }
5780
5781 if (var_start[lhs->lhs_varlen] == '['
5782 || var_start[lhs->lhs_varlen] == '.')
5783 {
5784 char_u *after = var_start + lhs->lhs_varlen;
5785 char_u *p;
5786
5787 // Only the last index is used below, if there are others
5788 // before it generate code for the expression. Thus for
5789 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5790 for (;;)
5791 {
5792 p = skip_index(after);
5793 if (*p != '[' && *p != '.')
5794 break;
5795 after = p;
5796 }
5797 if (after > var_start + lhs->lhs_varlen)
5798 {
5799 lhs->lhs_varlen = after - var_start;
5800 lhs->lhs_dest = dest_expr;
5801 // We don't know the type before evaluating the expression,
5802 // use "any" until then.
5803 lhs->lhs_type = &t_any;
5804 }
5805
Bram Moolenaar752fc692021-01-04 21:57:11 +01005806 if (lhs->lhs_type->tt_member == NULL)
5807 lhs->lhs_member_type = &t_any;
5808 else
5809 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5810 }
5811 else
5812 {
5813 semsg("Not supported yet: %s", var_start);
5814 return FAIL;
5815 }
5816 }
5817 return OK;
5818}
5819
5820/*
5821 * Assignment to a list or dict member, or ":unlet" for the item, using the
5822 * information in "lhs".
5823 * Returns OK or FAIL.
5824 */
5825 static int
5826compile_assign_unlet(
5827 char_u *var_start,
5828 lhs_T *lhs,
5829 int is_assign,
5830 type_T *rhs_type,
5831 cctx_T *cctx)
5832{
5833 char_u *p;
5834 int r;
5835 vartype_T dest_type;
5836 size_t varlen = lhs->lhs_varlen;
5837 garray_T *stack = &cctx->ctx_type_stack;
5838
5839 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5840 p = var_start + varlen;
5841 if (*p == '[')
5842 {
5843 p = skipwhite(p + 1);
5844 r = compile_expr0(&p, cctx);
5845 if (r == OK && *skipwhite(p) != ']')
5846 {
5847 // this should not happen
5848 emsg(_(e_missbrac));
5849 r = FAIL;
5850 }
5851 }
5852 else // if (*p == '.')
5853 {
5854 char_u *key_end = to_name_end(p + 1, TRUE);
5855 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5856
5857 r = generate_PUSHS(cctx, key);
5858 }
5859 if (r == FAIL)
5860 return FAIL;
5861
5862 if (lhs->lhs_type == &t_any)
5863 {
5864 // Index on variable of unknown type: check at runtime.
5865 dest_type = VAR_ANY;
5866 }
5867 else
5868 {
5869 dest_type = lhs->lhs_type->tt_type;
5870 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5871 return FAIL;
5872 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005873 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5874 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005875 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005876 }
5877
5878 // Load the dict or list. On the stack we then have:
5879 // - value (for assignment, not for :unlet)
5880 // - index
5881 // - variable
5882 if (lhs->lhs_dest == dest_expr)
5883 {
5884 int c = var_start[varlen];
5885
5886 // Evaluate "ll[expr]" of "ll[expr][idx]"
5887 p = var_start;
5888 var_start[varlen] = NUL;
5889 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5890 {
5891 // this should not happen
5892 emsg(_(e_missbrac));
5893 return FAIL;
5894 }
5895 var_start[varlen] = c;
5896
5897 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5898 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5899 // now we can properly check the type
5900 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005901 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005902 FALSE, FALSE) == FAIL)
5903 return FAIL;
5904 }
5905 else
5906 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5907 lhs->lhs_lvar, lhs->lhs_type);
5908
5909 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5910 {
5911 if (is_assign)
5912 {
5913 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5914
5915 if (isn == NULL)
5916 return FAIL;
5917 isn->isn_arg.vartype = dest_type;
5918 }
5919 else
5920 {
5921 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5922 return FAIL;
5923 }
5924 }
5925 else
5926 {
5927 emsg(_(e_indexable_type_required));
5928 return FAIL;
5929 }
5930
5931 return OK;
5932}
5933
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005934/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005935 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005936 * "let name"
5937 * "var name = expr"
5938 * "final name = expr"
5939 * "const name = expr"
5940 * "name = expr"
5941 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005942 * Return NULL for an error.
5943 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 */
5945 static char_u *
5946compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5947{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005948 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005949 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005950 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 char_u *ret = NULL;
5952 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005953 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005956 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 int oplen = 0;
5959 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005960 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005962 int is_decl = is_decl_command(cmdidx);
5963 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 // Skip over the "var" or "[var, var]" to get to any "=".
5966 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5967 if (p == NULL)
5968 return *arg == '[' ? arg : NULL;
5969
5970 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005972 // TODO: should we allow this, and figure out type inference from list
5973 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005974 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 return NULL;
5976 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005977 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005979 sp = p;
5980 p = skipwhite(p);
5981 op = p;
5982 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005983
5984 if (var_count > 0 && oplen == 0)
5985 // can be something like "[1, 2]->func()"
5986 return arg;
5987
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005988 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005989 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005990 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005991 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005992 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 if (heredoc)
5995 {
5996 list_T *l;
5997 listitem_T *li;
5998
5999 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006000 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006002 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006003 if (l == NULL)
6004 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005
Bram Moolenaar078269b2020-09-21 20:35:55 +02006006 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006008 // Push each line and the create the list.
6009 FOR_ALL_LIST_ITEMS(l, li)
6010 {
6011 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6012 li->li_tv.vval.v_string = NULL;
6013 }
6014 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 list_free(l);
6017 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006018 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006019 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006020 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006022 char_u *wp;
6023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006024 // for "[var, var] = expr" evaluate the expression here, loop over the
6025 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006026 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006027
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006028 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006029 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006030 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006031 if (compile_expr0(&p, cctx) == FAIL)
6032 return NULL;
6033 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006034
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006035 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006037 type_T *stacktype;
6038
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006039 stacktype = stack->ga_len == 0 ? &t_void
6040 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006041 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006043 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006044 goto theend;
6045 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006046 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006047 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006048 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006049 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006050 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6051 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006052 if (stacktype->tt_member != NULL)
6053 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006054 }
6055 }
6056
6057 /*
6058 * Loop over variables in "[var, var] = expr".
6059 * For "var = expr" and "let var: type" this is done only once.
6060 */
6061 if (var_count > 0)
6062 var_start = skipwhite(arg + 1); // skip over the "["
6063 else
6064 var_start = arg;
6065 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6066 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006067 int instr_count = -1;
6068
Bram Moolenaar752fc692021-01-04 21:57:11 +01006069 vim_free(lhs.lhs_name);
6070
6071 /*
6072 * Figure out the LHS type and other properties.
6073 */
6074 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6075 goto theend;
6076
6077 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006078 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006079 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006080 goto theend;
6081 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006082 if (!is_decl && lhs.lhs_lvar != NULL
6083 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006085 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006086 goto theend;
6087 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006088
6089 if (!heredoc)
6090 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006091 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006092 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006093 if (oplen > 0 && var_count == 0)
6094 {
6095 // skip over the "=" and the expression
6096 p = skipwhite(op + oplen);
6097 compile_expr0(&p, cctx);
6098 }
6099 }
6100 else if (oplen > 0)
6101 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006102 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006103 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006104
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006105 // For "var = expr" evaluate the expression.
6106 if (var_count == 0)
6107 {
6108 int r;
6109
6110 // for "+=", "*=", "..=" etc. first load the current value
6111 if (*op != '=')
6112 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006113 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6114 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006115
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006117 {
6118 // TODO: get member from list or dict
6119 emsg("Index with operation not supported yet");
6120 goto theend;
6121 }
6122 }
6123
6124 // Compile the expression. Temporarily hide the new local
6125 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006126 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006127 --cctx->ctx_locals.ga_len;
6128 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006129 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006130 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006131 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006132 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006133 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006134 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006135 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006136 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006137 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006138 ++cctx->ctx_locals.ga_len;
6139 if (r == FAIL)
6140 goto theend;
6141 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006142 else if (semicolon && var_idx == var_count - 1)
6143 {
6144 // For "[var; var] = expr" get the rest of the list
6145 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6146 goto theend;
6147 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006148 else
6149 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006150 // For "[var, var] = expr" get the "var_idx" item from the
6151 // list.
6152 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006153 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006154 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006155
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006156 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006157 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006159 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006160 if ((rhs_type->tt_type == VAR_FUNC
6161 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006162 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006163 goto theend;
6164
Bram Moolenaar752fc692021-01-04 21:57:11 +01006165 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006166 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006167 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006168 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006169 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006170 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006171 }
6172 else
6173 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006174 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006175 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006176 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006177 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006178 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006179 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006180 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006181 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006182 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006183 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006184 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006185 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006186 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006187 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006188 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006189
Bram Moolenaar71abe482020-09-14 22:28:30 +02006190 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006191 if (lhs.lhs_has_index)
6192 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006193 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006194 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006195 goto theend;
6196 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006197 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006199 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006200 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006201 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006202 else if (cmdidx == CMD_final)
6203 {
6204 emsg(_(e_final_requires_a_value));
6205 goto theend;
6206 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006207 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006208 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006209 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006210 goto theend;
6211 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006212 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006213 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006214 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006215 goto theend;
6216 }
6217 else
6218 {
6219 // variables are always initialized
6220 if (ga_grow(instr, 1) == FAIL)
6221 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006222 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006223 {
6224 case VAR_BOOL:
6225 generate_PUSHBOOL(cctx, VVAL_FALSE);
6226 break;
6227 case VAR_FLOAT:
6228#ifdef FEAT_FLOAT
6229 generate_PUSHF(cctx, 0.0);
6230#endif
6231 break;
6232 case VAR_STRING:
6233 generate_PUSHS(cctx, NULL);
6234 break;
6235 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006236 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006237 break;
6238 case VAR_FUNC:
6239 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6240 break;
6241 case VAR_LIST:
6242 generate_NEWLIST(cctx, 0);
6243 break;
6244 case VAR_DICT:
6245 generate_NEWDICT(cctx, 0);
6246 break;
6247 case VAR_JOB:
6248 generate_PUSHJOB(cctx, NULL);
6249 break;
6250 case VAR_CHANNEL:
6251 generate_PUSHCHANNEL(cctx, NULL);
6252 break;
6253 case VAR_NUMBER:
6254 case VAR_UNKNOWN:
6255 case VAR_ANY:
6256 case VAR_PARTIAL:
6257 case VAR_VOID:
6258 case VAR_SPECIAL: // cannot happen
6259 generate_PUSHNR(cctx, 0);
6260 break;
6261 }
6262 }
6263 if (var_count == 0)
6264 end = p;
6265 }
6266
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006267 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006268 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006269 break;
6270
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006271 if (oplen > 0 && *op != '=')
6272 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006273 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006274 type_T *stacktype;
6275
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006276 if (*op == '.')
6277 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006278 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006279 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006280 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006281 if (
6282#ifdef FEAT_FLOAT
6283 // If variable is float operation with number is OK.
6284 !(expected == &t_float && stacktype == &t_number) &&
6285#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006286 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006287 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006288 goto theend;
6289
6290 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006291 {
6292 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6293 goto theend;
6294 }
6295 else if (*op == '+')
6296 {
6297 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006298 operator_type(lhs.lhs_member_type, stacktype),
6299 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006300 goto theend;
6301 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006302 else if (generate_two_op(cctx, op) == FAIL)
6303 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305
Bram Moolenaar752fc692021-01-04 21:57:11 +01006306 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006307 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 // Use the info in "lhs" to store the value at the index in the
6309 // list or dict.
6310 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6311 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006312 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006313 }
6314 else
6315 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006316 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6317 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006318 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006319 generate_LOCKCONST(cctx);
6320
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006321 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006322 && (lhs.lhs_type->tt_type == VAR_DICT
6323 || lhs.lhs_type->tt_type == VAR_LIST)
6324 && lhs.lhs_type->tt_member != NULL
6325 && lhs.lhs_type->tt_member != &t_any
6326 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006327 // Set the type in the list or dict, so that it can be checked,
6328 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006329 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006330
Bram Moolenaar752fc692021-01-04 21:57:11 +01006331 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006332 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 if (generate_store_var(cctx, lhs.lhs_dest,
6334 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6335 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6336 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006337 goto theend;
6338 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006340 {
6341 isn_T *isn = ((isn_T *)instr->ga_data)
6342 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006343
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006344 // optimization: turn "var = 123" from ISN_PUSHNR +
6345 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006346 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006347 && instr->ga_len == instr_count + 1
6348 && isn->isn_type == ISN_PUSHNR)
6349 {
6350 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006351
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006352 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006354 isn->isn_arg.storenr.stnr_val = val;
6355 if (stack->ga_len > 0)
6356 --stack->ga_len;
6357 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006358 else if (lhs.lhs_lvar->lv_from_outer > 0)
6359 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6360 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006361 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006362 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006363 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006364 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006365
6366 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006367 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006368 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006369
6370 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006371 if (var_count > 0 && !semicolon)
6372 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006373 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006374 goto theend;
6375 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006376
Bram Moolenaarb2097502020-07-19 17:17:02 +02006377 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378
6379theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006380 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 return ret;
6382}
6383
6384/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006385 * Check for an assignment at "eap->cmd", compile it if found.
6386 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6387 */
6388 static int
6389may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6390{
6391 char_u *pskip;
6392 char_u *p;
6393
6394 // Assuming the command starts with a variable or function name,
6395 // find what follows.
6396 // Skip over "var.member", "var[idx]" and the like.
6397 // Also "&opt = val", "$ENV = val" and "@r = val".
6398 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6399 ? eap->cmd + 1 : eap->cmd;
6400 p = to_name_end(pskip, TRUE);
6401 if (p > eap->cmd && *p != NUL)
6402 {
6403 char_u *var_end;
6404 int oplen;
6405 int heredoc;
6406
6407 if (eap->cmd[0] == '@')
6408 var_end = eap->cmd + 2;
6409 else
6410 var_end = find_name_end(pskip, NULL, NULL,
6411 FNE_CHECK_START | FNE_INCL_BR);
6412 oplen = assignment_len(skipwhite(var_end), &heredoc);
6413 if (oplen > 0)
6414 {
6415 size_t len = p - eap->cmd;
6416
6417 // Recognize an assignment if we recognize the variable
6418 // name:
6419 // "g:var = expr"
6420 // "local = expr" where "local" is a local var.
6421 // "script = expr" where "script" is a script-local var.
6422 // "import = expr" where "import" is an imported var
6423 // "&opt = expr"
6424 // "$ENV = expr"
6425 // "@r = expr"
6426 if (*eap->cmd == '&'
6427 || *eap->cmd == '$'
6428 || *eap->cmd == '@'
6429 || ((len) > 2 && eap->cmd[1] == ':')
6430 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6431 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6432 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6433 || find_imported(eap->cmd, len, cctx) != NULL)
6434 {
6435 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6436 if (*line == NULL || *line == eap->cmd)
6437 return FAIL;
6438 return OK;
6439 }
6440 }
6441 }
6442
6443 if (*eap->cmd == '[')
6444 {
6445 // [var, var] = expr
6446 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6447 if (*line == NULL)
6448 return FAIL;
6449 if (*line != eap->cmd)
6450 return OK;
6451 }
6452 return NOTDONE;
6453}
6454
6455/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006456 * Check if "name" can be "unlet".
6457 */
6458 int
6459check_vim9_unlet(char_u *name)
6460{
6461 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6462 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006463 // "unlet s:var" is allowed in legacy script.
6464 if (*name == 's' && !script_is_vim9())
6465 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006466 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006467 return FAIL;
6468 }
6469 return OK;
6470}
6471
6472/*
6473 * Callback passed to ex_unletlock().
6474 */
6475 static int
6476compile_unlet(
6477 lval_T *lvp,
6478 char_u *name_end,
6479 exarg_T *eap,
6480 int deep UNUSED,
6481 void *coookie)
6482{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006483 cctx_T *cctx = coookie;
6484 char_u *p = lvp->ll_name;
6485 int cc = *name_end;
6486 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006487
Bram Moolenaar752fc692021-01-04 21:57:11 +01006488 if (cctx->ctx_skip == SKIP_YES)
6489 return OK;
6490
6491 *name_end = NUL;
6492 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006493 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006494 // :unlet $ENV_VAR
6495 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6496 }
6497 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6498 {
6499 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006500
Bram Moolenaar752fc692021-01-04 21:57:11 +01006501 // This is similar to assigning: lookup the list/dict, compile the
6502 // idx/key. Then instead of storing the value unlet the item.
6503 // unlet {list}[idx]
6504 // unlet {dict}[key] dict.key
6505 //
6506 // Figure out the LHS type and other properties.
6507 //
6508 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6509
6510 // : unlet an indexed item
6511 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006512 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006513 iemsg("called compile_lhs() without an index");
6514 ret = FAIL;
6515 }
6516 else
6517 {
6518 // Use the info in "lhs" to unlet the item at the index in the
6519 // list or dict.
6520 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006521 }
6522
Bram Moolenaar752fc692021-01-04 21:57:11 +01006523 vim_free(lhs.lhs_name);
6524 }
6525 else if (check_vim9_unlet(p) == FAIL)
6526 {
6527 ret = FAIL;
6528 }
6529 else
6530 {
6531 // Normal name. Only supports g:, w:, t: and b: namespaces.
6532 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006533 }
6534
Bram Moolenaar752fc692021-01-04 21:57:11 +01006535 *name_end = cc;
6536 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006537}
6538
6539/*
6540 * compile "unlet var", "lock var" and "unlock var"
6541 * "arg" points to "var".
6542 */
6543 static char_u *
6544compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6545{
6546 char_u *p = arg;
6547
6548 if (eap->cmdidx != CMD_unlet)
6549 {
6550 emsg("Sorry, :lock and unlock not implemented yet");
6551 return NULL;
6552 }
6553
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006554 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6555 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006556 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6557}
6558
6559/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560 * Compile an :import command.
6561 */
6562 static char_u *
6563compile_import(char_u *arg, cctx_T *cctx)
6564{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006565 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006566}
6567
6568/*
6569 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6570 */
6571 static int
6572compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6573{
6574 garray_T *instr = &cctx->ctx_instr;
6575 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6576
6577 if (endlabel == NULL)
6578 return FAIL;
6579 endlabel->el_next = *el;
6580 *el = endlabel;
6581 endlabel->el_end_label = instr->ga_len;
6582
6583 generate_JUMP(cctx, when, 0);
6584 return OK;
6585}
6586
6587 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006588compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006589{
6590 garray_T *instr = &cctx->ctx_instr;
6591
6592 while (*el != NULL)
6593 {
6594 endlabel_T *cur = (*el);
6595 isn_T *isn;
6596
6597 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006598 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 *el = cur->el_next;
6600 vim_free(cur);
6601 }
6602}
6603
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006604 static void
6605compile_free_jump_to_end(endlabel_T **el)
6606{
6607 while (*el != NULL)
6608 {
6609 endlabel_T *cur = (*el);
6610
6611 *el = cur->el_next;
6612 vim_free(cur);
6613 }
6614}
6615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616/*
6617 * Create a new scope and set up the generic items.
6618 */
6619 static scope_T *
6620new_scope(cctx_T *cctx, scopetype_T type)
6621{
6622 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6623
6624 if (scope == NULL)
6625 return NULL;
6626 scope->se_outer = cctx->ctx_scope;
6627 cctx->ctx_scope = scope;
6628 scope->se_type = type;
6629 scope->se_local_count = cctx->ctx_locals.ga_len;
6630 return scope;
6631}
6632
6633/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006634 * Free the current scope and go back to the outer scope.
6635 */
6636 static void
6637drop_scope(cctx_T *cctx)
6638{
6639 scope_T *scope = cctx->ctx_scope;
6640
6641 if (scope == NULL)
6642 {
6643 iemsg("calling drop_scope() without a scope");
6644 return;
6645 }
6646 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006647 switch (scope->se_type)
6648 {
6649 case IF_SCOPE:
6650 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6651 case FOR_SCOPE:
6652 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6653 case WHILE_SCOPE:
6654 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6655 case TRY_SCOPE:
6656 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6657 case NO_SCOPE:
6658 case BLOCK_SCOPE:
6659 break;
6660 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006661 vim_free(scope);
6662}
6663
6664/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 * compile "if expr"
6666 *
6667 * "if expr" Produces instructions:
6668 * EVAL expr Push result of "expr"
6669 * JUMP_IF_FALSE end
6670 * ... body ...
6671 * end:
6672 *
6673 * "if expr | else" Produces instructions:
6674 * EVAL expr Push result of "expr"
6675 * JUMP_IF_FALSE else
6676 * ... body ...
6677 * JUMP_ALWAYS end
6678 * else:
6679 * ... body ...
6680 * end:
6681 *
6682 * "if expr1 | elseif expr2 | else" Produces instructions:
6683 * EVAL expr Push result of "expr"
6684 * JUMP_IF_FALSE elseif
6685 * ... body ...
6686 * JUMP_ALWAYS end
6687 * elseif:
6688 * EVAL expr Push result of "expr"
6689 * JUMP_IF_FALSE else
6690 * ... body ...
6691 * JUMP_ALWAYS end
6692 * else:
6693 * ... body ...
6694 * end:
6695 */
6696 static char_u *
6697compile_if(char_u *arg, cctx_T *cctx)
6698{
6699 char_u *p = arg;
6700 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006701 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006702 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006703 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006704 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006705
Bram Moolenaara5565e42020-05-09 15:44:01 +02006706 CLEAR_FIELD(ppconst);
6707 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006708 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006709 clear_ppconst(&ppconst);
6710 return NULL;
6711 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006712 if (!ends_excmd2(arg, skipwhite(p)))
6713 {
6714 semsg(_(e_trailing_arg), p);
6715 return NULL;
6716 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006717 if (cctx->ctx_skip == SKIP_YES)
6718 clear_ppconst(&ppconst);
6719 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006720 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006721 int error = FALSE;
6722 int v;
6723
Bram Moolenaara5565e42020-05-09 15:44:01 +02006724 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006725 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006726 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006727 if (error)
6728 return NULL;
6729 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006730 }
6731 else
6732 {
6733 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006734 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006735 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006736 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006737 if (bool_on_stack(cctx) == FAIL)
6738 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740
6741 scope = new_scope(cctx, IF_SCOPE);
6742 if (scope == NULL)
6743 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006744 scope->se_skip_save = skip_save;
6745 // "is_had_return" will be reset if any block does not end in :return
6746 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006748 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006749 {
6750 // "where" is set when ":elseif", "else" or ":endif" is found
6751 scope->se_u.se_if.is_if_label = instr->ga_len;
6752 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6753 }
6754 else
6755 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006756
Bram Moolenaarced68a02021-01-24 17:53:47 +01006757#ifdef FEAT_PROFILE
6758 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6759 && skip_save != SKIP_YES)
6760 {
6761 // generated a profile start, need to generate a profile end, since it
6762 // won't be done after returning
6763 cctx->ctx_skip = SKIP_NOT;
6764 generate_instr(cctx, ISN_PROF_END);
6765 cctx->ctx_skip = SKIP_YES;
6766 }
6767#endif
6768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006769 return p;
6770}
6771
6772 static char_u *
6773compile_elseif(char_u *arg, cctx_T *cctx)
6774{
6775 char_u *p = arg;
6776 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006777 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 isn_T *isn;
6779 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006780 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006781 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006782
6783 if (scope == NULL || scope->se_type != IF_SCOPE)
6784 {
6785 emsg(_(e_elseif_without_if));
6786 return NULL;
6787 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006788 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006789 if (!cctx->ctx_had_return)
6790 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006791
Bram Moolenaarced68a02021-01-24 17:53:47 +01006792 if (cctx->ctx_skip == SKIP_NOT)
6793 {
6794 // previous block was executed, this one and following will not
6795 cctx->ctx_skip = SKIP_YES;
6796 scope->se_u.se_if.is_seen_skip_not = TRUE;
6797 }
6798 if (scope->se_u.se_if.is_seen_skip_not)
6799 {
6800 // A previous block was executed, skip over expression and bail out.
6801 // Do not count the "elseif" for profiling.
6802#ifdef FEAT_PROFILE
6803 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6804 .isn_type == ISN_PROF_START)
6805 --instr->ga_len;
6806#endif
6807 skip_expr_cctx(&p, cctx);
6808 return p;
6809 }
6810
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006811 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006812 {
6813 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006814 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006815 return NULL;
6816 // previous "if" or "elseif" jumps here
6817 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6818 isn->isn_arg.jump.jump_where = instr->ga_len;
6819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006820
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006821 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006822 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006823 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006824 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006825 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006826#ifdef FEAT_PROFILE
6827 if (cctx->ctx_profiling)
6828 {
6829 // the previous block was skipped, need to profile this line
6830 generate_instr(cctx, ISN_PROF_START);
6831 instr_count = instr->ga_len;
6832 }
6833#endif
6834 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006835 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006836 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006837 clear_ppconst(&ppconst);
6838 return NULL;
6839 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006840 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006841 if (!ends_excmd2(arg, skipwhite(p)))
6842 {
6843 semsg(_(e_trailing_arg), p);
6844 return NULL;
6845 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006846 if (scope->se_skip_save == SKIP_YES)
6847 clear_ppconst(&ppconst);
6848 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006849 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006850 int error = FALSE;
6851 int v;
6852
Bram Moolenaar7f141552020-05-09 17:35:53 +02006853 // The expression results in a constant.
6854 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006855 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6856 if (error)
6857 return NULL;
6858 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006859 clear_ppconst(&ppconst);
6860 scope->se_u.se_if.is_if_label = -1;
6861 }
6862 else
6863 {
6864 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006865 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006866 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006867 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006868 if (bool_on_stack(cctx) == FAIL)
6869 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006870
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006871 // "where" is set when ":elseif", "else" or ":endif" is found
6872 scope->se_u.se_if.is_if_label = instr->ga_len;
6873 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006875
6876 return p;
6877}
6878
6879 static char_u *
6880compile_else(char_u *arg, cctx_T *cctx)
6881{
6882 char_u *p = arg;
6883 garray_T *instr = &cctx->ctx_instr;
6884 isn_T *isn;
6885 scope_T *scope = cctx->ctx_scope;
6886
6887 if (scope == NULL || scope->se_type != IF_SCOPE)
6888 {
6889 emsg(_(e_else_without_if));
6890 return NULL;
6891 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006892 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006893 if (!cctx->ctx_had_return)
6894 scope->se_u.se_if.is_had_return = FALSE;
6895 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006896
Bram Moolenaarced68a02021-01-24 17:53:47 +01006897#ifdef FEAT_PROFILE
6898 if (cctx->ctx_profiling)
6899 {
6900 if (cctx->ctx_skip == SKIP_NOT
6901 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6902 .isn_type == ISN_PROF_START)
6903 // the previous block was executed, do not count "else" for profiling
6904 --instr->ga_len;
6905 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
6906 {
6907 // the previous block was not executed, this one will, do count the
6908 // "else" for profiling
6909 cctx->ctx_skip = SKIP_NOT;
6910 generate_instr(cctx, ISN_PROF_END);
6911 generate_instr(cctx, ISN_PROF_START);
6912 cctx->ctx_skip = SKIP_YES;
6913 }
6914 }
6915#endif
6916
6917 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006918 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006919 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006920 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006921 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006922 if (!cctx->ctx_had_return
6923 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6924 JUMP_ALWAYS, cctx) == FAIL)
6925 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006926 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006927
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006928 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006929 {
6930 if (scope->se_u.se_if.is_if_label >= 0)
6931 {
6932 // previous "if" or "elseif" jumps here
6933 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6934 isn->isn_arg.jump.jump_where = instr->ga_len;
6935 scope->se_u.se_if.is_if_label = -1;
6936 }
6937 }
6938
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006939 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006940 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
6943 return p;
6944}
6945
6946 static char_u *
6947compile_endif(char_u *arg, cctx_T *cctx)
6948{
6949 scope_T *scope = cctx->ctx_scope;
6950 ifscope_T *ifscope;
6951 garray_T *instr = &cctx->ctx_instr;
6952 isn_T *isn;
6953
6954 if (scope == NULL || scope->se_type != IF_SCOPE)
6955 {
6956 emsg(_(e_endif_without_if));
6957 return NULL;
6958 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006959 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006960 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006961 if (!cctx->ctx_had_return)
6962 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006964 if (scope->se_u.se_if.is_if_label >= 0)
6965 {
6966 // previous "if" or "elseif" jumps here
6967 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6968 isn->isn_arg.jump.jump_where = instr->ga_len;
6969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006971 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01006972
6973#ifdef FEAT_PROFILE
6974 // even when skipping we count the endif as executed, unless the block it's
6975 // in is skipped
6976 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6977 && scope->se_skip_save != SKIP_YES)
6978 {
6979 cctx->ctx_skip = SKIP_NOT;
6980 generate_instr(cctx, ISN_PROF_START);
6981 }
6982#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02006983 cctx->ctx_skip = scope->se_skip_save;
6984
6985 // If all the blocks end in :return and there is an :else then the
6986 // had_return flag is set.
6987 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006988
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006989 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990 return arg;
6991}
6992
6993/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006994 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006995 *
6996 * Produces instructions:
6997 * PUSHNR -1
6998 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006999 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007000 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7001 * - if beyond end, jump to "end"
7002 * - otherwise get item from list and push it
7003 * STORE var Store item in "var"
7004 * ... body ...
7005 * JUMP top Jump back to repeat
7006 * end: DROP Drop the result of "expr"
7007 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007008 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7009 * UNPACK 2 Split item in 2
7010 * STORE var1 Store item in "var1"
7011 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 */
7013 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007014compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007015{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007016 char_u *arg;
7017 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007018 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007019 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007020 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007021 int var_count = 0;
7022 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 size_t varlen;
7024 garray_T *instr = &cctx->ctx_instr;
7025 garray_T *stack = &cctx->ctx_type_stack;
7026 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007027 lvar_T *loop_lvar; // loop iteration variable
7028 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007030 type_T *item_type = &t_any;
7031 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032
Bram Moolenaar792f7862020-11-23 08:31:18 +01007033 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007034 if (p == NULL)
7035 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007036 if (var_count == 0)
7037 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038
7039 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007040 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007041 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7042 return NULL;
7043 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 {
7045 emsg(_(e_missing_in));
7046 return NULL;
7047 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007048 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007049 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7050 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 scope = new_scope(cctx, FOR_SCOPE);
7053 if (scope == NULL)
7054 return NULL;
7055
Bram Moolenaar792f7862020-11-23 08:31:18 +01007056 // Reserve a variable to store the loop iteration counter and initialize it
7057 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007058 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7059 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007060 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007061 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007062 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007063 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007064 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007065 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066
7067 // compile "expr", it remains on the stack until "endfor"
7068 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007069 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007070 {
7071 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007073 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007074 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007075
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007076 // Now that we know the type of "var", check that it is a list, now or at
7077 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007079 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007081 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 return NULL;
7083 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007084
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007085 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007086 {
7087 if (var_count == 1)
7088 item_type = vartype->tt_member;
7089 else if (vartype->tt_member->tt_type == VAR_LIST
7090 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7091 item_type = vartype->tt_member->tt_member;
7092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007093
7094 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007095 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007096 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
Bram Moolenaar792f7862020-11-23 08:31:18 +01007098 arg = arg_start;
7099 if (var_count > 1)
7100 {
7101 generate_UNPACK(cctx, var_count, semicolon);
7102 arg = skipwhite(arg + 1); // skip white after '['
7103
7104 // the list item is replaced by a number of items
7105 if (ga_grow(stack, var_count - 1) == FAIL)
7106 {
7107 drop_scope(cctx);
7108 return NULL;
7109 }
7110 --stack->ga_len;
7111 for (idx = 0; idx < var_count; ++idx)
7112 {
7113 ((type_T **)stack->ga_data)[stack->ga_len] =
7114 (semicolon && idx == 0) ? vartype : item_type;
7115 ++stack->ga_len;
7116 }
7117 }
7118
7119 for (idx = 0; idx < var_count; ++idx)
7120 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007121 assign_dest_T dest = dest_local;
7122 int opt_flags = 0;
7123 int vimvaridx = -1;
7124 type_T *type = &t_any;
7125
7126 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007127 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007128 name = vim_strnsave(arg, varlen);
7129 if (name == NULL)
7130 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007131
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007132 // TODO: script var not supported?
7133 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7134 &vimvaridx, &type, cctx) == FAIL)
7135 goto failed;
7136 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007137 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007138 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7139 0, 0, type, name) == FAIL)
7140 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007141 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007142 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007143 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007144 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007145 {
7146 semsg(_(e_variable_already_declared), arg);
7147 goto failed;
7148 }
7149
Bram Moolenaarea870692020-12-02 14:24:30 +01007150 if (STRNCMP(name, "s:", 2) == 0)
7151 {
7152 semsg(_(e_cannot_declare_script_variable_in_function), name);
7153 goto failed;
7154 }
7155
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007156 // Reserve a variable to store "var".
7157 // TODO: check for type
7158 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7159 if (var_lvar == NULL)
7160 // out of memory or used as an argument
7161 goto failed;
7162
7163 if (semicolon && idx == var_count - 1)
7164 var_lvar->lv_type = vartype;
7165 else
7166 var_lvar->lv_type = item_type;
7167 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7168 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007169
Bram Moolenaar036d0712021-01-17 20:23:38 +01007170 if (*p == ':')
7171 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007172 if (*p == ',' || *p == ';')
7173 ++p;
7174 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007175 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007176 }
7177
7178 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007179
7180failed:
7181 vim_free(name);
7182 drop_scope(cctx);
7183 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184}
7185
7186/*
7187 * compile "endfor"
7188 */
7189 static char_u *
7190compile_endfor(char_u *arg, cctx_T *cctx)
7191{
7192 garray_T *instr = &cctx->ctx_instr;
7193 scope_T *scope = cctx->ctx_scope;
7194 forscope_T *forscope;
7195 isn_T *isn;
7196
7197 if (scope == NULL || scope->se_type != FOR_SCOPE)
7198 {
7199 emsg(_(e_for));
7200 return NULL;
7201 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007202 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007203 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007204 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007205
7206 // At end of ":for" scope jump back to the FOR instruction.
7207 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7208
7209 // Fill in the "end" label in the FOR statement so it can jump here
7210 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7211 isn->isn_arg.forloop.for_end = instr->ga_len;
7212
7213 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007214 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007215
7216 // Below the ":for" scope drop the "expr" list from the stack.
7217 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7218 return NULL;
7219
7220 vim_free(scope);
7221
7222 return arg;
7223}
7224
7225/*
7226 * compile "while expr"
7227 *
7228 * Produces instructions:
7229 * top: EVAL expr Push result of "expr"
7230 * JUMP_IF_FALSE end jump if false
7231 * ... body ...
7232 * JUMP top Jump back to repeat
7233 * end:
7234 *
7235 */
7236 static char_u *
7237compile_while(char_u *arg, cctx_T *cctx)
7238{
7239 char_u *p = arg;
7240 garray_T *instr = &cctx->ctx_instr;
7241 scope_T *scope;
7242
7243 scope = new_scope(cctx, WHILE_SCOPE);
7244 if (scope == NULL)
7245 return NULL;
7246
Bram Moolenaarb2049902021-01-24 12:53:53 +01007247 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007248 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007249#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007250 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7251 .isn_type == ISN_PROF_START)
7252 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007253#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
7255 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007256 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007257 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007258 if (!ends_excmd2(arg, skipwhite(p)))
7259 {
7260 semsg(_(e_trailing_arg), p);
7261 return NULL;
7262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007263
Bram Moolenaar13106602020-10-04 16:06:05 +02007264 if (bool_on_stack(cctx) == FAIL)
7265 return FAIL;
7266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007267 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007268 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007269 JUMP_IF_FALSE, cctx) == FAIL)
7270 return FAIL;
7271
7272 return p;
7273}
7274
7275/*
7276 * compile "endwhile"
7277 */
7278 static char_u *
7279compile_endwhile(char_u *arg, cctx_T *cctx)
7280{
7281 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007282 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007283
7284 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7285 {
7286 emsg(_(e_while));
7287 return NULL;
7288 }
7289 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007290 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007291
Bram Moolenaarf002a412021-01-24 13:34:18 +01007292#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007293 // count the endwhile before jumping
7294 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007295#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007297 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007298 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007299
7300 // Fill in the "end" label in the WHILE statement so it can jump here.
7301 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007302 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7303 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007304
7305 vim_free(scope);
7306
7307 return arg;
7308}
7309
7310/*
7311 * compile "continue"
7312 */
7313 static char_u *
7314compile_continue(char_u *arg, cctx_T *cctx)
7315{
7316 scope_T *scope = cctx->ctx_scope;
7317
7318 for (;;)
7319 {
7320 if (scope == NULL)
7321 {
7322 emsg(_(e_continue));
7323 return NULL;
7324 }
7325 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7326 break;
7327 scope = scope->se_outer;
7328 }
7329
7330 // Jump back to the FOR or WHILE instruction.
7331 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007332 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7333 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334 return arg;
7335}
7336
7337/*
7338 * compile "break"
7339 */
7340 static char_u *
7341compile_break(char_u *arg, cctx_T *cctx)
7342{
7343 scope_T *scope = cctx->ctx_scope;
7344 endlabel_T **el;
7345
7346 for (;;)
7347 {
7348 if (scope == NULL)
7349 {
7350 emsg(_(e_break));
7351 return NULL;
7352 }
7353 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7354 break;
7355 scope = scope->se_outer;
7356 }
7357
7358 // Jump to the end of the FOR or WHILE loop.
7359 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007360 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007362 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7364 return FAIL;
7365
7366 return arg;
7367}
7368
7369/*
7370 * compile "{" start of block
7371 */
7372 static char_u *
7373compile_block(char_u *arg, cctx_T *cctx)
7374{
7375 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7376 return NULL;
7377 return skipwhite(arg + 1);
7378}
7379
7380/*
7381 * compile end of block: drop one scope
7382 */
7383 static void
7384compile_endblock(cctx_T *cctx)
7385{
7386 scope_T *scope = cctx->ctx_scope;
7387
7388 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007389 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 vim_free(scope);
7391}
7392
7393/*
7394 * compile "try"
7395 * Creates a new scope for the try-endtry, pointing to the first catch and
7396 * finally.
7397 * Creates another scope for the "try" block itself.
7398 * TRY instruction sets up exception handling at runtime.
7399 *
7400 * "try"
7401 * TRY -> catch1, -> finally push trystack entry
7402 * ... try block
7403 * "throw {exception}"
7404 * EVAL {exception}
7405 * THROW create exception
7406 * ... try block
7407 * " catch {expr}"
7408 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007409 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 * EVAL {expr}
7411 * MATCH
7412 * JUMP nomatch -> catch2
7413 * CATCH remove exception
7414 * ... catch block
7415 * " catch"
7416 * JUMP -> finally
7417 * catch2: CATCH remove exception
7418 * ... catch block
7419 * " finally"
7420 * finally:
7421 * ... finally block
7422 * " endtry"
7423 * ENDTRY pop trystack entry, may rethrow
7424 */
7425 static char_u *
7426compile_try(char_u *arg, cctx_T *cctx)
7427{
7428 garray_T *instr = &cctx->ctx_instr;
7429 scope_T *try_scope;
7430 scope_T *scope;
7431
7432 // scope that holds the jumps that go to catch/finally/endtry
7433 try_scope = new_scope(cctx, TRY_SCOPE);
7434 if (try_scope == NULL)
7435 return NULL;
7436
Bram Moolenaar69f70502021-01-01 16:10:46 +01007437 if (cctx->ctx_skip != SKIP_YES)
7438 {
7439 // "catch" is set when the first ":catch" is found.
7440 // "finally" is set when ":finally" or ":endtry" is found
7441 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7442 if (generate_instr(cctx, ISN_TRY) == NULL)
7443 return NULL;
7444 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007445
7446 // scope for the try block itself
7447 scope = new_scope(cctx, BLOCK_SCOPE);
7448 if (scope == NULL)
7449 return NULL;
7450
7451 return arg;
7452}
7453
7454/*
7455 * compile "catch {expr}"
7456 */
7457 static char_u *
7458compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7459{
7460 scope_T *scope = cctx->ctx_scope;
7461 garray_T *instr = &cctx->ctx_instr;
7462 char_u *p;
7463 isn_T *isn;
7464
7465 // end block scope from :try or :catch
7466 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7467 compile_endblock(cctx);
7468 scope = cctx->ctx_scope;
7469
7470 // Error if not in a :try scope
7471 if (scope == NULL || scope->se_type != TRY_SCOPE)
7472 {
7473 emsg(_(e_catch));
7474 return NULL;
7475 }
7476
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007477 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007479 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 return NULL;
7481 }
7482
Bram Moolenaar69f70502021-01-01 16:10:46 +01007483 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007484 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007485#ifdef FEAT_PROFILE
7486 // the profile-start should be after the jump
7487 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7488 .isn_type == ISN_PROF_START)
7489 --instr->ga_len;
7490#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007491 // Jump from end of previous block to :finally or :endtry
7492 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7493 JUMP_ALWAYS, cctx) == FAIL)
7494 return NULL;
7495
7496 // End :try or :catch scope: set value in ISN_TRY instruction
7497 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7498 if (isn->isn_arg.try.try_catch == 0)
7499 isn->isn_arg.try.try_catch = instr->ga_len;
7500 if (scope->se_u.se_try.ts_catch_label != 0)
7501 {
7502 // Previous catch without match jumps here
7503 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7504 isn->isn_arg.jump.jump_where = instr->ga_len;
7505 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007506#ifdef FEAT_PROFILE
7507 if (cctx->ctx_profiling)
7508 {
7509 // a "throw" that jumps here needs to be counted
7510 generate_instr(cctx, ISN_PROF_END);
7511 // the "catch" is also counted
7512 generate_instr(cctx, ISN_PROF_START);
7513 }
7514#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 }
7516
7517 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007518 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007520 scope->se_u.se_try.ts_caught_all = TRUE;
7521 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 }
7523 else
7524 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007525 char_u *end;
7526 char_u *pat;
7527 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007528 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007529 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007531 // Push v:exception, push {expr} and MATCH
7532 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7533
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007534 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007535 if (*end != *p)
7536 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007537 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007538 vim_free(tofree);
7539 return FAIL;
7540 }
7541 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007542 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007543 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007544 len = (int)(end - tofree);
7545 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007546 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007547 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007548 if (pat == NULL)
7549 return FAIL;
7550 if (generate_PUSHS(cctx, pat) == FAIL)
7551 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007553 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7554 return NULL;
7555
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007556 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7558 return NULL;
7559 }
7560
Bram Moolenaar69f70502021-01-01 16:10:46 +01007561 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007562 return NULL;
7563
7564 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7565 return NULL;
7566 return p;
7567}
7568
7569 static char_u *
7570compile_finally(char_u *arg, cctx_T *cctx)
7571{
7572 scope_T *scope = cctx->ctx_scope;
7573 garray_T *instr = &cctx->ctx_instr;
7574 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007575 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007576
7577 // end block scope from :try or :catch
7578 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7579 compile_endblock(cctx);
7580 scope = cctx->ctx_scope;
7581
7582 // Error if not in a :try scope
7583 if (scope == NULL || scope->se_type != TRY_SCOPE)
7584 {
7585 emsg(_(e_finally));
7586 return NULL;
7587 }
7588
7589 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007590 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007591 if (isn->isn_arg.try.try_finally != 0)
7592 {
7593 emsg(_(e_finally_dup));
7594 return NULL;
7595 }
7596
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007597 this_instr = instr->ga_len;
7598#ifdef FEAT_PROFILE
7599 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7600 .isn_type == ISN_PROF_START)
7601 // jump to the profile start of the "finally"
7602 --this_instr;
7603#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007604
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007605 // Fill in the "end" label in jumps at the end of the blocks.
7606 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7607 this_instr, cctx);
7608
7609 isn->isn_arg.try.try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007610 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 {
7612 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007613 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007614 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007615 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007616 }
7617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007618 // TODO: set index in ts_finally_label jumps
7619
7620 return arg;
7621}
7622
7623 static char_u *
7624compile_endtry(char_u *arg, cctx_T *cctx)
7625{
7626 scope_T *scope = cctx->ctx_scope;
7627 garray_T *instr = &cctx->ctx_instr;
7628 isn_T *isn;
7629
7630 // end block scope from :catch or :finally
7631 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7632 compile_endblock(cctx);
7633 scope = cctx->ctx_scope;
7634
7635 // Error if not in a :try scope
7636 if (scope == NULL || scope->se_type != TRY_SCOPE)
7637 {
7638 if (scope == NULL)
7639 emsg(_(e_no_endtry));
7640 else if (scope->se_type == WHILE_SCOPE)
7641 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007642 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007643 emsg(_(e_endfor));
7644 else
7645 emsg(_(e_endif));
7646 return NULL;
7647 }
7648
Bram Moolenaar69f70502021-01-01 16:10:46 +01007649 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007650 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007651 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7652 if (isn->isn_arg.try.try_catch == 0
7653 && isn->isn_arg.try.try_finally == 0)
7654 {
7655 emsg(_(e_missing_catch_or_finally));
7656 return NULL;
7657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007658
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007659#ifdef FEAT_PROFILE
7660 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7661 .isn_type == ISN_PROF_START)
7662 // move the profile start after "endtry" so that it's not counted when
7663 // the exception is rethrown.
7664 --instr->ga_len;
7665#endif
7666
Bram Moolenaar69f70502021-01-01 16:10:46 +01007667 // Fill in the "end" label in jumps at the end of the blocks, if not
7668 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007669 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7670 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007671
Bram Moolenaar69f70502021-01-01 16:10:46 +01007672 // End :catch or :finally scope: set value in ISN_TRY instruction
7673 if (isn->isn_arg.try.try_catch == 0)
7674 isn->isn_arg.try.try_catch = instr->ga_len;
7675 if (isn->isn_arg.try.try_finally == 0)
7676 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007677
Bram Moolenaar69f70502021-01-01 16:10:46 +01007678 if (scope->se_u.se_try.ts_catch_label != 0)
7679 {
7680 // Last catch without match jumps here
7681 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7682 isn->isn_arg.jump.jump_where = instr->ga_len;
7683 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007684 }
7685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007686 compile_endblock(cctx);
7687
Bram Moolenaar69f70502021-01-01 16:10:46 +01007688 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007689 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007690#ifdef FEAT_PROFILE
7691 if (cctx->ctx_profiling)
7692 generate_instr(cctx, ISN_PROF_START);
7693#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007694 return arg;
7695}
7696
7697/*
7698 * compile "throw {expr}"
7699 */
7700 static char_u *
7701compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7702{
7703 char_u *p = skipwhite(arg);
7704
Bram Moolenaara5565e42020-05-09 15:44:01 +02007705 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007706 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007707 if (cctx->ctx_skip == SKIP_YES)
7708 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007709 if (may_generate_2STRING(-1, cctx) == FAIL)
7710 return NULL;
7711 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7712 return NULL;
7713
7714 return p;
7715}
7716
7717/*
7718 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007719 * compile "echomsg expr"
7720 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007721 * compile "execute expr"
7722 */
7723 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007724compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007725{
7726 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007727 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007728 int count = 0;
7729
7730 for (;;)
7731 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007732 if (ends_excmd2(prev, p))
7733 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007734 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007735 return NULL;
7736 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007737 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007738 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007739 }
7740
Bram Moolenaare4984292020-12-13 14:19:25 +01007741 if (count > 0)
7742 {
7743 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7744 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7745 else if (cmdidx == CMD_execute)
7746 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7747 else if (cmdidx == CMD_echomsg)
7748 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7749 else
7750 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 return p;
7753}
7754
7755/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007756 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007757 * instruction to compute it and return OK.
7758 * Otherwise return FAIL, the caller must deal with any range.
7759 */
7760 static int
7761compile_variable_range(exarg_T *eap, cctx_T *cctx)
7762{
7763 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7764 char_u *p = skipdigits(eap->cmd);
7765
7766 if (p == range_end)
7767 return FAIL;
7768 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7769}
7770
7771/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007772 * :put r
7773 * :put ={expr}
7774 */
7775 static char_u *
7776compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7777{
7778 char_u *line = arg;
7779 linenr_T lnum;
7780 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007781 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007782
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007783 eap->regname = *line;
7784
7785 if (eap->regname == '=')
7786 {
7787 char_u *p = line + 1;
7788
7789 if (compile_expr0(&p, cctx) == FAIL)
7790 return NULL;
7791 line = p;
7792 }
7793 else if (eap->regname != NUL)
7794 ++line;
7795
Bram Moolenaar08597872020-12-10 19:43:40 +01007796 if (compile_variable_range(eap, cctx) == OK)
7797 {
7798 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7799 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007800 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007801 {
7802 // Either no range or a number.
7803 // "errormsg" will not be set because the range is ADDR_LINES.
7804 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007805 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007806 return NULL;
7807 if (eap->addr_count == 0)
7808 lnum = -1;
7809 else
7810 lnum = eap->line2;
7811 if (above)
7812 --lnum;
7813 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007814
7815 generate_PUT(cctx, eap->regname, lnum);
7816 return line;
7817}
7818
7819/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007820 * A command that is not compiled, execute with legacy code.
7821 */
7822 static char_u *
7823compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7824{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007825 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007826 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007827 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007828
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007829 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007830 goto theend;
7831
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007832 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007833 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007834 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007835 int usefilter = FALSE;
7836
7837 has_expr = argt & (EX_XFILE | EX_EXPAND);
7838
7839 // If the command can be followed by a bar, find the bar and truncate
7840 // it, so that the following command can be compiled.
7841 // The '|' is overwritten with a NUL, it is put back below.
7842 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7843 && *eap->arg == '!')
7844 // :w !filter or :r !filter or :r! filter
7845 usefilter = TRUE;
7846 if ((argt & EX_TRLBAR) && !usefilter)
7847 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007848 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007849 separate_nextcmd(eap);
7850 if (eap->nextcmd != NULL)
7851 nextcmd = eap->nextcmd;
7852 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007853 else if (eap->cmdidx == CMD_wincmd)
7854 {
7855 p = eap->arg;
7856 if (*p != NUL)
7857 ++p;
7858 if (*p == 'g' || *p == Ctrl_G)
7859 ++p;
7860 p = skipwhite(p);
7861 if (*p == '|')
7862 {
7863 *p = NUL;
7864 nextcmd = p + 1;
7865 }
7866 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007867 }
7868
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007869 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7870 {
7871 // expand filename in "syntax include [@group] filename"
7872 has_expr = TRUE;
7873 eap->arg = skipwhite(eap->arg + 7);
7874 if (*eap->arg == '@')
7875 eap->arg = skiptowhite(eap->arg);
7876 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007877
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007878 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7879 && STRLEN(eap->arg) > 4)
7880 {
7881 int delim = *eap->arg;
7882
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007883 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007884 if (*p == delim)
7885 {
7886 eap->arg = p + 1;
7887 has_expr = TRUE;
7888 }
7889 }
7890
Bram Moolenaarecac5912021-01-05 19:23:28 +01007891 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7892 {
7893 // TODO: should only expand when appropriate for the command
7894 eap->arg = skiptowhite(eap->arg);
7895 has_expr = TRUE;
7896 }
7897
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007898 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007899 {
7900 int count = 0;
7901 char_u *start = skipwhite(line);
7902
7903 // :cmd xxx`=expr1`yyy`=expr2`zzz
7904 // PUSHS ":cmd xxx"
7905 // eval expr1
7906 // PUSHS "yyy"
7907 // eval expr2
7908 // PUSHS "zzz"
7909 // EXECCONCAT 5
7910 for (;;)
7911 {
7912 if (p > start)
7913 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007914 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007915 ++count;
7916 }
7917 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007918 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007919 return NULL;
7920 may_generate_2STRING(-1, cctx);
7921 ++count;
7922 p = skipwhite(p);
7923 if (*p != '`')
7924 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007925 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007926 return NULL;
7927 }
7928 start = p + 1;
7929
7930 p = (char_u *)strstr((char *)start, "`=");
7931 if (p == NULL)
7932 {
7933 if (*skipwhite(start) != NUL)
7934 {
7935 generate_PUSHS(cctx, vim_strsave(start));
7936 ++count;
7937 }
7938 break;
7939 }
7940 }
7941 generate_EXECCONCAT(cctx, count);
7942 }
7943 else
7944 generate_EXEC(cctx, line);
7945
7946theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007947 if (*nextcmd != NUL)
7948 {
7949 // the parser expects a pointer to the bar, put it back
7950 --nextcmd;
7951 *nextcmd = '|';
7952 }
7953
7954 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007955}
7956
7957/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007958 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007959 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007960 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007961 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007962add_def_function(ufunc_T *ufunc)
7963{
7964 dfunc_T *dfunc;
7965
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007966 if (def_functions.ga_len == 0)
7967 {
7968 // The first position is not used, so that a zero uf_dfunc_idx means it
7969 // wasn't set.
7970 if (ga_grow(&def_functions, 1) == FAIL)
7971 return FAIL;
7972 ++def_functions.ga_len;
7973 }
7974
Bram Moolenaar09689a02020-05-09 22:50:08 +02007975 // Add the function to "def_functions".
7976 if (ga_grow(&def_functions, 1) == FAIL)
7977 return FAIL;
7978 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7979 CLEAR_POINTER(dfunc);
7980 dfunc->df_idx = def_functions.ga_len;
7981 ufunc->uf_dfunc_idx = dfunc->df_idx;
7982 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007983 dfunc->df_name = vim_strsave(ufunc->uf_name);
7984 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007985 ++def_functions.ga_len;
7986 return OK;
7987}
7988
7989/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007990 * After ex_function() has collected all the function lines: parse and compile
7991 * the lines into instructions.
7992 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007993 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7994 * the return statement (used for lambda). When uf_ret_type is already set
7995 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01007996 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007997 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007998 * This can be used recursively through compile_lambda(), which may reallocate
7999 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008000 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008001 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008002 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008003compile_def_function(
8004 ufunc_T *ufunc,
8005 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008006 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008007 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008008{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008009 char_u *line = NULL;
8010 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008011 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 cctx_T cctx;
8013 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008014 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015 int ret = FAIL;
8016 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008017 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008018 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008019 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008020#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008021 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008022#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008023
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008024 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008025 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008026 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008027 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008028 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8029 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008030 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008032 else
8033 {
8034 if (add_def_function(ufunc) == FAIL)
8035 return FAIL;
8036 new_def_function = TRUE;
8037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008038
Bram Moolenaar985116a2020-07-12 17:31:09 +02008039 ufunc->uf_def_status = UF_COMPILING;
8040
Bram Moolenaara80faa82020-04-12 19:37:17 +02008041 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008042
Bram Moolenaarf002a412021-01-24 13:34:18 +01008043#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008044 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008045#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046 cctx.ctx_ufunc = ufunc;
8047 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008048 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008049 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8050 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8051 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8052 cctx.ctx_type_list = &ufunc->uf_type_list;
8053 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8054 instr = &cctx.ctx_instr;
8055
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008056 // Set the context to the function, it may be compiled when called from
8057 // another script. Set the script version to the most modern one.
8058 // The line number will be set in next_line_from_context().
8059 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008060 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8061
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008062 // Make sure error messages are OK.
8063 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8064 if (do_estack_push)
8065 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008066 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008067
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008068 if (ufunc->uf_def_args.ga_len > 0)
8069 {
8070 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008071 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008072 int i;
8073 char_u *arg;
8074 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008075 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008076
8077 // Produce instructions for the default values of optional arguments.
8078 // Store the instruction index in uf_def_arg_idx[] so that we know
8079 // where to start when the function is called, depending on the number
8080 // of arguments.
8081 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8082 if (ufunc->uf_def_arg_idx == NULL)
8083 goto erret;
8084 for (i = 0; i < count; ++i)
8085 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008086 garray_T *stack = &cctx.ctx_type_stack;
8087 type_T *val_type;
8088 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008089 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008090
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008091 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8092 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008093 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008094 goto erret;
8095
8096 // If no type specified use the type of the default value.
8097 // Otherwise check that the default value type matches the
8098 // specified type.
8099 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008100 where.wt_index = arg_idx + 1;
8101 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008102 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008103 {
8104 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008105 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008106 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008107 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008108 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008109 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008110
8111 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008112 goto erret;
8113 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008114 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008115
8116 if (did_set_arg_type)
8117 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008118 }
8119
8120 /*
8121 * Loop over all the lines of the function and generate instructions.
8122 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008123 for (;;)
8124 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008125 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008126 int starts_with_colon = FALSE;
8127 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008128 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008129
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008130 // Bail out on the first error to avoid a flood of errors and report
8131 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008132 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008133 goto erret;
8134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 if (line != NULL && *line == '|')
8136 // the line continues after a '|'
8137 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008138 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008139 && !(*line == '#' && (line == cctx.ctx_line_start
8140 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008141 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008142 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008143 goto erret;
8144 }
8145 else
8146 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008147 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008148 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008149 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008150 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008151#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008152 if (cctx.ctx_skip != SKIP_YES)
8153 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008154#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008156 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008157 }
8158
Bram Moolenaara80faa82020-04-12 19:37:17 +02008159 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008160 ea.cmdlinep = &line;
8161 ea.cmd = skipwhite(line);
8162
Bram Moolenaarb2049902021-01-24 12:53:53 +01008163 if (*ea.cmd == '#')
8164 {
8165 // "#" starts a comment
8166 line = (char_u *)"";
8167 continue;
8168 }
8169
Bram Moolenaarf002a412021-01-24 13:34:18 +01008170#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008171 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8172 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008173 {
8174 may_generate_prof_end(&cctx, prof_lnum);
8175
8176 prof_lnum = cctx.ctx_lnum;
8177 generate_instr(&cctx, ISN_PROF_START);
8178 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008179#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008180
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008181 // Some things can be recognized by the first character.
8182 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008183 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008184 case '}':
8185 {
8186 // "}" ends a block scope
8187 scopetype_T stype = cctx.ctx_scope == NULL
8188 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008190 if (stype == BLOCK_SCOPE)
8191 {
8192 compile_endblock(&cctx);
8193 line = ea.cmd;
8194 }
8195 else
8196 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008197 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008198 goto erret;
8199 }
8200 if (line != NULL)
8201 line = skipwhite(ea.cmd + 1);
8202 continue;
8203 }
8204
8205 case '{':
8206 // "{" starts a block scope
8207 // "{'a': 1}->func() is something else
8208 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8209 {
8210 line = compile_block(ea.cmd, &cctx);
8211 continue;
8212 }
8213 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008214 }
8215
8216 /*
8217 * COMMAND MODIFIERS
8218 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008219 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008220 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8221 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008222 {
8223 if (errormsg != NULL)
8224 goto erret;
8225 // empty line or comment
8226 line = (char_u *)"";
8227 continue;
8228 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008229 generate_cmdmods(&cctx, &local_cmdmod);
8230 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008232 // Check if there was a colon after the last command modifier or before
8233 // the current position.
8234 for (p = ea.cmd; p >= line; --p)
8235 {
8236 if (*p == ':')
8237 starts_with_colon = TRUE;
8238 if (p < ea.cmd && !VIM_ISWHITE(*p))
8239 break;
8240 }
8241
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008243 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008244 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008245 {
8246 if (*ea.cmd == '(')
8247 // not for "call()"
8248 ea.cmd = p;
8249 else
8250 ea.cmd = skipwhite(ea.cmd);
8251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008252
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008253 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008254 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008255 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008256
Bram Moolenaar17126b12021-01-07 22:03:02 +01008257 // Check for assignment after command modifiers.
8258 assign = may_compile_assignment(&ea, &line, &cctx);
8259 if (assign == OK)
8260 goto nextline;
8261 if (assign == FAIL)
8262 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 }
8264
8265 /*
8266 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008267 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008268 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008269 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008270 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008271 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008272 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008273 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008274 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008275 if (!starts_with_colon)
8276 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008277 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008278 goto erret;
8279 }
8280 if (ends_excmd2(line, ea.cmd))
8281 {
8282 // A range without a command: jump to the line.
8283 // TODO: compile to a more efficient command, possibly
8284 // calling parse_cmd_address().
8285 ea.cmdidx = CMD_SIZE;
8286 line = compile_exec(line, &ea, &cctx);
8287 goto nextline;
8288 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008289 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008290 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008291 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008292 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008293 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008295 if (p == NULL)
8296 {
8297 if (cctx.ctx_skip != SKIP_YES)
8298 emsg(_(e_ambiguous_use_of_user_defined_command));
8299 goto erret;
8300 }
8301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008302 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8303 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008304 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008305 {
8306 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008307 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008308 }
8309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008310 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008311 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008312 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008313 // CMD_var cannot happen, compile_assignment() above would be
8314 // used. Most likely an assignment to a non-existing variable.
8315 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008316 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 }
8319
Bram Moolenaar3988f642020-08-27 22:43:03 +02008320 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008321 && ea.cmdidx != CMD_elseif
8322 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008323 && ea.cmdidx != CMD_endif
8324 && ea.cmdidx != CMD_endfor
8325 && ea.cmdidx != CMD_endwhile
8326 && ea.cmdidx != CMD_catch
8327 && ea.cmdidx != CMD_finally
8328 && ea.cmdidx != CMD_endtry)
8329 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008330 emsg(_(e_unreachable_code_after_return));
8331 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008332 }
8333
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008334 p = skipwhite(p);
8335 if (ea.cmdidx != CMD_SIZE
8336 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8337 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008338 if (ea.cmdidx >= 0)
8339 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008340 if ((ea.argt & EX_BANG) && *p == '!')
8341 {
8342 ea.forceit = TRUE;
8343 p = skipwhite(p + 1);
8344 }
8345 }
8346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008347 switch (ea.cmdidx)
8348 {
8349 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008350 ea.arg = p;
8351 line = compile_nested_function(&ea, &cctx);
8352 break;
8353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008355 // TODO: should we allow this, e.g. to declare a global
8356 // function?
8357 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008358 goto erret;
8359
8360 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008361 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008362 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 break;
8364
8365 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008366 emsg(_(e_cannot_use_let_in_vim9_script));
8367 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008368 case CMD_var:
8369 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008370 case CMD_const:
8371 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008372 if (line == p)
8373 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008374 break;
8375
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008376 case CMD_unlet:
8377 case CMD_unlockvar:
8378 case CMD_lockvar:
8379 line = compile_unletlock(p, &ea, &cctx);
8380 break;
8381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 case CMD_import:
8383 line = compile_import(p, &cctx);
8384 break;
8385
8386 case CMD_if:
8387 line = compile_if(p, &cctx);
8388 break;
8389 case CMD_elseif:
8390 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008391 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008392 break;
8393 case CMD_else:
8394 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008395 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008396 break;
8397 case CMD_endif:
8398 line = compile_endif(p, &cctx);
8399 break;
8400
8401 case CMD_while:
8402 line = compile_while(p, &cctx);
8403 break;
8404 case CMD_endwhile:
8405 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008406 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 break;
8408
8409 case CMD_for:
8410 line = compile_for(p, &cctx);
8411 break;
8412 case CMD_endfor:
8413 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008414 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008415 break;
8416 case CMD_continue:
8417 line = compile_continue(p, &cctx);
8418 break;
8419 case CMD_break:
8420 line = compile_break(p, &cctx);
8421 break;
8422
8423 case CMD_try:
8424 line = compile_try(p, &cctx);
8425 break;
8426 case CMD_catch:
8427 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008428 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008429 break;
8430 case CMD_finally:
8431 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008432 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008433 break;
8434 case CMD_endtry:
8435 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008436 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008437 break;
8438 case CMD_throw:
8439 line = compile_throw(p, &cctx);
8440 break;
8441
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008442 case CMD_eval:
8443 if (compile_expr0(&p, &cctx) == FAIL)
8444 goto erret;
8445
Bram Moolenaar3988f642020-08-27 22:43:03 +02008446 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008447 generate_instr_drop(&cctx, ISN_DROP, 1);
8448
8449 line = skipwhite(p);
8450 break;
8451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008452 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008453 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008454 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008455 case CMD_echomsg:
8456 case CMD_echoerr:
8457 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008459
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008460 case CMD_put:
8461 ea.cmd = cmd;
8462 line = compile_put(p, &ea, &cctx);
8463 break;
8464
Bram Moolenaar3988f642020-08-27 22:43:03 +02008465 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008466
Bram Moolenaarae616492020-07-28 20:07:27 +02008467 case CMD_append:
8468 case CMD_change:
8469 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008470 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008471 case CMD_xit:
8472 not_in_vim9(&ea);
8473 goto erret;
8474
Bram Moolenaar002262f2020-07-08 17:47:57 +02008475 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008476 if (cctx.ctx_skip != SKIP_YES)
8477 {
8478 semsg(_(e_invalid_command_str), ea.cmd);
8479 goto erret;
8480 }
8481 // We don't check for a next command here.
8482 line = (char_u *)"";
8483 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008485 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008486 if (cctx.ctx_skip == SKIP_YES)
8487 {
8488 // We don't check for a next command here.
8489 line = (char_u *)"";
8490 }
8491 else
8492 {
8493 // Not recognized, execute with do_cmdline_cmd().
8494 ea.arg = p;
8495 line = compile_exec(line, &ea, &cctx);
8496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008497 break;
8498 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008499nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008500 if (line == NULL)
8501 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008502 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008503
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008504 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008505 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008507 if (cctx.ctx_type_stack.ga_len < 0)
8508 {
8509 iemsg("Type stack underflow");
8510 goto erret;
8511 }
8512 }
8513
8514 if (cctx.ctx_scope != NULL)
8515 {
8516 if (cctx.ctx_scope->se_type == IF_SCOPE)
8517 emsg(_(e_endif));
8518 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8519 emsg(_(e_endwhile));
8520 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8521 emsg(_(e_endfor));
8522 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008523 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524 goto erret;
8525 }
8526
Bram Moolenaarefd88552020-06-18 20:50:10 +02008527 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 {
8529 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8530 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008531 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008532 goto erret;
8533 }
8534
8535 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008536 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008537 }
8538
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008539 {
8540 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8541 + ufunc->uf_dfunc_idx;
8542 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008543 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008544#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008545 if (cctx.ctx_profiling)
8546 {
8547 dfunc->df_instr_prof = instr->ga_data;
8548 dfunc->df_instr_prof_count = instr->ga_len;
8549 }
8550 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008551#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008552 {
8553 dfunc->df_instr = instr->ga_data;
8554 dfunc->df_instr_count = instr->ga_len;
8555 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008556 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008557 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008558 if (cctx.ctx_outer_used)
8559 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008560 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008562
8563 ret = OK;
8564
8565erret:
8566 if (ret == FAIL)
8567 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008568 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008569 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8570 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008571
8572 for (idx = 0; idx < instr->ga_len; ++idx)
8573 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008574 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008575 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008576
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008577 // If using the last entry in the table and it was added above, we
8578 // might as well remove it.
8579 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008580 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008581 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008582 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008583 ufunc->uf_dfunc_idx = 0;
8584 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008585 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008586
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008587 while (cctx.ctx_scope != NULL)
8588 drop_scope(&cctx);
8589
Bram Moolenaar20431c92020-03-20 18:39:46 +01008590 // Don't execute this function body.
8591 ga_clear_strings(&ufunc->uf_lines);
8592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008593 if (errormsg != NULL)
8594 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008595 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008596 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008597 }
8598
8599 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008600 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008601 if (do_estack_push)
8602 estack_pop();
8603
Bram Moolenaar20431c92020-03-20 18:39:46 +01008604 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008605 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008606 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008607 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008608}
8609
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008610 void
8611set_function_type(ufunc_T *ufunc)
8612{
8613 int varargs = ufunc->uf_va_name != NULL;
8614 int argcount = ufunc->uf_args.ga_len;
8615
8616 // Create a type for the function, with the return type and any
8617 // argument types.
8618 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8619 // The type is included in "tt_args".
8620 if (argcount > 0 || varargs)
8621 {
8622 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8623 argcount, &ufunc->uf_type_list);
8624 // Add argument types to the function type.
8625 if (func_type_add_arg_types(ufunc->uf_func_type,
8626 argcount + varargs,
8627 &ufunc->uf_type_list) == FAIL)
8628 return;
8629 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8630 ufunc->uf_func_type->tt_min_argcount =
8631 argcount - ufunc->uf_def_args.ga_len;
8632 if (ufunc->uf_arg_types == NULL)
8633 {
8634 int i;
8635
8636 // lambda does not have argument types.
8637 for (i = 0; i < argcount; ++i)
8638 ufunc->uf_func_type->tt_args[i] = &t_any;
8639 }
8640 else
8641 mch_memmove(ufunc->uf_func_type->tt_args,
8642 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8643 if (varargs)
8644 {
8645 ufunc->uf_func_type->tt_args[argcount] =
8646 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8647 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8648 }
8649 }
8650 else
8651 // No arguments, can use a predefined type.
8652 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8653 argcount, &ufunc->uf_type_list);
8654}
8655
8656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657/*
8658 * Delete an instruction, free what it contains.
8659 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008660 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008661delete_instr(isn_T *isn)
8662{
8663 switch (isn->isn_type)
8664 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008665 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008666 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008667 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008668 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008669 case ISN_LOADENV:
8670 case ISN_LOADG:
8671 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008672 case ISN_LOADT:
8673 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008675 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008677 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008678 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008679 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008680 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008681 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008682 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008683 case ISN_STOREW:
8684 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008685 vim_free(isn->isn_arg.string);
8686 break;
8687
8688 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008689 case ISN_STORES:
8690 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691 break;
8692
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008693 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008694 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008695 vim_free(isn->isn_arg.unlet.ul_name);
8696 break;
8697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008698 case ISN_STOREOPT:
8699 vim_free(isn->isn_arg.storeopt.so_name);
8700 break;
8701
8702 case ISN_PUSHBLOB: // push blob isn_arg.blob
8703 blob_unref(isn->isn_arg.blob);
8704 break;
8705
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008706 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008708 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008709#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008710 break;
8711
8712 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008714 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008715#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008716 break;
8717
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008718 case ISN_UCALL:
8719 vim_free(isn->isn_arg.ufunc.cuf_name);
8720 break;
8721
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008722 case ISN_FUNCREF:
8723 {
8724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8725 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008726 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008727
Bram Moolenaar077a4232020-12-22 18:33:27 +01008728 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8729 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008730 }
8731 break;
8732
8733 case ISN_DCALL:
8734 {
8735 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8736 + isn->isn_arg.dfunc.cdf_idx;
8737
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008738 if (dfunc->df_ufunc != NULL
8739 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008740 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008741 }
8742 break;
8743
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008744 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008745 {
8746 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8747 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8748
8749 if (ufunc != NULL)
8750 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008751 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008752 func_ptr_unref(ufunc);
8753 }
8754
8755 vim_free(lambda);
8756 vim_free(isn->isn_arg.newfunc.nf_global);
8757 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008758 break;
8759
Bram Moolenaar5e654232020-09-16 15:22:00 +02008760 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008761 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008762 free_type(isn->isn_arg.type.ct_type);
8763 break;
8764
Bram Moolenaar02194d22020-10-24 23:08:38 +02008765 case ISN_CMDMOD:
8766 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8767 ->cmod_filter_regmatch.regprog);
8768 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8769 break;
8770
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008771 case ISN_LOADSCRIPT:
8772 case ISN_STORESCRIPT:
8773 vim_free(isn->isn_arg.script.scriptref);
8774 break;
8775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008776 case ISN_2BOOL:
8777 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008778 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008779 case ISN_ADDBLOB:
8780 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008781 case ISN_ANYINDEX:
8782 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008783 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008784 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008785 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008786 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008787 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008788 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008789 case ISN_COMPAREANY:
8790 case ISN_COMPAREBLOB:
8791 case ISN_COMPAREBOOL:
8792 case ISN_COMPAREDICT:
8793 case ISN_COMPAREFLOAT:
8794 case ISN_COMPAREFUNC:
8795 case ISN_COMPARELIST:
8796 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008797 case ISN_COMPARESPECIAL:
8798 case ISN_COMPARESTRING:
8799 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008800 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008801 case ISN_DROP:
8802 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008803 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008804 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008805 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008806 case ISN_EXECCONCAT:
8807 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008808 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008809 case ISN_GETITEM:
8810 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008811 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008812 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008813 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008814 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008815 case ISN_LOADBDICT:
8816 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008817 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008818 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008819 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008820 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008821 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008822 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008823 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008824 case ISN_NEGATENR:
8825 case ISN_NEWDICT:
8826 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008827 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008828 case ISN_OPFLOAT:
8829 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008831 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008832 case ISN_PROF_END:
8833 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008834 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008835 case ISN_PUSHF:
8836 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008837 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008838 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008839 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008840 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008841 case ISN_SHUFFLE:
8842 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008843 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008844 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008845 case ISN_STORENR:
8846 case ISN_STOREOUTER:
8847 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008848 case ISN_STOREV:
8849 case ISN_STRINDEX:
8850 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 case ISN_THROW:
8852 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008853 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008854 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008855 // nothing allocated
8856 break;
8857 }
8858}
8859
8860/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008861 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008862 */
8863 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008864delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008865{
8866 int idx;
8867
8868 ga_clear(&dfunc->df_def_args_isn);
8869
8870 if (dfunc->df_instr != NULL)
8871 {
8872 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8873 delete_instr(dfunc->df_instr + idx);
8874 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008875 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008876 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01008877#ifdef FEAT_PROFILE
8878 if (dfunc->df_instr_prof != NULL)
8879 {
8880 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
8881 delete_instr(dfunc->df_instr_prof + idx);
8882 VIM_CLEAR(dfunc->df_instr_prof);
8883 dfunc->df_instr_prof = NULL;
8884 }
8885#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01008886
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008887 if (mark_deleted)
8888 dfunc->df_deleted = TRUE;
8889 if (dfunc->df_ufunc != NULL)
8890 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008891}
8892
8893/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008894 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008895 * function, unless another user function still uses it.
8896 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008897 */
8898 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008899unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008900{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008901 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008902 {
8903 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8904 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008905
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008906 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008907 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008908 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008909 ufunc->uf_dfunc_idx = 0;
8910 if (dfunc->df_ufunc == ufunc)
8911 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008912 }
8913}
8914
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008915/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008916 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008917 */
8918 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008919link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008920{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008921 if (ufunc->uf_dfunc_idx > 0)
8922 {
8923 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8924 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008925
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008926 ++dfunc->df_refcount;
8927 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008928}
8929
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008930#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008931/*
8932 * Free all functions defined with ":def".
8933 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008934 void
8935free_def_functions(void)
8936{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008937 int idx;
8938
8939 for (idx = 0; idx < def_functions.ga_len; ++idx)
8940 {
8941 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8942
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008943 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008944 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008945 }
8946
8947 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008948}
8949#endif
8950
8951
8952#endif // FEAT_EVAL