blob: 6d0a748824cee6a2841c0ce051feebe3ddf61bab [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}
Bram Moolenaarc150c092021-02-13 15:02:46 +01001595/*
1596 * Generate an ISN_TRYCONT instruction.
1597 */
1598 static int
1599generate_TRYCONT(cctx_T *cctx, int levels, int where)
1600{
1601 isn_T *isn;
1602
1603 RETURN_OK_IF_SKIP(cctx);
1604 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1605 return FAIL;
1606 isn->isn_arg.trycont.tct_levels = levels;
1607 isn->isn_arg.trycont.tct_where = where;
1608
1609 return OK;
1610}
1611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612
1613/*
1614 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001615 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 * Return FAIL if the number of arguments is wrong.
1617 */
1618 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001619generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620{
1621 isn_T *isn;
1622 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001623 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001624 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001625 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626
Bram Moolenaar080457c2020-03-03 21:53:32 +01001627 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001628 argoff = check_internal_func(func_idx, argcount);
1629 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 return FAIL;
1631
Bram Moolenaar389df252020-07-09 21:20:47 +02001632 if (method_call && argoff > 1)
1633 {
1634 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1635 return FAIL;
1636 isn->isn_arg.shuffle.shfl_item = argcount;
1637 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1638 }
1639
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001640 if (argcount > 0)
1641 {
1642 // Check the types of the arguments.
1643 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001644 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1645 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001646 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001647 if (internal_func_is_map(func_idx))
1648 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001649 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1652 return FAIL;
1653 isn->isn_arg.bfunc.cbf_idx = func_idx;
1654 isn->isn_arg.bfunc.cbf_argcount = argcount;
1655
Bram Moolenaar94738d82020-10-21 14:25:07 +02001656 // Drop the argument types and push the return type.
1657 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 if (ga_grow(stack, 1) == FAIL)
1659 return FAIL;
1660 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001661 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001662 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001664 if (maptype != NULL && maptype->tt_member != NULL
1665 && maptype->tt_member != &t_any)
1666 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001667 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 return OK;
1670}
1671
1672/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001673 * Generate an ISN_LISTAPPEND instruction. Works like add().
1674 * Argument count is already checked.
1675 */
1676 static int
1677generate_LISTAPPEND(cctx_T *cctx)
1678{
1679 garray_T *stack = &cctx->ctx_type_stack;
1680 type_T *list_type;
1681 type_T *item_type;
1682 type_T *expected;
1683
1684 // Caller already checked that list_type is a list.
1685 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1686 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1687 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001688 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001689 return FAIL;
1690
1691 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1692 return FAIL;
1693
1694 --stack->ga_len; // drop the argument
1695 return OK;
1696}
1697
1698/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001699 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1700 * Argument count is already checked.
1701 */
1702 static int
1703generate_BLOBAPPEND(cctx_T *cctx)
1704{
1705 garray_T *stack = &cctx->ctx_type_stack;
1706 type_T *item_type;
1707
1708 // Caller already checked that blob_type is a blob.
1709 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001710 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001711 return FAIL;
1712
1713 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1714 return FAIL;
1715
1716 --stack->ga_len; // drop the argument
1717 return OK;
1718}
1719
1720/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001721 * Return TRUE if "ufunc" should be compiled, taking into account whether
1722 * "profile" indicates profiling is to be done.
1723 */
1724 int
Bram Moolenaarf002a412021-01-24 13:34:18 +01001725func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001726{
1727 switch (ufunc->uf_def_status)
1728 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001729 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001730 case UF_TO_BE_COMPILED: return TRUE;
1731 case UF_COMPILED:
1732 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001733#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1735 + ufunc->uf_dfunc_idx;
1736
1737 return profile ? dfunc->df_instr_prof == NULL
1738 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001739#else
1740 break;
1741#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001742 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001743 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001744 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001745 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001746}
1747
1748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 * Generate an ISN_DCALL or ISN_UCALL instruction.
1750 * Return FAIL if the number of arguments is wrong.
1751 */
1752 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001753generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754{
1755 isn_T *isn;
1756 garray_T *stack = &cctx->ctx_type_stack;
1757 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001758 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759
Bram Moolenaar080457c2020-03-03 21:53:32 +01001760 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 if (argcount > regular_args && !has_varargs(ufunc))
1762 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001763 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 return FAIL;
1765 }
1766 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1767 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001768 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 return FAIL;
1770 }
1771
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001772 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001773 {
1774 int i;
1775
1776 for (i = 0; i < argcount; ++i)
1777 {
1778 type_T *expected;
1779 type_T *actual;
1780
1781 if (i < regular_args)
1782 {
1783 if (ufunc->uf_arg_types == NULL)
1784 continue;
1785 expected = ufunc->uf_arg_types[i];
1786 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001787 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1788 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001789 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001790 else
1791 expected = ufunc->uf_va_type->tt_member;
1792 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001793 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001794 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001795 {
1796 arg_type_mismatch(expected, actual, i + 1);
1797 return FAIL;
1798 }
1799 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001800 if (func_needs_compiling(ufunc, PROFILING(ufunc))
Bram Moolenaarb2049902021-01-24 12:53:53 +01001801 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001802 PROFILING(ufunc), NULL) == FAIL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001803 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001804 }
1805
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001807 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001808 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001810 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 {
1812 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1813 isn->isn_arg.dfunc.cdf_argcount = argcount;
1814 }
1815 else
1816 {
1817 // A user function may be deleted and redefined later, can't use the
1818 // ufunc pointer, need to look it up again at runtime.
1819 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1820 isn->isn_arg.ufunc.cuf_argcount = argcount;
1821 }
1822
1823 stack->ga_len -= argcount; // drop the arguments
1824 if (ga_grow(stack, 1) == FAIL)
1825 return FAIL;
1826 // add return value
1827 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1828 ++stack->ga_len;
1829
1830 return OK;
1831}
1832
1833/*
1834 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1835 */
1836 static int
1837generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1838{
1839 isn_T *isn;
1840 garray_T *stack = &cctx->ctx_type_stack;
1841
Bram Moolenaar080457c2020-03-03 21:53:32 +01001842 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1844 return FAIL;
1845 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1846 isn->isn_arg.ufunc.cuf_argcount = argcount;
1847
1848 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001849 if (ga_grow(stack, 1) == FAIL)
1850 return FAIL;
1851 // add return value
1852 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1853 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854
1855 return OK;
1856}
1857
1858/*
1859 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001860 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861 */
1862 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001863generate_PCALL(
1864 cctx_T *cctx,
1865 int argcount,
1866 char_u *name,
1867 type_T *type,
1868 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869{
1870 isn_T *isn;
1871 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001872 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873
Bram Moolenaar080457c2020-03-03 21:53:32 +01001874 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001875
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001876 if (type->tt_type == VAR_ANY)
1877 ret_type = &t_any;
1878 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001879 {
1880 if (type->tt_argcount != -1)
1881 {
1882 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1883
1884 if (argcount < type->tt_min_argcount - varargs)
1885 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001886 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001887 return FAIL;
1888 }
1889 if (!varargs && argcount > type->tt_argcount)
1890 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001891 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001892 return FAIL;
1893 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001894 if (type->tt_args != NULL)
1895 {
1896 int i;
1897
1898 for (i = 0; i < argcount; ++i)
1899 {
1900 int offset = -argcount + i - 1;
1901 type_T *actual = ((type_T **)stack->ga_data)[
1902 stack->ga_len + offset];
1903 type_T *expected;
1904
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001905 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001906 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001907 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001908 else
1909 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001910 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001911 cctx, TRUE, FALSE) == FAIL)
1912 {
1913 arg_type_mismatch(expected, actual, i + 1);
1914 return FAIL;
1915 }
1916 }
1917 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001918 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001919 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001920 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001921 else
1922 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001923 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001924 return FAIL;
1925 }
1926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1928 return FAIL;
1929 isn->isn_arg.pfunc.cpf_top = at_top;
1930 isn->isn_arg.pfunc.cpf_argcount = argcount;
1931
1932 stack->ga_len -= argcount; // drop the arguments
1933
1934 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001935 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001937 // If partial is above the arguments it must be cleared and replaced with
1938 // the return value.
1939 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1940 return FAIL;
1941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 return OK;
1943}
1944
1945/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001946 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 */
1948 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001949generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950{
1951 isn_T *isn;
1952 garray_T *stack = &cctx->ctx_type_stack;
1953 type_T *type;
1954
Bram Moolenaar080457c2020-03-03 21:53:32 +01001955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001956 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001958 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001960 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001962 if (type->tt_type != VAR_DICT && type != &t_any)
1963 {
1964 emsg(_(e_dictreq));
1965 return FAIL;
1966 }
1967 // change dict type to dict member type
1968 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001969 {
1970 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1971 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973
1974 return OK;
1975}
1976
1977/*
1978 * Generate an ISN_ECHO instruction.
1979 */
1980 static int
1981generate_ECHO(cctx_T *cctx, int with_white, int count)
1982{
1983 isn_T *isn;
1984
Bram Moolenaar080457c2020-03-03 21:53:32 +01001985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1987 return FAIL;
1988 isn->isn_arg.echo.echo_with_white = with_white;
1989 isn->isn_arg.echo.echo_count = count;
1990
1991 return OK;
1992}
1993
Bram Moolenaarad39c092020-02-26 18:23:43 +01001994/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001995 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001996 */
1997 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001998generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001999{
2000 isn_T *isn;
2001
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002002 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002003 return FAIL;
2004 isn->isn_arg.number = count;
2005
2006 return OK;
2007}
2008
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002009/*
2010 * Generate an ISN_PUT instruction.
2011 */
2012 static int
2013generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2014{
2015 isn_T *isn;
2016
2017 RETURN_OK_IF_SKIP(cctx);
2018 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2019 return FAIL;
2020 isn->isn_arg.put.put_regname = regname;
2021 isn->isn_arg.put.put_lnum = lnum;
2022 return OK;
2023}
2024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 static int
2026generate_EXEC(cctx_T *cctx, char_u *line)
2027{
2028 isn_T *isn;
2029
Bram Moolenaar080457c2020-03-03 21:53:32 +01002030 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2032 return FAIL;
2033 isn->isn_arg.string = vim_strsave(line);
2034 return OK;
2035}
2036
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002037 static int
2038generate_EXECCONCAT(cctx_T *cctx, int count)
2039{
2040 isn_T *isn;
2041
2042 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.number = count;
2045 return OK;
2046}
2047
Bram Moolenaar08597872020-12-10 19:43:40 +01002048/*
2049 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2050 */
2051 static int
2052generate_RANGE(cctx_T *cctx, char_u *range)
2053{
2054 isn_T *isn;
2055 garray_T *stack = &cctx->ctx_type_stack;
2056
2057 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2058 return FAIL;
2059 isn->isn_arg.string = range;
2060
2061 if (ga_grow(stack, 1) == FAIL)
2062 return FAIL;
2063 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2064 ++stack->ga_len;
2065 return OK;
2066}
2067
Bram Moolenaar792f7862020-11-23 08:31:18 +01002068 static int
2069generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2070{
2071 isn_T *isn;
2072
2073 RETURN_OK_IF_SKIP(cctx);
2074 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.unpack.unp_count = var_count;
2077 isn->isn_arg.unpack.unp_semicolon = semicolon;
2078 return OK;
2079}
2080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002082 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002083 */
2084 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002085generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002086{
2087 isn_T *isn;
2088
Bram Moolenaar02194d22020-10-24 23:08:38 +02002089 if (cmod->cmod_flags != 0
2090 || cmod->cmod_split != 0
2091 || cmod->cmod_verbose != 0
2092 || cmod->cmod_tab != 0
2093 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002094 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002095 cctx->ctx_has_cmdmod = TRUE;
2096
2097 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002098 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002099 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2100 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2101 return FAIL;
2102 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002103 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002104 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002105 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002106
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002107 return OK;
2108}
2109
2110 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002111generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002112{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002113 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2114 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002115 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002116 return OK;
2117}
2118
Bram Moolenaarf002a412021-01-24 13:34:18 +01002119#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002120 static void
2121may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2122{
2123 if (cctx->ctx_profiling && prof_lnum >= 0)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002124 generate_instr(cctx, ISN_PROF_END);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002125}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002126#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002127
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002128/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002130 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002132 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002133reserve_local(
2134 cctx_T *cctx,
2135 char_u *name,
2136 size_t len,
2137 int isConst,
2138 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 lvar_T *lvar;
2141
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002142 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002144 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002145 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 }
2147
2148 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002149 return NULL;
2150 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002151 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002152
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002153 // Every local variable uses the next entry on the stack. We could re-use
2154 // the last ones when leaving a scope, but then variables used in a closure
2155 // might get overwritten. To keep things simple do not re-use stack
2156 // entries. This is less efficient, but memory is cheap these days.
2157 lvar->lv_idx = cctx->ctx_locals_count++;
2158
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002159 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 lvar->lv_const = isConst;
2161 lvar->lv_type = type;
2162
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002163 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164}
2165
2166/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002167 * Remove local variables above "new_top".
2168 */
2169 static void
2170unwind_locals(cctx_T *cctx, int new_top)
2171{
2172 if (cctx->ctx_locals.ga_len > new_top)
2173 {
2174 int idx;
2175 lvar_T *lvar;
2176
2177 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2178 {
2179 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2180 vim_free(lvar->lv_name);
2181 }
2182 }
2183 cctx->ctx_locals.ga_len = new_top;
2184}
2185
2186/*
2187 * Free all local variables.
2188 */
2189 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002190free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002191{
2192 unwind_locals(cctx, 0);
2193 ga_clear(&cctx->ctx_locals);
2194}
2195
2196/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002197 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2198 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2199 * error if the variable was defined with :const.
2200 */
2201 static int
2202check_item_writable(svar_T *sv, int check_writable, char_u *name)
2203{
2204 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2205 || (check_writable == ASSIGN_FINAL
2206 && sv->sv_const == ASSIGN_CONST))
2207 {
2208 semsg(_(e_readonlyvar), name);
2209 return FAIL;
2210 }
2211 return OK;
2212}
2213
2214/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002216 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 * Returns the index in "sn_var_vals" if found.
2218 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002219 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 */
2221 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002222get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223{
2224 hashtab_T *ht;
2225 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002226 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002227 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 int idx;
2229
Bram Moolenaare3d46852020-08-29 13:39:17 +02002230 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002232 if (sid == current_sctx.sc_sid)
2233 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002234 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002235
2236 if (sav == NULL)
2237 return -2;
2238 idx = sav->sav_var_vals_idx;
2239 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002240 if (check_item_writable(sv, check_writable, name) == FAIL)
2241 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002242 return idx;
2243 }
2244
2245 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 ht = &SCRIPT_VARS(sid);
2247 di = find_var_in_ht(ht, 0, name, TRUE);
2248 if (di == NULL)
2249 return -2;
2250
2251 // Now find the svar_T index in sn_var_vals.
2252 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2253 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002254 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 if (sv->sv_tv == &di->di_tv)
2256 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002257 if (check_item_writable(sv, check_writable, name) == FAIL)
2258 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 return idx;
2260 }
2261 }
2262 return -1;
2263}
2264
2265/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002266 * Find "name" in imported items of the current script or in "cctx" if not
2267 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 */
2269 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002270find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 int idx;
2273
Bram Moolenaare3d46852020-08-29 13:39:17 +02002274 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002275 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 if (cctx != NULL)
2277 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2278 {
2279 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2280 + idx;
2281
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002282 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2283 : STRLEN(import->imp_name) == len
2284 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 return import;
2286 }
2287
Bram Moolenaarefa94442020-08-08 22:16:00 +02002288 return find_imported_in_script(name, len, current_sctx.sc_sid);
2289}
2290
2291 imported_T *
2292find_imported_in_script(char_u *name, size_t len, int sid)
2293{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002294 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002295 int idx;
2296
Bram Moolenaare3d46852020-08-29 13:39:17 +02002297 if (!SCRIPT_ID_VALID(sid))
2298 return NULL;
2299 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2301 {
2302 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2303
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002304 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2305 : STRLEN(import->imp_name) == len
2306 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 return import;
2308 }
2309 return NULL;
2310}
2311
2312/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002313 * Free all imported variables.
2314 */
2315 static void
2316free_imported(cctx_T *cctx)
2317{
2318 int idx;
2319
2320 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2321 {
2322 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2323
2324 vim_free(import->imp_name);
2325 }
2326 ga_clear(&cctx->ctx_imports);
2327}
2328
2329/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002330 * Return a pointer to the next line that isn't empty or only contains a
2331 * comment. Skips over white space.
2332 * Returns NULL if there is none.
2333 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002334 char_u *
2335peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002336{
2337 int lnum = cctx->ctx_lnum;
2338
2339 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2340 {
2341 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002342 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002343
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002344 // ignore NULLs inserted for continuation lines
2345 if (line != NULL)
2346 {
2347 p = skipwhite(line);
2348 if (*p != NUL && !vim9_comment_start(p))
2349 return p;
2350 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002351 }
2352 return NULL;
2353}
2354
2355/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002356 * Called when checking for a following operator at "arg". When the rest of
2357 * the line is empty or only a comment, peek the next line. If there is a next
2358 * line return a pointer to it and set "nextp".
2359 * Otherwise skip over white space.
2360 */
2361 static char_u *
2362may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2363{
2364 char_u *p = skipwhite(arg);
2365
2366 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002367 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002368 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002369 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002370 if (*nextp != NULL)
2371 return *nextp;
2372 }
2373 return p;
2374}
2375
2376/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002377 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002378 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002379 * Returns NULL when at the end.
2380 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002381 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002382next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002383{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002384 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002385
2386 do
2387 {
2388 ++cctx->ctx_lnum;
2389 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002390 {
2391 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002392 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002393 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002394 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002395 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002396 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002397 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002398 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002399 return line;
2400}
2401
2402/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002403 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002404 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002405 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002406 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2407 */
2408 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002409may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002410{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002411 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002412 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002413 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002414 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002415
2416 if (next == NULL)
2417 return FAIL;
2418 *arg = skipwhite(next);
2419 }
2420 return OK;
2421}
2422
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002423/*
2424 * Idem, and give an error when failed.
2425 */
2426 static int
2427may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2428{
2429 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2430 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002431 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002432 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002433 return FAIL;
2434 }
2435 return OK;
2436}
2437
2438
Bram Moolenaara5565e42020-05-09 15:44:01 +02002439// Structure passed between the compile_expr* functions to keep track of
2440// constants that have been parsed but for which no code was produced yet. If
2441// possible expressions on these constants are applied at compile time. If
2442// that is not possible, the code to push the constants needs to be generated
2443// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002444// Using 50 should be more than enough of 5 levels of ().
2445#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002446typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002447 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002448 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002449 int pp_is_const; // all generated code was constants, used for a
2450 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002451} ppconst_T;
2452
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002453static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002454static int compile_expr0(char_u **arg, cctx_T *cctx);
2455static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2456
Bram Moolenaara5565e42020-05-09 15:44:01 +02002457/*
2458 * Generate a PUSH instruction for "tv".
2459 * "tv" will be consumed or cleared.
2460 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2461 */
2462 static int
2463generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2464{
2465 if (tv != NULL)
2466 {
2467 switch (tv->v_type)
2468 {
2469 case VAR_UNKNOWN:
2470 break;
2471 case VAR_BOOL:
2472 generate_PUSHBOOL(cctx, tv->vval.v_number);
2473 break;
2474 case VAR_SPECIAL:
2475 generate_PUSHSPEC(cctx, tv->vval.v_number);
2476 break;
2477 case VAR_NUMBER:
2478 generate_PUSHNR(cctx, tv->vval.v_number);
2479 break;
2480#ifdef FEAT_FLOAT
2481 case VAR_FLOAT:
2482 generate_PUSHF(cctx, tv->vval.v_float);
2483 break;
2484#endif
2485 case VAR_BLOB:
2486 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2487 tv->vval.v_blob = NULL;
2488 break;
2489 case VAR_STRING:
2490 generate_PUSHS(cctx, tv->vval.v_string);
2491 tv->vval.v_string = NULL;
2492 break;
2493 default:
2494 iemsg("constant type not supported");
2495 clear_tv(tv);
2496 return FAIL;
2497 }
2498 tv->v_type = VAR_UNKNOWN;
2499 }
2500 return OK;
2501}
2502
2503/*
2504 * Generate code for any ppconst entries.
2505 */
2506 static int
2507generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2508{
2509 int i;
2510 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002511 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002512
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002513 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002514 for (i = 0; i < ppconst->pp_used; ++i)
2515 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2516 ret = FAIL;
2517 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002518 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002519 return ret;
2520}
2521
2522/*
2523 * Clear ppconst constants. Used when failing.
2524 */
2525 static void
2526clear_ppconst(ppconst_T *ppconst)
2527{
2528 int i;
2529
2530 for (i = 0; i < ppconst->pp_used; ++i)
2531 clear_tv(&ppconst->pp_tv[i]);
2532 ppconst->pp_used = 0;
2533}
2534
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002535/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002536 * Generate an instruction to load script-local variable "name", without the
2537 * leading "s:".
2538 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 */
2540 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002541compile_load_scriptvar(
2542 cctx_T *cctx,
2543 char_u *name, // variable NUL terminated
2544 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002545 char_u **end, // end of variable
2546 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002548 scriptitem_T *si;
2549 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 imported_T *import;
2551
Bram Moolenaare3d46852020-08-29 13:39:17 +02002552 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2553 return FAIL;
2554 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002555 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002556 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002558 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002559 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2560 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562 if (idx >= 0)
2563 {
2564 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2565
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002566 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 current_sctx.sc_sid, idx, sv->sv_type);
2568 return OK;
2569 }
2570
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002571 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 if (import != NULL)
2573 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002574 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002575 {
2576 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002577 char_u *exp_name;
2578 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002579 ufunc_T *ufunc;
2580 type_T *type;
2581
2582 // Used "import * as Name", need to lookup the member.
2583 if (*p != '.')
2584 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002585 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002586 return FAIL;
2587 }
2588 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002589 if (VIM_ISWHITE(*p))
2590 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002591 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002592 return FAIL;
2593 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002594
Bram Moolenaar1c991142020-07-04 13:15:31 +02002595 // isolate one name
2596 exp_name = p;
2597 while (eval_isnamec(*p))
2598 ++p;
2599 cc = *p;
2600 *p = NUL;
2601
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002602 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002603 *p = cc;
2604 p = skipwhite(p);
2605
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002606 // TODO: what if it is a function?
2607 if (idx < 0)
2608 return FAIL;
2609 *end = p;
2610
2611 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2612 import->imp_sid,
2613 idx,
2614 type);
2615 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002616 else if (import->imp_funcname != NULL)
2617 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002618 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002619 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2620 import->imp_sid,
2621 import->imp_var_vals_idx,
2622 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 return OK;
2624 }
2625
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002626 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002627 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 return FAIL;
2629}
2630
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002631 static int
2632generate_funcref(cctx_T *cctx, char_u *name)
2633{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002634 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002635
2636 if (ufunc == NULL)
2637 return FAIL;
2638
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002639 // Need to compile any default values to get the argument types.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01002640 if (func_needs_compiling(ufunc, PROFILING(ufunc))
2641 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002642 == FAIL)
2643 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002644 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002645}
2646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647/*
2648 * Compile a variable name into a load instruction.
2649 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002650 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 * When "error" is FALSE do not give an error when not found.
2652 */
2653 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002654compile_load(
2655 char_u **arg,
2656 char_u *end_arg,
2657 cctx_T *cctx,
2658 int is_expr,
2659 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660{
2661 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002662 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002663 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002665 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666
2667 if (*(*arg + 1) == ':')
2668 {
2669 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002670 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002671 {
2672 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002674 switch (**arg)
2675 {
2676 case 'g': isn_type = ISN_LOADGDICT; break;
2677 case 'w': isn_type = ISN_LOADWDICT; break;
2678 case 't': isn_type = ISN_LOADTDICT; break;
2679 case 'b': isn_type = ISN_LOADBDICT; break;
2680 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002681 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002682 goto theend;
2683 }
2684 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2685 goto theend;
2686 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 else
2689 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002690 isntype_T isn_type = ISN_DROP;
2691
2692 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2693 if (name == NULL)
2694 return FAIL;
2695
2696 switch (**arg)
2697 {
2698 case 'v': res = generate_LOADV(cctx, name, error);
2699 break;
2700 case 's': res = compile_load_scriptvar(cctx, name,
2701 NULL, NULL, error);
2702 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002703 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2704 isn_type = ISN_LOADG;
2705 else
2706 {
2707 isn_type = ISN_LOADAUTO;
2708 vim_free(name);
2709 name = vim_strnsave(*arg, end - *arg);
2710 if (name == NULL)
2711 return FAIL;
2712 }
2713 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002714 case 'w': isn_type = ISN_LOADW; break;
2715 case 't': isn_type = ISN_LOADT; break;
2716 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002717 default: // cannot happen, just in case
2718 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002719 goto theend;
2720 }
2721 if (isn_type != ISN_DROP)
2722 {
2723 // Global, Buffer-local, Window-local and Tabpage-local
2724 // variables can be defined later, thus we don't check if it
2725 // exists, give error at runtime.
2726 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 }
2730 else
2731 {
2732 size_t len = end - *arg;
2733 int idx;
2734 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002735 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736
2737 name = vim_strnsave(*arg, end - *arg);
2738 if (name == NULL)
2739 return FAIL;
2740
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002741 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002743 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002744 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 }
2746 else
2747 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002748 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002749
Bram Moolenaar709664c2020-12-12 14:33:41 +01002750 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002752 type = lvar.lv_type;
2753 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002754 if (lvar.lv_from_outer != 0)
2755 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002756 else
2757 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759 else
2760 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002761 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002762 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002763 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002764 || find_imported(name, 0, cctx) != NULL)
2765 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002766
Bram Moolenaar0f769812020-09-12 18:32:34 +02002767 // When evaluating an expression and the name starts with an
2768 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002769 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002770 if (res == FAIL && is_expr
2771 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002772 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 }
2774 }
2775 if (gen_load)
2776 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002777 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002778 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002779 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002780 cctx->ctx_outer_used = TRUE;
2781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 }
2783
2784 *arg = end;
2785
2786theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002787 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002788 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 vim_free(name);
2790 return res;
2791}
2792
2793/*
2794 * Compile the argument expressions.
2795 * "arg" points to just after the "(" and is advanced to after the ")"
2796 */
2797 static int
2798compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2799{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002800 char_u *p = *arg;
2801 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002802 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
Bram Moolenaare6085c52020-04-12 20:19:16 +02002804 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002806 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2807 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002808 if (*p == ')')
2809 {
2810 *arg = p + 1;
2811 return OK;
2812 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002813 if (must_end)
2814 {
2815 semsg(_(e_missing_comma_before_argument_str), p);
2816 return FAIL;
2817 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002818
Bram Moolenaara5565e42020-05-09 15:44:01 +02002819 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 return FAIL;
2821 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002822
2823 if (*p != ',' && *skipwhite(p) == ',')
2824 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01002825 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002826 p = skipwhite(p);
2827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002829 {
2830 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002831 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01002832 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002833 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002834 else
2835 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002836 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002837 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002839failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002840 emsg(_(e_missing_close));
2841 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842}
2843
2844/*
2845 * Compile a function call: name(arg1, arg2)
2846 * "arg" points to "name", "arg + varlen" to the "(".
2847 * "argcount_init" is 1 for "value->method()"
2848 * Instructions:
2849 * EVAL arg1
2850 * EVAL arg2
2851 * BCALL / DCALL / UCALL
2852 */
2853 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002854compile_call(
2855 char_u **arg,
2856 size_t varlen,
2857 cctx_T *cctx,
2858 ppconst_T *ppconst,
2859 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860{
2861 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002862 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 int argcount = argcount_init;
2864 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002865 char_u fname_buf[FLEN_FIXED + 1];
2866 char_u *tofree = NULL;
2867 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002868 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002869 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002870 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871
Bram Moolenaara5565e42020-05-09 15:44:01 +02002872 // we can evaluate "has('name')" at compile time
2873 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2874 {
2875 char_u *s = skipwhite(*arg + varlen + 1);
2876 typval_T argvars[2];
2877
2878 argvars[0].v_type = VAR_UNKNOWN;
2879 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002880 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002881 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002882 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002883 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002884 if (*s == ')' && argvars[0].v_type == VAR_STRING
2885 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002886 {
2887 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2888
2889 *arg = s + 1;
2890 argvars[1].v_type = VAR_UNKNOWN;
2891 tv->v_type = VAR_NUMBER;
2892 tv->vval.v_number = 0;
2893 f_has(argvars, tv);
2894 clear_tv(&argvars[0]);
2895 ++ppconst->pp_used;
2896 return OK;
2897 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002898 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002899 }
2900
2901 if (generate_ppconst(cctx, ppconst) == FAIL)
2902 return FAIL;
2903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 if (varlen >= sizeof(namebuf))
2905 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002906 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 return FAIL;
2908 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002909 vim_strncpy(namebuf, *arg, varlen);
2910 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911
2912 *arg = skipwhite(*arg + varlen + 1);
2913 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002914 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915
Bram Moolenaar03290b82020-12-19 16:30:44 +01002916 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002917 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 {
2919 int idx;
2920
2921 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002922 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002924 {
Bram Moolenaar3b690062021-02-01 20:14:51 +01002925 if (STRCMP(name, "flatten") == 0)
2926 {
2927 emsg(_(e_cannot_use_flatten_in_vim9_script));
2928 goto theend;
2929 }
2930
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002931 if (STRCMP(name, "add") == 0 && argcount == 2)
2932 {
2933 garray_T *stack = &cctx->ctx_type_stack;
2934 type_T *type = ((type_T **)stack->ga_data)[
2935 stack->ga_len - 2];
2936
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002937 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002938 if (type->tt_type == VAR_LIST)
2939 {
2940 // inline "add(list, item)" so that the type can be checked
2941 res = generate_LISTAPPEND(cctx);
2942 idx = -1;
2943 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002944 else if (type->tt_type == VAR_BLOB)
2945 {
2946 // inline "add(blob, nr)" so that the type can be checked
2947 res = generate_BLOBAPPEND(cctx);
2948 idx = -1;
2949 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002950 }
2951
2952 if (idx >= 0)
2953 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2954 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002955 else
2956 semsg(_(e_unknownfunc), namebuf);
2957 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 }
2959
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002960 // An argument or local variable can be a function reference, this
2961 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002962 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002963 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002964 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002965 // If we can find the function by name generate the right call.
2966 // Skip global functions here, a local funcref takes precedence.
2967 ufunc = find_func(name, FALSE, cctx);
2968 if (ufunc != NULL && !func_is_global(ufunc))
2969 {
2970 res = generate_CALL(cctx, ufunc, argcount);
2971 goto theend;
2972 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974
2975 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002976 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002977 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002979 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002980 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002981 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002982 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002983 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002984
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002985 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002986 goto theend;
2987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988
Bram Moolenaar0f769812020-09-12 18:32:34 +02002989 // If we can find a global function by name generate the right call.
2990 if (ufunc != NULL)
2991 {
2992 res = generate_CALL(cctx, ufunc, argcount);
2993 goto theend;
2994 }
2995
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002996 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002997 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002998 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002999 res = generate_UCALL(cctx, name, argcount);
3000 else
3001 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01003002
3003theend:
3004 vim_free(tofree);
3005 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006}
3007
3008// like NAMESPACE_CHAR but with 'a' and 'l'.
3009#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
3010
3011/*
3012 * Find the end of a variable or function name. Unlike find_name_end() this
3013 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003014 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 * Return a pointer to just after the name. Equal to "arg" if there is no
3016 * valid name.
3017 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01003018 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003019to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020{
3021 char_u *p;
3022
3023 // Quick check for valid starting character.
3024 if (!eval_isnamec1(*arg))
3025 return arg;
3026
3027 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3028 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3029 // and can be used in slice "[n:]".
3030 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003031 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3033 break;
3034 return p;
3035}
3036
3037/*
3038 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003039 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 */
3041 char_u *
3042to_name_const_end(char_u *arg)
3043{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003044 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 typval_T rettv;
3046
3047 if (p == arg && *arg == '[')
3048 {
3049
3050 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003051 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 p = arg;
3053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 return p;
3055}
3056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057/*
3058 * parse a list: [expr, expr]
3059 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003060 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 */
3062 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003063compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064{
3065 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003066 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003068 int is_const;
3069 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003073 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003074 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003075 semsg(_(e_list_end), *arg);
3076 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003077 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003078 if (*p == ',')
3079 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003080 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
Bram Moolenaardb199212020-08-12 18:01:53 +02003081 return FAIL;
3082 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003083 if (*p == ']')
3084 {
3085 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003086 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003087 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003088 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003089 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003090 if (!is_const)
3091 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 ++count;
3093 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003094 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003096 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3097 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003098 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003099 return FAIL;
3100 }
3101 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003102 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 p = skipwhite(p);
3104 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003105 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003107 ppconst->pp_is_const = is_all_const;
3108 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109}
3110
3111/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003112 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003114 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 */
3116 static int
3117compile_lambda(char_u **arg, cctx_T *cctx)
3118{
Bram Moolenaare462f522020-12-27 14:43:30 +01003119 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 typval_T rettv;
3121 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003122 evalarg_T evalarg;
3123
3124 CLEAR_FIELD(evalarg);
3125 evalarg.eval_flags = EVAL_EVALUATE;
3126 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127
3128 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003129 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3130 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003131 {
3132 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003133 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003134 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003135
Bram Moolenaar65c44152020-12-24 15:14:01 +01003136 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003138 ++ufunc->uf_refcount;
3139 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
Bram Moolenaar65c44152020-12-24 15:14:01 +01003141 // Compile the function into instructions.
Bram Moolenaare5ea3462021-01-25 21:01:48 +01003142 compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003144 clear_evalarg(&evalarg, NULL);
3145
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003146 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003147 {
3148 // The return type will now be known.
3149 set_function_type(ufunc);
3150
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003151 // The function reference count will be 1. When the ISN_FUNCREF
3152 // instruction is deleted the reference count is decremented and the
3153 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003154 return generate_FUNCREF(cctx, ufunc);
3155 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003156
3157 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 return FAIL;
3159}
3160
3161/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003162 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003164 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 */
3166 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003167compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168{
3169 garray_T *instr = &cctx->ctx_instr;
3170 int count = 0;
3171 dict_T *d = dict_alloc();
3172 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003173 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003174 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003175 int is_const;
3176 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
3178 if (d == NULL)
3179 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003180 if (generate_ppconst(cctx, ppconst) == FAIL)
3181 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003182 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003184 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185
Bram Moolenaar23c55272020-06-21 16:58:13 +02003186 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003187 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003188 *arg = NULL;
3189 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003190 }
3191
3192 if (**arg == '}')
3193 break;
3194
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003195 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003197 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003199 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003200 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003201 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003204 if (isn->isn_type == ISN_PUSHNR)
3205 {
3206 char buf[NUMBUFLEN];
3207
3208 // Convert to string at compile time.
3209 vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
3210 isn->isn_type = ISN_PUSHS;
3211 isn->isn_arg.string = vim_strsave((char_u *)buf);
3212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 if (isn->isn_type == ISN_PUSHS)
3214 key = isn->isn_arg.string;
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003215 else if (may_generate_2STRING(-1, cctx) == FAIL)
3216 return FAIL;
Bram Moolenaare0de1712020-12-02 17:36:54 +01003217 *arg = skipwhite(*arg);
3218 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003219 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003220 emsg(_(e_missing_matching_bracket_after_dict_key));
3221 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003222 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003223 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003225 else
3226 {
3227 // {"name": value},
3228 // {'name': value},
3229 // {name: value} use "name" as a literal key
3230 key = get_literal_key(arg);
3231 if (key == NULL)
3232 return FAIL;
3233 if (generate_PUSHS(cctx, key) == FAIL)
3234 return FAIL;
3235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236
3237 // Check for duplicate keys, if using string keys.
3238 if (key != NULL)
3239 {
3240 item = dict_find(d, key, -1);
3241 if (item != NULL)
3242 {
3243 semsg(_(e_duplicate_key), key);
3244 goto failret;
3245 }
3246 item = dictitem_alloc(key);
3247 if (item != NULL)
3248 {
3249 item->di_tv.v_type = VAR_UNKNOWN;
3250 item->di_tv.v_lock = 0;
3251 if (dict_add(d, item) == FAIL)
3252 dictitem_free(item);
3253 }
3254 }
3255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 if (**arg != ':')
3257 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003258 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003259 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003260 else
3261 semsg(_(e_missing_dict_colon), *arg);
3262 return FAIL;
3263 }
3264 whitep = *arg + 1;
3265 if (!IS_WHITE_OR_NUL(*whitep))
3266 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003267 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 return FAIL;
3269 }
3270
Bram Moolenaar23c55272020-06-21 16:58:13 +02003271 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003272 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003273 *arg = NULL;
3274 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003275 }
3276
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003277 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003279 if (!is_const)
3280 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 ++count;
3282
Bram Moolenaar2c330432020-04-13 14:41:35 +02003283 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003284 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003285 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003286 *arg = NULL;
3287 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 if (**arg == '}')
3290 break;
3291 if (**arg != ',')
3292 {
3293 semsg(_(e_missing_dict_comma), *arg);
3294 goto failret;
3295 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003296 if (IS_WHITE_OR_NUL(*whitep))
3297 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003298 semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003299 return FAIL;
3300 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003301 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003302 if (!IS_WHITE_OR_NUL(*whitep))
3303 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003304 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003305 return FAIL;
3306 }
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01003307 *arg = skipwhite(whitep);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 }
3309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 *arg = *arg + 1;
3311
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003312 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003313 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003314 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003315 *arg += STRLEN(*arg);
3316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003318 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 return generate_NEWDICT(cctx, count);
3320
3321failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003322 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003323 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003324 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003325 *arg = (char_u *)"";
3326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 dict_unref(d);
3328 return FAIL;
3329}
3330
3331/*
3332 * Compile "&option".
3333 */
3334 static int
3335compile_get_option(char_u **arg, cctx_T *cctx)
3336{
3337 typval_T rettv;
3338 char_u *start = *arg;
3339 int ret;
3340
3341 // parse the option and get the current value to get the type.
3342 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003343 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 if (ret == OK)
3345 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003346 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003347 char_u *name = vim_strnsave(start, *arg - start);
3348 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3349 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350
3351 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3352 vim_free(name);
3353 }
3354 clear_tv(&rettv);
3355
3356 return ret;
3357}
3358
3359/*
3360 * Compile "$VAR".
3361 */
3362 static int
3363compile_get_env(char_u **arg, cctx_T *cctx)
3364{
3365 char_u *start = *arg;
3366 int len;
3367 int ret;
3368 char_u *name;
3369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 ++*arg;
3371 len = get_env_len(arg);
3372 if (len == 0)
3373 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003374 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 return FAIL;
3376 }
3377
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003378 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 name = vim_strnsave(start, len + 1);
3380 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3381 vim_free(name);
3382 return ret;
3383}
3384
3385/*
3386 * Compile "@r".
3387 */
3388 static int
3389compile_get_register(char_u **arg, cctx_T *cctx)
3390{
3391 int ret;
3392
3393 ++*arg;
3394 if (**arg == NUL)
3395 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003396 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 return FAIL;
3398 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003399 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 {
3401 emsg_invreg(**arg);
3402 return FAIL;
3403 }
3404 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3405 ++*arg;
3406 return ret;
3407}
3408
3409/*
3410 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003411 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 */
3413 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003414apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003416 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
3418 // this works from end to start
3419 while (p > start)
3420 {
3421 --p;
3422 if (*p == '-' || *p == '+')
3423 {
3424 // only '-' has an effect, for '+' we only check the type
3425#ifdef FEAT_FLOAT
3426 if (rettv->v_type == VAR_FLOAT)
3427 {
3428 if (*p == '-')
3429 rettv->vval.v_float = -rettv->vval.v_float;
3430 }
3431 else
3432#endif
3433 {
3434 varnumber_T val;
3435 int error = FALSE;
3436
3437 // tv_get_number_chk() accepts a string, but we don't want that
3438 // here
3439 if (check_not_string(rettv) == FAIL)
3440 return FAIL;
3441 val = tv_get_number_chk(rettv, &error);
3442 clear_tv(rettv);
3443 if (error)
3444 return FAIL;
3445 if (*p == '-')
3446 val = -val;
3447 rettv->v_type = VAR_NUMBER;
3448 rettv->vval.v_number = val;
3449 }
3450 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003451 else if (numeric_only)
3452 {
3453 ++p;
3454 break;
3455 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003456 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 {
3458 int v = tv2bool(rettv);
3459
3460 // '!' is permissive in the type.
3461 clear_tv(rettv);
3462 rettv->v_type = VAR_BOOL;
3463 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3464 }
3465 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003466 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 return OK;
3468}
3469
3470/*
3471 * Recognize v: variables that are constants and set "rettv".
3472 */
3473 static void
3474get_vim_constant(char_u **arg, typval_T *rettv)
3475{
3476 if (STRNCMP(*arg, "v:true", 6) == 0)
3477 {
3478 rettv->v_type = VAR_BOOL;
3479 rettv->vval.v_number = VVAL_TRUE;
3480 *arg += 6;
3481 }
3482 else if (STRNCMP(*arg, "v:false", 7) == 0)
3483 {
3484 rettv->v_type = VAR_BOOL;
3485 rettv->vval.v_number = VVAL_FALSE;
3486 *arg += 7;
3487 }
3488 else if (STRNCMP(*arg, "v:null", 6) == 0)
3489 {
3490 rettv->v_type = VAR_SPECIAL;
3491 rettv->vval.v_number = VVAL_NULL;
3492 *arg += 6;
3493 }
3494 else if (STRNCMP(*arg, "v:none", 6) == 0)
3495 {
3496 rettv->v_type = VAR_SPECIAL;
3497 rettv->vval.v_number = VVAL_NONE;
3498 *arg += 6;
3499 }
3500}
3501
Bram Moolenaar657137c2021-01-09 15:45:23 +01003502 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003503get_compare_type(char_u *p, int *len, int *type_is)
3504{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003505 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003506 int i;
3507
3508 switch (p[0])
3509 {
3510 case '=': if (p[1] == '=')
3511 type = EXPR_EQUAL;
3512 else if (p[1] == '~')
3513 type = EXPR_MATCH;
3514 break;
3515 case '!': if (p[1] == '=')
3516 type = EXPR_NEQUAL;
3517 else if (p[1] == '~')
3518 type = EXPR_NOMATCH;
3519 break;
3520 case '>': if (p[1] != '=')
3521 {
3522 type = EXPR_GREATER;
3523 *len = 1;
3524 }
3525 else
3526 type = EXPR_GEQUAL;
3527 break;
3528 case '<': if (p[1] != '=')
3529 {
3530 type = EXPR_SMALLER;
3531 *len = 1;
3532 }
3533 else
3534 type = EXPR_SEQUAL;
3535 break;
3536 case 'i': if (p[1] == 's')
3537 {
3538 // "is" and "isnot"; but not a prefix of a name
3539 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3540 *len = 5;
3541 i = p[*len];
3542 if (!isalnum(i) && i != '_')
3543 {
3544 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3545 *type_is = TRUE;
3546 }
3547 }
3548 break;
3549 }
3550 return type;
3551}
3552
3553/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003554 * Skip over an expression, ignoring most errors.
3555 */
3556 static void
3557skip_expr_cctx(char_u **arg, cctx_T *cctx)
3558{
3559 evalarg_T evalarg;
3560
3561 CLEAR_FIELD(evalarg);
3562 evalarg.eval_cctx = cctx;
3563 skip_expr(arg, &evalarg);
3564}
3565
3566/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003568 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 */
3570 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003571compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003573 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574
3575 // this works from end to start
3576 while (p > start)
3577 {
3578 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003579 while (VIM_ISWHITE(*p))
3580 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 if (*p == '-' || *p == '+')
3582 {
3583 int negate = *p == '-';
3584 isn_T *isn;
3585
3586 // TODO: check type
3587 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3588 {
3589 --p;
3590 if (*p == '-')
3591 negate = !negate;
3592 }
3593 // only '-' has an effect, for '+' we only check the type
3594 if (negate)
3595 isn = generate_instr(cctx, ISN_NEGATENR);
3596 else
3597 isn = generate_instr(cctx, ISN_CHECKNR);
3598 if (isn == NULL)
3599 return FAIL;
3600 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003601 else if (numeric_only)
3602 {
3603 ++p;
3604 break;
3605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 else
3607 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003608 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003610 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003612 if (p[-1] == '!')
3613 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 }
3616 if (generate_2BOOL(cctx, invert) == FAIL)
3617 return FAIL;
3618 }
3619 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003620 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 return OK;
3622}
3623
3624/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003625 * Compile "(expression)": recursive!
3626 * Return FAIL/OK.
3627 */
3628 static int
3629compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3630{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003631 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003632 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003633
Bram Moolenaar24156692021-01-14 20:35:49 +01003634 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3635 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003636 if (ppconst->pp_used <= PPSIZE - 10)
3637 {
3638 ret = compile_expr1(arg, cctx, ppconst);
3639 }
3640 else
3641 {
3642 // Not enough space in ppconst, flush constants.
3643 if (generate_ppconst(cctx, ppconst) == FAIL)
3644 return FAIL;
3645 ret = compile_expr0(arg, cctx);
3646 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003647 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3648 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003649 if (**arg == ')')
3650 ++*arg;
3651 else if (ret == OK)
3652 {
3653 emsg(_(e_missing_close));
3654 ret = FAIL;
3655 }
3656 return ret;
3657}
3658
3659/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003661 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 */
3663 static int
3664compile_subscript(
3665 char_u **arg,
3666 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003667 char_u *start_leader,
3668 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003669 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003671 char_u *name_start = *end_leader;
3672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 for (;;)
3674 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003675 char_u *p = skipwhite(*arg);
3676
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003677 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003678 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003679 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003680
3681 // If a following line starts with "->{" or "->X" advance to that
3682 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003683 // Also if a following line starts with ".x".
3684 if (next != NULL &&
3685 ((next[0] == '-' && next[1] == '>'
3686 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003687 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003688 {
3689 next = next_line_from_context(cctx, TRUE);
3690 if (next == NULL)
3691 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003692 *arg = next;
3693 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003694 }
3695 }
3696
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003697 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003698 // not a function call.
3699 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003701 garray_T *stack = &cctx->ctx_type_stack;
3702 type_T *type;
3703 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704
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 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003710 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3711
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003712 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3714 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003715 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 return FAIL;
3717 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003718 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003720 char_u *pstart = p;
3721
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003722 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003723 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003724 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 // something->method()
3727 // Apply the '!', '-' and '+' first:
3728 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003729 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003732 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003733 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003734 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003735 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003736 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003737 int argcount = 1;
3738 char_u *expr;
3739 garray_T *stack;
3740 type_T *type;
3741
3742 // Funcref call: list->(Refs[2])(arg)
3743 // or lambda: list->((arg) => expr)(arg)
3744 // Fist compile the arguments.
3745 expr = *arg;
3746 *arg = skipwhite(*arg + 1);
3747 skip_expr_cctx(arg, cctx);
3748 *arg = skipwhite(*arg);
3749 if (**arg != ')')
3750 {
3751 semsg(_(e_missing_paren), *arg);
3752 return FAIL;
3753 }
3754 ++*arg;
3755 if (**arg != '(')
3756 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003757 if (*skipwhite(*arg) == '(')
3758 emsg(_(e_nowhitespace));
3759 else
3760 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003761 return FAIL;
3762 }
3763
3764 *arg = skipwhite(*arg + 1);
3765 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3766 return FAIL;
3767
3768 // Compile the function expression.
3769 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3770 return FAIL;
3771
3772 stack = &cctx->ctx_type_stack;
3773 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3774 if (generate_PCALL(cctx, argcount,
3775 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 return FAIL;
3777 }
3778 else
3779 {
3780 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003781 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003782 if (!eval_isnamec1(*p))
3783 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003784 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003785 return FAIL;
3786 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003787 if (ASCII_ISALPHA(*p) && p[1] == ':')
3788 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003789 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 ;
3791 if (*p != '(')
3792 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003793 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 return FAIL;
3795 }
3796 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003797 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 return FAIL;
3799 }
3800 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003801 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003803 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003804 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003805 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003806 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003807 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003808
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003809 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003810 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003811 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003812 // TODO: blob index
3813 // TODO: more arguments
3814 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003815 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003816 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003817 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003818
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003819 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003820 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003821 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003822 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003823 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003824 // missing first index is equal to zero
3825 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003826 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003827 else
3828 {
3829 if (compile_expr0(arg, cctx) == FAIL)
3830 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003831 if (**arg == ':')
3832 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003833 semsg(_(e_white_space_required_before_and_after_str_at_str),
3834 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003835 return FAIL;
3836 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003837 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003838 return FAIL;
3839 *arg = skipwhite(*arg);
3840 }
3841 if (**arg == ':')
3842 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003843 is_slice = TRUE;
3844 ++*arg;
3845 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3846 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003847 semsg(_(e_white_space_required_before_and_after_str_at_str),
3848 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003849 return FAIL;
3850 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003851 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003852 return FAIL;
3853 if (**arg == ']')
3854 // missing second index is equal to end of string
3855 generate_PUSHNR(cctx, -1);
3856 else
3857 {
3858 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003859 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003860 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003861 return FAIL;
3862 *arg = skipwhite(*arg);
3863 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
3866 if (**arg != ']')
3867 {
3868 emsg(_(e_missbrac));
3869 return FAIL;
3870 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003871 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 // We can index a list and a dict. If we don't know the type
3874 // we can use the index value type.
3875 // TODO: If we don't know use an instruction to figure it out at
3876 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003877 typep = ((type_T **)stack->ga_data) + stack->ga_len
3878 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003879 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003880 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3881 // If the index is a string, the variable must be a Dict.
3882 if (*typep == &t_any && valtype == &t_string)
3883 vtype = VAR_DICT;
3884 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003885 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003886 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003887 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003888 return FAIL;
3889 if (is_slice)
3890 {
3891 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003892 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003893 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003894 return FAIL;
3895 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003896 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003897
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003898 if (vtype == VAR_DICT)
3899 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003900 if (is_slice)
3901 {
3902 emsg(_(e_cannot_slice_dictionary));
3903 return FAIL;
3904 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003905 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003906 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003907 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003908 if (*typep == &t_unknown)
3909 // empty dict was used
3910 *typep = &t_any;
3911 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003912 else
3913 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003914 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003915 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003916 return FAIL;
3917 *typep = &t_any;
3918 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003919 if (may_generate_2STRING(-1, cctx) == FAIL)
3920 return FAIL;
3921 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3922 return FAIL;
3923 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003924 else if (vtype == VAR_STRING)
3925 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003926 *typep = &t_string;
3927 if ((is_slice
3928 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3929 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003930 return FAIL;
3931 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003932 else if (vtype == VAR_BLOB)
3933 {
3934 emsg("Sorry, blob index and slice not implemented yet");
3935 return FAIL;
3936 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003937 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003938 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003939 if (is_slice)
3940 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003941 if (generate_instr_drop(cctx,
3942 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3943 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003944 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003945 }
Bram Moolenaared591872020-08-15 22:14:53 +02003946 else
3947 {
3948 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003949 {
Bram Moolenaared591872020-08-15 22:14:53 +02003950 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003951 if (*typep == &t_unknown)
3952 // empty list was used
3953 *typep = &t_any;
3954 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003955 if (generate_instr_drop(cctx,
3956 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3957 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003958 return FAIL;
3959 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003960 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003961 else
3962 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003963 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003964 return FAIL;
3965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003967 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003969 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003970 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003971 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003972 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003973
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003974 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003975 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003976 {
3977 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003978 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003979 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003980 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003981 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 while (eval_isnamec(*p))
3983 MB_PTR_ADV(p);
3984 if (p == *arg)
3985 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003986 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 return FAIL;
3988 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003989 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 return FAIL;
3991 *arg = p;
3992 }
3993 else
3994 break;
3995 }
3996
3997 // TODO - see handle_subscript():
3998 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3999 // Don't do this when "Func" is already a partial that was bound
4000 // explicitly (pt_auto is FALSE).
4001
4002 return OK;
4003}
4004
4005/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004006 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
4007 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004009 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004010 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004011 *
4012 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 */
4014
4015/*
4016 * number number constant
4017 * 0zFFFFFFFF Blob constant
4018 * "string" string constant
4019 * 'string' literal string constant
4020 * &option-name option value
4021 * @r register contents
4022 * identifier variable value
4023 * function() function call
4024 * $VAR environment variable
4025 * (expression) nested expression
4026 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004027 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 *
4029 * Also handle:
4030 * ! in front logical NOT
4031 * - in front unary minus
4032 * + in front unary plus (ignored)
4033 * trailing (arg) funcref/partial call
4034 * trailing [] subscript in String or List
4035 * trailing .name entry in Dictionary
4036 * trailing ->name() method call
4037 */
4038 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004039compile_expr7(
4040 char_u **arg,
4041 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004042 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 char_u *start_leader, *end_leader;
4045 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004046 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004047 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004049 ppconst->pp_is_const = FALSE;
4050
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 /*
4052 * Skip '!', '-' and '+' characters. They are handled later.
4053 */
4054 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004055 if (eval_leader(arg, TRUE) == FAIL)
4056 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 end_leader = *arg;
4058
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004059 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 switch (**arg)
4061 {
4062 /*
4063 * Number constant.
4064 */
4065 case '0': // also for blob starting with 0z
4066 case '1':
4067 case '2':
4068 case '3':
4069 case '4':
4070 case '5':
4071 case '6':
4072 case '7':
4073 case '8':
4074 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004075 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004077 // Apply "-" and "+" just before the number now, right to
4078 // left. Matters especially when "->" follows. Stops at
4079 // '!'.
4080 if (apply_leader(rettv, TRUE,
4081 start_leader, &end_leader) == FAIL)
4082 {
4083 clear_tv(rettv);
4084 return FAIL;
4085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 break;
4087
4088 /*
4089 * String constant: "string".
4090 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004091 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 return FAIL;
4093 break;
4094
4095 /*
4096 * Literal string constant: 'str''ing'.
4097 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004098 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 return FAIL;
4100 break;
4101
4102 /*
4103 * Constant Vim variable.
4104 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004105 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 ret = NOTDONE;
4107 break;
4108
4109 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004110 * "true" constant
4111 */
4112 case 't': if (STRNCMP(*arg, "true", 4) == 0
4113 && !eval_isnamec((*arg)[4]))
4114 {
4115 *arg += 4;
4116 rettv->v_type = VAR_BOOL;
4117 rettv->vval.v_number = VVAL_TRUE;
4118 }
4119 else
4120 ret = NOTDONE;
4121 break;
4122
4123 /*
4124 * "false" constant
4125 */
4126 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4127 && !eval_isnamec((*arg)[5]))
4128 {
4129 *arg += 5;
4130 rettv->v_type = VAR_BOOL;
4131 rettv->vval.v_number = VVAL_FALSE;
4132 }
4133 else
4134 ret = NOTDONE;
4135 break;
4136
4137 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004138 * "null" constant
4139 */
4140 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4141 && !eval_isnamec((*arg)[5]))
4142 {
4143 *arg += 4;
4144 rettv->v_type = VAR_SPECIAL;
4145 rettv->vval.v_number = VVAL_NULL;
4146 }
4147 else
4148 ret = NOTDONE;
4149 break;
4150
4151 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 * List: [expr, expr]
4153 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004154 case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
4155 return FAIL;
4156 ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 break;
4158
4159 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 * Dictionary: {'key': val, 'key': val}
4161 */
Bram Moolenaare507ff12021-01-31 21:47:42 +01004162 case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
4163 return FAIL;
4164 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 break;
4166
4167 /*
4168 * Option value: &name
4169 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004170 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4171 return FAIL;
4172 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 break;
4174
4175 /*
4176 * Environment variable: $VAR.
4177 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004178 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4179 return FAIL;
4180 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 break;
4182
4183 /*
4184 * Register contents: @r.
4185 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004186 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4187 return FAIL;
4188 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 break;
4190 /*
4191 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004192 * lambda: (arg, arg) => expr
4193 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004195 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4196 ret = compile_lambda(arg, cctx);
4197 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004198 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 break;
4200
4201 default: ret = NOTDONE;
4202 break;
4203 }
4204 if (ret == FAIL)
4205 return FAIL;
4206
Bram Moolenaar1c747212020-05-09 18:28:34 +02004207 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004209 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004210 clear_tv(rettv);
4211 else
4212 // A constant expression can possibly be handled compile time,
4213 // return the value instead of generating code.
4214 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 }
4216 else if (ret == NOTDONE)
4217 {
4218 char_u *p;
4219 int r;
4220
4221 if (!eval_isnamec1(**arg))
4222 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004223 if (ends_excmd(*skipwhite(*arg)))
4224 semsg(_(e_empty_expression_str), *arg);
4225 else
4226 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 return FAIL;
4228 }
4229
4230 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004231 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004233 {
4234 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004237 {
4238 if (generate_ppconst(cctx, ppconst) == FAIL)
4239 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004240 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 if (r == FAIL)
4243 return FAIL;
4244 }
4245
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004246 // Handle following "[]", ".member", etc.
4247 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004248 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004249 ppconst) == FAIL)
4250 return FAIL;
4251 if (ppconst->pp_used > 0)
4252 {
4253 // apply the '!', '-' and '+' before the constant
4254 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004255 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004256 return FAIL;
4257 return OK;
4258 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004259 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004261 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262}
4263
4264/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004265 * Give the "white on both sides" error, taking the operator from "p[len]".
4266 */
4267 void
4268error_white_both(char_u *op, int len)
4269{
4270 char_u buf[10];
4271
4272 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004273 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004274}
4275
4276/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004277 * <type>expr7: runtime type check / conversion
4278 */
4279 static int
4280compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4281{
4282 type_T *want_type = NULL;
4283
4284 // Recognize <type>
4285 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4286 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004287 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004288 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4289 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004290 return FAIL;
4291
4292 if (**arg != '>')
4293 {
4294 if (*skipwhite(*arg) == '>')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004295 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004296 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004297 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004298 return FAIL;
4299 }
4300 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004301 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004302 return FAIL;
4303 }
4304
4305 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4306 return FAIL;
4307
4308 if (want_type != NULL)
4309 {
4310 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004311 type_T *actual;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004312 where_T where;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004313
Bram Moolenaard1103582020-08-14 22:44:25 +02004314 generate_ppconst(cctx, ppconst);
4315 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004316 where.wt_index = 0;
4317 where.wt_variable = FALSE;
4318 if (check_type(want_type, actual, FALSE, where) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004319 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004320 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004321 return FAIL;
4322 }
4323 }
4324
4325 return OK;
4326}
4327
4328/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 * * number multiplication
4330 * / number division
4331 * % number modulo
4332 */
4333 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004334compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335{
4336 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004337 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004338 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004341 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 return FAIL;
4343
4344 /*
4345 * Repeat computing, until no "*", "/" or "%" is following.
4346 */
4347 for (;;)
4348 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004349 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 if (*op != '*' && *op != '/' && *op != '%')
4351 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004352 if (next != NULL)
4353 {
4354 *arg = next_line_from_context(cctx, TRUE);
4355 op = skipwhite(*arg);
4356 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004357
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004358 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004360 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004363 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004364 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004365
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004366 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004367 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004369
4370 if (ppconst->pp_used == ppconst_used + 2
4371 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4372 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004373 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004374 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4375 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4376 varnumber_T res = 0;
4377 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004379 // both are numbers: compute the result
4380 switch (*op)
4381 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004382 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004383 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004384 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004385 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004386 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004387 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004388 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004389 break;
4390 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004391 if (failed)
4392 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004393 tv1->vval.v_number = res;
4394 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004395 }
4396 else
4397 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004398 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004399 generate_two_op(cctx, op);
4400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 }
4402
4403 return OK;
4404}
4405
4406/*
4407 * + number addition
4408 * - number subtraction
4409 * .. string concatenation
4410 */
4411 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413{
4414 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004415 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004417 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418
4419 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004420 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 return FAIL;
4422
4423 /*
4424 * Repeat computing, until no "+", "-" or ".." is following.
4425 */
4426 for (;;)
4427 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004428 op = may_peek_next_line(cctx, *arg, &next);
4429 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 break;
4431 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004432 if (next != NULL)
4433 {
4434 *arg = next_line_from_context(cctx, TRUE);
4435 op = skipwhite(*arg);
4436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004438 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004440 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004441 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 }
4443
Bram Moolenaare0de1712020-12-02 17:36:54 +01004444 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004445 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004447 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004448 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 return FAIL;
4450
Bram Moolenaara5565e42020-05-09 15:44:01 +02004451 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004452 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004453 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4454 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4455 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4456 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4459 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004460
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004461 // concat/subtract/add constant numbers
4462 if (*op == '+')
4463 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4464 else if (*op == '-')
4465 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4466 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004467 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004468 // concatenate constant strings
4469 char_u *s1 = tv1->vval.v_string;
4470 char_u *s2 = tv2->vval.v_string;
4471 size_t len1 = STRLEN(s1);
4472
4473 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4474 if (tv1->vval.v_string == NULL)
4475 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004476 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004477 return FAIL;
4478 }
4479 mch_memmove(tv1->vval.v_string, s1, len1);
4480 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004481 vim_free(s1);
4482 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004483 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004484 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 }
4486 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004487 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004488 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004489 if (*op == '.')
4490 {
4491 if (may_generate_2STRING(-2, cctx) == FAIL
4492 || may_generate_2STRING(-1, cctx) == FAIL)
4493 return FAIL;
4494 generate_instr_drop(cctx, ISN_CONCAT, 1);
4495 }
4496 else
4497 generate_two_op(cctx, op);
4498 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 }
4500
4501 return OK;
4502}
4503
4504/*
4505 * expr5a == expr5b
4506 * expr5a =~ expr5b
4507 * expr5a != expr5b
4508 * expr5a !~ expr5b
4509 * expr5a > expr5b
4510 * expr5a >= expr5b
4511 * expr5a < expr5b
4512 * expr5a <= expr5b
4513 * expr5a is expr5b
4514 * expr5a isnot expr5b
4515 *
4516 * Produces instructions:
4517 * EVAL expr5a Push result of "expr5a"
4518 * EVAL expr5b Push result of "expr5b"
4519 * COMPARE one of the compare instructions
4520 */
4521 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004522compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004524 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004526 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004529 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530
4531 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004532 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 return FAIL;
4534
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004535 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004536 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537
4538 /*
4539 * If there is a comparative operator, use it.
4540 */
4541 if (type != EXPR_UNKNOWN)
4542 {
4543 int ic = FALSE; // Default: do not ignore case
4544
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004545 if (next != NULL)
4546 {
4547 *arg = next_line_from_context(cctx, TRUE);
4548 p = skipwhite(*arg);
4549 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 if (type_is && (p[len] == '?' || p[len] == '#'))
4551 {
4552 semsg(_(e_invexpr2), *arg);
4553 return FAIL;
4554 }
4555 // extra question mark appended: ignore case
4556 if (p[len] == '?')
4557 {
4558 ic = TRUE;
4559 ++len;
4560 }
4561 // extra '#' appended: match case (ignored)
4562 else if (p[len] == '#')
4563 ++len;
4564 // nothing appended: match case
4565
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004566 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004568 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004569 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 }
4571
4572 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004573 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004574 return FAIL;
4575
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 return FAIL;
4578
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579 if (ppconst->pp_used == ppconst_used + 2)
4580 {
4581 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4582 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4583 int ret;
4584
4585 // Both sides are a constant, compute the result now.
4586 // First check for a valid combination of types, this is more
4587 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004588 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004589 ret = FAIL;
4590 else
4591 {
4592 ret = typval_compare(tv1, tv2, type, ic);
4593 tv1->v_type = VAR_BOOL;
4594 tv1->vval.v_number = tv1->vval.v_number
4595 ? VVAL_TRUE : VVAL_FALSE;
4596 clear_tv(tv2);
4597 --ppconst->pp_used;
4598 }
4599 return ret;
4600 }
4601
4602 generate_ppconst(cctx, ppconst);
4603 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 }
4605
4606 return OK;
4607}
4608
Bram Moolenaar7f141552020-05-09 17:35:53 +02004609static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611/*
4612 * Compile || or &&.
4613 */
4614 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004615compile_and_or(
4616 char_u **arg,
4617 cctx_T *cctx,
4618 char *op,
4619 ppconst_T *ppconst,
4620 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004622 char_u *next;
4623 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 int opchar = *op;
4625
4626 if (p[0] == opchar && p[1] == opchar)
4627 {
4628 garray_T *instr = &cctx->ctx_instr;
4629 garray_T end_ga;
4630
4631 /*
4632 * Repeat until there is no following "||" or "&&"
4633 */
4634 ga_init2(&end_ga, sizeof(int), 10);
4635 while (p[0] == opchar && p[1] == opchar)
4636 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004637 if (next != NULL)
4638 {
4639 *arg = next_line_from_context(cctx, TRUE);
4640 p = skipwhite(*arg);
4641 }
4642
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004643 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4644 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004645 semsg(_(e_white_space_required_before_and_after_str_at_str),
4646 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004647 return FAIL;
4648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004650 // TODO: use ppconst if the value is a constant and check
4651 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004652 generate_ppconst(cctx, ppconst);
4653
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004654 // Every part must evaluate to a bool.
4655 if (bool_on_stack(cctx) == FAIL)
4656 {
4657 ga_clear(&end_ga);
4658 return FAIL;
4659 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004660
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 if (ga_grow(&end_ga, 1) == FAIL)
4662 {
4663 ga_clear(&end_ga);
4664 return FAIL;
4665 }
4666 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4667 ++end_ga.ga_len;
4668 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004669 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670
4671 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004672 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004673 {
4674 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004675 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004676 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004677
Bram Moolenaara5565e42020-05-09 15:44:01 +02004678 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4679 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 {
4681 ga_clear(&end_ga);
4682 return FAIL;
4683 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004684
4685 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004687 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004689 // Every part must evaluate to a bool.
4690 if (bool_on_stack(cctx) == FAIL)
4691 {
4692 ga_clear(&end_ga);
4693 return FAIL;
4694 }
4695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 // Fill in the end label in all jumps.
4697 while (end_ga.ga_len > 0)
4698 {
4699 isn_T *isn;
4700
4701 --end_ga.ga_len;
4702 isn = ((isn_T *)instr->ga_data)
4703 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4704 isn->isn_arg.jump.jump_where = instr->ga_len;
4705 }
4706 ga_clear(&end_ga);
4707 }
4708
4709 return OK;
4710}
4711
4712/*
4713 * expr4a && expr4a && expr4a logical AND
4714 *
4715 * Produces instructions:
4716 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004717 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004718 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004720 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 * EVAL expr4c Push result of "expr4c"
4722 * end:
4723 */
4724 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004725compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 int ppconst_used = ppconst->pp_used;
4728
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004730 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731 return FAIL;
4732
4733 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735}
4736
4737/*
4738 * expr3a || expr3b || expr3c logical OR
4739 *
4740 * Produces instructions:
4741 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004742 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004743 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004745 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 * EVAL expr3c Push result of "expr3c"
4747 * end:
4748 */
4749 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004750compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004752 int ppconst_used = ppconst->pp_used;
4753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 return FAIL;
4757
4758 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004759 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760}
4761
4762/*
4763 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004765 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 * JUMP_IF_FALSE alt jump if false
4767 * EVAL expr1a
4768 * JUMP_ALWAYS end
4769 * alt: EVAL expr1b
4770 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004771 *
4772 * Toplevel expression: expr2 ?? expr1
4773 * Produces instructions:
4774 * EVAL expr2 Push result of "expr2"
4775 * JUMP_AND_KEEP_IF_TRUE end jump if true
4776 * EVAL expr1
4777 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 */
4779 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004780compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781{
4782 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004784 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004785
Bram Moolenaar3988f642020-08-27 22:43:03 +02004786 // Ignore all kinds of errors when not producing code.
4787 if (cctx->ctx_skip == SKIP_YES)
4788 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004789 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004790 return OK;
4791 }
4792
Bram Moolenaar61a89812020-05-07 16:58:17 +02004793 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004794 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 return FAIL;
4796
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004797 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 if (*p == '?')
4799 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004800 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 garray_T *instr = &cctx->ctx_instr;
4802 garray_T *stack = &cctx->ctx_type_stack;
4803 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004804 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004806 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004807 int has_const_expr = FALSE;
4808 int const_value = FALSE;
4809 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004811 if (next != NULL)
4812 {
4813 *arg = next_line_from_context(cctx, TRUE);
4814 p = skipwhite(*arg);
4815 }
4816
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004817 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004818 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004819 semsg(_(e_white_space_required_before_and_after_str_at_str),
4820 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004821 return FAIL;
4822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823
Bram Moolenaara5565e42020-05-09 15:44:01 +02004824 if (ppconst->pp_used == ppconst_used + 1)
4825 {
4826 // the condition is a constant, we know whether the ? or the :
4827 // expression is to be evaluated.
4828 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004829 if (op_falsy)
4830 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4831 else
4832 {
4833 int error = FALSE;
4834
4835 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4836 &error);
4837 if (error)
4838 return FAIL;
4839 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004840 cctx->ctx_skip = save_skip == SKIP_YES ||
4841 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4842
4843 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4844 // "left ?? right" and "left" is truthy: produce "left"
4845 generate_ppconst(cctx, ppconst);
4846 else
4847 {
4848 clear_tv(&ppconst->pp_tv[ppconst_used]);
4849 --ppconst->pp_used;
4850 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004851 }
4852 else
4853 {
4854 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004855 if (op_falsy)
4856 end_idx = instr->ga_len;
4857 generate_JUMP(cctx, op_falsy
4858 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4859 if (op_falsy)
4860 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862
4863 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004864 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004865 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004866 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004867 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868
Bram Moolenaara5565e42020-05-09 15:44:01 +02004869 if (!has_const_expr)
4870 {
4871 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004873 if (!op_falsy)
4874 {
4875 // remember the type and drop it
4876 --stack->ga_len;
4877 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004879 end_idx = instr->ga_len;
4880 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004882 // jump here from JUMP_IF_FALSE
4883 isn = ((isn_T *)instr->ga_data) + alt_idx;
4884 isn->isn_arg.jump.jump_where = instr->ga_len;
4885 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004888 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004890 // Check for the ":".
4891 p = may_peek_next_line(cctx, *arg, &next);
4892 if (*p != ':')
4893 {
4894 emsg(_(e_missing_colon));
4895 return FAIL;
4896 }
4897 if (next != NULL)
4898 {
4899 *arg = next_line_from_context(cctx, TRUE);
4900 p = skipwhite(*arg);
4901 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004902
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004903 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4904 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004905 semsg(_(e_white_space_required_before_and_after_str_at_str),
4906 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004907 return FAIL;
4908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004910 // evaluate the third expression
4911 if (has_const_expr)
4912 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004913 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004914 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004915 return FAIL;
4916 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4917 return FAIL;
4918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919
Bram Moolenaara5565e42020-05-09 15:44:01 +02004920 if (!has_const_expr)
4921 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004922 type_T **typep;
4923
Bram Moolenaara5565e42020-05-09 15:44:01 +02004924 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
Bram Moolenaara5565e42020-05-09 15:44:01 +02004926 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004927 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4928 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004929
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004930 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004931 isn = ((isn_T *)instr->ga_data) + end_idx;
4932 isn->isn_arg.jump.jump_where = instr->ga_len;
4933 }
4934
4935 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
4937 return OK;
4938}
4939
4940/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004941 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004942 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4943 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004944 */
4945 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004946compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004947{
4948 ppconst_T ppconst;
4949
4950 CLEAR_FIELD(ppconst);
4951 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4952 {
4953 clear_ppconst(&ppconst);
4954 return FAIL;
4955 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004956 if (is_const != NULL)
4957 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004958 if (generate_ppconst(cctx, &ppconst) == FAIL)
4959 return FAIL;
4960 return OK;
4961}
4962
4963/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004964 * Toplevel expression.
4965 */
4966 static int
4967compile_expr0(char_u **arg, cctx_T *cctx)
4968{
4969 return compile_expr0_ext(arg, cctx, NULL);
4970}
4971
4972/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 * compile "return [expr]"
4974 */
4975 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004976compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977{
4978 char_u *p = arg;
4979 garray_T *stack = &cctx->ctx_type_stack;
4980 type_T *stack_type;
4981
4982 if (*p != NUL && *p != '|' && *p != '\n')
4983 {
4984 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004985 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 return NULL;
4987
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004988 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004989 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004990 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004991 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004992 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4993 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004994 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004995 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004996 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004997 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004998 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004999 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
5000 && stack_type->tt_type != VAR_VOID
5001 && stack_type->tt_type != VAR_UNKNOWN)
5002 {
5003 emsg(_(e_returning_value_in_function_without_return_type));
5004 return NULL;
5005 }
5006 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01005007 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005008 return NULL;
5009 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02005010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 }
5012 else
5013 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005014 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02005015 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02005016 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
5017 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005019 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 return NULL;
5021 }
5022
5023 // No argument, return zero.
5024 generate_PUSHNR(cctx, 0);
5025 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01005026
5027 // Undo any command modifiers.
5028 generate_undo_cmdmods(cctx);
5029
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01005030 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 return NULL;
5032
5033 // "return val | endif" is possible
5034 return skipwhite(p);
5035}
5036
5037/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005038 * Get a line from the compilation context, compatible with exarg_T getline().
5039 * Return a pointer to the line in allocated memory.
5040 * Return NULL for end-of-file or some error.
5041 */
5042 static char_u *
5043exarg_getline(
5044 int c UNUSED,
5045 void *cookie,
5046 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005047 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005048{
5049 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005050 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005051
Bram Moolenaar66250c92020-08-20 15:02:42 +02005052 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005053 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005054 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005055 return NULL;
5056 ++cctx->ctx_lnum;
5057 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5058 // Comment lines result in NULL pointers, skip them.
5059 if (p != NULL)
5060 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005061 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005062}
5063
5064/*
5065 * Compile a nested :def command.
5066 */
5067 static char_u *
5068compile_nested_function(exarg_T *eap, cctx_T *cctx)
5069{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005070 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005071 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005072 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005073 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005074 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005075 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005076
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005077 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005078 {
5079 emsg(_(e_cannot_use_bang_with_nested_def));
5080 return NULL;
5081 }
5082
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005083 if (*name_start == '/')
5084 {
5085 name_end = skip_regexp(name_start + 1, '/', TRUE);
5086 if (*name_end == '/')
5087 ++name_end;
5088 eap->nextcmd = check_nextcmd(name_end);
5089 }
5090 if (name_end == name_start || *skipwhite(name_end) != '(')
5091 {
5092 if (!ends_excmd2(name_start, name_end))
5093 {
5094 semsg(_(e_invalid_command_str), eap->cmd);
5095 return NULL;
5096 }
5097
5098 // "def" or "def Name": list functions
5099 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5100 return NULL;
5101 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5102 }
5103
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005104 // Only g:Func() can use a namespace.
5105 if (name_start[1] == ':' && !is_global)
5106 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005107 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005108 return NULL;
5109 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005110 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5111 return NULL;
5112
Bram Moolenaar04b12692020-05-04 23:24:44 +02005113 eap->arg = name_end;
5114 eap->getline = exarg_getline;
5115 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005116 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005117 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005118 lambda_name = vim_strsave(get_lambda_name());
5119 if (lambda_name == NULL)
5120 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005121 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005122
Bram Moolenaar822ba242020-05-24 23:00:18 +02005123 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005124 {
5125 r = eap->skip ? OK : FAIL;
5126 goto theend;
5127 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +01005128 if (func_needs_compiling(ufunc, PROFILING(ufunc))
5129 && compile_def_function(ufunc, TRUE, PROFILING(ufunc), cctx)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005130 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005131 {
5132 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005133 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005134 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005135
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005136 if (is_global)
5137 {
5138 char_u *func_name = vim_strnsave(name_start + 2,
5139 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005140
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005141 if (func_name == NULL)
5142 r = FAIL;
5143 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005144 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005145 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005146 lambda_name = NULL;
5147 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005148 }
5149 else
5150 {
5151 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005152 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005153 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005154 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005155
Bram Moolenaareef21022020-08-01 22:16:43 +02005156 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005157 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005158 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005159 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005160 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005161
5162 // copy over the block scope IDs
5163 if (block_depth > 0)
5164 {
5165 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5166 if (ufunc->uf_block_ids != NULL)
5167 {
5168 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5169 sizeof(int) * block_depth);
5170 ufunc->uf_block_depth = block_depth;
5171 }
5172 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005173 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005174 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005175
5176theend:
5177 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005178 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005179}
5180
5181/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 * Return the length of an assignment operator, or zero if there isn't one.
5183 */
5184 int
5185assignment_len(char_u *p, int *heredoc)
5186{
5187 if (*p == '=')
5188 {
5189 if (p[1] == '<' && p[2] == '<')
5190 {
5191 *heredoc = TRUE;
5192 return 3;
5193 }
5194 return 1;
5195 }
5196 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5197 return 2;
5198 if (STRNCMP(p, "..=", 3) == 0)
5199 return 3;
5200 return 0;
5201}
5202
5203// words that cannot be used as a variable
5204static char *reserved[] = {
5205 "true",
5206 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005207 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 NULL
5209};
5210
Bram Moolenaar752fc692021-01-04 21:57:11 +01005211// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005212typedef enum {
5213 dest_local,
5214 dest_option,
5215 dest_env,
5216 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005217 dest_buffer,
5218 dest_window,
5219 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005220 dest_vimvar,
5221 dest_script,
5222 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005223 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005224} assign_dest_T;
5225
Bram Moolenaar752fc692021-01-04 21:57:11 +01005226// Used by compile_lhs() to store information about the LHS of an assignment
5227// and one argument of ":unlet" with an index.
5228typedef struct {
5229 assign_dest_T lhs_dest; // type of destination
5230
5231 char_u *lhs_name; // allocated name including
5232 // "[expr]" or ".name".
5233 size_t lhs_varlen; // length of the variable without
5234 // "[expr]" or ".name"
5235 char_u *lhs_dest_end; // end of the destination, including
5236 // "[expr]" or ".name".
5237
5238 int lhs_has_index; // has "[expr]" or ".name"
5239
5240 int lhs_new_local; // create new local variable
5241 int lhs_opt_flags; // for when destination is an option
5242 int lhs_vimvaridx; // for when destination is a v:var
5243
5244 lvar_T lhs_local_lvar; // used for existing local destination
5245 lvar_T lhs_arg_lvar; // used for argument destination
5246 lvar_T *lhs_lvar; // points to destination lvar
5247 int lhs_scriptvar_sid;
5248 int lhs_scriptvar_idx;
5249
5250 int lhs_has_type; // type was specified
5251 type_T *lhs_type;
5252 type_T *lhs_member_type;
5253} lhs_T;
5254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005255/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005256 * Generate the load instruction for "name".
5257 */
5258 static void
5259generate_loadvar(
5260 cctx_T *cctx,
5261 assign_dest_T dest,
5262 char_u *name,
5263 lvar_T *lvar,
5264 type_T *type)
5265{
5266 switch (dest)
5267 {
5268 case dest_option:
5269 // TODO: check the option exists
5270 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5271 break;
5272 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005273 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5274 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5275 else
5276 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005277 break;
5278 case dest_buffer:
5279 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5280 break;
5281 case dest_window:
5282 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5283 break;
5284 case dest_tab:
5285 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5286 break;
5287 case dest_script:
5288 compile_load_scriptvar(cctx,
5289 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5290 break;
5291 case dest_env:
5292 // Include $ in the name here
5293 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5294 break;
5295 case dest_reg:
5296 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5297 break;
5298 case dest_vimvar:
5299 generate_LOADV(cctx, name + 2, TRUE);
5300 break;
5301 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005302 if (lvar->lv_from_outer > 0)
5303 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5304 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005305 else
5306 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5307 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005308 case dest_expr:
5309 // list or dict value should already be on the stack.
5310 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005311 }
5312}
5313
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005314/*
5315 * Skip over "[expr]" or ".member".
5316 * Does not check for any errors.
5317 */
5318 static char_u *
5319skip_index(char_u *start)
5320{
5321 char_u *p = start;
5322
5323 if (*p == '[')
5324 {
5325 p = skipwhite(p + 1);
5326 (void)skip_expr(&p, NULL);
5327 p = skipwhite(p);
5328 if (*p == ']')
5329 return p + 1;
5330 return p;
5331 }
5332 // if (*p == '.')
5333 return to_name_end(p + 1, TRUE);
5334}
5335
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005336 void
5337vim9_declare_error(char_u *name)
5338{
5339 char *scope = "";
5340
5341 switch (*name)
5342 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005343 case 'g': scope = _("global"); break;
5344 case 'b': scope = _("buffer"); break;
5345 case 'w': scope = _("window"); break;
5346 case 't': scope = _("tab"); break;
5347 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005348 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005349 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005350 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005351 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005352 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005353 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005354 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005355 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005356 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005357}
5358
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005359/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005360 * For one assignment figure out the type of destination. Return it in "dest".
5361 * When not recognized "dest" is not set.
5362 * For an option "opt_flags" is set.
5363 * For a v:var "vimvaridx" is set.
5364 * "type" is set to the destination type if known, unchanted otherwise.
5365 * Return FAIL if an error message was given.
5366 */
5367 static int
5368get_var_dest(
5369 char_u *name,
5370 assign_dest_T *dest,
5371 int cmdidx,
5372 int *opt_flags,
5373 int *vimvaridx,
5374 type_T **type,
5375 cctx_T *cctx)
5376{
5377 char_u *p;
5378
5379 if (*name == '&')
5380 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005381 int cc;
5382 long numval;
5383 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005384
5385 *dest = dest_option;
5386 if (cmdidx == CMD_final || cmdidx == CMD_const)
5387 {
5388 emsg(_(e_const_option));
5389 return FAIL;
5390 }
5391 p = name;
5392 p = find_option_end(&p, opt_flags);
5393 if (p == NULL)
5394 {
5395 // cannot happen?
5396 emsg(_(e_letunexp));
5397 return FAIL;
5398 }
5399 cc = *p;
5400 *p = NUL;
5401 opt_type = get_option_value(skip_option_env_lead(name),
5402 &numval, NULL, *opt_flags);
5403 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005404 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005405 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005406 case gov_unknown:
5407 semsg(_(e_unknown_option), name);
5408 return FAIL;
5409 case gov_string:
5410 case gov_hidden_string:
5411 *type = &t_string;
5412 break;
5413 case gov_bool:
5414 case gov_hidden_bool:
5415 *type = &t_bool;
5416 break;
5417 case gov_number:
5418 case gov_hidden_number:
5419 *type = &t_number;
5420 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005421 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005422 }
5423 else if (*name == '$')
5424 {
5425 *dest = dest_env;
5426 *type = &t_string;
5427 }
5428 else if (*name == '@')
5429 {
5430 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5431 {
5432 emsg_invreg(name[1]);
5433 return FAIL;
5434 }
5435 *dest = dest_reg;
5436 *type = &t_string;
5437 }
5438 else if (STRNCMP(name, "g:", 2) == 0)
5439 {
5440 *dest = dest_global;
5441 }
5442 else if (STRNCMP(name, "b:", 2) == 0)
5443 {
5444 *dest = dest_buffer;
5445 }
5446 else if (STRNCMP(name, "w:", 2) == 0)
5447 {
5448 *dest = dest_window;
5449 }
5450 else if (STRNCMP(name, "t:", 2) == 0)
5451 {
5452 *dest = dest_tab;
5453 }
5454 else if (STRNCMP(name, "v:", 2) == 0)
5455 {
5456 typval_T *vtv;
5457 int di_flags;
5458
5459 *vimvaridx = find_vim_var(name + 2, &di_flags);
5460 if (*vimvaridx < 0)
5461 {
5462 semsg(_(e_variable_not_found_str), name);
5463 return FAIL;
5464 }
5465 // We use the current value of "sandbox" here, is that OK?
5466 if (var_check_ro(di_flags, name, FALSE))
5467 return FAIL;
5468 *dest = dest_vimvar;
5469 vtv = get_vim_var_tv(*vimvaridx);
5470 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5471 }
5472 return OK;
5473}
5474
5475/*
5476 * Generate a STORE instruction for "dest", not being "dest_local".
5477 * Return FAIL when out of memory.
5478 */
5479 static int
5480generate_store_var(
5481 cctx_T *cctx,
5482 assign_dest_T dest,
5483 int opt_flags,
5484 int vimvaridx,
5485 int scriptvar_idx,
5486 int scriptvar_sid,
5487 type_T *type,
5488 char_u *name)
5489{
5490 switch (dest)
5491 {
5492 case dest_option:
5493 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5494 opt_flags);
5495 case dest_global:
5496 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005497 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5498 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005499 case dest_buffer:
5500 // include b: with the name, easier to execute that way
5501 return generate_STORE(cctx, ISN_STOREB, 0, name);
5502 case dest_window:
5503 // include w: with the name, easier to execute that way
5504 return generate_STORE(cctx, ISN_STOREW, 0, name);
5505 case dest_tab:
5506 // include t: with the name, easier to execute that way
5507 return generate_STORE(cctx, ISN_STORET, 0, name);
5508 case dest_env:
5509 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5510 case dest_reg:
5511 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5512 case dest_vimvar:
5513 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5514 case dest_script:
5515 if (scriptvar_idx < 0)
5516 {
5517 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005518 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005519
Bram Moolenaar41d61962020-12-06 20:12:43 +01005520 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005521 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5522 scriptvar_sid, type);
5523 if (name_s != name)
5524 vim_free(name_s);
5525 return r;
5526 }
5527 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5528 scriptvar_sid, scriptvar_idx, type);
5529 case dest_local:
5530 case dest_expr:
5531 // cannot happen
5532 break;
5533 }
5534 return FAIL;
5535}
5536
Bram Moolenaar752fc692021-01-04 21:57:11 +01005537 static int
5538is_decl_command(int cmdidx)
5539{
5540 return cmdidx == CMD_let || cmdidx == CMD_var
5541 || cmdidx == CMD_final || cmdidx == CMD_const;
5542}
5543
5544/*
5545 * Figure out the LHS type and other properties for an assignment or one item
5546 * of ":unlet" with an index.
5547 * Returns OK or FAIL.
5548 */
5549 static int
5550compile_lhs(
5551 char_u *var_start,
5552 lhs_T *lhs,
5553 int cmdidx,
5554 int heredoc,
5555 int oplen,
5556 cctx_T *cctx)
5557{
5558 char_u *var_end;
5559 int is_decl = is_decl_command(cmdidx);
5560
5561 CLEAR_POINTER(lhs);
5562 lhs->lhs_dest = dest_local;
5563 lhs->lhs_vimvaridx = -1;
5564 lhs->lhs_scriptvar_idx = -1;
5565
5566 // "dest_end" is the end of the destination, including "[expr]" or
5567 // ".name".
5568 // "var_end" is the end of the variable/option/etc. name.
5569 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5570 if (*var_start == '@')
5571 var_end = var_start + 2;
5572 else
5573 {
5574 // skip over the leading "&", "&l:", "&g:" and "$"
5575 var_end = skip_option_env_lead(var_start);
5576 var_end = to_name_end(var_end, TRUE);
5577 }
5578
5579 // "a: type" is declaring variable "a" with a type, not dict "a:".
5580 if (is_decl && lhs->lhs_dest_end == var_start + 2
5581 && lhs->lhs_dest_end[-1] == ':')
5582 --lhs->lhs_dest_end;
5583 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5584 --var_end;
5585
5586 // compute the length of the destination without "[expr]" or ".name"
5587 lhs->lhs_varlen = var_end - var_start;
5588 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5589 if (lhs->lhs_name == NULL)
5590 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005591
5592 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5593 // Something follows after the variable: "var[idx]" or "var.key".
5594 lhs->lhs_has_index = TRUE;
5595
Bram Moolenaar752fc692021-01-04 21:57:11 +01005596 if (heredoc)
5597 lhs->lhs_type = &t_list_string;
5598 else
5599 lhs->lhs_type = &t_any;
5600
5601 if (cctx->ctx_skip != SKIP_YES)
5602 {
5603 int declare_error = FALSE;
5604
5605 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5606 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5607 &lhs->lhs_type, cctx) == FAIL)
5608 return FAIL;
5609 if (lhs->lhs_dest != dest_local)
5610 {
5611 // Specific kind of variable recognized.
5612 declare_error = is_decl;
5613 }
5614 else
5615 {
5616 int idx;
5617
5618 // No specific kind of variable recognized, just a name.
5619 for (idx = 0; reserved[idx] != NULL; ++idx)
5620 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5621 {
5622 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5623 return FAIL;
5624 }
5625
5626
5627 if (lookup_local(var_start, lhs->lhs_varlen,
5628 &lhs->lhs_local_lvar, cctx) == OK)
5629 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5630 else
5631 {
5632 CLEAR_FIELD(lhs->lhs_arg_lvar);
5633 if (arg_exists(var_start, lhs->lhs_varlen,
5634 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5635 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5636 {
5637 if (is_decl)
5638 {
5639 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5640 return FAIL;
5641 }
5642 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5643 }
5644 }
5645 if (lhs->lhs_lvar != NULL)
5646 {
5647 if (is_decl)
5648 {
5649 semsg(_(e_variable_already_declared), lhs->lhs_name);
5650 return FAIL;
5651 }
5652 }
5653 else
5654 {
5655 int script_namespace = lhs->lhs_varlen > 1
5656 && STRNCMP(var_start, "s:", 2) == 0;
5657 int script_var = (script_namespace
5658 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5659 FALSE, cctx)
5660 : script_var_exists(var_start, lhs->lhs_varlen,
5661 TRUE, cctx)) == OK;
5662 imported_T *import =
5663 find_imported(var_start, lhs->lhs_varlen, cctx);
5664
5665 if (script_namespace || script_var || import != NULL)
5666 {
5667 char_u *rawname = lhs->lhs_name
5668 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5669
5670 if (is_decl)
5671 {
5672 if (script_namespace)
5673 semsg(_(e_cannot_declare_script_variable_in_function),
5674 lhs->lhs_name);
5675 else
5676 semsg(_(e_variable_already_declared_in_script),
5677 lhs->lhs_name);
5678 return FAIL;
5679 }
5680 else if (cctx->ctx_ufunc->uf_script_ctx_version
5681 == SCRIPT_VERSION_VIM9
5682 && script_namespace
5683 && !script_var && import == NULL)
5684 {
5685 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5686 return FAIL;
5687 }
5688
5689 lhs->lhs_dest = dest_script;
5690
5691 // existing script-local variables should have a type
5692 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5693 if (import != NULL)
5694 lhs->lhs_scriptvar_sid = import->imp_sid;
5695 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5696 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005697 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005698 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005699 lhs->lhs_scriptvar_sid, rawname,
5700 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5701 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005702 if (lhs->lhs_scriptvar_idx >= 0)
5703 {
5704 scriptitem_T *si = SCRIPT_ITEM(
5705 lhs->lhs_scriptvar_sid);
5706 svar_T *sv =
5707 ((svar_T *)si->sn_var_vals.ga_data)
5708 + lhs->lhs_scriptvar_idx;
5709 lhs->lhs_type = sv->sv_type;
5710 }
5711 }
5712 }
5713 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5714 == FAIL)
5715 return FAIL;
5716 }
5717 }
5718
5719 if (declare_error)
5720 {
5721 vim9_declare_error(lhs->lhs_name);
5722 return FAIL;
5723 }
5724 }
5725
5726 // handle "a:name" as a name, not index "name" on "a"
5727 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5728 var_end = lhs->lhs_dest_end;
5729
5730 if (lhs->lhs_dest != dest_option)
5731 {
5732 if (is_decl && *var_end == ':')
5733 {
5734 char_u *p;
5735
5736 // parse optional type: "let var: type = expr"
5737 if (!VIM_ISWHITE(var_end[1]))
5738 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01005739 semsg(_(e_white_space_required_after_str_str), ":", var_end);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005740 return FAIL;
5741 }
5742 p = skipwhite(var_end + 1);
5743 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5744 if (lhs->lhs_type == NULL)
5745 return FAIL;
5746 lhs->lhs_has_type = TRUE;
5747 }
5748 else if (lhs->lhs_lvar != NULL)
5749 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5750 }
5751
5752 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5753 && lhs->lhs_type->tt_type != VAR_STRING
5754 && lhs->lhs_type->tt_type != VAR_ANY)
5755 {
5756 emsg(_(e_can_only_concatenate_to_string));
5757 return FAIL;
5758 }
5759
5760 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5761 && cctx->ctx_skip != SKIP_YES)
5762 {
5763 if (oplen > 1 && !heredoc)
5764 {
5765 // +=, /=, etc. require an existing variable
5766 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5767 return FAIL;
5768 }
5769 if (!is_decl)
5770 {
5771 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5772 return FAIL;
5773 }
5774
5775 // new local variable
5776 if ((lhs->lhs_type->tt_type == VAR_FUNC
5777 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5778 && var_wrong_func_name(lhs->lhs_name, TRUE))
5779 return FAIL;
5780 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5781 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5782 if (lhs->lhs_lvar == NULL)
5783 return FAIL;
5784 lhs->lhs_new_local = TRUE;
5785 }
5786
5787 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005788 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005789 {
5790 // Something follows after the variable: "var[idx]" or "var.key".
5791 // TODO: should we also handle "->func()" here?
5792 if (is_decl)
5793 {
5794 emsg(_(e_cannot_use_index_when_declaring_variable));
5795 return FAIL;
5796 }
5797
5798 if (var_start[lhs->lhs_varlen] == '['
5799 || var_start[lhs->lhs_varlen] == '.')
5800 {
5801 char_u *after = var_start + lhs->lhs_varlen;
5802 char_u *p;
5803
5804 // Only the last index is used below, if there are others
5805 // before it generate code for the expression. Thus for
5806 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5807 for (;;)
5808 {
5809 p = skip_index(after);
5810 if (*p != '[' && *p != '.')
5811 break;
5812 after = p;
5813 }
5814 if (after > var_start + lhs->lhs_varlen)
5815 {
5816 lhs->lhs_varlen = after - var_start;
5817 lhs->lhs_dest = dest_expr;
5818 // We don't know the type before evaluating the expression,
5819 // use "any" until then.
5820 lhs->lhs_type = &t_any;
5821 }
5822
Bram Moolenaar752fc692021-01-04 21:57:11 +01005823 if (lhs->lhs_type->tt_member == NULL)
5824 lhs->lhs_member_type = &t_any;
5825 else
5826 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5827 }
5828 else
5829 {
5830 semsg("Not supported yet: %s", var_start);
5831 return FAIL;
5832 }
5833 }
5834 return OK;
5835}
5836
5837/*
5838 * Assignment to a list or dict member, or ":unlet" for the item, using the
5839 * information in "lhs".
5840 * Returns OK or FAIL.
5841 */
5842 static int
5843compile_assign_unlet(
5844 char_u *var_start,
5845 lhs_T *lhs,
5846 int is_assign,
5847 type_T *rhs_type,
5848 cctx_T *cctx)
5849{
5850 char_u *p;
5851 int r;
5852 vartype_T dest_type;
5853 size_t varlen = lhs->lhs_varlen;
5854 garray_T *stack = &cctx->ctx_type_stack;
5855
5856 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5857 p = var_start + varlen;
5858 if (*p == '[')
5859 {
5860 p = skipwhite(p + 1);
5861 r = compile_expr0(&p, cctx);
5862 if (r == OK && *skipwhite(p) != ']')
5863 {
5864 // this should not happen
5865 emsg(_(e_missbrac));
5866 r = FAIL;
5867 }
5868 }
5869 else // if (*p == '.')
5870 {
5871 char_u *key_end = to_name_end(p + 1, TRUE);
5872 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5873
5874 r = generate_PUSHS(cctx, key);
5875 }
5876 if (r == FAIL)
5877 return FAIL;
5878
5879 if (lhs->lhs_type == &t_any)
5880 {
5881 // Index on variable of unknown type: check at runtime.
5882 dest_type = VAR_ANY;
5883 }
5884 else
5885 {
5886 dest_type = lhs->lhs_type->tt_type;
5887 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5888 return FAIL;
5889 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005890 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5891 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005892 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005893 }
5894
5895 // Load the dict or list. On the stack we then have:
5896 // - value (for assignment, not for :unlet)
5897 // - index
5898 // - variable
5899 if (lhs->lhs_dest == dest_expr)
5900 {
5901 int c = var_start[varlen];
5902
5903 // Evaluate "ll[expr]" of "ll[expr][idx]"
5904 p = var_start;
5905 var_start[varlen] = NUL;
5906 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5907 {
5908 // this should not happen
5909 emsg(_(e_missbrac));
5910 return FAIL;
5911 }
5912 var_start[varlen] = c;
5913
5914 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5915 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5916 // now we can properly check the type
5917 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005918 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005919 FALSE, FALSE) == FAIL)
5920 return FAIL;
5921 }
5922 else
5923 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5924 lhs->lhs_lvar, lhs->lhs_type);
5925
5926 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5927 {
5928 if (is_assign)
5929 {
5930 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5931
5932 if (isn == NULL)
5933 return FAIL;
5934 isn->isn_arg.vartype = dest_type;
5935 }
5936 else
5937 {
5938 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5939 return FAIL;
5940 }
5941 }
5942 else
5943 {
5944 emsg(_(e_indexable_type_required));
5945 return FAIL;
5946 }
5947
5948 return OK;
5949}
5950
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005951/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005952 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005953 * "let name"
5954 * "var name = expr"
5955 * "final name = expr"
5956 * "const name = expr"
5957 * "name = expr"
5958 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005959 * Return NULL for an error.
5960 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 */
5962 static char_u *
5963compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5964{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005965 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005966 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005967 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 char_u *ret = NULL;
5969 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005970 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005972 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005973 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 int oplen = 0;
5976 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005977 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005978 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005979 int is_decl = is_decl_command(cmdidx);
5980 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005982 // Skip over the "var" or "[var, var]" to get to any "=".
5983 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5984 if (p == NULL)
5985 return *arg == '[' ? arg : NULL;
5986
5987 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005988 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005989 // TODO: should we allow this, and figure out type inference from list
5990 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005991 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005992 return NULL;
5993 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005994 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 sp = p;
5997 p = skipwhite(p);
5998 op = p;
5999 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006000
6001 if (var_count > 0 && oplen == 0)
6002 // can be something like "[1, 2]->func()"
6003 return arg;
6004
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006005 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006006 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006007 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006008 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02006009 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 if (heredoc)
6012 {
6013 list_T *l;
6014 listitem_T *li;
6015
6016 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02006017 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02006019 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02006020 if (l == NULL)
6021 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022
Bram Moolenaar078269b2020-09-21 20:35:55 +02006023 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02006025 // Push each line and the create the list.
6026 FOR_ALL_LIST_ITEMS(l, li)
6027 {
6028 generate_PUSHS(cctx, li->li_tv.vval.v_string);
6029 li->li_tv.vval.v_string = NULL;
6030 }
6031 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 list_free(l);
6034 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006035 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006037 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006039 char_u *wp;
6040
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006041 // for "[var, var] = expr" evaluate the expression here, loop over the
6042 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006043 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006044
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006045 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006046 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006047 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006048 if (compile_expr0(&p, cctx) == FAIL)
6049 return NULL;
6050 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006051
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006052 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006054 type_T *stacktype;
6055
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006056 stacktype = stack->ga_len == 0 ? &t_void
6057 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006058 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006060 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006061 goto theend;
6062 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006063 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006064 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006065 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006066 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006067 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6068 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006069 if (stacktype->tt_member != NULL)
6070 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006071 }
6072 }
6073
6074 /*
6075 * Loop over variables in "[var, var] = expr".
6076 * For "var = expr" and "let var: type" this is done only once.
6077 */
6078 if (var_count > 0)
6079 var_start = skipwhite(arg + 1); // skip over the "["
6080 else
6081 var_start = arg;
6082 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6083 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 int instr_count = -1;
6085
Bram Moolenaar752fc692021-01-04 21:57:11 +01006086 vim_free(lhs.lhs_name);
6087
6088 /*
6089 * Figure out the LHS type and other properties.
6090 */
6091 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6092 goto theend;
6093
6094 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006095 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006096 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006097 goto theend;
6098 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 if (!is_decl && lhs.lhs_lvar != NULL
6100 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006101 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006102 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006103 goto theend;
6104 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006105
6106 if (!heredoc)
6107 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006108 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006109 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006110 if (oplen > 0 && var_count == 0)
6111 {
6112 // skip over the "=" and the expression
6113 p = skipwhite(op + oplen);
6114 compile_expr0(&p, cctx);
6115 }
6116 }
6117 else if (oplen > 0)
6118 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006119 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006120 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006121
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006122 // For "var = expr" evaluate the expression.
6123 if (var_count == 0)
6124 {
6125 int r;
6126
6127 // for "+=", "*=", "..=" etc. first load the current value
6128 if (*op != '=')
6129 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006130 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6131 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006132
Bram Moolenaar752fc692021-01-04 21:57:11 +01006133 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006134 {
6135 // TODO: get member from list or dict
6136 emsg("Index with operation not supported yet");
6137 goto theend;
6138 }
6139 }
6140
6141 // Compile the expression. Temporarily hide the new local
6142 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006143 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006144 --cctx->ctx_locals.ga_len;
6145 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006146 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006147 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006148 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006149 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006150 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006151 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006152 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006153 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006154 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006155 ++cctx->ctx_locals.ga_len;
6156 if (r == FAIL)
6157 goto theend;
6158 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006159 else if (semicolon && var_idx == var_count - 1)
6160 {
6161 // For "[var; var] = expr" get the rest of the list
6162 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6163 goto theend;
6164 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006165 else
6166 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006167 // For "[var, var] = expr" get the "var_idx" item from the
6168 // list.
6169 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006170 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006171 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006172
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006173 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006174 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006175 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006176 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006177 if ((rhs_type->tt_type == VAR_FUNC
6178 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006179 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006180 goto theend;
6181
Bram Moolenaar752fc692021-01-04 21:57:11 +01006182 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006183 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006184 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006185 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006186 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006187 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006188 }
6189 else
6190 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006191 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006192 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006193 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006194 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006195 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006196 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006197 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006199 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006200 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006201 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006202 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006203 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006204 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006205 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006206
Bram Moolenaar71abe482020-09-14 22:28:30 +02006207 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006208 if (lhs.lhs_has_index)
6209 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006210 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006211 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006212 goto theend;
6213 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006214 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006215 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006216 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006217 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006219 else if (cmdidx == CMD_final)
6220 {
6221 emsg(_(e_final_requires_a_value));
6222 goto theend;
6223 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006224 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006226 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006227 goto theend;
6228 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006229 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006230 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006231 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006232 goto theend;
6233 }
6234 else
6235 {
6236 // variables are always initialized
6237 if (ga_grow(instr, 1) == FAIL)
6238 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006239 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006240 {
6241 case VAR_BOOL:
6242 generate_PUSHBOOL(cctx, VVAL_FALSE);
6243 break;
6244 case VAR_FLOAT:
6245#ifdef FEAT_FLOAT
6246 generate_PUSHF(cctx, 0.0);
6247#endif
6248 break;
6249 case VAR_STRING:
6250 generate_PUSHS(cctx, NULL);
6251 break;
6252 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006253 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006254 break;
6255 case VAR_FUNC:
6256 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6257 break;
6258 case VAR_LIST:
6259 generate_NEWLIST(cctx, 0);
6260 break;
6261 case VAR_DICT:
6262 generate_NEWDICT(cctx, 0);
6263 break;
6264 case VAR_JOB:
6265 generate_PUSHJOB(cctx, NULL);
6266 break;
6267 case VAR_CHANNEL:
6268 generate_PUSHCHANNEL(cctx, NULL);
6269 break;
6270 case VAR_NUMBER:
6271 case VAR_UNKNOWN:
6272 case VAR_ANY:
6273 case VAR_PARTIAL:
6274 case VAR_VOID:
6275 case VAR_SPECIAL: // cannot happen
6276 generate_PUSHNR(cctx, 0);
6277 break;
6278 }
6279 }
6280 if (var_count == 0)
6281 end = p;
6282 }
6283
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006284 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006285 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006286 break;
6287
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006288 if (oplen > 0 && *op != '=')
6289 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006290 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006291 type_T *stacktype;
6292
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006293 if (*op == '.')
6294 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006295 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006296 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006297 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006298 if (
6299#ifdef FEAT_FLOAT
6300 // If variable is float operation with number is OK.
6301 !(expected == &t_float && stacktype == &t_number) &&
6302#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006303 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006304 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006305 goto theend;
6306
6307 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006308 {
6309 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6310 goto theend;
6311 }
6312 else if (*op == '+')
6313 {
6314 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 operator_type(lhs.lhs_member_type, stacktype),
6316 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006317 goto theend;
6318 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006319 else if (generate_two_op(cctx, op) == FAIL)
6320 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322
Bram Moolenaar752fc692021-01-04 21:57:11 +01006323 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006324 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006325 // Use the info in "lhs" to store the value at the index in the
6326 // list or dict.
6327 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6328 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006329 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006330 }
6331 else
6332 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006333 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6334 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006335 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006336 generate_LOCKCONST(cctx);
6337
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006338 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 && (lhs.lhs_type->tt_type == VAR_DICT
6340 || lhs.lhs_type->tt_type == VAR_LIST)
6341 && lhs.lhs_type->tt_member != NULL
6342 && lhs.lhs_type->tt_member != &t_any
6343 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006344 // Set the type in the list or dict, so that it can be checked,
6345 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006347
Bram Moolenaar752fc692021-01-04 21:57:11 +01006348 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006349 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006350 if (generate_store_var(cctx, lhs.lhs_dest,
6351 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6352 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6353 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006354 goto theend;
6355 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006356 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006357 {
6358 isn_T *isn = ((isn_T *)instr->ga_data)
6359 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006360
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006361 // optimization: turn "var = 123" from ISN_PUSHNR +
6362 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006363 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006364 && instr->ga_len == instr_count + 1
6365 && isn->isn_type == ISN_PUSHNR)
6366 {
6367 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006368
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006369 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006370 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006371 isn->isn_arg.storenr.stnr_val = val;
6372 if (stack->ga_len > 0)
6373 --stack->ga_len;
6374 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006375 else if (lhs.lhs_lvar->lv_from_outer > 0)
6376 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6377 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006378 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006379 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006380 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006381 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006382
6383 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006384 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006385 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006386
6387 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006388 if (var_count > 0 && !semicolon)
6389 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006390 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006391 goto theend;
6392 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006393
Bram Moolenaarb2097502020-07-19 17:17:02 +02006394 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395
6396theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006397 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006398 return ret;
6399}
6400
6401/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006402 * Check for an assignment at "eap->cmd", compile it if found.
6403 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6404 */
6405 static int
6406may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6407{
6408 char_u *pskip;
6409 char_u *p;
6410
6411 // Assuming the command starts with a variable or function name,
6412 // find what follows.
6413 // Skip over "var.member", "var[idx]" and the like.
6414 // Also "&opt = val", "$ENV = val" and "@r = val".
6415 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6416 ? eap->cmd + 1 : eap->cmd;
6417 p = to_name_end(pskip, TRUE);
6418 if (p > eap->cmd && *p != NUL)
6419 {
6420 char_u *var_end;
6421 int oplen;
6422 int heredoc;
6423
6424 if (eap->cmd[0] == '@')
6425 var_end = eap->cmd + 2;
6426 else
6427 var_end = find_name_end(pskip, NULL, NULL,
6428 FNE_CHECK_START | FNE_INCL_BR);
6429 oplen = assignment_len(skipwhite(var_end), &heredoc);
6430 if (oplen > 0)
6431 {
6432 size_t len = p - eap->cmd;
6433
6434 // Recognize an assignment if we recognize the variable
6435 // name:
6436 // "g:var = expr"
6437 // "local = expr" where "local" is a local var.
6438 // "script = expr" where "script" is a script-local var.
6439 // "import = expr" where "import" is an imported var
6440 // "&opt = expr"
6441 // "$ENV = expr"
6442 // "@r = expr"
6443 if (*eap->cmd == '&'
6444 || *eap->cmd == '$'
6445 || *eap->cmd == '@'
6446 || ((len) > 2 && eap->cmd[1] == ':')
6447 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6448 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6449 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6450 || find_imported(eap->cmd, len, cctx) != NULL)
6451 {
6452 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6453 if (*line == NULL || *line == eap->cmd)
6454 return FAIL;
6455 return OK;
6456 }
6457 }
6458 }
6459
6460 if (*eap->cmd == '[')
6461 {
6462 // [var, var] = expr
6463 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6464 if (*line == NULL)
6465 return FAIL;
6466 if (*line != eap->cmd)
6467 return OK;
6468 }
6469 return NOTDONE;
6470}
6471
6472/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006473 * Check if "name" can be "unlet".
6474 */
6475 int
6476check_vim9_unlet(char_u *name)
6477{
6478 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6479 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006480 // "unlet s:var" is allowed in legacy script.
6481 if (*name == 's' && !script_is_vim9())
6482 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006483 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006484 return FAIL;
6485 }
6486 return OK;
6487}
6488
6489/*
6490 * Callback passed to ex_unletlock().
6491 */
6492 static int
6493compile_unlet(
6494 lval_T *lvp,
6495 char_u *name_end,
6496 exarg_T *eap,
6497 int deep UNUSED,
6498 void *coookie)
6499{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006500 cctx_T *cctx = coookie;
6501 char_u *p = lvp->ll_name;
6502 int cc = *name_end;
6503 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006504
Bram Moolenaar752fc692021-01-04 21:57:11 +01006505 if (cctx->ctx_skip == SKIP_YES)
6506 return OK;
6507
6508 *name_end = NUL;
6509 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006510 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006511 // :unlet $ENV_VAR
6512 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6513 }
6514 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6515 {
6516 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006517
Bram Moolenaar752fc692021-01-04 21:57:11 +01006518 // This is similar to assigning: lookup the list/dict, compile the
6519 // idx/key. Then instead of storing the value unlet the item.
6520 // unlet {list}[idx]
6521 // unlet {dict}[key] dict.key
6522 //
6523 // Figure out the LHS type and other properties.
6524 //
6525 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6526
6527 // : unlet an indexed item
6528 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006529 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006530 iemsg("called compile_lhs() without an index");
6531 ret = FAIL;
6532 }
6533 else
6534 {
6535 // Use the info in "lhs" to unlet the item at the index in the
6536 // list or dict.
6537 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006538 }
6539
Bram Moolenaar752fc692021-01-04 21:57:11 +01006540 vim_free(lhs.lhs_name);
6541 }
6542 else if (check_vim9_unlet(p) == FAIL)
6543 {
6544 ret = FAIL;
6545 }
6546 else
6547 {
6548 // Normal name. Only supports g:, w:, t: and b: namespaces.
6549 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006550 }
6551
Bram Moolenaar752fc692021-01-04 21:57:11 +01006552 *name_end = cc;
6553 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006554}
6555
6556/*
6557 * compile "unlet var", "lock var" and "unlock var"
6558 * "arg" points to "var".
6559 */
6560 static char_u *
6561compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6562{
6563 char_u *p = arg;
6564
6565 if (eap->cmdidx != CMD_unlet)
6566 {
6567 emsg("Sorry, :lock and unlock not implemented yet");
6568 return NULL;
6569 }
6570
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006571 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6572 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006573 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6574}
6575
6576/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 * Compile an :import command.
6578 */
6579 static char_u *
6580compile_import(char_u *arg, cctx_T *cctx)
6581{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006582 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006583}
6584
6585/*
6586 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6587 */
6588 static int
6589compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6590{
6591 garray_T *instr = &cctx->ctx_instr;
6592 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6593
6594 if (endlabel == NULL)
6595 return FAIL;
6596 endlabel->el_next = *el;
6597 *el = endlabel;
6598 endlabel->el_end_label = instr->ga_len;
6599
6600 generate_JUMP(cctx, when, 0);
6601 return OK;
6602}
6603
6604 static void
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006605compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606{
6607 garray_T *instr = &cctx->ctx_instr;
6608
6609 while (*el != NULL)
6610 {
6611 endlabel_T *cur = (*el);
6612 isn_T *isn;
6613
6614 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006615 isn->isn_arg.jump.jump_where = jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006616 *el = cur->el_next;
6617 vim_free(cur);
6618 }
6619}
6620
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006621 static void
6622compile_free_jump_to_end(endlabel_T **el)
6623{
6624 while (*el != NULL)
6625 {
6626 endlabel_T *cur = (*el);
6627
6628 *el = cur->el_next;
6629 vim_free(cur);
6630 }
6631}
6632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006633/*
6634 * Create a new scope and set up the generic items.
6635 */
6636 static scope_T *
6637new_scope(cctx_T *cctx, scopetype_T type)
6638{
6639 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6640
6641 if (scope == NULL)
6642 return NULL;
6643 scope->se_outer = cctx->ctx_scope;
6644 cctx->ctx_scope = scope;
6645 scope->se_type = type;
6646 scope->se_local_count = cctx->ctx_locals.ga_len;
6647 return scope;
6648}
6649
6650/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006651 * Free the current scope and go back to the outer scope.
6652 */
6653 static void
6654drop_scope(cctx_T *cctx)
6655{
6656 scope_T *scope = cctx->ctx_scope;
6657
6658 if (scope == NULL)
6659 {
6660 iemsg("calling drop_scope() without a scope");
6661 return;
6662 }
6663 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006664 switch (scope->se_type)
6665 {
6666 case IF_SCOPE:
6667 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6668 case FOR_SCOPE:
6669 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6670 case WHILE_SCOPE:
6671 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6672 case TRY_SCOPE:
6673 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6674 case NO_SCOPE:
6675 case BLOCK_SCOPE:
6676 break;
6677 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006678 vim_free(scope);
6679}
6680
6681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006682 * compile "if expr"
6683 *
6684 * "if expr" Produces instructions:
6685 * EVAL expr Push result of "expr"
6686 * JUMP_IF_FALSE end
6687 * ... body ...
6688 * end:
6689 *
6690 * "if expr | else" Produces instructions:
6691 * EVAL expr Push result of "expr"
6692 * JUMP_IF_FALSE else
6693 * ... body ...
6694 * JUMP_ALWAYS end
6695 * else:
6696 * ... body ...
6697 * end:
6698 *
6699 * "if expr1 | elseif expr2 | else" Produces instructions:
6700 * EVAL expr Push result of "expr"
6701 * JUMP_IF_FALSE elseif
6702 * ... body ...
6703 * JUMP_ALWAYS end
6704 * elseif:
6705 * EVAL expr Push result of "expr"
6706 * JUMP_IF_FALSE else
6707 * ... body ...
6708 * JUMP_ALWAYS end
6709 * else:
6710 * ... body ...
6711 * end:
6712 */
6713 static char_u *
6714compile_if(char_u *arg, cctx_T *cctx)
6715{
6716 char_u *p = arg;
6717 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006718 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006719 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006720 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006721 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722
Bram Moolenaara5565e42020-05-09 15:44:01 +02006723 CLEAR_FIELD(ppconst);
6724 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006725 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006726 clear_ppconst(&ppconst);
6727 return NULL;
6728 }
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006729 if (!ends_excmd2(arg, skipwhite(p)))
6730 {
6731 semsg(_(e_trailing_arg), p);
6732 return NULL;
6733 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006734 if (cctx->ctx_skip == SKIP_YES)
6735 clear_ppconst(&ppconst);
6736 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006737 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006738 int error = FALSE;
6739 int v;
6740
Bram Moolenaara5565e42020-05-09 15:44:01 +02006741 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006742 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006743 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006744 if (error)
6745 return NULL;
6746 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006747 }
6748 else
6749 {
6750 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006751 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006752 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006753 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006754 if (bool_on_stack(cctx) == FAIL)
6755 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757
6758 scope = new_scope(cctx, IF_SCOPE);
6759 if (scope == NULL)
6760 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006761 scope->se_skip_save = skip_save;
6762 // "is_had_return" will be reset if any block does not end in :return
6763 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006765 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006766 {
6767 // "where" is set when ":elseif", "else" or ":endif" is found
6768 scope->se_u.se_if.is_if_label = instr->ga_len;
6769 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6770 }
6771 else
6772 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773
Bram Moolenaarced68a02021-01-24 17:53:47 +01006774#ifdef FEAT_PROFILE
6775 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6776 && skip_save != SKIP_YES)
6777 {
6778 // generated a profile start, need to generate a profile end, since it
6779 // won't be done after returning
6780 cctx->ctx_skip = SKIP_NOT;
6781 generate_instr(cctx, ISN_PROF_END);
6782 cctx->ctx_skip = SKIP_YES;
6783 }
6784#endif
6785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006786 return p;
6787}
6788
6789 static char_u *
6790compile_elseif(char_u *arg, cctx_T *cctx)
6791{
6792 char_u *p = arg;
6793 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006794 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006795 isn_T *isn;
6796 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006797 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006798 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006799
6800 if (scope == NULL || scope->se_type != IF_SCOPE)
6801 {
6802 emsg(_(e_elseif_without_if));
6803 return NULL;
6804 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006805 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006806 if (!cctx->ctx_had_return)
6807 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
Bram Moolenaarced68a02021-01-24 17:53:47 +01006809 if (cctx->ctx_skip == SKIP_NOT)
6810 {
6811 // previous block was executed, this one and following will not
6812 cctx->ctx_skip = SKIP_YES;
6813 scope->se_u.se_if.is_seen_skip_not = TRUE;
6814 }
6815 if (scope->se_u.se_if.is_seen_skip_not)
6816 {
6817 // A previous block was executed, skip over expression and bail out.
6818 // Do not count the "elseif" for profiling.
6819#ifdef FEAT_PROFILE
6820 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6821 .isn_type == ISN_PROF_START)
6822 --instr->ga_len;
6823#endif
6824 skip_expr_cctx(&p, cctx);
6825 return p;
6826 }
6827
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006828 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006829 {
6830 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006832 return NULL;
6833 // previous "if" or "elseif" jumps here
6834 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6835 isn->isn_arg.jump.jump_where = instr->ga_len;
6836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006837
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006838 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006839 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006840 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarced68a02021-01-24 17:53:47 +01006841 {
Bram Moolenaar749639e2020-08-27 23:08:47 +02006842 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaarced68a02021-01-24 17:53:47 +01006843#ifdef FEAT_PROFILE
6844 if (cctx->ctx_profiling)
6845 {
6846 // the previous block was skipped, need to profile this line
6847 generate_instr(cctx, ISN_PROF_START);
6848 instr_count = instr->ga_len;
6849 }
6850#endif
6851 }
Bram Moolenaar7f141552020-05-09 17:35:53 +02006852 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006853 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006854 clear_ppconst(&ppconst);
6855 return NULL;
6856 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006857 cctx->ctx_skip = save_skip;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01006858 if (!ends_excmd2(arg, skipwhite(p)))
6859 {
6860 semsg(_(e_trailing_arg), p);
6861 return NULL;
6862 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006863 if (scope->se_skip_save == SKIP_YES)
6864 clear_ppconst(&ppconst);
6865 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006866 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006867 int error = FALSE;
6868 int v;
6869
Bram Moolenaar7f141552020-05-09 17:35:53 +02006870 // The expression results in a constant.
6871 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006872 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6873 if (error)
6874 return NULL;
6875 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006876 clear_ppconst(&ppconst);
6877 scope->se_u.se_if.is_if_label = -1;
6878 }
6879 else
6880 {
6881 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006882 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006883 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006884 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006885 if (bool_on_stack(cctx) == FAIL)
6886 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006888 // "where" is set when ":elseif", "else" or ":endif" is found
6889 scope->se_u.se_if.is_if_label = instr->ga_len;
6890 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892
6893 return p;
6894}
6895
6896 static char_u *
6897compile_else(char_u *arg, cctx_T *cctx)
6898{
6899 char_u *p = arg;
6900 garray_T *instr = &cctx->ctx_instr;
6901 isn_T *isn;
6902 scope_T *scope = cctx->ctx_scope;
6903
6904 if (scope == NULL || scope->se_type != IF_SCOPE)
6905 {
6906 emsg(_(e_else_without_if));
6907 return NULL;
6908 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006909 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006910 if (!cctx->ctx_had_return)
6911 scope->se_u.se_if.is_had_return = FALSE;
6912 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006913
Bram Moolenaarced68a02021-01-24 17:53:47 +01006914#ifdef FEAT_PROFILE
6915 if (cctx->ctx_profiling)
6916 {
6917 if (cctx->ctx_skip == SKIP_NOT
6918 && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
6919 .isn_type == ISN_PROF_START)
6920 // the previous block was executed, do not count "else" for profiling
6921 --instr->ga_len;
6922 if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
6923 {
6924 // the previous block was not executed, this one will, do count the
6925 // "else" for profiling
6926 cctx->ctx_skip = SKIP_NOT;
6927 generate_instr(cctx, ISN_PROF_END);
6928 generate_instr(cctx, ISN_PROF_START);
6929 cctx->ctx_skip = SKIP_YES;
6930 }
6931 }
6932#endif
6933
6934 if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006935 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006936 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006937 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006938 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006939 if (!cctx->ctx_had_return
6940 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6941 JUMP_ALWAYS, cctx) == FAIL)
6942 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006943 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006944
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006945 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006946 {
6947 if (scope->se_u.se_if.is_if_label >= 0)
6948 {
6949 // previous "if" or "elseif" jumps here
6950 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6951 isn->isn_arg.jump.jump_where = instr->ga_len;
6952 scope->se_u.se_if.is_if_label = -1;
6953 }
6954 }
6955
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006956 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006957 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006959
6960 return p;
6961}
6962
6963 static char_u *
6964compile_endif(char_u *arg, cctx_T *cctx)
6965{
6966 scope_T *scope = cctx->ctx_scope;
6967 ifscope_T *ifscope;
6968 garray_T *instr = &cctx->ctx_instr;
6969 isn_T *isn;
6970
6971 if (scope == NULL || scope->se_type != IF_SCOPE)
6972 {
6973 emsg(_(e_endif_without_if));
6974 return NULL;
6975 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006976 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006977 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006978 if (!cctx->ctx_had_return)
6979 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006980
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006981 if (scope->se_u.se_if.is_if_label >= 0)
6982 {
6983 // previous "if" or "elseif" jumps here
6984 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6985 isn->isn_arg.jump.jump_where = instr->ga_len;
6986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01006988 compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
Bram Moolenaarced68a02021-01-24 17:53:47 +01006989
6990#ifdef FEAT_PROFILE
6991 // even when skipping we count the endif as executed, unless the block it's
6992 // in is skipped
6993 if (cctx->ctx_profiling && cctx->ctx_skip == SKIP_YES
6994 && scope->se_skip_save != SKIP_YES)
6995 {
6996 cctx->ctx_skip = SKIP_NOT;
6997 generate_instr(cctx, ISN_PROF_START);
6998 }
6999#endif
Bram Moolenaarefd88552020-06-18 20:50:10 +02007000 cctx->ctx_skip = scope->se_skip_save;
7001
7002 // If all the blocks end in :return and there is an :else then the
7003 // had_return flag is set.
7004 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007006 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007007 return arg;
7008}
7009
7010/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01007011 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007012 *
7013 * Produces instructions:
7014 * PUSHNR -1
7015 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01007016 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017 * top: FOR loop-idx, end Increment index, use list on bottom of stack
7018 * - if beyond end, jump to "end"
7019 * - otherwise get item from list and push it
7020 * STORE var Store item in "var"
7021 * ... body ...
7022 * JUMP top Jump back to repeat
7023 * end: DROP Drop the result of "expr"
7024 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007025 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
7026 * UNPACK 2 Split item in 2
7027 * STORE var1 Store item in "var1"
7028 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 */
7030 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01007031compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032{
Bram Moolenaar792f7862020-11-23 08:31:18 +01007033 char_u *arg;
7034 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007035 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007037 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007038 int var_count = 0;
7039 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 size_t varlen;
7041 garray_T *instr = &cctx->ctx_instr;
7042 garray_T *stack = &cctx->ctx_type_stack;
7043 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007044 lvar_T *loop_lvar; // loop iteration variable
7045 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007046 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007047 type_T *item_type = &t_any;
7048 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049
Bram Moolenaar792f7862020-11-23 08:31:18 +01007050 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01007051 if (p == NULL)
7052 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007053 if (var_count == 0)
7054 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055
7056 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007057 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007058 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7059 return NULL;
7060 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007061 {
7062 emsg(_(e_missing_in));
7063 return NULL;
7064 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007065 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01007066 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
7067 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007069 scope = new_scope(cctx, FOR_SCOPE);
7070 if (scope == NULL)
7071 return NULL;
7072
Bram Moolenaar792f7862020-11-23 08:31:18 +01007073 // Reserve a variable to store the loop iteration counter and initialize it
7074 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007075 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
7076 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007077 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007078 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007079 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007081 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007082 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007083
7084 // compile "expr", it remains on the stack until "endfor"
7085 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007086 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007087 {
7088 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007090 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007091 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007092
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007093 // Now that we know the type of "var", check that it is a list, now or at
7094 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01007096 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02007098 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099 return NULL;
7100 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007101
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02007102 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007103 {
7104 if (var_count == 1)
7105 item_type = vartype->tt_member;
7106 else if (vartype->tt_member->tt_type == VAR_LIST
7107 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
7108 item_type = vartype->tt_member->tt_member;
7109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007110
7111 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007112 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007113 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
Bram Moolenaar792f7862020-11-23 08:31:18 +01007115 arg = arg_start;
7116 if (var_count > 1)
7117 {
7118 generate_UNPACK(cctx, var_count, semicolon);
7119 arg = skipwhite(arg + 1); // skip white after '['
7120
7121 // the list item is replaced by a number of items
7122 if (ga_grow(stack, var_count - 1) == FAIL)
7123 {
7124 drop_scope(cctx);
7125 return NULL;
7126 }
7127 --stack->ga_len;
7128 for (idx = 0; idx < var_count; ++idx)
7129 {
7130 ((type_T **)stack->ga_data)[stack->ga_len] =
7131 (semicolon && idx == 0) ? vartype : item_type;
7132 ++stack->ga_len;
7133 }
7134 }
7135
7136 for (idx = 0; idx < var_count; ++idx)
7137 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007138 assign_dest_T dest = dest_local;
7139 int opt_flags = 0;
7140 int vimvaridx = -1;
7141 type_T *type = &t_any;
7142
7143 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007144 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007145 name = vim_strnsave(arg, varlen);
7146 if (name == NULL)
7147 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007148
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007149 // TODO: script var not supported?
7150 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7151 &vimvaridx, &type, cctx) == FAIL)
7152 goto failed;
7153 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007154 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007155 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7156 0, 0, type, name) == FAIL)
7157 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007158 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007159 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007160 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007161 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007162 {
7163 semsg(_(e_variable_already_declared), arg);
7164 goto failed;
7165 }
7166
Bram Moolenaarea870692020-12-02 14:24:30 +01007167 if (STRNCMP(name, "s:", 2) == 0)
7168 {
7169 semsg(_(e_cannot_declare_script_variable_in_function), name);
7170 goto failed;
7171 }
7172
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007173 // Reserve a variable to store "var".
7174 // TODO: check for type
7175 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7176 if (var_lvar == NULL)
7177 // out of memory or used as an argument
7178 goto failed;
7179
7180 if (semicolon && idx == var_count - 1)
7181 var_lvar->lv_type = vartype;
7182 else
7183 var_lvar->lv_type = item_type;
7184 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7185 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007186
Bram Moolenaar036d0712021-01-17 20:23:38 +01007187 if (*p == ':')
7188 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007189 if (*p == ',' || *p == ';')
7190 ++p;
7191 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007192 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007193 }
7194
7195 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007196
7197failed:
7198 vim_free(name);
7199 drop_scope(cctx);
7200 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201}
7202
7203/*
7204 * compile "endfor"
7205 */
7206 static char_u *
7207compile_endfor(char_u *arg, cctx_T *cctx)
7208{
7209 garray_T *instr = &cctx->ctx_instr;
7210 scope_T *scope = cctx->ctx_scope;
7211 forscope_T *forscope;
7212 isn_T *isn;
7213
7214 if (scope == NULL || scope->se_type != FOR_SCOPE)
7215 {
7216 emsg(_(e_for));
7217 return NULL;
7218 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007219 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007221 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007222
7223 // At end of ":for" scope jump back to the FOR instruction.
7224 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7225
7226 // Fill in the "end" label in the FOR statement so it can jump here
7227 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7228 isn->isn_arg.forloop.for_end = instr->ga_len;
7229
7230 // Fill in the "end" label any BREAK statements
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007231 compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007232
7233 // Below the ":for" scope drop the "expr" list from the stack.
7234 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7235 return NULL;
7236
7237 vim_free(scope);
7238
7239 return arg;
7240}
7241
7242/*
7243 * compile "while expr"
7244 *
7245 * Produces instructions:
7246 * top: EVAL expr Push result of "expr"
7247 * JUMP_IF_FALSE end jump if false
7248 * ... body ...
7249 * JUMP top Jump back to repeat
7250 * end:
7251 *
7252 */
7253 static char_u *
7254compile_while(char_u *arg, cctx_T *cctx)
7255{
7256 char_u *p = arg;
7257 garray_T *instr = &cctx->ctx_instr;
7258 scope_T *scope;
7259
7260 scope = new_scope(cctx, WHILE_SCOPE);
7261 if (scope == NULL)
7262 return NULL;
7263
Bram Moolenaarb2049902021-01-24 12:53:53 +01007264 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007265 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007266#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007267 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7268 .isn_type == ISN_PROF_START)
7269 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007270#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271
7272 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007273 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007274 return NULL;
Bram Moolenaar6628b7e2021-02-07 16:33:35 +01007275 if (!ends_excmd2(arg, skipwhite(p)))
7276 {
7277 semsg(_(e_trailing_arg), p);
7278 return NULL;
7279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007280
Bram Moolenaar13106602020-10-04 16:06:05 +02007281 if (bool_on_stack(cctx) == FAIL)
7282 return FAIL;
7283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007284 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007285 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007286 JUMP_IF_FALSE, cctx) == FAIL)
7287 return FAIL;
7288
7289 return p;
7290}
7291
7292/*
7293 * compile "endwhile"
7294 */
7295 static char_u *
7296compile_endwhile(char_u *arg, cctx_T *cctx)
7297{
7298 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007299 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007300
7301 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7302 {
7303 emsg(_(e_while));
7304 return NULL;
7305 }
7306 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007307 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007308
Bram Moolenaarf002a412021-01-24 13:34:18 +01007309#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007310 // count the endwhile before jumping
7311 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007312#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007313
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007314 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007315 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007316
7317 // Fill in the "end" label in the WHILE statement so it can jump here.
7318 // And in any jumps for ":break"
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007319 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
7320 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007321
7322 vim_free(scope);
7323
7324 return arg;
7325}
7326
7327/*
7328 * compile "continue"
7329 */
7330 static char_u *
7331compile_continue(char_u *arg, cctx_T *cctx)
7332{
7333 scope_T *scope = cctx->ctx_scope;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007334 int try_scopes = 0;
7335 int loop_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007336
7337 for (;;)
7338 {
7339 if (scope == NULL)
7340 {
7341 emsg(_(e_continue));
7342 return NULL;
7343 }
Bram Moolenaarc150c092021-02-13 15:02:46 +01007344 if (scope->se_type == FOR_SCOPE)
7345 {
7346 loop_label = scope->se_u.se_for.fs_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007347 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007348 }
7349 if (scope->se_type == WHILE_SCOPE)
7350 {
7351 loop_label = scope->se_u.se_while.ws_top_label;
7352 break;
7353 }
7354 if (scope->se_type == TRY_SCOPE)
7355 ++try_scopes;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 scope = scope->se_outer;
7357 }
7358
Bram Moolenaarc150c092021-02-13 15:02:46 +01007359 if (try_scopes > 0)
7360 // Inside one or more try/catch blocks we first need to jump to the
7361 // "finally" or "endtry" to cleanup.
7362 generate_TRYCONT(cctx, try_scopes, loop_label);
7363 else
7364 // Jump back to the FOR or WHILE instruction.
7365 generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
7366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007367 return arg;
7368}
7369
7370/*
7371 * compile "break"
7372 */
7373 static char_u *
7374compile_break(char_u *arg, cctx_T *cctx)
7375{
7376 scope_T *scope = cctx->ctx_scope;
7377 endlabel_T **el;
7378
7379 for (;;)
7380 {
7381 if (scope == NULL)
7382 {
7383 emsg(_(e_break));
7384 return NULL;
7385 }
7386 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7387 break;
7388 scope = scope->se_outer;
7389 }
7390
7391 // Jump to the end of the FOR or WHILE loop.
7392 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007393 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007395 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007396 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7397 return FAIL;
7398
7399 return arg;
7400}
7401
7402/*
7403 * compile "{" start of block
7404 */
7405 static char_u *
7406compile_block(char_u *arg, cctx_T *cctx)
7407{
7408 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7409 return NULL;
7410 return skipwhite(arg + 1);
7411}
7412
7413/*
7414 * compile end of block: drop one scope
7415 */
7416 static void
7417compile_endblock(cctx_T *cctx)
7418{
7419 scope_T *scope = cctx->ctx_scope;
7420
7421 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007422 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 vim_free(scope);
7424}
7425
7426/*
7427 * compile "try"
7428 * Creates a new scope for the try-endtry, pointing to the first catch and
7429 * finally.
7430 * Creates another scope for the "try" block itself.
7431 * TRY instruction sets up exception handling at runtime.
7432 *
7433 * "try"
7434 * TRY -> catch1, -> finally push trystack entry
7435 * ... try block
7436 * "throw {exception}"
7437 * EVAL {exception}
7438 * THROW create exception
7439 * ... try block
7440 * " catch {expr}"
7441 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007442 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 * EVAL {expr}
7444 * MATCH
7445 * JUMP nomatch -> catch2
7446 * CATCH remove exception
7447 * ... catch block
7448 * " catch"
7449 * JUMP -> finally
7450 * catch2: CATCH remove exception
7451 * ... catch block
7452 * " finally"
7453 * finally:
7454 * ... finally block
7455 * " endtry"
7456 * ENDTRY pop trystack entry, may rethrow
7457 */
7458 static char_u *
7459compile_try(char_u *arg, cctx_T *cctx)
7460{
7461 garray_T *instr = &cctx->ctx_instr;
7462 scope_T *try_scope;
7463 scope_T *scope;
7464
7465 // scope that holds the jumps that go to catch/finally/endtry
7466 try_scope = new_scope(cctx, TRY_SCOPE);
7467 if (try_scope == NULL)
7468 return NULL;
7469
Bram Moolenaar69f70502021-01-01 16:10:46 +01007470 if (cctx->ctx_skip != SKIP_YES)
7471 {
7472 // "catch" is set when the first ":catch" is found.
7473 // "finally" is set when ":finally" or ":endtry" is found
7474 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7475 if (generate_instr(cctx, ISN_TRY) == NULL)
7476 return NULL;
7477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478
7479 // scope for the try block itself
7480 scope = new_scope(cctx, BLOCK_SCOPE);
7481 if (scope == NULL)
7482 return NULL;
7483
7484 return arg;
7485}
7486
7487/*
7488 * compile "catch {expr}"
7489 */
7490 static char_u *
7491compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7492{
7493 scope_T *scope = cctx->ctx_scope;
7494 garray_T *instr = &cctx->ctx_instr;
7495 char_u *p;
7496 isn_T *isn;
7497
7498 // end block scope from :try or :catch
7499 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7500 compile_endblock(cctx);
7501 scope = cctx->ctx_scope;
7502
7503 // Error if not in a :try scope
7504 if (scope == NULL || scope->se_type != TRY_SCOPE)
7505 {
7506 emsg(_(e_catch));
7507 return NULL;
7508 }
7509
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007510 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007512 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007513 return NULL;
7514 }
7515
Bram Moolenaar69f70502021-01-01 16:10:46 +01007516 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007517 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007518#ifdef FEAT_PROFILE
7519 // the profile-start should be after the jump
7520 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7521 .isn_type == ISN_PROF_START)
7522 --instr->ga_len;
7523#endif
Bram Moolenaar69f70502021-01-01 16:10:46 +01007524 // Jump from end of previous block to :finally or :endtry
7525 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7526 JUMP_ALWAYS, cctx) == FAIL)
7527 return NULL;
7528
7529 // End :try or :catch scope: set value in ISN_TRY instruction
7530 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7531 if (isn->isn_arg.try.try_catch == 0)
7532 isn->isn_arg.try.try_catch = instr->ga_len;
7533 if (scope->se_u.se_try.ts_catch_label != 0)
7534 {
7535 // Previous catch without match jumps here
7536 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7537 isn->isn_arg.jump.jump_where = instr->ga_len;
7538 }
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007539#ifdef FEAT_PROFILE
7540 if (cctx->ctx_profiling)
7541 {
7542 // a "throw" that jumps here needs to be counted
7543 generate_instr(cctx, ISN_PROF_END);
7544 // the "catch" is also counted
7545 generate_instr(cctx, ISN_PROF_START);
7546 }
7547#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 }
7549
7550 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007551 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007553 scope->se_u.se_try.ts_caught_all = TRUE;
7554 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 }
7556 else
7557 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007558 char_u *end;
7559 char_u *pat;
7560 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007561 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007562 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 // Push v:exception, push {expr} and MATCH
7565 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7566
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007567 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007568 if (*end != *p)
7569 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007570 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007571 vim_free(tofree);
7572 return FAIL;
7573 }
7574 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007575 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007576 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007577 len = (int)(end - tofree);
7578 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007579 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007580 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007581 if (pat == NULL)
7582 return FAIL;
7583 if (generate_PUSHS(cctx, pat) == FAIL)
7584 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007586 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7587 return NULL;
7588
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007589 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007590 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7591 return NULL;
7592 }
7593
Bram Moolenaar69f70502021-01-01 16:10:46 +01007594 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007595 return NULL;
7596
7597 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7598 return NULL;
7599 return p;
7600}
7601
7602 static char_u *
7603compile_finally(char_u *arg, cctx_T *cctx)
7604{
7605 scope_T *scope = cctx->ctx_scope;
7606 garray_T *instr = &cctx->ctx_instr;
7607 isn_T *isn;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007608 int this_instr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007609
7610 // end block scope from :try or :catch
7611 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7612 compile_endblock(cctx);
7613 scope = cctx->ctx_scope;
7614
7615 // Error if not in a :try scope
7616 if (scope == NULL || scope->se_type != TRY_SCOPE)
7617 {
7618 emsg(_(e_finally));
7619 return NULL;
7620 }
7621
7622 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007623 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 if (isn->isn_arg.try.try_finally != 0)
7625 {
7626 emsg(_(e_finally_dup));
7627 return NULL;
7628 }
7629
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007630 this_instr = instr->ga_len;
7631#ifdef FEAT_PROFILE
7632 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7633 .isn_type == ISN_PROF_START)
7634 // jump to the profile start of the "finally"
7635 --this_instr;
7636#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007637
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007638 // Fill in the "end" label in jumps at the end of the blocks.
7639 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7640 this_instr, cctx);
7641
7642 isn->isn_arg.try.try_finally = this_instr;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007643 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007644 {
7645 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007646 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007647 isn->isn_arg.jump.jump_where = this_instr;
Bram Moolenaare8593122020-07-18 15:17:02 +02007648 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007649 }
7650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 // TODO: set index in ts_finally_label jumps
7652
7653 return arg;
7654}
7655
7656 static char_u *
7657compile_endtry(char_u *arg, cctx_T *cctx)
7658{
7659 scope_T *scope = cctx->ctx_scope;
7660 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007661 isn_T *try_isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007662
7663 // end block scope from :catch or :finally
7664 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7665 compile_endblock(cctx);
7666 scope = cctx->ctx_scope;
7667
7668 // Error if not in a :try scope
7669 if (scope == NULL || scope->se_type != TRY_SCOPE)
7670 {
7671 if (scope == NULL)
7672 emsg(_(e_no_endtry));
7673 else if (scope->se_type == WHILE_SCOPE)
7674 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007675 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 emsg(_(e_endfor));
7677 else
7678 emsg(_(e_endif));
7679 return NULL;
7680 }
7681
Bram Moolenaarc150c092021-02-13 15:02:46 +01007682 try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007683 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 {
Bram Moolenaarc150c092021-02-13 15:02:46 +01007685 if (try_isn->isn_arg.try.try_catch == 0
7686 && try_isn->isn_arg.try.try_finally == 0)
Bram Moolenaar69f70502021-01-01 16:10:46 +01007687 {
7688 emsg(_(e_missing_catch_or_finally));
7689 return NULL;
7690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007692#ifdef FEAT_PROFILE
7693 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7694 .isn_type == ISN_PROF_START)
7695 // move the profile start after "endtry" so that it's not counted when
7696 // the exception is rethrown.
7697 --instr->ga_len;
7698#endif
7699
Bram Moolenaar69f70502021-01-01 16:10:46 +01007700 // Fill in the "end" label in jumps at the end of the blocks, if not
7701 // done by ":finally".
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007702 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
7703 instr->ga_len, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007704
Bram Moolenaar69f70502021-01-01 16:10:46 +01007705 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaarc150c092021-02-13 15:02:46 +01007706 if (try_isn->isn_arg.try.try_catch == 0)
7707 try_isn->isn_arg.try.try_catch = instr->ga_len;
7708 if (try_isn->isn_arg.try.try_finally == 0)
7709 try_isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007710
Bram Moolenaar69f70502021-01-01 16:10:46 +01007711 if (scope->se_u.se_try.ts_catch_label != 0)
7712 {
7713 // Last catch without match jumps here
Bram Moolenaarc150c092021-02-13 15:02:46 +01007714 isn_T *isn = ((isn_T *)instr->ga_data)
7715 + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar69f70502021-01-01 16:10:46 +01007716 isn->isn_arg.jump.jump_where = instr->ga_len;
7717 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007718 }
7719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720 compile_endblock(cctx);
7721
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007722 if (cctx->ctx_skip != SKIP_YES)
7723 {
7724 if (try_isn->isn_arg.try.try_finally == 0)
7725 // No :finally encountered, use the try_finaly field to point to
7726 // ENDTRY, so that TRYCONT can jump there.
7727 try_isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaarc150c092021-02-13 15:02:46 +01007728
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007729 if (cctx->ctx_skip != SKIP_YES
7730 && generate_instr(cctx, ISN_ENDTRY) == NULL)
7731 return NULL;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01007732#ifdef FEAT_PROFILE
7733 if (cctx->ctx_profiling)
7734 generate_instr(cctx, ISN_PROF_START);
7735#endif
Bram Moolenaar4afa7742021-02-14 16:34:59 +01007736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007737 return arg;
7738}
7739
7740/*
7741 * compile "throw {expr}"
7742 */
7743 static char_u *
7744compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7745{
7746 char_u *p = skipwhite(arg);
7747
Bram Moolenaara5565e42020-05-09 15:44:01 +02007748 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007749 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007750 if (cctx->ctx_skip == SKIP_YES)
7751 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007752 if (may_generate_2STRING(-1, cctx) == FAIL)
7753 return NULL;
7754 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7755 return NULL;
7756
7757 return p;
7758}
7759
7760/*
7761 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007762 * compile "echomsg expr"
7763 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007764 * compile "execute expr"
7765 */
7766 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007767compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007768{
7769 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007770 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007771 int count = 0;
7772
7773 for (;;)
7774 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007775 if (ends_excmd2(prev, p))
7776 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007777 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007778 return NULL;
7779 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007780 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007781 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007782 }
7783
Bram Moolenaare4984292020-12-13 14:19:25 +01007784 if (count > 0)
7785 {
7786 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7787 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7788 else if (cmdidx == CMD_execute)
7789 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7790 else if (cmdidx == CMD_echomsg)
7791 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7792 else
7793 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 return p;
7796}
7797
7798/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007799 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007800 * instruction to compute it and return OK.
7801 * Otherwise return FAIL, the caller must deal with any range.
7802 */
7803 static int
7804compile_variable_range(exarg_T *eap, cctx_T *cctx)
7805{
7806 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7807 char_u *p = skipdigits(eap->cmd);
7808
7809 if (p == range_end)
7810 return FAIL;
7811 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7812}
7813
7814/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007815 * :put r
7816 * :put ={expr}
7817 */
7818 static char_u *
7819compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7820{
7821 char_u *line = arg;
7822 linenr_T lnum;
7823 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007824 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007825
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007826 eap->regname = *line;
7827
7828 if (eap->regname == '=')
7829 {
7830 char_u *p = line + 1;
7831
7832 if (compile_expr0(&p, cctx) == FAIL)
7833 return NULL;
7834 line = p;
7835 }
7836 else if (eap->regname != NUL)
7837 ++line;
7838
Bram Moolenaar08597872020-12-10 19:43:40 +01007839 if (compile_variable_range(eap, cctx) == OK)
7840 {
7841 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7842 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007843 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007844 {
7845 // Either no range or a number.
7846 // "errormsg" will not be set because the range is ADDR_LINES.
7847 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007848 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007849 return NULL;
7850 if (eap->addr_count == 0)
7851 lnum = -1;
7852 else
7853 lnum = eap->line2;
7854 if (above)
7855 --lnum;
7856 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007857
7858 generate_PUT(cctx, eap->regname, lnum);
7859 return line;
7860}
7861
7862/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007863 * A command that is not compiled, execute with legacy code.
7864 */
7865 static char_u *
7866compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7867{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007868 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007869 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007870 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007871
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007872 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007873 goto theend;
7874
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007875 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007876 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007877 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007878 int usefilter = FALSE;
7879
7880 has_expr = argt & (EX_XFILE | EX_EXPAND);
7881
7882 // If the command can be followed by a bar, find the bar and truncate
7883 // it, so that the following command can be compiled.
7884 // The '|' is overwritten with a NUL, it is put back below.
7885 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7886 && *eap->arg == '!')
7887 // :w !filter or :r !filter or :r! filter
7888 usefilter = TRUE;
7889 if ((argt & EX_TRLBAR) && !usefilter)
7890 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007891 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007892 separate_nextcmd(eap);
7893 if (eap->nextcmd != NULL)
7894 nextcmd = eap->nextcmd;
7895 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007896 else if (eap->cmdidx == CMD_wincmd)
7897 {
7898 p = eap->arg;
7899 if (*p != NUL)
7900 ++p;
7901 if (*p == 'g' || *p == Ctrl_G)
7902 ++p;
7903 p = skipwhite(p);
7904 if (*p == '|')
7905 {
7906 *p = NUL;
7907 nextcmd = p + 1;
7908 }
7909 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007910 }
7911
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007912 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7913 {
7914 // expand filename in "syntax include [@group] filename"
7915 has_expr = TRUE;
7916 eap->arg = skipwhite(eap->arg + 7);
7917 if (*eap->arg == '@')
7918 eap->arg = skiptowhite(eap->arg);
7919 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007920
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007921 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7922 && STRLEN(eap->arg) > 4)
7923 {
7924 int delim = *eap->arg;
7925
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007926 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007927 if (*p == delim)
7928 {
7929 eap->arg = p + 1;
7930 has_expr = TRUE;
7931 }
7932 }
7933
Bram Moolenaarecac5912021-01-05 19:23:28 +01007934 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7935 {
7936 // TODO: should only expand when appropriate for the command
7937 eap->arg = skiptowhite(eap->arg);
7938 has_expr = TRUE;
7939 }
7940
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007941 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007942 {
7943 int count = 0;
7944 char_u *start = skipwhite(line);
7945
7946 // :cmd xxx`=expr1`yyy`=expr2`zzz
7947 // PUSHS ":cmd xxx"
7948 // eval expr1
7949 // PUSHS "yyy"
7950 // eval expr2
7951 // PUSHS "zzz"
7952 // EXECCONCAT 5
7953 for (;;)
7954 {
7955 if (p > start)
7956 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007957 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007958 ++count;
7959 }
7960 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007961 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007962 return NULL;
7963 may_generate_2STRING(-1, cctx);
7964 ++count;
7965 p = skipwhite(p);
7966 if (*p != '`')
7967 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007968 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007969 return NULL;
7970 }
7971 start = p + 1;
7972
7973 p = (char_u *)strstr((char *)start, "`=");
7974 if (p == NULL)
7975 {
7976 if (*skipwhite(start) != NUL)
7977 {
7978 generate_PUSHS(cctx, vim_strsave(start));
7979 ++count;
7980 }
7981 break;
7982 }
7983 }
7984 generate_EXECCONCAT(cctx, count);
7985 }
7986 else
7987 generate_EXEC(cctx, line);
7988
7989theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007990 if (*nextcmd != NUL)
7991 {
7992 // the parser expects a pointer to the bar, put it back
7993 --nextcmd;
7994 *nextcmd = '|';
7995 }
7996
7997 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007998}
7999
8000/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02008001 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008002 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02008003 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008004 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02008005add_def_function(ufunc_T *ufunc)
8006{
8007 dfunc_T *dfunc;
8008
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008009 if (def_functions.ga_len == 0)
8010 {
8011 // The first position is not used, so that a zero uf_dfunc_idx means it
8012 // wasn't set.
8013 if (ga_grow(&def_functions, 1) == FAIL)
8014 return FAIL;
8015 ++def_functions.ga_len;
8016 }
8017
Bram Moolenaar09689a02020-05-09 22:50:08 +02008018 // Add the function to "def_functions".
8019 if (ga_grow(&def_functions, 1) == FAIL)
8020 return FAIL;
8021 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
8022 CLEAR_POINTER(dfunc);
8023 dfunc->df_idx = def_functions.ga_len;
8024 ufunc->uf_dfunc_idx = dfunc->df_idx;
8025 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008026 dfunc->df_name = vim_strsave(ufunc->uf_name);
8027 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02008028 ++def_functions.ga_len;
8029 return OK;
8030}
8031
8032/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008033 * After ex_function() has collected all the function lines: parse and compile
8034 * the lines into instructions.
8035 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008036 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
8037 * the return statement (used for lambda). When uf_ret_type is already set
8038 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01008039 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008040 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008041 * This can be used recursively through compile_lambda(), which may reallocate
8042 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02008043 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008044 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02008045 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01008046compile_def_function(
8047 ufunc_T *ufunc,
8048 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01008049 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01008050 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008051{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008052 char_u *line = NULL;
8053 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008054 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008055 cctx_T cctx;
8056 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01008057 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008058 int ret = FAIL;
8059 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008060 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008061 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008062 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008063#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008064 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008065#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008067 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008068 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008069 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008070 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02008071 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8072 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008073 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008074 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008075 else
8076 {
8077 if (add_def_function(ufunc) == FAIL)
8078 return FAIL;
8079 new_def_function = TRUE;
8080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081
Bram Moolenaar985116a2020-07-12 17:31:09 +02008082 ufunc->uf_def_status = UF_COMPILING;
8083
Bram Moolenaara80faa82020-04-12 19:37:17 +02008084 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01008085
Bram Moolenaarf002a412021-01-24 13:34:18 +01008086#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008087 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008088#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 cctx.ctx_ufunc = ufunc;
8090 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008091 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008092 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
8093 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
8094 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
8095 cctx.ctx_type_list = &ufunc->uf_type_list;
8096 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
8097 instr = &cctx.ctx_instr;
8098
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008099 // Set the context to the function, it may be compiled when called from
8100 // another script. Set the script version to the most modern one.
8101 // The line number will be set in next_line_from_context().
8102 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
8104
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008105 // Make sure error messages are OK.
8106 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
8107 if (do_estack_push)
8108 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008109 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008110
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008111 if (ufunc->uf_def_args.ga_len > 0)
8112 {
8113 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008114 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008115 int i;
8116 char_u *arg;
8117 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008118 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008119
8120 // Produce instructions for the default values of optional arguments.
8121 // Store the instruction index in uf_def_arg_idx[] so that we know
8122 // where to start when the function is called, depending on the number
8123 // of arguments.
8124 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
8125 if (ufunc->uf_def_arg_idx == NULL)
8126 goto erret;
8127 for (i = 0; i < count; ++i)
8128 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008129 garray_T *stack = &cctx.ctx_type_stack;
8130 type_T *val_type;
8131 int arg_idx = first_def_arg + i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008132 where_T where;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008133
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008134 ufunc->uf_def_arg_idx[i] = instr->ga_len;
8135 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02008136 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008137 goto erret;
8138
8139 // If no type specified use the type of the default value.
8140 // Otherwise check that the default value type matches the
8141 // specified type.
8142 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008143 where.wt_index = arg_idx + 1;
8144 where.wt_variable = FALSE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008145 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008146 {
8147 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008148 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008149 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02008150 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01008151 TRUE, where) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008152 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02008153
8154 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008155 goto erret;
8156 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008157 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02008158
8159 if (did_set_arg_type)
8160 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01008161 }
8162
8163 /*
8164 * Loop over all the lines of the function and generate instructions.
8165 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 for (;;)
8167 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008168 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02008169 int starts_with_colon = FALSE;
8170 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02008171 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008172
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008173 // Bail out on the first error to avoid a flood of errors and report
8174 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01008175 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02008176 goto erret;
8177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 if (line != NULL && *line == '|')
8179 // the line continues after a '|'
8180 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02008181 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02008182 && !(*line == '#' && (line == cctx.ctx_line_start
8183 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02008185 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 goto erret;
8187 }
8188 else
8189 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008190 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008191 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008192 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008193 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008194#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008195 if (cctx.ctx_skip != SKIP_YES)
8196 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008197#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 }
8201
Bram Moolenaara80faa82020-04-12 19:37:17 +02008202 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008203 ea.cmdlinep = &line;
8204 ea.cmd = skipwhite(line);
8205
Bram Moolenaarb2049902021-01-24 12:53:53 +01008206 if (*ea.cmd == '#')
8207 {
8208 // "#" starts a comment
8209 line = (char_u *)"";
8210 continue;
8211 }
8212
Bram Moolenaarf002a412021-01-24 13:34:18 +01008213#ifdef FEAT_PROFILE
Bram Moolenaarced68a02021-01-24 17:53:47 +01008214 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum &&
8215 cctx.ctx_skip != SKIP_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008216 {
8217 may_generate_prof_end(&cctx, prof_lnum);
8218
8219 prof_lnum = cctx.ctx_lnum;
8220 generate_instr(&cctx, ISN_PROF_START);
8221 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008222#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008223
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008224 // Some things can be recognized by the first character.
8225 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008226 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008227 case '}':
8228 {
8229 // "}" ends a block scope
8230 scopetype_T stype = cctx.ctx_scope == NULL
8231 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008232
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008233 if (stype == BLOCK_SCOPE)
8234 {
8235 compile_endblock(&cctx);
8236 line = ea.cmd;
8237 }
8238 else
8239 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008240 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008241 goto erret;
8242 }
8243 if (line != NULL)
8244 line = skipwhite(ea.cmd + 1);
8245 continue;
8246 }
8247
8248 case '{':
8249 // "{" starts a block scope
8250 // "{'a': 1}->func() is something else
8251 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8252 {
8253 line = compile_block(ea.cmd, &cctx);
8254 continue;
8255 }
8256 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257 }
8258
8259 /*
8260 * COMMAND MODIFIERS
8261 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008262 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008263 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8264 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008265 {
8266 if (errormsg != NULL)
8267 goto erret;
8268 // empty line or comment
8269 line = (char_u *)"";
8270 continue;
8271 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008272 generate_cmdmods(&cctx, &local_cmdmod);
8273 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008275 // Check if there was a colon after the last command modifier or before
8276 // the current position.
8277 for (p = ea.cmd; p >= line; --p)
8278 {
8279 if (*p == ':')
8280 starts_with_colon = TRUE;
8281 if (p < ea.cmd && !VIM_ISWHITE(*p))
8282 break;
8283 }
8284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008285 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008286 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008288 {
8289 if (*ea.cmd == '(')
8290 // not for "call()"
8291 ea.cmd = p;
8292 else
8293 ea.cmd = skipwhite(ea.cmd);
8294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008295
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008296 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008297 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008298 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008299
Bram Moolenaar17126b12021-01-07 22:03:02 +01008300 // Check for assignment after command modifiers.
8301 assign = may_compile_assignment(&ea, &line, &cctx);
8302 if (assign == OK)
8303 goto nextline;
8304 if (assign == FAIL)
8305 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 }
8307
8308 /*
8309 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008310 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008312 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008313 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008314 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008315 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008316 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008317 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008318 if (!starts_with_colon)
8319 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008320 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008321 goto erret;
8322 }
8323 if (ends_excmd2(line, ea.cmd))
8324 {
8325 // A range without a command: jump to the line.
8326 // TODO: compile to a more efficient command, possibly
8327 // calling parse_cmd_address().
8328 ea.cmdidx = CMD_SIZE;
8329 line = compile_exec(line, &ea, &cctx);
8330 goto nextline;
8331 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008332 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008333 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008334 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008335 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008336 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008337
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008338 if (p == NULL)
8339 {
8340 if (cctx.ctx_skip != SKIP_YES)
8341 emsg(_(e_ambiguous_use_of_user_defined_command));
8342 goto erret;
8343 }
8344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008345 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8346 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008347 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008348 {
8349 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008350 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008351 }
8352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008353 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008354 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008355 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008356 // CMD_var cannot happen, compile_assignment() above would be
8357 // used. Most likely an assignment to a non-existing variable.
8358 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008359 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 }
8362
Bram Moolenaar3988f642020-08-27 22:43:03 +02008363 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008364 && ea.cmdidx != CMD_elseif
8365 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008366 && ea.cmdidx != CMD_endif
8367 && ea.cmdidx != CMD_endfor
8368 && ea.cmdidx != CMD_endwhile
8369 && ea.cmdidx != CMD_catch
8370 && ea.cmdidx != CMD_finally
8371 && ea.cmdidx != CMD_endtry)
8372 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008373 emsg(_(e_unreachable_code_after_return));
8374 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008375 }
8376
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008377 p = skipwhite(p);
8378 if (ea.cmdidx != CMD_SIZE
8379 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8380 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008381 if (ea.cmdidx >= 0)
8382 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008383 if ((ea.argt & EX_BANG) && *p == '!')
8384 {
8385 ea.forceit = TRUE;
8386 p = skipwhite(p + 1);
8387 }
8388 }
8389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008390 switch (ea.cmdidx)
8391 {
8392 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008393 ea.arg = p;
8394 line = compile_nested_function(&ea, &cctx);
8395 break;
8396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008397 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008398 // TODO: should we allow this, e.g. to declare a global
8399 // function?
8400 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008401 goto erret;
8402
8403 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008404 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008405 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008406 break;
8407
8408 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008409 emsg(_(e_cannot_use_let_in_vim9_script));
8410 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008411 case CMD_var:
8412 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008413 case CMD_const:
8414 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008415 if (line == p)
8416 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008417 break;
8418
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008419 case CMD_unlet:
8420 case CMD_unlockvar:
8421 case CMD_lockvar:
8422 line = compile_unletlock(p, &ea, &cctx);
8423 break;
8424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008425 case CMD_import:
8426 line = compile_import(p, &cctx);
8427 break;
8428
8429 case CMD_if:
8430 line = compile_if(p, &cctx);
8431 break;
8432 case CMD_elseif:
8433 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008434 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008435 break;
8436 case CMD_else:
8437 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008438 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 break;
8440 case CMD_endif:
8441 line = compile_endif(p, &cctx);
8442 break;
8443
8444 case CMD_while:
8445 line = compile_while(p, &cctx);
8446 break;
8447 case CMD_endwhile:
8448 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008449 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008450 break;
8451
8452 case CMD_for:
8453 line = compile_for(p, &cctx);
8454 break;
8455 case CMD_endfor:
8456 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008457 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008458 break;
8459 case CMD_continue:
8460 line = compile_continue(p, &cctx);
8461 break;
8462 case CMD_break:
8463 line = compile_break(p, &cctx);
8464 break;
8465
8466 case CMD_try:
8467 line = compile_try(p, &cctx);
8468 break;
8469 case CMD_catch:
8470 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008471 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008472 break;
8473 case CMD_finally:
8474 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008475 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008476 break;
8477 case CMD_endtry:
8478 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008479 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008480 break;
8481 case CMD_throw:
8482 line = compile_throw(p, &cctx);
8483 break;
8484
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008485 case CMD_eval:
8486 if (compile_expr0(&p, &cctx) == FAIL)
8487 goto erret;
8488
Bram Moolenaar3988f642020-08-27 22:43:03 +02008489 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008490 generate_instr_drop(&cctx, ISN_DROP, 1);
8491
8492 line = skipwhite(p);
8493 break;
8494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008495 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008496 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008497 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008498 case CMD_echomsg:
8499 case CMD_echoerr:
8500 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008501 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008503 case CMD_put:
8504 ea.cmd = cmd;
8505 line = compile_put(p, &ea, &cctx);
8506 break;
8507
Bram Moolenaar3988f642020-08-27 22:43:03 +02008508 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008509
Bram Moolenaarae616492020-07-28 20:07:27 +02008510 case CMD_append:
8511 case CMD_change:
8512 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008513 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008514 case CMD_xit:
8515 not_in_vim9(&ea);
8516 goto erret;
8517
Bram Moolenaar002262f2020-07-08 17:47:57 +02008518 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008519 if (cctx.ctx_skip != SKIP_YES)
8520 {
8521 semsg(_(e_invalid_command_str), ea.cmd);
8522 goto erret;
8523 }
8524 // We don't check for a next command here.
8525 line = (char_u *)"";
8526 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008529 if (cctx.ctx_skip == SKIP_YES)
8530 {
8531 // We don't check for a next command here.
8532 line = (char_u *)"";
8533 }
8534 else
8535 {
8536 // Not recognized, execute with do_cmdline_cmd().
8537 ea.arg = p;
8538 line = compile_exec(line, &ea, &cctx);
8539 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008540 break;
8541 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008542nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008543 if (line == NULL)
8544 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008545 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008546
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008547 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008548 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008550 if (cctx.ctx_type_stack.ga_len < 0)
8551 {
8552 iemsg("Type stack underflow");
8553 goto erret;
8554 }
8555 }
8556
8557 if (cctx.ctx_scope != NULL)
8558 {
8559 if (cctx.ctx_scope->se_type == IF_SCOPE)
8560 emsg(_(e_endif));
8561 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8562 emsg(_(e_endwhile));
8563 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8564 emsg(_(e_endfor));
8565 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008566 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008567 goto erret;
8568 }
8569
Bram Moolenaarefd88552020-06-18 20:50:10 +02008570 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008571 {
8572 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8573 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008574 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008575 goto erret;
8576 }
8577
8578 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008579 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008580 }
8581
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008582 {
8583 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8584 + ufunc->uf_dfunc_idx;
8585 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008586 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008587#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008588 if (cctx.ctx_profiling)
8589 {
8590 dfunc->df_instr_prof = instr->ga_data;
8591 dfunc->df_instr_prof_count = instr->ga_len;
8592 }
8593 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008594#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008595 {
8596 dfunc->df_instr = instr->ga_data;
8597 dfunc->df_instr_count = instr->ga_len;
8598 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008599 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008600 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008601 if (cctx.ctx_outer_used)
8602 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008603 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008605
8606 ret = OK;
8607
8608erret:
8609 if (ret == FAIL)
8610 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008611 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008612 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8613 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008614
8615 for (idx = 0; idx < instr->ga_len; ++idx)
8616 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008617 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008618 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008619
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008620 // If using the last entry in the table and it was added above, we
8621 // might as well remove it.
8622 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008623 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008624 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008625 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008626 ufunc->uf_dfunc_idx = 0;
8627 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008628 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008629
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008630 while (cctx.ctx_scope != NULL)
8631 drop_scope(&cctx);
8632
Bram Moolenaar20431c92020-03-20 18:39:46 +01008633 // Don't execute this function body.
8634 ga_clear_strings(&ufunc->uf_lines);
8635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008636 if (errormsg != NULL)
8637 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008638 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008639 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008640 }
8641
8642 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008643 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008644 if (do_estack_push)
8645 estack_pop();
8646
Bram Moolenaar20431c92020-03-20 18:39:46 +01008647 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008648 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008649 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008650 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651}
8652
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008653 void
8654set_function_type(ufunc_T *ufunc)
8655{
8656 int varargs = ufunc->uf_va_name != NULL;
8657 int argcount = ufunc->uf_args.ga_len;
8658
8659 // Create a type for the function, with the return type and any
8660 // argument types.
8661 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8662 // The type is included in "tt_args".
8663 if (argcount > 0 || varargs)
8664 {
8665 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8666 argcount, &ufunc->uf_type_list);
8667 // Add argument types to the function type.
8668 if (func_type_add_arg_types(ufunc->uf_func_type,
8669 argcount + varargs,
8670 &ufunc->uf_type_list) == FAIL)
8671 return;
8672 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8673 ufunc->uf_func_type->tt_min_argcount =
8674 argcount - ufunc->uf_def_args.ga_len;
8675 if (ufunc->uf_arg_types == NULL)
8676 {
8677 int i;
8678
8679 // lambda does not have argument types.
8680 for (i = 0; i < argcount; ++i)
8681 ufunc->uf_func_type->tt_args[i] = &t_any;
8682 }
8683 else
8684 mch_memmove(ufunc->uf_func_type->tt_args,
8685 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8686 if (varargs)
8687 {
8688 ufunc->uf_func_type->tt_args[argcount] =
8689 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8690 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8691 }
8692 }
8693 else
8694 // No arguments, can use a predefined type.
8695 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8696 argcount, &ufunc->uf_type_list);
8697}
8698
8699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008700/*
8701 * Delete an instruction, free what it contains.
8702 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008703 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008704delete_instr(isn_T *isn)
8705{
8706 switch (isn->isn_type)
8707 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008708 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008709 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008710 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008711 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008712 case ISN_LOADENV:
8713 case ISN_LOADG:
8714 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008715 case ISN_LOADT:
8716 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008717 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008718 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008719 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008720 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008721 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008722 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008723 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008724 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008725 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008726 case ISN_STOREW:
8727 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008728 vim_free(isn->isn_arg.string);
8729 break;
8730
8731 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008732 case ISN_STORES:
8733 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008734 break;
8735
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008736 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008737 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008738 vim_free(isn->isn_arg.unlet.ul_name);
8739 break;
8740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741 case ISN_STOREOPT:
8742 vim_free(isn->isn_arg.storeopt.so_name);
8743 break;
8744
8745 case ISN_PUSHBLOB: // push blob isn_arg.blob
8746 blob_unref(isn->isn_arg.blob);
8747 break;
8748
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008749 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008751 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008752#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008753 break;
8754
8755 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008756#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008757 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008758#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008759 break;
8760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008761 case ISN_UCALL:
8762 vim_free(isn->isn_arg.ufunc.cuf_name);
8763 break;
8764
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008765 case ISN_FUNCREF:
8766 {
8767 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8768 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008769 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008770
Bram Moolenaar077a4232020-12-22 18:33:27 +01008771 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8772 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008773 }
8774 break;
8775
8776 case ISN_DCALL:
8777 {
8778 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8779 + isn->isn_arg.dfunc.cdf_idx;
8780
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008781 if (dfunc->df_ufunc != NULL
8782 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008783 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008784 }
8785 break;
8786
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008787 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008788 {
8789 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8790 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8791
8792 if (ufunc != NULL)
8793 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008794 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008795 func_ptr_unref(ufunc);
8796 }
8797
8798 vim_free(lambda);
8799 vim_free(isn->isn_arg.newfunc.nf_global);
8800 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008801 break;
8802
Bram Moolenaar5e654232020-09-16 15:22:00 +02008803 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008804 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008805 free_type(isn->isn_arg.type.ct_type);
8806 break;
8807
Bram Moolenaar02194d22020-10-24 23:08:38 +02008808 case ISN_CMDMOD:
8809 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8810 ->cmod_filter_regmatch.regprog);
8811 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8812 break;
8813
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008814 case ISN_LOADSCRIPT:
8815 case ISN_STORESCRIPT:
8816 vim_free(isn->isn_arg.script.scriptref);
8817 break;
8818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008819 case ISN_2BOOL:
8820 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008821 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008822 case ISN_ADDBLOB:
8823 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008824 case ISN_ANYINDEX:
8825 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008826 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008827 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008828 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008829 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008830 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008831 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008832 case ISN_COMPAREANY:
8833 case ISN_COMPAREBLOB:
8834 case ISN_COMPAREBOOL:
8835 case ISN_COMPAREDICT:
8836 case ISN_COMPAREFLOAT:
8837 case ISN_COMPAREFUNC:
8838 case ISN_COMPARELIST:
8839 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008840 case ISN_COMPARESPECIAL:
8841 case ISN_COMPARESTRING:
8842 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008843 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008844 case ISN_DROP:
8845 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008846 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008847 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008848 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008849 case ISN_EXECCONCAT:
8850 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008851 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008852 case ISN_GETITEM:
8853 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008854 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008855 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008856 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008857 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008858 case ISN_LOADBDICT:
8859 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008860 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008861 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008862 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008863 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008864 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008865 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008866 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008867 case ISN_NEGATENR:
8868 case ISN_NEWDICT:
8869 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008870 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008871 case ISN_OPFLOAT:
8872 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008873 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008874 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008875 case ISN_PROF_END:
8876 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008877 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008878 case ISN_PUSHF:
8879 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008880 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008881 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008882 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008883 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008884 case ISN_SHUFFLE:
8885 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008886 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008887 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008888 case ISN_STORENR:
8889 case ISN_STOREOUTER:
8890 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008891 case ISN_STOREV:
8892 case ISN_STRINDEX:
8893 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008894 case ISN_THROW:
8895 case ISN_TRY:
Bram Moolenaarc150c092021-02-13 15:02:46 +01008896 case ISN_TRYCONT:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008897 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008898 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008899 // nothing allocated
8900 break;
8901 }
8902}
8903
8904/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008905 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008906 */
8907 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008908delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008909{
8910 int idx;
8911
8912 ga_clear(&dfunc->df_def_args_isn);
8913
8914 if (dfunc->df_instr != NULL)
8915 {
8916 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8917 delete_instr(dfunc->df_instr + idx);
8918 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008919 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008920 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01008921#ifdef FEAT_PROFILE
8922 if (dfunc->df_instr_prof != NULL)
8923 {
8924 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
8925 delete_instr(dfunc->df_instr_prof + idx);
8926 VIM_CLEAR(dfunc->df_instr_prof);
8927 dfunc->df_instr_prof = NULL;
8928 }
8929#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01008930
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008931 if (mark_deleted)
8932 dfunc->df_deleted = TRUE;
8933 if (dfunc->df_ufunc != NULL)
8934 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008935}
8936
8937/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008938 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008939 * function, unless another user function still uses it.
8940 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008941 */
8942 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008943unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008944{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008945 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008946 {
8947 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8948 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008949
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008950 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008951 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008952 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008953 ufunc->uf_dfunc_idx = 0;
8954 if (dfunc->df_ufunc == ufunc)
8955 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008956 }
8957}
8958
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008959/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008960 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008961 */
8962 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008963link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008964{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008965 if (ufunc->uf_dfunc_idx > 0)
8966 {
8967 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8968 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008969
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008970 ++dfunc->df_refcount;
8971 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008972}
8973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008974#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008975/*
8976 * Free all functions defined with ":def".
8977 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008978 void
8979free_def_functions(void)
8980{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008981 int idx;
8982
8983 for (idx = 0; idx < def_functions.ga_len; ++idx)
8984 {
8985 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8986
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008987 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008988 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008989 }
8990
8991 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008992}
8993#endif
8994
8995
8996#endif // FEAT_EVAL