blob: 5a7da1247a7eff912e8797506a73905f6e191668 [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;
47 int is_had_return; // every block ends in :return
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048 int is_if_label; // instruction idx at IF or ELSEIF
49 endlabel_T *is_end_label; // instructions to set end label
50} ifscope_T;
51
52/*
53 * info specific for the scope of :while
54 */
55typedef struct {
56 int ws_top_label; // instruction idx at WHILE
57 endlabel_T *ws_end_label; // instructions to set end
58} whilescope_T;
59
60/*
61 * info specific for the scope of :for
62 */
63typedef struct {
64 int fs_top_label; // instruction idx at FOR
65 endlabel_T *fs_end_label; // break instructions
66} forscope_T;
67
68/*
69 * info specific for the scope of :try
70 */
71typedef struct {
72 int ts_try_label; // instruction idx at TRY
73 endlabel_T *ts_end_label; // jump to :finally or :endtry
74 int ts_catch_label; // instruction idx of last CATCH
75 int ts_caught_all; // "catch" without argument encountered
76} tryscope_T;
77
78typedef enum {
79 NO_SCOPE,
80 IF_SCOPE,
81 WHILE_SCOPE,
82 FOR_SCOPE,
83 TRY_SCOPE,
84 BLOCK_SCOPE
85} scopetype_T;
86
87/*
88 * Info for one scope, pointed to by "ctx_scope".
89 */
90typedef struct scope_S scope_T;
91struct scope_S {
92 scope_T *se_outer; // scope containing this one
93 scopetype_T se_type;
94 int se_local_count; // ctx_locals.ga_len before scope
Bram Moolenaarefd88552020-06-18 20:50:10 +020095 skip_T se_skip_save; // ctx_skip before the block
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 union {
97 ifscope_T se_if;
98 whilescope_T se_while;
99 forscope_T se_for;
100 tryscope_T se_try;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100101 } se_u;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102};
103
104/*
105 * Entry for "ctx_locals". Used for arguments and local variables.
106 */
107typedef struct {
108 char_u *lv_name;
109 type_T *lv_type;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200110 int lv_idx; // index of the variable on the stack
Bram Moolenaarab360522021-01-10 14:02:28 +0100111 int lv_from_outer; // nesting level, using ctx_outer scope
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200112 int lv_const; // when TRUE cannot be assigned to
113 int lv_arg; // when TRUE this is an argument
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114} lvar_T;
115
116/*
117 * Context for compiling lines of Vim script.
118 * Stores info about the local variables and condition stack.
119 */
120struct cctx_S {
121 ufunc_T *ctx_ufunc; // current function
122 int ctx_lnum; // line number in current function
Bram Moolenaar7a092242020-04-16 22:10:49 +0200123 char_u *ctx_line_start; // start of current line or NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 garray_T ctx_instr; // generated instructions
125
Bram Moolenaarb2049902021-01-24 12:53:53 +0100126 int ctx_profiling; // when TRUE generate ISN_PROF_START
127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200129 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100130
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200131 int ctx_has_closure; // set to one if a closures was created in
132 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 garray_T ctx_imports; // imported items
135
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200136 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200138 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200140 cctx_T *ctx_outer; // outer scope for lambda or nested
141 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200142 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100144 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200145 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200146
Bram Moolenaar02194d22020-10-24 23:08:38 +0200147 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148};
149
Bram Moolenaarcdc40c42020-12-26 17:43:08 +0100150static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151
152/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100153 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +0100154 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +0100155 * If "lvar" is NULL only check if the variable can be found.
156 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100158 static int
159lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100160{
161 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100162 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100164 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100165 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200166
167 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
169 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100170 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
171 if (STRNCMP(name, lvp->lv_name, len) == 0
172 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200173 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100174 if (lvar != NULL)
175 {
176 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100177 lvar->lv_from_outer = 0;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100178 }
179 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200182
183 // Find local in outer function scope.
184 if (cctx->ctx_outer != NULL)
185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200187 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100188 if (lvar != NULL)
189 {
190 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100191 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100192 }
193 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200194 }
195 }
196
Bram Moolenaar709664c2020-12-12 14:33:41 +0100197 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100198}
199
200/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200201 * Lookup an argument in the current function and an enclosing function.
202 * Returns the argument index in "idxp"
203 * Returns the argument type in "type"
204 * Sets "gen_load_outer" to TRUE if found in outer scope.
205 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206 */
207 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200208arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200209 char_u *name,
210 size_t len,
211 int *idxp,
212 type_T **type,
213 int *gen_load_outer,
214 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215{
216 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200217 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100219 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200220 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100221 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
222 {
223 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
224
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200225 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
226 {
227 if (idxp != NULL)
228 {
229 // Arguments are located above the frame pointer. One further
230 // if there is a vararg argument
231 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
232 + STACK_FRAME_SIZE)
233 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
234
235 if (cctx->ctx_ufunc->uf_arg_types != NULL)
236 *type = cctx->ctx_ufunc->uf_arg_types[idx];
237 else
238 *type = &t_any;
239 }
240 return OK;
241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200244 va_name = cctx->ctx_ufunc->uf_va_name;
245 if (va_name != NULL
246 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
247 {
248 if (idxp != NULL)
249 {
250 // varargs is always the last argument
251 *idxp = -STACK_FRAME_SIZE - 1;
252 *type = cctx->ctx_ufunc->uf_va_type;
253 }
254 return OK;
255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200257 if (cctx->ctx_outer != NULL)
258 {
259 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200260 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200261 == OK)
262 {
Bram Moolenaarab360522021-01-10 14:02:28 +0100263 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200264 return OK;
265 }
266 }
267
268 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269}
270
271/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200272 * Lookup a script-local variable in the current script, possibly defined in a
273 * block that contains the function "cctx->ctx_ufunc".
274 * "cctx" is NULL at the script level.
275 * if "len" is <= 0 "name" must be NUL terminated.
276 * Return NULL when not found.
277 */
278 static sallvar_T *
279find_script_var(char_u *name, size_t len, cctx_T *cctx)
280{
281 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
282 hashitem_T *hi;
283 int cc;
284 sallvar_T *sav;
285 ufunc_T *ufunc;
286
287 // Find the list of all script variables with the right name.
288 if (len > 0)
289 {
290 cc = name[len];
291 name[len] = NUL;
292 }
293 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
294 if (len > 0)
295 name[len] = cc;
296 if (HASHITEM_EMPTY(hi))
297 return NULL;
298
299 sav = HI2SAV(hi);
300 if (sav->sav_block_id == 0 || cctx == NULL)
301 // variable defined in the script scope or not in a function.
302 return sav;
303
304 // Go over the variables with this name and find one that was visible
305 // from the function.
306 ufunc = cctx->ctx_ufunc;
307 while (sav != NULL)
308 {
309 int idx;
310
311 // Go over the blocks that this function was defined in. If the
312 // variable block ID matches it was visible to the function.
313 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
314 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
315 return sav;
316 sav = sav->sav_next;
317 }
318
319 return NULL;
320}
321
322/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100323 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200324 */
325 static int
326script_is_vim9()
327{
328 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
329}
330
331/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200332 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200333 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
334 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200335 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Returns OK or FAIL.
337 */
338 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200341 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200343 if (current_sctx.sc_sid <= 0)
344 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200345 is_vim9_script = script_is_vim9();
346 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200347 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200348 if (is_vim9_script)
349 {
350 // Check script variables that were visible where the function was
351 // defined.
352 if (find_script_var(name, len, cctx) != NULL)
353 return OK;
354 }
355 else
356 {
357 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
358 dictitem_T *di;
359 int cc;
360
361 // Check script variables that are currently visible
362 cc = name[len];
363 name[len] = NUL;
364 di = find_var_in_ht(ht, 0, name, TRUE);
365 name[len] = cc;
366 if (di != NULL)
367 return OK;
368 }
369
370 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100371}
372
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100373/*
374 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200375 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200376 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100377 * Return FAIL and give an error if it defined.
378 */
379 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200380check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100381{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200382 int c = p[len];
383 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200384
385 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200386 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100387 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100388 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200389 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200390 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100392 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200393 // A local or script-local function can shadow a global function.
394 if (ufunc == NULL || !func_is_global(ufunc)
395 || (p[0] == 'g' && p[1] == ':'))
396 {
397 p[len] = c;
398 semsg(_(e_name_already_defined_str), p);
399 return FAIL;
400 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200402 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100403 return OK;
404}
405
Bram Moolenaar65b95452020-07-19 14:03:09 +0200406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407/////////////////////////////////////////////////////////////////////
408// Following generate_ functions expect the caller to call ga_grow().
409
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200410#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
411#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413/*
414 * Generate an instruction without arguments.
415 * Returns a pointer to the new instruction, NULL if failed.
416 */
417 static isn_T *
418generate_instr(cctx_T *cctx, isntype_T isn_type)
419{
420 garray_T *instr = &cctx->ctx_instr;
421 isn_T *isn;
422
Bram Moolenaar080457c2020-03-03 21:53:32 +0100423 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100424 if (ga_grow(instr, 1) == FAIL)
425 return NULL;
426 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
427 isn->isn_type = isn_type;
428 isn->isn_lnum = cctx->ctx_lnum + 1;
429 ++instr->ga_len;
430
431 return isn;
432}
433
434/*
435 * Generate an instruction without arguments.
436 * "drop" will be removed from the stack.
437 * Returns a pointer to the new instruction, NULL if failed.
438 */
439 static isn_T *
440generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
441{
442 garray_T *stack = &cctx->ctx_type_stack;
443
Bram Moolenaar080457c2020-03-03 21:53:32 +0100444 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445 stack->ga_len -= drop;
446 return generate_instr(cctx, isn_type);
447}
448
449/*
450 * Generate instruction "isn_type" and put "type" on the type stack.
451 */
452 static isn_T *
453generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
454{
455 isn_T *isn;
456 garray_T *stack = &cctx->ctx_type_stack;
457
458 if ((isn = generate_instr(cctx, isn_type)) == NULL)
459 return NULL;
460
461 if (ga_grow(stack, 1) == FAIL)
462 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200463 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 ++stack->ga_len;
465
466 return isn;
467}
468
469/*
470 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200471 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 */
473 static int
474may_generate_2STRING(int offset, cctx_T *cctx)
475{
476 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200477 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100479 type_T **type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100480
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +0100481 RETURN_OK_IF_SKIP(cctx);
482 type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200483 switch ((*type)->tt_type)
484 {
485 // nothing to be done
486 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200488 // conversion possible
489 case VAR_SPECIAL:
490 case VAR_BOOL:
491 case VAR_NUMBER:
492 case VAR_FLOAT:
493 break;
494
495 // conversion possible (with runtime check)
496 case VAR_ANY:
497 case VAR_UNKNOWN:
498 isntype = ISN_2STRING_ANY;
499 break;
500
501 // conversion not possible
502 case VAR_VOID:
503 case VAR_BLOB:
504 case VAR_FUNC:
505 case VAR_PARTIAL:
506 case VAR_LIST:
507 case VAR_DICT:
508 case VAR_JOB:
509 case VAR_CHANNEL:
510 to_string_error((*type)->tt_type);
511 return FAIL;
512 }
513
514 *type = &t_string;
515 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 return FAIL;
517 isn->isn_arg.number = offset;
518
519 return OK;
520}
521
522 static int
523check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
524{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200525 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200527 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 {
529 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200530 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200532 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return FAIL;
534 }
535 return OK;
536}
537
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200538 static int
539generate_add_instr(
540 cctx_T *cctx,
541 vartype_T vartype,
542 type_T *type1,
543 type_T *type2)
544{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100545 garray_T *stack = &cctx->ctx_type_stack;
546 isn_T *isn = generate_instr_drop(cctx,
547 vartype == VAR_NUMBER ? ISN_OPNR
548 : vartype == VAR_LIST ? ISN_ADDLIST
549 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100551 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200552#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100553 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200554
555 if (vartype != VAR_LIST && vartype != VAR_BLOB
556 && type1->tt_type != VAR_ANY
557 && type2->tt_type != VAR_ANY
558 && check_number_or_float(
559 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
560 return FAIL;
561
562 if (isn != NULL)
563 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100564
565 // When concatenating two lists with different member types the member type
566 // becomes "any".
567 if (vartype == VAR_LIST
568 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
569 && type1->tt_member != type2->tt_member)
570 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
571
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200572 return isn == NULL ? FAIL : OK;
573}
574
575/*
576 * Get the type to use for an instruction for an operation on "type1" and
577 * "type2". If they are matching use a type-specific instruction. Otherwise
578 * fall back to runtime type checking.
579 */
580 static vartype_T
581operator_type(type_T *type1, type_T *type2)
582{
583 if (type1->tt_type == type2->tt_type
584 && (type1->tt_type == VAR_NUMBER
585 || type1->tt_type == VAR_LIST
586#ifdef FEAT_FLOAT
587 || type1->tt_type == VAR_FLOAT
588#endif
589 || type1->tt_type == VAR_BLOB))
590 return type1->tt_type;
591 return VAR_ANY;
592}
593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594/*
595 * Generate an instruction with two arguments. The instruction depends on the
596 * type of the arguments.
597 */
598 static int
599generate_two_op(cctx_T *cctx, char_u *op)
600{
601 garray_T *stack = &cctx->ctx_type_stack;
602 type_T *type1;
603 type_T *type2;
604 vartype_T vartype;
605 isn_T *isn;
606
Bram Moolenaar080457c2020-03-03 21:53:32 +0100607 RETURN_OK_IF_SKIP(cctx);
608
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200609 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
611 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
614 switch (*op)
615 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200616 case '+':
617 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 break;
620
621 case '-':
622 case '*':
623 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
624 op) == FAIL)
625 return FAIL;
626 if (vartype == VAR_NUMBER)
627 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
628#ifdef FEAT_FLOAT
629 else if (vartype == VAR_FLOAT)
630 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
631#endif
632 else
633 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
634 if (isn != NULL)
635 isn->isn_arg.op.op_type = *op == '*'
636 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
637 break;
638
Bram Moolenaar4c683752020-04-05 21:38:23 +0200639 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200641 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 && type2->tt_type != VAR_NUMBER))
643 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200644 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 return FAIL;
646 }
647 isn = generate_instr_drop(cctx,
648 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
649 if (isn != NULL)
650 isn->isn_arg.op.op_type = EXPR_REM;
651 break;
652 }
653
654 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200655 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 {
657 type_T *type = &t_any;
658
659#ifdef FEAT_FLOAT
660 // float+number and number+float results in float
661 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
662 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
663 type = &t_float;
664#endif
665 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
666 }
667
668 return OK;
669}
670
671/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200672 * Get the instruction to use for comparing "type1" with "type2"
673 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200675 static isntype_T
Bram Moolenaar657137c2021-01-09 15:45:23 +0100676get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677{
678 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
Bram Moolenaar4c683752020-04-05 21:38:23 +0200680 if (type1 == VAR_UNKNOWN)
681 type1 = VAR_ANY;
682 if (type2 == VAR_UNKNOWN)
683 type2 = VAR_ANY;
684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 if (type1 == type2)
686 {
687 switch (type1)
688 {
689 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
690 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
691 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
692 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
693 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
694 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
695 case VAR_LIST: isntype = ISN_COMPARELIST; break;
696 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
697 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 default: isntype = ISN_COMPAREANY; break;
699 }
700 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200701 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
703 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
704 isntype = ISN_COMPAREANY;
705
Bram Moolenaar657137c2021-01-09 15:45:23 +0100706 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 && (isntype == ISN_COMPAREBOOL
708 || isntype == ISN_COMPARESPECIAL
709 || isntype == ISN_COMPARENR
710 || isntype == ISN_COMPAREFLOAT))
711 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200712 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar657137c2021-01-09 15:45:23 +0100713 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200714 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 }
716 if (isntype == ISN_DROP
Bram Moolenaar657137c2021-01-09 15:45:23 +0100717 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
719 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
Bram Moolenaar657137c2021-01-09 15:45:23 +0100720 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
721 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 && (type1 == VAR_BLOB || type2 == VAR_BLOB
723 || type1 == VAR_LIST || type2 == VAR_LIST))))
724 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200725 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200727 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200729 return isntype;
730}
731
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200732 int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100733check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200734{
735 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
736 return FAIL;
737 return OK;
738}
739
Bram Moolenaara5565e42020-05-09 15:44:01 +0200740/*
741 * Generate an ISN_COMPARE* instruction with a boolean result.
742 */
743 static int
Bram Moolenaar657137c2021-01-09 15:45:23 +0100744generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
Bram Moolenaara5565e42020-05-09 15:44:01 +0200745{
746 isntype_T isntype;
747 isn_T *isn;
748 garray_T *stack = &cctx->ctx_type_stack;
749 vartype_T type1;
750 vartype_T type2;
751
752 RETURN_OK_IF_SKIP(cctx);
753
754 // Get the known type of the two items on the stack. If they are matching
755 // use a type-specific instruction. Otherwise fall back to runtime type
756 // checking.
757 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
758 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100759 isntype = get_compare_isn(exprtype, type1, type2);
Bram Moolenaara5565e42020-05-09 15:44:01 +0200760 if (isntype == ISN_DROP)
761 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
763 if ((isn = generate_instr(cctx, isntype)) == NULL)
764 return FAIL;
Bram Moolenaar657137c2021-01-09 15:45:23 +0100765 isn->isn_arg.op.op_type = exprtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 isn->isn_arg.op.op_ic = ic;
767
768 // takes two arguments, puts one bool back
769 if (stack->ga_len >= 2)
770 {
771 --stack->ga_len;
772 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
773 }
774
775 return OK;
776}
777
778/*
779 * Generate an ISN_2BOOL instruction.
780 */
781 static int
782generate_2BOOL(cctx_T *cctx, int invert)
783{
784 isn_T *isn;
785 garray_T *stack = &cctx->ctx_type_stack;
786
Bram Moolenaar080457c2020-03-03 21:53:32 +0100787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
789 return FAIL;
790 isn->isn_arg.number = invert;
791
792 // type becomes bool
793 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
794
795 return OK;
796}
797
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200798/*
799 * Generate an ISN_COND2BOOL instruction.
800 */
801 static int
802generate_COND2BOOL(cctx_T *cctx)
803{
804 isn_T *isn;
805 garray_T *stack = &cctx->ctx_type_stack;
806
807 RETURN_OK_IF_SKIP(cctx);
808 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
809 return FAIL;
810
811 // type becomes bool
812 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
813
814 return OK;
815}
816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200818generate_TYPECHECK(
819 cctx_T *cctx,
820 type_T *expected,
Bram Moolenaare32e5162021-01-21 20:21:29 +0100821 int offset,
822 int argidx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823{
824 isn_T *isn;
825 garray_T *stack = &cctx->ctx_type_stack;
826
Bram Moolenaar080457c2020-03-03 21:53:32 +0100827 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
829 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200830 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaarb3005ce2021-01-22 17:51:06 +0100831 isn->isn_arg.type.ct_off = (int8_T)offset;
832 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833
Bram Moolenaar5e654232020-09-16 15:22:00 +0200834 // type becomes expected
835 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836
837 return OK;
838}
839
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100840 static int
841generate_SETTYPE(
842 cctx_T *cctx,
843 type_T *expected)
844{
845 isn_T *isn;
846
847 RETURN_OK_IF_SKIP(cctx);
848 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
849 return FAIL;
850 isn->isn_arg.type.ct_type = alloc_type(expected);
851 return OK;
852}
853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200855 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
856 * used. Return FALSE if the types will never match.
857 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100858 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200859use_typecheck(type_T *actual, type_T *expected)
860{
861 if (actual->tt_type == VAR_ANY
862 || actual->tt_type == VAR_UNKNOWN
863 || (actual->tt_type == VAR_FUNC
864 && (expected->tt_type == VAR_FUNC
865 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100866 && (actual->tt_member == &t_any || actual->tt_argcount < 0)
867 && ((actual->tt_member == &t_void)
868 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200869 return TRUE;
870 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
871 && actual->tt_type == expected->tt_type)
872 // This takes care of a nested list or dict.
873 return use_typecheck(actual->tt_member, expected->tt_member);
874 return FALSE;
875}
876
877/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200878 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200879 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200880 * - "actual" is a type that can be "expected" type: add a runtime check; or
881 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200882 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
883 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200884 */
Bram Moolenaar351ead02021-01-16 16:07:01 +0100885 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200886need_type(
887 type_T *actual,
888 type_T *expected,
889 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100890 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200891 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200892 int silent,
893 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200894{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200895 if (expected == &t_bool && actual != &t_bool
896 && (actual->tt_flags & TTFLAG_BOOL_OK))
897 {
898 // Using "0", "1" or the result of an expression with "&&" or "||" as a
899 // boolean is OK but requires a conversion.
900 generate_2BOOL(cctx, FALSE);
901 return OK;
902 }
903
Bram Moolenaar351ead02021-01-16 16:07:01 +0100904 if (check_type(expected, actual, FALSE, arg_idx) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200905 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200906
907 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200908 // If it's a constant a runtime check makes no sense.
909 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200910 {
Bram Moolenaare32e5162021-01-21 20:21:29 +0100911 generate_TYPECHECK(cctx, expected, offset, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200912 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200913 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200914
915 if (!silent)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100916 arg_type_mismatch(expected, actual, arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +0200917 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200918}
919
920/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100921 * Check that the top of the type stack has a type that can be used as a
922 * condition. Give an error and return FAIL if not.
923 */
924 static int
925bool_on_stack(cctx_T *cctx)
926{
927 garray_T *stack = &cctx->ctx_type_stack;
928 type_T *type;
929
930 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
931 if (type == &t_bool)
932 return OK;
933
934 if (type == &t_any || type == &t_number)
935 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
936 // This requires a runtime type check.
937 return generate_COND2BOOL(cctx);
938
Bram Moolenaar351ead02021-01-16 16:07:01 +0100939 return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100940}
941
942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 * Generate an ISN_PUSHNR instruction.
944 */
945 static int
946generate_PUSHNR(cctx_T *cctx, varnumber_T number)
947{
948 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200949 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950
Bram Moolenaar080457c2020-03-03 21:53:32 +0100951 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
953 return FAIL;
954 isn->isn_arg.number = number;
955
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200956 if (number == 0 || number == 1)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200957 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100958 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 return OK;
960}
961
962/*
963 * Generate an ISN_PUSHBOOL instruction.
964 */
965 static int
966generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
967{
968 isn_T *isn;
969
Bram Moolenaar080457c2020-03-03 21:53:32 +0100970 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
972 return FAIL;
973 isn->isn_arg.number = number;
974
975 return OK;
976}
977
978/*
979 * Generate an ISN_PUSHSPEC instruction.
980 */
981 static int
982generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
983{
984 isn_T *isn;
985
Bram Moolenaar080457c2020-03-03 21:53:32 +0100986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
988 return FAIL;
989 isn->isn_arg.number = number;
990
991 return OK;
992}
993
994#ifdef FEAT_FLOAT
995/*
996 * Generate an ISN_PUSHF instruction.
997 */
998 static int
999generate_PUSHF(cctx_T *cctx, float_T fnumber)
1000{
1001 isn_T *isn;
1002
Bram Moolenaar080457c2020-03-03 21:53:32 +01001003 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
1005 return FAIL;
1006 isn->isn_arg.fnumber = fnumber;
1007
1008 return OK;
1009}
1010#endif
1011
1012/*
1013 * Generate an ISN_PUSHS instruction.
1014 * Consumes "str".
1015 */
1016 static int
1017generate_PUSHS(cctx_T *cctx, char_u *str)
1018{
1019 isn_T *isn;
1020
Bram Moolenaar508b5612021-01-02 18:17:26 +01001021 if (cctx->ctx_skip == SKIP_YES)
1022 {
1023 vim_free(str);
1024 return OK;
1025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1027 return FAIL;
1028 isn->isn_arg.string = str;
1029
1030 return OK;
1031}
1032
1033/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 * Generate an ISN_PUSHCHANNEL instruction.
1035 * Consumes "channel".
1036 */
1037 static int
1038generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1039{
1040 isn_T *isn;
1041
Bram Moolenaar080457c2020-03-03 21:53:32 +01001042 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001043 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.channel = channel;
1046
1047 return OK;
1048}
1049
1050/*
1051 * Generate an ISN_PUSHJOB instruction.
1052 * Consumes "job".
1053 */
1054 static int
1055generate_PUSHJOB(cctx_T *cctx, job_T *job)
1056{
1057 isn_T *isn;
1058
Bram Moolenaar080457c2020-03-03 21:53:32 +01001059 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001060 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001061 return FAIL;
1062 isn->isn_arg.job = job;
1063
1064 return OK;
1065}
1066
1067/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 * Generate an ISN_PUSHBLOB instruction.
1069 * Consumes "blob".
1070 */
1071 static int
1072generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1073{
1074 isn_T *isn;
1075
Bram Moolenaar080457c2020-03-03 21:53:32 +01001076 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1078 return FAIL;
1079 isn->isn_arg.blob = blob;
1080
1081 return OK;
1082}
1083
1084/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001085 * Generate an ISN_PUSHFUNC instruction with name "name".
1086 * Consumes "name".
1087 */
1088 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001089generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001090{
1091 isn_T *isn;
1092
Bram Moolenaar080457c2020-03-03 21:53:32 +01001093 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001094 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001095 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001096 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001097
1098 return OK;
1099}
1100
1101/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001102 * Generate an ISN_GETITEM instruction with "index".
1103 */
1104 static int
1105generate_GETITEM(cctx_T *cctx, int index)
1106{
1107 isn_T *isn;
1108 garray_T *stack = &cctx->ctx_type_stack;
1109 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1110 type_T *item_type = &t_any;
1111
1112 RETURN_OK_IF_SKIP(cctx);
1113
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001114 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001115 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001116 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001117 emsg(_(e_listreq));
1118 return FAIL;
1119 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001120 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001121 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1122 return FAIL;
1123 isn->isn_arg.number = index;
1124
1125 // add the item type to the type stack
1126 if (ga_grow(stack, 1) == FAIL)
1127 return FAIL;
1128 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1129 ++stack->ga_len;
1130 return OK;
1131}
1132
1133/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001134 * Generate an ISN_SLICE instruction with "count".
1135 */
1136 static int
1137generate_SLICE(cctx_T *cctx, int count)
1138{
1139 isn_T *isn;
1140
1141 RETURN_OK_IF_SKIP(cctx);
1142 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1143 return FAIL;
1144 isn->isn_arg.number = count;
1145 return OK;
1146}
1147
1148/*
1149 * Generate an ISN_CHECKLEN instruction with "min_len".
1150 */
1151 static int
1152generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1153{
1154 isn_T *isn;
1155
1156 RETURN_OK_IF_SKIP(cctx);
1157
1158 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.checklen.cl_min_len = min_len;
1161 isn->isn_arg.checklen.cl_more_OK = more_OK;
1162
1163 return OK;
1164}
1165
1166/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 * Generate an ISN_STORE instruction.
1168 */
1169 static int
1170generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1171{
1172 isn_T *isn;
1173
Bram Moolenaar080457c2020-03-03 21:53:32 +01001174 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1176 return FAIL;
1177 if (name != NULL)
1178 isn->isn_arg.string = vim_strsave(name);
1179 else
1180 isn->isn_arg.number = idx;
1181
1182 return OK;
1183}
1184
1185/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001186 * Generate an ISN_STOREOUTER instruction.
1187 */
1188 static int
1189generate_STOREOUTER(cctx_T *cctx, int idx, int level)
1190{
1191 isn_T *isn;
1192
1193 RETURN_OK_IF_SKIP(cctx);
1194 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1195 return FAIL;
1196 isn->isn_arg.outer.outer_idx = idx;
1197 isn->isn_arg.outer.outer_depth = level;
1198
1199 return OK;
1200}
1201
1202/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1204 */
1205 static int
1206generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1207{
1208 isn_T *isn;
1209
Bram Moolenaar080457c2020-03-03 21:53:32 +01001210 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1212 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001213 isn->isn_arg.storenr.stnr_idx = idx;
1214 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215
1216 return OK;
1217}
1218
1219/*
1220 * Generate an ISN_STOREOPT instruction
1221 */
1222 static int
1223generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1224{
1225 isn_T *isn;
1226
Bram Moolenaar080457c2020-03-03 21:53:32 +01001227 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001228 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 return FAIL;
1230 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1231 isn->isn_arg.storeopt.so_flags = opt_flags;
1232
1233 return OK;
1234}
1235
1236/*
1237 * Generate an ISN_LOAD or similar instruction.
1238 */
1239 static int
1240generate_LOAD(
1241 cctx_T *cctx,
1242 isntype_T isn_type,
1243 int idx,
1244 char_u *name,
1245 type_T *type)
1246{
1247 isn_T *isn;
1248
Bram Moolenaar080457c2020-03-03 21:53:32 +01001249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1251 return FAIL;
1252 if (name != NULL)
1253 isn->isn_arg.string = vim_strsave(name);
1254 else
1255 isn->isn_arg.number = idx;
1256
1257 return OK;
1258}
1259
1260/*
Bram Moolenaarab360522021-01-10 14:02:28 +01001261 * Generate an ISN_LOADOUTER instruction
1262 */
1263 static int
1264generate_LOADOUTER(
1265 cctx_T *cctx,
1266 int idx,
1267 int nesting,
1268 type_T *type)
1269{
1270 isn_T *isn;
1271
1272 RETURN_OK_IF_SKIP(cctx);
1273 if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
1274 return FAIL;
1275 isn->isn_arg.outer.outer_idx = idx;
1276 isn->isn_arg.outer.outer_depth = nesting;
1277
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001282 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001283 */
1284 static int
1285generate_LOADV(
1286 cctx_T *cctx,
1287 char_u *name,
1288 int error)
1289{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001290 int di_flags;
1291 int vidx = find_vim_var(name, &di_flags);
1292 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 if (vidx < 0)
1296 {
1297 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001298 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299 return FAIL;
1300 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001301 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001303 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001304}
1305
1306/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001307 * Generate an ISN_UNLET instruction.
1308 */
1309 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001310generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001311{
1312 isn_T *isn;
1313
1314 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001315 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001316 return FAIL;
1317 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1318 isn->isn_arg.unlet.ul_forceit = forceit;
1319
1320 return OK;
1321}
1322
1323/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001324 * Generate an ISN_LOCKCONST instruction.
1325 */
1326 static int
1327generate_LOCKCONST(cctx_T *cctx)
1328{
1329 isn_T *isn;
1330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1333 return FAIL;
1334 return OK;
1335}
1336
1337/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 * Generate an ISN_LOADS instruction.
1339 */
1340 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 int sid,
1346 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347{
1348 isn_T *isn;
1349
Bram Moolenaar080457c2020-03-03 21:53:32 +01001350 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 if (isn_type == ISN_LOADS)
1352 isn = generate_instr_type(cctx, isn_type, type);
1353 else
1354 isn = generate_instr_drop(cctx, isn_type, 1);
1355 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1358 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
1360 return OK;
1361}
1362
1363/*
1364 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1365 */
1366 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001367generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 cctx_T *cctx,
1369 isntype_T isn_type,
1370 int sid,
1371 int idx,
1372 type_T *type)
1373{
1374 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001375 scriptref_T *sref;
1376 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
Bram Moolenaar080457c2020-03-03 21:53:32 +01001378 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 if (isn_type == ISN_LOADSCRIPT)
1380 isn = generate_instr_type(cctx, isn_type, type);
1381 else
1382 isn = generate_instr_drop(cctx, isn_type, 1);
1383 if (isn == NULL)
1384 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001385
1386 // This requires three arguments, which doesn't fit in an instruction, thus
1387 // we need to allocate a struct for this.
1388 sref = ALLOC_ONE(scriptref_T);
1389 if (sref == NULL)
1390 return FAIL;
1391 isn->isn_arg.script.scriptref = sref;
1392 sref->sref_sid = sid;
1393 sref->sref_idx = idx;
1394 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001395 sref->sref_type = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 return OK;
1397}
1398
1399/*
1400 * Generate an ISN_NEWLIST instruction.
1401 */
1402 static int
1403generate_NEWLIST(cctx_T *cctx, int count)
1404{
1405 isn_T *isn;
1406 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 type_T *type;
1408 type_T *member;
1409
Bram Moolenaar080457c2020-03-03 21:53:32 +01001410 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1412 return FAIL;
1413 isn->isn_arg.number = count;
1414
Bram Moolenaar127542b2020-08-09 17:22:04 +02001415 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001416 if (count == 0)
1417 member = &t_void;
1418 else
1419 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001420 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1421 cctx->ctx_type_list);
1422 type = get_list_type(member, cctx->ctx_type_list);
1423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 // drop the value types
1425 stack->ga_len -= count;
1426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 // add the list type to the type stack
1428 if (ga_grow(stack, 1) == FAIL)
1429 return FAIL;
1430 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1431 ++stack->ga_len;
1432
1433 return OK;
1434}
1435
1436/*
1437 * Generate an ISN_NEWDICT instruction.
1438 */
1439 static int
1440generate_NEWDICT(cctx_T *cctx, int count)
1441{
1442 isn_T *isn;
1443 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 type_T *type;
1445 type_T *member;
1446
Bram Moolenaar080457c2020-03-03 21:53:32 +01001447 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1449 return FAIL;
1450 isn->isn_arg.number = count;
1451
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001452 if (count == 0)
1453 member = &t_void;
1454 else
1455 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001456 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1457 cctx->ctx_type_list);
1458 type = get_dict_type(member, cctx->ctx_type_list);
1459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 // drop the key and value types
1461 stack->ga_len -= 2 * count;
1462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 // add the dict type to the type stack
1464 if (ga_grow(stack, 1) == FAIL)
1465 return FAIL;
1466 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1467 ++stack->ga_len;
1468
1469 return OK;
1470}
1471
1472/*
1473 * Generate an ISN_FUNCREF instruction.
1474 */
1475 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001476generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477{
1478 isn_T *isn;
1479 garray_T *stack = &cctx->ctx_type_stack;
1480
Bram Moolenaar080457c2020-03-03 21:53:32 +01001481 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1483 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001484 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001485 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486
Bram Moolenaarab360522021-01-10 14:02:28 +01001487 // if the referenced function is a closure, it may use items further up in
1488 // the nested context, including this one.
1489 if (ufunc->uf_flags & FC_CLOSURE)
1490 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 if (ga_grow(stack, 1) == FAIL)
1493 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001494 ((type_T **)stack->ga_data)[stack->ga_len] =
1495 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 ++stack->ga_len;
1497
1498 return OK;
1499}
1500
1501/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001502 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001503 * "lambda_name" and "func_name" must be in allocated memory and will be
1504 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001505 */
1506 static int
1507generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1508{
1509 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001510
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001511 if (cctx->ctx_skip == SKIP_YES)
1512 {
1513 vim_free(lambda_name);
1514 vim_free(func_name);
1515 return OK;
1516 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001517 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001518 {
1519 vim_free(lambda_name);
1520 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001521 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001522 }
1523 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001524 isn->isn_arg.newfunc.nf_global = func_name;
1525
1526 return OK;
1527}
1528
1529/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001530 * Generate an ISN_DEF instruction: list functions
1531 */
1532 static int
1533generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1534{
1535 isn_T *isn;
1536
1537 RETURN_OK_IF_SKIP(cctx);
1538 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1539 return FAIL;
1540 if (len > 0)
1541 {
1542 isn->isn_arg.string = vim_strnsave(name, len);
1543 if (isn->isn_arg.string == NULL)
1544 return FAIL;
1545 }
1546 return OK;
1547}
1548
1549/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 * Generate an ISN_JUMP instruction.
1551 */
1552 static int
1553generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1554{
1555 isn_T *isn;
1556 garray_T *stack = &cctx->ctx_type_stack;
1557
Bram Moolenaar080457c2020-03-03 21:53:32 +01001558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.jump.jump_when = when;
1562 isn->isn_arg.jump.jump_where = where;
1563
1564 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1565 --stack->ga_len;
1566
1567 return OK;
1568}
1569
1570 static int
1571generate_FOR(cctx_T *cctx, int loop_idx)
1572{
1573 isn_T *isn;
1574 garray_T *stack = &cctx->ctx_type_stack;
1575
Bram Moolenaar080457c2020-03-03 21:53:32 +01001576 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1578 return FAIL;
1579 isn->isn_arg.forloop.for_idx = loop_idx;
1580
1581 if (ga_grow(stack, 1) == FAIL)
1582 return FAIL;
1583 // type doesn't matter, will be stored next
1584 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1585 ++stack->ga_len;
1586
1587 return OK;
1588}
1589
1590/*
1591 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001592 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 * Return FAIL if the number of arguments is wrong.
1594 */
1595 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001596generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597{
1598 isn_T *isn;
1599 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001600 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001601 type_T **argtypes = NULL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001602 type_T *maptype = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603
Bram Moolenaar080457c2020-03-03 21:53:32 +01001604 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001605 argoff = check_internal_func(func_idx, argcount);
1606 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 return FAIL;
1608
Bram Moolenaar389df252020-07-09 21:20:47 +02001609 if (method_call && argoff > 1)
1610 {
1611 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1612 return FAIL;
1613 isn->isn_arg.shuffle.shfl_item = argcount;
1614 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1615 }
1616
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001617 if (argcount > 0)
1618 {
1619 // Check the types of the arguments.
1620 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001621 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1622 cctx) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001623 return FAIL;
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001624 if (internal_func_is_map(func_idx))
1625 maptype = *argtypes;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001626 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.bfunc.cbf_idx = func_idx;
1631 isn->isn_arg.bfunc.cbf_argcount = argcount;
1632
Bram Moolenaar94738d82020-10-21 14:25:07 +02001633 // Drop the argument types and push the return type.
1634 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (ga_grow(stack, 1) == FAIL)
1636 return FAIL;
1637 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001638 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001639 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001641 if (maptype != NULL && maptype->tt_member != NULL
1642 && maptype->tt_member != &t_any)
1643 // Check that map() didn't change the item types.
Bram Moolenaare32e5162021-01-21 20:21:29 +01001644 generate_TYPECHECK(cctx, maptype, -1, 1);
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 return OK;
1647}
1648
1649/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001650 * Generate an ISN_LISTAPPEND instruction. Works like add().
1651 * Argument count is already checked.
1652 */
1653 static int
1654generate_LISTAPPEND(cctx_T *cctx)
1655{
1656 garray_T *stack = &cctx->ctx_type_stack;
1657 type_T *list_type;
1658 type_T *item_type;
1659 type_T *expected;
1660
1661 // Caller already checked that list_type is a list.
1662 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1663 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1664 expected = list_type->tt_member;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001665 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001666 return FAIL;
1667
1668 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1669 return FAIL;
1670
1671 --stack->ga_len; // drop the argument
1672 return OK;
1673}
1674
1675/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001676 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1677 * Argument count is already checked.
1678 */
1679 static int
1680generate_BLOBAPPEND(cctx_T *cctx)
1681{
1682 garray_T *stack = &cctx->ctx_type_stack;
1683 type_T *item_type;
1684
1685 // Caller already checked that blob_type is a blob.
1686 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01001687 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001688 return FAIL;
1689
1690 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1691 return FAIL;
1692
1693 --stack->ga_len; // drop the argument
1694 return OK;
1695}
1696
1697/*
Bram Moolenaarb2049902021-01-24 12:53:53 +01001698 * Return TRUE if "ufunc" should be compiled, taking into account whether
1699 * "profile" indicates profiling is to be done.
1700 */
1701 int
1702func_needs_compiling(ufunc_T *ufunc, int profile)
1703{
1704 switch (ufunc->uf_def_status)
1705 {
1706 case UF_NOT_COMPILED: return FALSE;
1707 case UF_TO_BE_COMPILED: return TRUE;
1708 case UF_COMPILED:
1709 {
1710 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1711 + ufunc->uf_dfunc_idx;
1712
1713 return profile ? dfunc->df_instr_prof == NULL
1714 : dfunc->df_instr == NULL;
1715 }
1716 case UF_COMPILING: return FALSE;
1717 }
1718}
1719
1720/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 * Generate an ISN_DCALL or ISN_UCALL instruction.
1722 * Return FAIL if the number of arguments is wrong.
1723 */
1724 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001725generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726{
1727 isn_T *isn;
1728 garray_T *stack = &cctx->ctx_type_stack;
1729 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001730 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
Bram Moolenaar080457c2020-03-03 21:53:32 +01001732 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 if (argcount > regular_args && !has_varargs(ufunc))
1734 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001735 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 return FAIL;
1737 }
1738 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1739 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001740 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 return FAIL;
1742 }
1743
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001744 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001745 {
1746 int i;
1747
1748 for (i = 0; i < argcount; ++i)
1749 {
1750 type_T *expected;
1751 type_T *actual;
1752
1753 if (i < regular_args)
1754 {
1755 if (ufunc->uf_arg_types == NULL)
1756 continue;
1757 expected = ufunc->uf_arg_types[i];
1758 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001759 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1760 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001761 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001762 else
1763 expected = ufunc->uf_va_type->tt_member;
1764 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001765 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001766 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001767 {
1768 arg_type_mismatch(expected, actual, i + 1);
1769 return FAIL;
1770 }
1771 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001772 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
1773 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1774 cctx->ctx_profiling, NULL) == FAIL)
1775 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001776 }
1777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001779 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001780 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001782 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 {
1784 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1785 isn->isn_arg.dfunc.cdf_argcount = argcount;
1786 }
1787 else
1788 {
1789 // A user function may be deleted and redefined later, can't use the
1790 // ufunc pointer, need to look it up again at runtime.
1791 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1792 isn->isn_arg.ufunc.cuf_argcount = argcount;
1793 }
1794
1795 stack->ga_len -= argcount; // drop the arguments
1796 if (ga_grow(stack, 1) == FAIL)
1797 return FAIL;
1798 // add return value
1799 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1800 ++stack->ga_len;
1801
1802 return OK;
1803}
1804
1805/*
1806 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1807 */
1808 static int
1809generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1810{
1811 isn_T *isn;
1812 garray_T *stack = &cctx->ctx_type_stack;
1813
Bram Moolenaar080457c2020-03-03 21:53:32 +01001814 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1816 return FAIL;
1817 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1818 isn->isn_arg.ufunc.cuf_argcount = argcount;
1819
1820 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001821 if (ga_grow(stack, 1) == FAIL)
1822 return FAIL;
1823 // add return value
1824 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1825 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827 return OK;
1828}
1829
1830/*
1831 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001832 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 */
1834 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001835generate_PCALL(
1836 cctx_T *cctx,
1837 int argcount,
1838 char_u *name,
1839 type_T *type,
1840 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841{
1842 isn_T *isn;
1843 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001844 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845
Bram Moolenaar080457c2020-03-03 21:53:32 +01001846 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001847
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001848 if (type->tt_type == VAR_ANY)
1849 ret_type = &t_any;
1850 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001851 {
1852 if (type->tt_argcount != -1)
1853 {
1854 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1855
1856 if (argcount < type->tt_min_argcount - varargs)
1857 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001858 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001859 return FAIL;
1860 }
1861 if (!varargs && argcount > type->tt_argcount)
1862 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001863 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001864 return FAIL;
1865 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001866 if (type->tt_args != NULL)
1867 {
1868 int i;
1869
1870 for (i = 0; i < argcount; ++i)
1871 {
1872 int offset = -argcount + i - 1;
1873 type_T *actual = ((type_T **)stack->ga_data)[
1874 stack->ga_len + offset];
1875 type_T *expected;
1876
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001877 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001878 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001879 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001880 else
1881 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001882 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001883 cctx, TRUE, FALSE) == FAIL)
1884 {
1885 arg_type_mismatch(expected, actual, i + 1);
1886 return FAIL;
1887 }
1888 }
1889 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001890 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001891 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001892 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001893 else
1894 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001895 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001896 return FAIL;
1897 }
1898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.pfunc.cpf_top = at_top;
1902 isn->isn_arg.pfunc.cpf_argcount = argcount;
1903
1904 stack->ga_len -= argcount; // drop the arguments
1905
1906 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001907 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001909 // If partial is above the arguments it must be cleared and replaced with
1910 // the return value.
1911 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1912 return FAIL;
1913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 return OK;
1915}
1916
1917/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001918 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 */
1920 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001921generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922{
1923 isn_T *isn;
1924 garray_T *stack = &cctx->ctx_type_stack;
1925 type_T *type;
1926
Bram Moolenaar080457c2020-03-03 21:53:32 +01001927 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001928 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001930 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001932 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001934 if (type->tt_type != VAR_DICT && type != &t_any)
1935 {
1936 emsg(_(e_dictreq));
1937 return FAIL;
1938 }
1939 // change dict type to dict member type
1940 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001941 {
1942 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1943 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945
1946 return OK;
1947}
1948
1949/*
1950 * Generate an ISN_ECHO instruction.
1951 */
1952 static int
1953generate_ECHO(cctx_T *cctx, int with_white, int count)
1954{
1955 isn_T *isn;
1956
Bram Moolenaar080457c2020-03-03 21:53:32 +01001957 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1959 return FAIL;
1960 isn->isn_arg.echo.echo_with_white = with_white;
1961 isn->isn_arg.echo.echo_count = count;
1962
1963 return OK;
1964}
1965
Bram Moolenaarad39c092020-02-26 18:23:43 +01001966/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001967 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001968 */
1969 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001970generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001971{
1972 isn_T *isn;
1973
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001974 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001975 return FAIL;
1976 isn->isn_arg.number = count;
1977
1978 return OK;
1979}
1980
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001981/*
1982 * Generate an ISN_PUT instruction.
1983 */
1984 static int
1985generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1986{
1987 isn_T *isn;
1988
1989 RETURN_OK_IF_SKIP(cctx);
1990 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1991 return FAIL;
1992 isn->isn_arg.put.put_regname = regname;
1993 isn->isn_arg.put.put_lnum = lnum;
1994 return OK;
1995}
1996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 static int
1998generate_EXEC(cctx_T *cctx, char_u *line)
1999{
2000 isn_T *isn;
2001
Bram Moolenaar080457c2020-03-03 21:53:32 +01002002 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2004 return FAIL;
2005 isn->isn_arg.string = vim_strsave(line);
2006 return OK;
2007}
2008
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002009 static int
2010generate_EXECCONCAT(cctx_T *cctx, int count)
2011{
2012 isn_T *isn;
2013
2014 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2015 return FAIL;
2016 isn->isn_arg.number = count;
2017 return OK;
2018}
2019
Bram Moolenaar08597872020-12-10 19:43:40 +01002020/*
2021 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2022 */
2023 static int
2024generate_RANGE(cctx_T *cctx, char_u *range)
2025{
2026 isn_T *isn;
2027 garray_T *stack = &cctx->ctx_type_stack;
2028
2029 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2030 return FAIL;
2031 isn->isn_arg.string = range;
2032
2033 if (ga_grow(stack, 1) == FAIL)
2034 return FAIL;
2035 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2036 ++stack->ga_len;
2037 return OK;
2038}
2039
Bram Moolenaar792f7862020-11-23 08:31:18 +01002040 static int
2041generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2042{
2043 isn_T *isn;
2044
2045 RETURN_OK_IF_SKIP(cctx);
2046 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2047 return FAIL;
2048 isn->isn_arg.unpack.unp_count = var_count;
2049 isn->isn_arg.unpack.unp_semicolon = semicolon;
2050 return OK;
2051}
2052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002054 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002055 */
2056 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002057generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002058{
2059 isn_T *isn;
2060
Bram Moolenaar02194d22020-10-24 23:08:38 +02002061 if (cmod->cmod_flags != 0
2062 || cmod->cmod_split != 0
2063 || cmod->cmod_verbose != 0
2064 || cmod->cmod_tab != 0
2065 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002066 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002067 cctx->ctx_has_cmdmod = TRUE;
2068
2069 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002070 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002071 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2072 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2073 return FAIL;
2074 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002075 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002076 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002077 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002078
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002079 return OK;
2080}
2081
2082 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002083generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002084{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002085 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2086 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002087 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002088 return OK;
2089}
2090
Bram Moolenaarb2049902021-01-24 12:53:53 +01002091 static void
2092may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2093{
2094 if (cctx->ctx_profiling && prof_lnum >= 0)
2095 {
2096 int save_lnum = cctx->ctx_lnum;
2097
2098 cctx->ctx_lnum = prof_lnum;
2099 generate_instr(cctx, ISN_PROF_END);
2100 cctx->ctx_lnum = save_lnum;
2101 }
2102}
2103
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002104/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002106 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002108 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002109reserve_local(
2110 cctx_T *cctx,
2111 char_u *name,
2112 size_t len,
2113 int isConst,
2114 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 lvar_T *lvar;
2117
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002118 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002120 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002121 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 }
2123
2124 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002125 return NULL;
2126 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002127 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002129 // Every local variable uses the next entry on the stack. We could re-use
2130 // the last ones when leaving a scope, but then variables used in a closure
2131 // might get overwritten. To keep things simple do not re-use stack
2132 // entries. This is less efficient, but memory is cheap these days.
2133 lvar->lv_idx = cctx->ctx_locals_count++;
2134
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002135 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 lvar->lv_const = isConst;
2137 lvar->lv_type = type;
2138
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002139 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140}
2141
2142/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002143 * Remove local variables above "new_top".
2144 */
2145 static void
2146unwind_locals(cctx_T *cctx, int new_top)
2147{
2148 if (cctx->ctx_locals.ga_len > new_top)
2149 {
2150 int idx;
2151 lvar_T *lvar;
2152
2153 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2154 {
2155 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2156 vim_free(lvar->lv_name);
2157 }
2158 }
2159 cctx->ctx_locals.ga_len = new_top;
2160}
2161
2162/*
2163 * Free all local variables.
2164 */
2165 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002166free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002167{
2168 unwind_locals(cctx, 0);
2169 ga_clear(&cctx->ctx_locals);
2170}
2171
2172/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002173 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2174 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2175 * error if the variable was defined with :const.
2176 */
2177 static int
2178check_item_writable(svar_T *sv, int check_writable, char_u *name)
2179{
2180 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2181 || (check_writable == ASSIGN_FINAL
2182 && sv->sv_const == ASSIGN_CONST))
2183 {
2184 semsg(_(e_readonlyvar), name);
2185 return FAIL;
2186 }
2187 return OK;
2188}
2189
2190/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002192 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 * Returns the index in "sn_var_vals" if found.
2194 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002195 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 */
2197 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002198get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199{
2200 hashtab_T *ht;
2201 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002202 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002203 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 int idx;
2205
Bram Moolenaare3d46852020-08-29 13:39:17 +02002206 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002208 if (sid == current_sctx.sc_sid)
2209 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002210 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002211
2212 if (sav == NULL)
2213 return -2;
2214 idx = sav->sav_var_vals_idx;
2215 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002216 if (check_item_writable(sv, check_writable, name) == FAIL)
2217 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002218 return idx;
2219 }
2220
2221 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 ht = &SCRIPT_VARS(sid);
2223 di = find_var_in_ht(ht, 0, name, TRUE);
2224 if (di == NULL)
2225 return -2;
2226
2227 // Now find the svar_T index in sn_var_vals.
2228 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2229 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002230 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 if (sv->sv_tv == &di->di_tv)
2232 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002233 if (check_item_writable(sv, check_writable, name) == FAIL)
2234 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 return idx;
2236 }
2237 }
2238 return -1;
2239}
2240
2241/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002242 * Find "name" in imported items of the current script or in "cctx" if not
2243 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002244 */
2245 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002246find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 int idx;
2249
Bram Moolenaare3d46852020-08-29 13:39:17 +02002250 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002251 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 if (cctx != NULL)
2253 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2254 {
2255 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2256 + idx;
2257
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002258 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2259 : STRLEN(import->imp_name) == len
2260 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 return import;
2262 }
2263
Bram Moolenaarefa94442020-08-08 22:16:00 +02002264 return find_imported_in_script(name, len, current_sctx.sc_sid);
2265}
2266
2267 imported_T *
2268find_imported_in_script(char_u *name, size_t len, int sid)
2269{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002270 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002271 int idx;
2272
Bram Moolenaare3d46852020-08-29 13:39:17 +02002273 if (!SCRIPT_ID_VALID(sid))
2274 return NULL;
2275 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2277 {
2278 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2279
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002280 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2281 : STRLEN(import->imp_name) == len
2282 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 return import;
2284 }
2285 return NULL;
2286}
2287
2288/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002289 * Free all imported variables.
2290 */
2291 static void
2292free_imported(cctx_T *cctx)
2293{
2294 int idx;
2295
2296 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2297 {
2298 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2299
2300 vim_free(import->imp_name);
2301 }
2302 ga_clear(&cctx->ctx_imports);
2303}
2304
2305/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002306 * Return a pointer to the next line that isn't empty or only contains a
2307 * comment. Skips over white space.
2308 * Returns NULL if there is none.
2309 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002310 char_u *
2311peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002312{
2313 int lnum = cctx->ctx_lnum;
2314
2315 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2316 {
2317 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002318 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002319
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002320 // ignore NULLs inserted for continuation lines
2321 if (line != NULL)
2322 {
2323 p = skipwhite(line);
2324 if (*p != NUL && !vim9_comment_start(p))
2325 return p;
2326 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002327 }
2328 return NULL;
2329}
2330
2331/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002332 * Called when checking for a following operator at "arg". When the rest of
2333 * the line is empty or only a comment, peek the next line. If there is a next
2334 * line return a pointer to it and set "nextp".
2335 * Otherwise skip over white space.
2336 */
2337 static char_u *
2338may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2339{
2340 char_u *p = skipwhite(arg);
2341
2342 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002343 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002344 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002345 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002346 if (*nextp != NULL)
2347 return *nextp;
2348 }
2349 return p;
2350}
2351
2352/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002353 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002354 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002355 * Returns NULL when at the end.
2356 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002357 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002358next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002359{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002360 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002361
2362 do
2363 {
2364 ++cctx->ctx_lnum;
2365 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002366 {
2367 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002368 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002369 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002370 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002371 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002372 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002373 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002374 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002375 return line;
2376}
2377
2378/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002379 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002380 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002381 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002382 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2383 */
2384 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002385may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002386{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002387 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002388 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002389 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002390 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002391
2392 if (next == NULL)
2393 return FAIL;
2394 *arg = skipwhite(next);
2395 }
2396 return OK;
2397}
2398
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002399/*
2400 * Idem, and give an error when failed.
2401 */
2402 static int
2403may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2404{
2405 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2406 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002407 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002408 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002409 return FAIL;
2410 }
2411 return OK;
2412}
2413
2414
Bram Moolenaara5565e42020-05-09 15:44:01 +02002415// Structure passed between the compile_expr* functions to keep track of
2416// constants that have been parsed but for which no code was produced yet. If
2417// possible expressions on these constants are applied at compile time. If
2418// that is not possible, the code to push the constants needs to be generated
2419// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002420// Using 50 should be more than enough of 5 levels of ().
2421#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002422typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002423 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002424 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002425 int pp_is_const; // all generated code was constants, used for a
2426 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002427} ppconst_T;
2428
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002429static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002430static int compile_expr0(char_u **arg, cctx_T *cctx);
2431static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2432
Bram Moolenaara5565e42020-05-09 15:44:01 +02002433/*
2434 * Generate a PUSH instruction for "tv".
2435 * "tv" will be consumed or cleared.
2436 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2437 */
2438 static int
2439generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2440{
2441 if (tv != NULL)
2442 {
2443 switch (tv->v_type)
2444 {
2445 case VAR_UNKNOWN:
2446 break;
2447 case VAR_BOOL:
2448 generate_PUSHBOOL(cctx, tv->vval.v_number);
2449 break;
2450 case VAR_SPECIAL:
2451 generate_PUSHSPEC(cctx, tv->vval.v_number);
2452 break;
2453 case VAR_NUMBER:
2454 generate_PUSHNR(cctx, tv->vval.v_number);
2455 break;
2456#ifdef FEAT_FLOAT
2457 case VAR_FLOAT:
2458 generate_PUSHF(cctx, tv->vval.v_float);
2459 break;
2460#endif
2461 case VAR_BLOB:
2462 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2463 tv->vval.v_blob = NULL;
2464 break;
2465 case VAR_STRING:
2466 generate_PUSHS(cctx, tv->vval.v_string);
2467 tv->vval.v_string = NULL;
2468 break;
2469 default:
2470 iemsg("constant type not supported");
2471 clear_tv(tv);
2472 return FAIL;
2473 }
2474 tv->v_type = VAR_UNKNOWN;
2475 }
2476 return OK;
2477}
2478
2479/*
2480 * Generate code for any ppconst entries.
2481 */
2482 static int
2483generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2484{
2485 int i;
2486 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002487 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002488
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002489 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002490 for (i = 0; i < ppconst->pp_used; ++i)
2491 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2492 ret = FAIL;
2493 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002494 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002495 return ret;
2496}
2497
2498/*
2499 * Clear ppconst constants. Used when failing.
2500 */
2501 static void
2502clear_ppconst(ppconst_T *ppconst)
2503{
2504 int i;
2505
2506 for (i = 0; i < ppconst->pp_used; ++i)
2507 clear_tv(&ppconst->pp_tv[i]);
2508 ppconst->pp_used = 0;
2509}
2510
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002511/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002512 * Generate an instruction to load script-local variable "name", without the
2513 * leading "s:".
2514 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 */
2516 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002517compile_load_scriptvar(
2518 cctx_T *cctx,
2519 char_u *name, // variable NUL terminated
2520 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002521 char_u **end, // end of variable
2522 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002524 scriptitem_T *si;
2525 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 imported_T *import;
2527
Bram Moolenaare3d46852020-08-29 13:39:17 +02002528 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2529 return FAIL;
2530 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002531 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002532 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002534 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002535 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2536 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 }
2538 if (idx >= 0)
2539 {
2540 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2541
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002542 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 current_sctx.sc_sid, idx, sv->sv_type);
2544 return OK;
2545 }
2546
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002547 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 if (import != NULL)
2549 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002550 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002551 {
2552 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002553 char_u *exp_name;
2554 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002555 ufunc_T *ufunc;
2556 type_T *type;
2557
2558 // Used "import * as Name", need to lookup the member.
2559 if (*p != '.')
2560 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002561 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002562 return FAIL;
2563 }
2564 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002565 if (VIM_ISWHITE(*p))
2566 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002567 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002568 return FAIL;
2569 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002570
Bram Moolenaar1c991142020-07-04 13:15:31 +02002571 // isolate one name
2572 exp_name = p;
2573 while (eval_isnamec(*p))
2574 ++p;
2575 cc = *p;
2576 *p = NUL;
2577
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002578 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002579 *p = cc;
2580 p = skipwhite(p);
2581
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002582 // TODO: what if it is a function?
2583 if (idx < 0)
2584 return FAIL;
2585 *end = p;
2586
2587 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2588 import->imp_sid,
2589 idx,
2590 type);
2591 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002592 else if (import->imp_funcname != NULL)
2593 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002594 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002595 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2596 import->imp_sid,
2597 import->imp_var_vals_idx,
2598 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 return OK;
2600 }
2601
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002602 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002603 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 return FAIL;
2605}
2606
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002607 static int
2608generate_funcref(cctx_T *cctx, char_u *name)
2609{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002610 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002611
2612 if (ufunc == NULL)
2613 return FAIL;
2614
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002615 // Need to compile any default values to get the argument types.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002616 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
2617 && compile_def_function(ufunc, TRUE, cctx->ctx_profiling, NULL)
2618 == FAIL)
2619 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002620 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002621}
2622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623/*
2624 * Compile a variable name into a load instruction.
2625 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002626 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 * When "error" is FALSE do not give an error when not found.
2628 */
2629 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002630compile_load(
2631 char_u **arg,
2632 char_u *end_arg,
2633 cctx_T *cctx,
2634 int is_expr,
2635 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636{
2637 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002638 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002639 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002641 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642
2643 if (*(*arg + 1) == ':')
2644 {
2645 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002646 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002647 {
2648 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002650 switch (**arg)
2651 {
2652 case 'g': isn_type = ISN_LOADGDICT; break;
2653 case 'w': isn_type = ISN_LOADWDICT; break;
2654 case 't': isn_type = ISN_LOADTDICT; break;
2655 case 'b': isn_type = ISN_LOADBDICT; break;
2656 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002657 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002658 goto theend;
2659 }
2660 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2661 goto theend;
2662 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 else
2665 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002666 isntype_T isn_type = ISN_DROP;
2667
2668 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2669 if (name == NULL)
2670 return FAIL;
2671
2672 switch (**arg)
2673 {
2674 case 'v': res = generate_LOADV(cctx, name, error);
2675 break;
2676 case 's': res = compile_load_scriptvar(cctx, name,
2677 NULL, NULL, error);
2678 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002679 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2680 isn_type = ISN_LOADG;
2681 else
2682 {
2683 isn_type = ISN_LOADAUTO;
2684 vim_free(name);
2685 name = vim_strnsave(*arg, end - *arg);
2686 if (name == NULL)
2687 return FAIL;
2688 }
2689 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002690 case 'w': isn_type = ISN_LOADW; break;
2691 case 't': isn_type = ISN_LOADT; break;
2692 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002693 default: // cannot happen, just in case
2694 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002695 goto theend;
2696 }
2697 if (isn_type != ISN_DROP)
2698 {
2699 // Global, Buffer-local, Window-local and Tabpage-local
2700 // variables can be defined later, thus we don't check if it
2701 // exists, give error at runtime.
2702 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 }
2705 }
2706 else
2707 {
2708 size_t len = end - *arg;
2709 int idx;
2710 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002711 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712
2713 name = vim_strnsave(*arg, end - *arg);
2714 if (name == NULL)
2715 return FAIL;
2716
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002717 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002719 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002720 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 }
2722 else
2723 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002724 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002725
Bram Moolenaar709664c2020-12-12 14:33:41 +01002726 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002728 type = lvar.lv_type;
2729 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002730 if (lvar.lv_from_outer != 0)
2731 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002732 else
2733 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 }
2735 else
2736 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002737 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002738 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002739 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002740 || find_imported(name, 0, cctx) != NULL)
2741 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002742
Bram Moolenaar0f769812020-09-12 18:32:34 +02002743 // When evaluating an expression and the name starts with an
2744 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002745 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002746 if (res == FAIL && is_expr
2747 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002748 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 }
2750 }
2751 if (gen_load)
2752 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002753 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002754 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002755 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002756 cctx->ctx_outer_used = TRUE;
2757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 }
2759
2760 *arg = end;
2761
2762theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002763 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002764 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 vim_free(name);
2766 return res;
2767}
2768
2769/*
2770 * Compile the argument expressions.
2771 * "arg" points to just after the "(" and is advanced to after the ")"
2772 */
2773 static int
2774compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2775{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002776 char_u *p = *arg;
2777 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002778 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779
Bram Moolenaare6085c52020-04-12 20:19:16 +02002780 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002782 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2783 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002784 if (*p == ')')
2785 {
2786 *arg = p + 1;
2787 return OK;
2788 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002789 if (must_end)
2790 {
2791 semsg(_(e_missing_comma_before_argument_str), p);
2792 return FAIL;
2793 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002794
Bram Moolenaara5565e42020-05-09 15:44:01 +02002795 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 return FAIL;
2797 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002798
2799 if (*p != ',' && *skipwhite(p) == ',')
2800 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002801 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002802 p = skipwhite(p);
2803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002805 {
2806 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002807 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002808 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002809 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002810 else
2811 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002812 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002813 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002815failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002816 emsg(_(e_missing_close));
2817 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818}
2819
2820/*
2821 * Compile a function call: name(arg1, arg2)
2822 * "arg" points to "name", "arg + varlen" to the "(".
2823 * "argcount_init" is 1 for "value->method()"
2824 * Instructions:
2825 * EVAL arg1
2826 * EVAL arg2
2827 * BCALL / DCALL / UCALL
2828 */
2829 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002830compile_call(
2831 char_u **arg,
2832 size_t varlen,
2833 cctx_T *cctx,
2834 ppconst_T *ppconst,
2835 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836{
2837 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002838 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 int argcount = argcount_init;
2840 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002841 char_u fname_buf[FLEN_FIXED + 1];
2842 char_u *tofree = NULL;
2843 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002844 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002845 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002846 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847
Bram Moolenaara5565e42020-05-09 15:44:01 +02002848 // we can evaluate "has('name')" at compile time
2849 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2850 {
2851 char_u *s = skipwhite(*arg + varlen + 1);
2852 typval_T argvars[2];
2853
2854 argvars[0].v_type = VAR_UNKNOWN;
2855 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002856 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002857 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002858 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002859 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002860 if (*s == ')' && argvars[0].v_type == VAR_STRING
2861 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002862 {
2863 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2864
2865 *arg = s + 1;
2866 argvars[1].v_type = VAR_UNKNOWN;
2867 tv->v_type = VAR_NUMBER;
2868 tv->vval.v_number = 0;
2869 f_has(argvars, tv);
2870 clear_tv(&argvars[0]);
2871 ++ppconst->pp_used;
2872 return OK;
2873 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002874 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002875 }
2876
2877 if (generate_ppconst(cctx, ppconst) == FAIL)
2878 return FAIL;
2879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if (varlen >= sizeof(namebuf))
2881 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002882 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 return FAIL;
2884 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002885 vim_strncpy(namebuf, *arg, varlen);
2886 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887
2888 *arg = skipwhite(*arg + varlen + 1);
2889 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002890 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891
Bram Moolenaar03290b82020-12-19 16:30:44 +01002892 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002893 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 {
2895 int idx;
2896
2897 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002898 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002900 {
2901 if (STRCMP(name, "add") == 0 && argcount == 2)
2902 {
2903 garray_T *stack = &cctx->ctx_type_stack;
2904 type_T *type = ((type_T **)stack->ga_data)[
2905 stack->ga_len - 2];
2906
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002907 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002908 if (type->tt_type == VAR_LIST)
2909 {
2910 // inline "add(list, item)" so that the type can be checked
2911 res = generate_LISTAPPEND(cctx);
2912 idx = -1;
2913 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002914 else if (type->tt_type == VAR_BLOB)
2915 {
2916 // inline "add(blob, nr)" so that the type can be checked
2917 res = generate_BLOBAPPEND(cctx);
2918 idx = -1;
2919 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002920 }
2921
2922 if (idx >= 0)
2923 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2924 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002925 else
2926 semsg(_(e_unknownfunc), namebuf);
2927 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002930 // An argument or local variable can be a function reference, this
2931 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002932 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002933 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002934 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002935 // If we can find the function by name generate the right call.
2936 // Skip global functions here, a local funcref takes precedence.
2937 ufunc = find_func(name, FALSE, cctx);
2938 if (ufunc != NULL && !func_is_global(ufunc))
2939 {
2940 res = generate_CALL(cctx, ufunc, argcount);
2941 goto theend;
2942 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
2945 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002946 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002947 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002949 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002950 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002951 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002952 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002953 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002954
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002955 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002956 goto theend;
2957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958
Bram Moolenaar0f769812020-09-12 18:32:34 +02002959 // If we can find a global function by name generate the right call.
2960 if (ufunc != NULL)
2961 {
2962 res = generate_CALL(cctx, ufunc, argcount);
2963 goto theend;
2964 }
2965
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002966 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002967 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002968 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002969 res = generate_UCALL(cctx, name, argcount);
2970 else
2971 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002972
2973theend:
2974 vim_free(tofree);
2975 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976}
2977
2978// like NAMESPACE_CHAR but with 'a' and 'l'.
2979#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2980
2981/*
2982 * Find the end of a variable or function name. Unlike find_name_end() this
2983 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002984 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 * Return a pointer to just after the name. Equal to "arg" if there is no
2986 * valid name.
2987 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002988 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002989to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990{
2991 char_u *p;
2992
2993 // Quick check for valid starting character.
2994 if (!eval_isnamec1(*arg))
2995 return arg;
2996
2997 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2998 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2999 // and can be used in slice "[n:]".
3000 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003001 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3003 break;
3004 return p;
3005}
3006
3007/*
3008 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003009 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 */
3011 char_u *
3012to_name_const_end(char_u *arg)
3013{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003014 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 typval_T rettv;
3016
3017 if (p == arg && *arg == '[')
3018 {
3019
3020 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003021 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 p = arg;
3023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 return p;
3025}
3026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027/*
3028 * parse a list: [expr, expr]
3029 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003030 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 */
3032 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003033compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034{
3035 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003036 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003038 int is_const;
3039 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003041 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003043 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003044 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003045 semsg(_(e_list_end), *arg);
3046 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003047 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003048 if (*p == ',')
3049 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003050 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003051 return FAIL;
3052 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003053 if (*p == ']')
3054 {
3055 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003056 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003057 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003058 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003059 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003060 if (!is_const)
3061 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 ++count;
3063 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003064 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003066 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3067 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003068 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003069 return FAIL;
3070 }
3071 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003072 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 p = skipwhite(p);
3074 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003075 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003077 ppconst->pp_is_const = is_all_const;
3078 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079}
3080
3081/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003082 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003084 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 */
3086 static int
3087compile_lambda(char_u **arg, cctx_T *cctx)
3088{
Bram Moolenaare462f522020-12-27 14:43:30 +01003089 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 typval_T rettv;
3091 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003092 evalarg_T evalarg;
3093
3094 CLEAR_FIELD(evalarg);
3095 evalarg.eval_flags = EVAL_EVALUATE;
3096 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097
3098 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003099 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3100 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003101 {
3102 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003103 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003104 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003105
Bram Moolenaar65c44152020-12-24 15:14:01 +01003106 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003108 ++ufunc->uf_refcount;
3109 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110
Bram Moolenaar65c44152020-12-24 15:14:01 +01003111 // Compile the function into instructions.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003112 compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003114 clear_evalarg(&evalarg, NULL);
3115
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003116 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003117 {
3118 // The return type will now be known.
3119 set_function_type(ufunc);
3120
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003121 // The function reference count will be 1. When the ISN_FUNCREF
3122 // instruction is deleted the reference count is decremented and the
3123 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003124 return generate_FUNCREF(cctx, ufunc);
3125 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003126
3127 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 return FAIL;
3129}
3130
3131/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003132 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003134 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 */
3136 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003137compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138{
3139 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003140 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 int count = 0;
3142 dict_T *d = dict_alloc();
3143 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003144 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003145 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003146 int is_const;
3147 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148
3149 if (d == NULL)
3150 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003151 if (generate_ppconst(cctx, ppconst) == FAIL)
3152 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003153 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003155 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156
Bram Moolenaar23c55272020-06-21 16:58:13 +02003157 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003158 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003159 *arg = NULL;
3160 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003161 }
3162
3163 if (**arg == '}')
3164 break;
3165
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003166 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003168 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003170 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003171 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003172 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3175 if (isn->isn_type == ISN_PUSHS)
3176 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003177 else
3178 {
3179 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003180 [stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003181 if (need_type(keytype, &t_string, -1, 0, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003182 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003183 return FAIL;
3184 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003185 *arg = skipwhite(*arg);
3186 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003187 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003188 emsg(_(e_missing_matching_bracket_after_dict_key));
3189 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003190 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003191 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003193 else
3194 {
3195 // {"name": value},
3196 // {'name': value},
3197 // {name: value} use "name" as a literal key
3198 key = get_literal_key(arg);
3199 if (key == NULL)
3200 return FAIL;
3201 if (generate_PUSHS(cctx, key) == FAIL)
3202 return FAIL;
3203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204
3205 // Check for duplicate keys, if using string keys.
3206 if (key != NULL)
3207 {
3208 item = dict_find(d, key, -1);
3209 if (item != NULL)
3210 {
3211 semsg(_(e_duplicate_key), key);
3212 goto failret;
3213 }
3214 item = dictitem_alloc(key);
3215 if (item != NULL)
3216 {
3217 item->di_tv.v_type = VAR_UNKNOWN;
3218 item->di_tv.v_lock = 0;
3219 if (dict_add(d, item) == FAIL)
3220 dictitem_free(item);
3221 }
3222 }
3223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 if (**arg != ':')
3225 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003226 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003227 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003228 else
3229 semsg(_(e_missing_dict_colon), *arg);
3230 return FAIL;
3231 }
3232 whitep = *arg + 1;
3233 if (!IS_WHITE_OR_NUL(*whitep))
3234 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003235 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 return FAIL;
3237 }
3238
Bram Moolenaar23c55272020-06-21 16:58:13 +02003239 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003240 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003241 *arg = NULL;
3242 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003243 }
3244
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003245 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003247 if (!is_const)
3248 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 ++count;
3250
Bram Moolenaar2c330432020-04-13 14:41:35 +02003251 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003252 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003253 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003254 *arg = NULL;
3255 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 if (**arg == '}')
3258 break;
3259 if (**arg != ',')
3260 {
3261 semsg(_(e_missing_dict_comma), *arg);
3262 goto failret;
3263 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003264 if (IS_WHITE_OR_NUL(*whitep))
3265 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003266 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003267 return FAIL;
3268 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003269 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003270 if (!IS_WHITE_OR_NUL(*whitep))
3271 {
3272 semsg(_(e_white_space_required_after_str), ",");
3273 return FAIL;
3274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 *arg = skipwhite(*arg + 1);
3276 }
3277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 *arg = *arg + 1;
3279
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003280 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003281 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003282 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003283 *arg += STRLEN(*arg);
3284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003286 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 return generate_NEWDICT(cctx, count);
3288
3289failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003291 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003292 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003293 *arg = (char_u *)"";
3294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 dict_unref(d);
3296 return FAIL;
3297}
3298
3299/*
3300 * Compile "&option".
3301 */
3302 static int
3303compile_get_option(char_u **arg, cctx_T *cctx)
3304{
3305 typval_T rettv;
3306 char_u *start = *arg;
3307 int ret;
3308
3309 // parse the option and get the current value to get the type.
3310 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003311 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 if (ret == OK)
3313 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003314 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003315 char_u *name = vim_strnsave(start, *arg - start);
3316 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3317 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318
3319 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3320 vim_free(name);
3321 }
3322 clear_tv(&rettv);
3323
3324 return ret;
3325}
3326
3327/*
3328 * Compile "$VAR".
3329 */
3330 static int
3331compile_get_env(char_u **arg, cctx_T *cctx)
3332{
3333 char_u *start = *arg;
3334 int len;
3335 int ret;
3336 char_u *name;
3337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 ++*arg;
3339 len = get_env_len(arg);
3340 if (len == 0)
3341 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003342 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 return FAIL;
3344 }
3345
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003346 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 name = vim_strnsave(start, len + 1);
3348 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3349 vim_free(name);
3350 return ret;
3351}
3352
3353/*
3354 * Compile "@r".
3355 */
3356 static int
3357compile_get_register(char_u **arg, cctx_T *cctx)
3358{
3359 int ret;
3360
3361 ++*arg;
3362 if (**arg == NUL)
3363 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003364 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 return FAIL;
3366 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003367 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 {
3369 emsg_invreg(**arg);
3370 return FAIL;
3371 }
3372 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3373 ++*arg;
3374 return ret;
3375}
3376
3377/*
3378 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003379 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 */
3381 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003382apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003384 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385
3386 // this works from end to start
3387 while (p > start)
3388 {
3389 --p;
3390 if (*p == '-' || *p == '+')
3391 {
3392 // only '-' has an effect, for '+' we only check the type
3393#ifdef FEAT_FLOAT
3394 if (rettv->v_type == VAR_FLOAT)
3395 {
3396 if (*p == '-')
3397 rettv->vval.v_float = -rettv->vval.v_float;
3398 }
3399 else
3400#endif
3401 {
3402 varnumber_T val;
3403 int error = FALSE;
3404
3405 // tv_get_number_chk() accepts a string, but we don't want that
3406 // here
3407 if (check_not_string(rettv) == FAIL)
3408 return FAIL;
3409 val = tv_get_number_chk(rettv, &error);
3410 clear_tv(rettv);
3411 if (error)
3412 return FAIL;
3413 if (*p == '-')
3414 val = -val;
3415 rettv->v_type = VAR_NUMBER;
3416 rettv->vval.v_number = val;
3417 }
3418 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003419 else if (numeric_only)
3420 {
3421 ++p;
3422 break;
3423 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003424 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 {
3426 int v = tv2bool(rettv);
3427
3428 // '!' is permissive in the type.
3429 clear_tv(rettv);
3430 rettv->v_type = VAR_BOOL;
3431 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3432 }
3433 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003434 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 return OK;
3436}
3437
3438/*
3439 * Recognize v: variables that are constants and set "rettv".
3440 */
3441 static void
3442get_vim_constant(char_u **arg, typval_T *rettv)
3443{
3444 if (STRNCMP(*arg, "v:true", 6) == 0)
3445 {
3446 rettv->v_type = VAR_BOOL;
3447 rettv->vval.v_number = VVAL_TRUE;
3448 *arg += 6;
3449 }
3450 else if (STRNCMP(*arg, "v:false", 7) == 0)
3451 {
3452 rettv->v_type = VAR_BOOL;
3453 rettv->vval.v_number = VVAL_FALSE;
3454 *arg += 7;
3455 }
3456 else if (STRNCMP(*arg, "v:null", 6) == 0)
3457 {
3458 rettv->v_type = VAR_SPECIAL;
3459 rettv->vval.v_number = VVAL_NULL;
3460 *arg += 6;
3461 }
3462 else if (STRNCMP(*arg, "v:none", 6) == 0)
3463 {
3464 rettv->v_type = VAR_SPECIAL;
3465 rettv->vval.v_number = VVAL_NONE;
3466 *arg += 6;
3467 }
3468}
3469
Bram Moolenaar657137c2021-01-09 15:45:23 +01003470 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003471get_compare_type(char_u *p, int *len, int *type_is)
3472{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003473 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003474 int i;
3475
3476 switch (p[0])
3477 {
3478 case '=': if (p[1] == '=')
3479 type = EXPR_EQUAL;
3480 else if (p[1] == '~')
3481 type = EXPR_MATCH;
3482 break;
3483 case '!': if (p[1] == '=')
3484 type = EXPR_NEQUAL;
3485 else if (p[1] == '~')
3486 type = EXPR_NOMATCH;
3487 break;
3488 case '>': if (p[1] != '=')
3489 {
3490 type = EXPR_GREATER;
3491 *len = 1;
3492 }
3493 else
3494 type = EXPR_GEQUAL;
3495 break;
3496 case '<': if (p[1] != '=')
3497 {
3498 type = EXPR_SMALLER;
3499 *len = 1;
3500 }
3501 else
3502 type = EXPR_SEQUAL;
3503 break;
3504 case 'i': if (p[1] == 's')
3505 {
3506 // "is" and "isnot"; but not a prefix of a name
3507 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3508 *len = 5;
3509 i = p[*len];
3510 if (!isalnum(i) && i != '_')
3511 {
3512 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3513 *type_is = TRUE;
3514 }
3515 }
3516 break;
3517 }
3518 return type;
3519}
3520
3521/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003522 * Skip over an expression, ignoring most errors.
3523 */
3524 static void
3525skip_expr_cctx(char_u **arg, cctx_T *cctx)
3526{
3527 evalarg_T evalarg;
3528
3529 CLEAR_FIELD(evalarg);
3530 evalarg.eval_cctx = cctx;
3531 skip_expr(arg, &evalarg);
3532}
3533
3534/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003536 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 */
3538 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003539compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003541 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542
3543 // this works from end to start
3544 while (p > start)
3545 {
3546 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003547 while (VIM_ISWHITE(*p))
3548 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 if (*p == '-' || *p == '+')
3550 {
3551 int negate = *p == '-';
3552 isn_T *isn;
3553
3554 // TODO: check type
3555 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3556 {
3557 --p;
3558 if (*p == '-')
3559 negate = !negate;
3560 }
3561 // only '-' has an effect, for '+' we only check the type
3562 if (negate)
3563 isn = generate_instr(cctx, ISN_NEGATENR);
3564 else
3565 isn = generate_instr(cctx, ISN_CHECKNR);
3566 if (isn == NULL)
3567 return FAIL;
3568 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003569 else if (numeric_only)
3570 {
3571 ++p;
3572 break;
3573 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 else
3575 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003576 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003578 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003580 if (p[-1] == '!')
3581 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 }
3584 if (generate_2BOOL(cctx, invert) == FAIL)
3585 return FAIL;
3586 }
3587 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003588 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 return OK;
3590}
3591
3592/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003593 * Compile "(expression)": recursive!
3594 * Return FAIL/OK.
3595 */
3596 static int
3597compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3598{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003599 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003600 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003601
Bram Moolenaar24156692021-01-14 20:35:49 +01003602 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3603 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003604 if (ppconst->pp_used <= PPSIZE - 10)
3605 {
3606 ret = compile_expr1(arg, cctx, ppconst);
3607 }
3608 else
3609 {
3610 // Not enough space in ppconst, flush constants.
3611 if (generate_ppconst(cctx, ppconst) == FAIL)
3612 return FAIL;
3613 ret = compile_expr0(arg, cctx);
3614 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003615 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3616 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003617 if (**arg == ')')
3618 ++*arg;
3619 else if (ret == OK)
3620 {
3621 emsg(_(e_missing_close));
3622 ret = FAIL;
3623 }
3624 return ret;
3625}
3626
3627/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003629 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 */
3631 static int
3632compile_subscript(
3633 char_u **arg,
3634 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003635 char_u *start_leader,
3636 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003637 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003639 char_u *name_start = *end_leader;
3640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 for (;;)
3642 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003643 char_u *p = skipwhite(*arg);
3644
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003645 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003646 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003647 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003648
3649 // If a following line starts with "->{" or "->X" advance to that
3650 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003651 // Also if a following line starts with ".x".
3652 if (next != NULL &&
3653 ((next[0] == '-' && next[1] == '>'
3654 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003655 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003656 {
3657 next = next_line_from_context(cctx, TRUE);
3658 if (next == NULL)
3659 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003660 *arg = next;
3661 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003662 }
3663 }
3664
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003665 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003666 // not a function call.
3667 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003669 garray_T *stack = &cctx->ctx_type_stack;
3670 type_T *type;
3671 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003673 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003674 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003675 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003678 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3679
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003680 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3682 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003683 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 return FAIL;
3685 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003686 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003688 char_u *pstart = p;
3689
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003690 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003691 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003692 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 // something->method()
3695 // Apply the '!', '-' and '+' first:
3696 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003697 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003700 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003701 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003702 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003703 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003704 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003705 int argcount = 1;
3706 char_u *expr;
3707 garray_T *stack;
3708 type_T *type;
3709
3710 // Funcref call: list->(Refs[2])(arg)
3711 // or lambda: list->((arg) => expr)(arg)
3712 // Fist compile the arguments.
3713 expr = *arg;
3714 *arg = skipwhite(*arg + 1);
3715 skip_expr_cctx(arg, cctx);
3716 *arg = skipwhite(*arg);
3717 if (**arg != ')')
3718 {
3719 semsg(_(e_missing_paren), *arg);
3720 return FAIL;
3721 }
3722 ++*arg;
3723 if (**arg != '(')
3724 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003725 if (*skipwhite(*arg) == '(')
3726 emsg(_(e_nowhitespace));
3727 else
3728 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003729 return FAIL;
3730 }
3731
3732 *arg = skipwhite(*arg + 1);
3733 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3734 return FAIL;
3735
3736 // Compile the function expression.
3737 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3738 return FAIL;
3739
3740 stack = &cctx->ctx_type_stack;
3741 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3742 if (generate_PCALL(cctx, argcount,
3743 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 return FAIL;
3745 }
3746 else
3747 {
3748 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003749 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003750 if (!eval_isnamec1(*p))
3751 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003752 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003753 return FAIL;
3754 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003755 if (ASCII_ISALPHA(*p) && p[1] == ':')
3756 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003757 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 ;
3759 if (*p != '(')
3760 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003761 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 return FAIL;
3763 }
3764 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003765 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 return FAIL;
3767 }
3768 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003769 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003771 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003772 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003773 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003774 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003775 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003776
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003777 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003778 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003779 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003780 // TODO: blob index
3781 // TODO: more arguments
3782 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003783 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003784 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003785 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003786
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003787 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003788 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003789 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003790 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003791 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003792 // missing first index is equal to zero
3793 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003794 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003795 else
3796 {
3797 if (compile_expr0(arg, cctx) == FAIL)
3798 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003799 if (**arg == ':')
3800 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003801 semsg(_(e_white_space_required_before_and_after_str_at_str),
3802 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003803 return FAIL;
3804 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003805 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003806 return FAIL;
3807 *arg = skipwhite(*arg);
3808 }
3809 if (**arg == ':')
3810 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003811 is_slice = TRUE;
3812 ++*arg;
3813 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3814 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003815 semsg(_(e_white_space_required_before_and_after_str_at_str),
3816 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003817 return FAIL;
3818 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003819 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003820 return FAIL;
3821 if (**arg == ']')
3822 // missing second index is equal to end of string
3823 generate_PUSHNR(cctx, -1);
3824 else
3825 {
3826 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003827 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003828 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003829 return FAIL;
3830 *arg = skipwhite(*arg);
3831 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833
3834 if (**arg != ']')
3835 {
3836 emsg(_(e_missbrac));
3837 return FAIL;
3838 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003839 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003841 // We can index a list and a dict. If we don't know the type
3842 // we can use the index value type.
3843 // TODO: If we don't know use an instruction to figure it out at
3844 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003845 typep = ((type_T **)stack->ga_data) + stack->ga_len
3846 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003847 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003848 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3849 // If the index is a string, the variable must be a Dict.
3850 if (*typep == &t_any && valtype == &t_string)
3851 vtype = VAR_DICT;
3852 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003853 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003854 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003855 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003856 return FAIL;
3857 if (is_slice)
3858 {
3859 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003860 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003861 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003862 return FAIL;
3863 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003864 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003865
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003866 if (vtype == VAR_DICT)
3867 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003868 if (is_slice)
3869 {
3870 emsg(_(e_cannot_slice_dictionary));
3871 return FAIL;
3872 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003874 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003875 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003876 if (*typep == &t_unknown)
3877 // empty dict was used
3878 *typep = &t_any;
3879 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003880 else
3881 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003882 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003883 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003884 return FAIL;
3885 *typep = &t_any;
3886 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003887 if (may_generate_2STRING(-1, cctx) == FAIL)
3888 return FAIL;
3889 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3890 return FAIL;
3891 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003892 else if (vtype == VAR_STRING)
3893 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003894 *typep = &t_string;
3895 if ((is_slice
3896 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3897 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003898 return FAIL;
3899 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003900 else if (vtype == VAR_BLOB)
3901 {
3902 emsg("Sorry, blob index and slice not implemented yet");
3903 return FAIL;
3904 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003905 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003906 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003907 if (is_slice)
3908 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003909 if (generate_instr_drop(cctx,
3910 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3911 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003912 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003913 }
Bram Moolenaared591872020-08-15 22:14:53 +02003914 else
3915 {
3916 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003917 {
Bram Moolenaared591872020-08-15 22:14:53 +02003918 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003919 if (*typep == &t_unknown)
3920 // empty list was used
3921 *typep = &t_any;
3922 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003923 if (generate_instr_drop(cctx,
3924 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3925 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003926 return FAIL;
3927 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003928 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003929 else
3930 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003931 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003932 return FAIL;
3933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003935 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003937 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003938 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003939 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003940 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003941
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003942 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003943 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003944 {
3945 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003946 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003947 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003948 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003949 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 while (eval_isnamec(*p))
3951 MB_PTR_ADV(p);
3952 if (p == *arg)
3953 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003954 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 return FAIL;
3956 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003957 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 return FAIL;
3959 *arg = p;
3960 }
3961 else
3962 break;
3963 }
3964
3965 // TODO - see handle_subscript():
3966 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3967 // Don't do this when "Func" is already a partial that was bound
3968 // explicitly (pt_auto is FALSE).
3969
3970 return OK;
3971}
3972
3973/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003974 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3975 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003977 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003978 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003979 *
3980 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 */
3982
3983/*
3984 * number number constant
3985 * 0zFFFFFFFF Blob constant
3986 * "string" string constant
3987 * 'string' literal string constant
3988 * &option-name option value
3989 * @r register contents
3990 * identifier variable value
3991 * function() function call
3992 * $VAR environment variable
3993 * (expression) nested expression
3994 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003995 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996 *
3997 * Also handle:
3998 * ! in front logical NOT
3999 * - in front unary minus
4000 * + in front unary plus (ignored)
4001 * trailing (arg) funcref/partial call
4002 * trailing [] subscript in String or List
4003 * trailing .name entry in Dictionary
4004 * trailing ->name() method call
4005 */
4006 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004007compile_expr7(
4008 char_u **arg,
4009 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004010 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 char_u *start_leader, *end_leader;
4013 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004014 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004015 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004017 ppconst->pp_is_const = FALSE;
4018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 /*
4020 * Skip '!', '-' and '+' characters. They are handled later.
4021 */
4022 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004023 if (eval_leader(arg, TRUE) == FAIL)
4024 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 end_leader = *arg;
4026
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004027 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 switch (**arg)
4029 {
4030 /*
4031 * Number constant.
4032 */
4033 case '0': // also for blob starting with 0z
4034 case '1':
4035 case '2':
4036 case '3':
4037 case '4':
4038 case '5':
4039 case '6':
4040 case '7':
4041 case '8':
4042 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004043 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004045 // Apply "-" and "+" just before the number now, right to
4046 // left. Matters especially when "->" follows. Stops at
4047 // '!'.
4048 if (apply_leader(rettv, TRUE,
4049 start_leader, &end_leader) == FAIL)
4050 {
4051 clear_tv(rettv);
4052 return FAIL;
4053 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 break;
4055
4056 /*
4057 * String constant: "string".
4058 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004059 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 return FAIL;
4061 break;
4062
4063 /*
4064 * Literal string constant: 'str''ing'.
4065 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004066 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068 break;
4069
4070 /*
4071 * Constant Vim variable.
4072 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004073 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 ret = NOTDONE;
4075 break;
4076
4077 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004078 * "true" constant
4079 */
4080 case 't': if (STRNCMP(*arg, "true", 4) == 0
4081 && !eval_isnamec((*arg)[4]))
4082 {
4083 *arg += 4;
4084 rettv->v_type = VAR_BOOL;
4085 rettv->vval.v_number = VVAL_TRUE;
4086 }
4087 else
4088 ret = NOTDONE;
4089 break;
4090
4091 /*
4092 * "false" constant
4093 */
4094 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4095 && !eval_isnamec((*arg)[5]))
4096 {
4097 *arg += 5;
4098 rettv->v_type = VAR_BOOL;
4099 rettv->vval.v_number = VVAL_FALSE;
4100 }
4101 else
4102 ret = NOTDONE;
4103 break;
4104
4105 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004106 * "null" constant
4107 */
4108 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4109 && !eval_isnamec((*arg)[5]))
4110 {
4111 *arg += 4;
4112 rettv->v_type = VAR_SPECIAL;
4113 rettv->vval.v_number = VVAL_NULL;
4114 }
4115 else
4116 ret = NOTDONE;
4117 break;
4118
4119 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 * List: [expr, expr]
4121 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004122 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 break;
4124
4125 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 * Dictionary: {'key': val, 'key': val}
4127 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004128 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 break;
4130
4131 /*
4132 * Option value: &name
4133 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004134 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4135 return FAIL;
4136 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 break;
4138
4139 /*
4140 * Environment variable: $VAR.
4141 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004142 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4143 return FAIL;
4144 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 break;
4146
4147 /*
4148 * Register contents: @r.
4149 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004150 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4151 return FAIL;
4152 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 break;
4154 /*
4155 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004156 * lambda: (arg, arg) => expr
4157 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004159 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4160 ret = compile_lambda(arg, cctx);
4161 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004162 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 break;
4164
4165 default: ret = NOTDONE;
4166 break;
4167 }
4168 if (ret == FAIL)
4169 return FAIL;
4170
Bram Moolenaar1c747212020-05-09 18:28:34 +02004171 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004173 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004174 clear_tv(rettv);
4175 else
4176 // A constant expression can possibly be handled compile time,
4177 // return the value instead of generating code.
4178 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 }
4180 else if (ret == NOTDONE)
4181 {
4182 char_u *p;
4183 int r;
4184
4185 if (!eval_isnamec1(**arg))
4186 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004187 if (ends_excmd(*skipwhite(*arg)))
4188 semsg(_(e_empty_expression_str), *arg);
4189 else
4190 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 return FAIL;
4192 }
4193
4194 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004195 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004197 {
4198 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004201 {
4202 if (generate_ppconst(cctx, ppconst) == FAIL)
4203 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004204 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 if (r == FAIL)
4207 return FAIL;
4208 }
4209
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004210 // Handle following "[]", ".member", etc.
4211 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004212 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004213 ppconst) == FAIL)
4214 return FAIL;
4215 if (ppconst->pp_used > 0)
4216 {
4217 // apply the '!', '-' and '+' before the constant
4218 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004219 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 return FAIL;
4221 return OK;
4222 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004223 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004225 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226}
4227
4228/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004229 * Give the "white on both sides" error, taking the operator from "p[len]".
4230 */
4231 void
4232error_white_both(char_u *op, int len)
4233{
4234 char_u buf[10];
4235
4236 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004237 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004238}
4239
4240/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004241 * <type>expr7: runtime type check / conversion
4242 */
4243 static int
4244compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4245{
4246 type_T *want_type = NULL;
4247
4248 // Recognize <type>
4249 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4250 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004251 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004252 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4253 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004254 return FAIL;
4255
4256 if (**arg != '>')
4257 {
4258 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004259 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004260 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004261 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004262 return FAIL;
4263 }
4264 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004265 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004266 return FAIL;
4267 }
4268
4269 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4270 return FAIL;
4271
4272 if (want_type != NULL)
4273 {
4274 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004275 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004276
Bram Moolenaard1103582020-08-14 22:44:25 +02004277 generate_ppconst(cctx, ppconst);
4278 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004279 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004280 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004281 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004282 return FAIL;
4283 }
4284 }
4285
4286 return OK;
4287}
4288
4289/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 * * number multiplication
4291 * / number division
4292 * % number modulo
4293 */
4294 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296{
4297 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004298 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004299 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004301 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004302 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return FAIL;
4304
4305 /*
4306 * Repeat computing, until no "*", "/" or "%" is following.
4307 */
4308 for (;;)
4309 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004310 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 if (*op != '*' && *op != '/' && *op != '%')
4312 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004313 if (next != NULL)
4314 {
4315 *arg = next_line_from_context(cctx, TRUE);
4316 op = skipwhite(*arg);
4317 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004318
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004319 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004321 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004324 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004325 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004327 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004328 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004330
4331 if (ppconst->pp_used == ppconst_used + 2
4332 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4333 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004334 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004335 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4336 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4337 varnumber_T res = 0;
4338 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004340 // both are numbers: compute the result
4341 switch (*op)
4342 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004343 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004344 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004345 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004346 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004347 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004348 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004349 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004350 break;
4351 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004352 if (failed)
4353 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004354 tv1->vval.v_number = res;
4355 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004356 }
4357 else
4358 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004359 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004360 generate_two_op(cctx, op);
4361 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 }
4363
4364 return OK;
4365}
4366
4367/*
4368 * + number addition
4369 * - number subtraction
4370 * .. string concatenation
4371 */
4372 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004373compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374{
4375 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004376 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004378 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379
4380 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004381 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 return FAIL;
4383
4384 /*
4385 * Repeat computing, until no "+", "-" or ".." is following.
4386 */
4387 for (;;)
4388 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004389 op = may_peek_next_line(cctx, *arg, &next);
4390 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 break;
4392 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004393 if (next != NULL)
4394 {
4395 *arg = next_line_from_context(cctx, TRUE);
4396 op = skipwhite(*arg);
4397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004399 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004401 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004402 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
4404
Bram Moolenaare0de1712020-12-02 17:36:54 +01004405 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004406 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004408 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 return FAIL;
4411
Bram Moolenaara5565e42020-05-09 15:44:01 +02004412 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004413 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004414 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4415 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4416 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4417 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4420 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004421
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004422 // concat/subtract/add constant numbers
4423 if (*op == '+')
4424 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4425 else if (*op == '-')
4426 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4427 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004428 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004429 // concatenate constant strings
4430 char_u *s1 = tv1->vval.v_string;
4431 char_u *s2 = tv2->vval.v_string;
4432 size_t len1 = STRLEN(s1);
4433
4434 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4435 if (tv1->vval.v_string == NULL)
4436 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004437 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004438 return FAIL;
4439 }
4440 mch_memmove(tv1->vval.v_string, s1, len1);
4441 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004442 vim_free(s1);
4443 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004444 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004445 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 }
4447 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004448 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004450 if (*op == '.')
4451 {
4452 if (may_generate_2STRING(-2, cctx) == FAIL
4453 || may_generate_2STRING(-1, cctx) == FAIL)
4454 return FAIL;
4455 generate_instr_drop(cctx, ISN_CONCAT, 1);
4456 }
4457 else
4458 generate_two_op(cctx, op);
4459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 }
4461
4462 return OK;
4463}
4464
4465/*
4466 * expr5a == expr5b
4467 * expr5a =~ expr5b
4468 * expr5a != expr5b
4469 * expr5a !~ expr5b
4470 * expr5a > expr5b
4471 * expr5a >= expr5b
4472 * expr5a < expr5b
4473 * expr5a <= expr5b
4474 * expr5a is expr5b
4475 * expr5a isnot expr5b
4476 *
4477 * Produces instructions:
4478 * EVAL expr5a Push result of "expr5a"
4479 * EVAL expr5b Push result of "expr5b"
4480 * COMPARE one of the compare instructions
4481 */
4482 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004483compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004485 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004487 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491
4492 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004493 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 return FAIL;
4495
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004496 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004497 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
4499 /*
4500 * If there is a comparative operator, use it.
4501 */
4502 if (type != EXPR_UNKNOWN)
4503 {
4504 int ic = FALSE; // Default: do not ignore case
4505
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004506 if (next != NULL)
4507 {
4508 *arg = next_line_from_context(cctx, TRUE);
4509 p = skipwhite(*arg);
4510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 if (type_is && (p[len] == '?' || p[len] == '#'))
4512 {
4513 semsg(_(e_invexpr2), *arg);
4514 return FAIL;
4515 }
4516 // extra question mark appended: ignore case
4517 if (p[len] == '?')
4518 {
4519 ic = TRUE;
4520 ++len;
4521 }
4522 // extra '#' appended: match case (ignored)
4523 else if (p[len] == '#')
4524 ++len;
4525 // nothing appended: match case
4526
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004527 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004529 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004530 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 }
4532
4533 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004534 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004535 return FAIL;
4536
Bram Moolenaara5565e42020-05-09 15:44:01 +02004537 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 return FAIL;
4539
Bram Moolenaara5565e42020-05-09 15:44:01 +02004540 if (ppconst->pp_used == ppconst_used + 2)
4541 {
4542 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4543 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4544 int ret;
4545
4546 // Both sides are a constant, compute the result now.
4547 // First check for a valid combination of types, this is more
4548 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004549 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 ret = FAIL;
4551 else
4552 {
4553 ret = typval_compare(tv1, tv2, type, ic);
4554 tv1->v_type = VAR_BOOL;
4555 tv1->vval.v_number = tv1->vval.v_number
4556 ? VVAL_TRUE : VVAL_FALSE;
4557 clear_tv(tv2);
4558 --ppconst->pp_used;
4559 }
4560 return ret;
4561 }
4562
4563 generate_ppconst(cctx, ppconst);
4564 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 }
4566
4567 return OK;
4568}
4569
Bram Moolenaar7f141552020-05-09 17:35:53 +02004570static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572/*
4573 * Compile || or &&.
4574 */
4575 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004576compile_and_or(
4577 char_u **arg,
4578 cctx_T *cctx,
4579 char *op,
4580 ppconst_T *ppconst,
4581 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004583 char_u *next;
4584 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 int opchar = *op;
4586
4587 if (p[0] == opchar && p[1] == opchar)
4588 {
4589 garray_T *instr = &cctx->ctx_instr;
4590 garray_T end_ga;
4591
4592 /*
4593 * Repeat until there is no following "||" or "&&"
4594 */
4595 ga_init2(&end_ga, sizeof(int), 10);
4596 while (p[0] == opchar && p[1] == opchar)
4597 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004598 if (next != NULL)
4599 {
4600 *arg = next_line_from_context(cctx, TRUE);
4601 p = skipwhite(*arg);
4602 }
4603
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004604 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4605 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004606 semsg(_(e_white_space_required_before_and_after_str_at_str),
4607 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004608 return FAIL;
4609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 // TODO: use ppconst if the value is a constant and check
4612 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004613 generate_ppconst(cctx, ppconst);
4614
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004615 // Every part must evaluate to a bool.
4616 if (bool_on_stack(cctx) == FAIL)
4617 {
4618 ga_clear(&end_ga);
4619 return FAIL;
4620 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 if (ga_grow(&end_ga, 1) == FAIL)
4623 {
4624 ga_clear(&end_ga);
4625 return FAIL;
4626 }
4627 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4628 ++end_ga.ga_len;
4629 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004630 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
4632 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004633 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004634 {
4635 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004636 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004637 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004638
Bram Moolenaara5565e42020-05-09 15:44:01 +02004639 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4640 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 {
4642 ga_clear(&end_ga);
4643 return FAIL;
4644 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004645
4646 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004650 // Every part must evaluate to a bool.
4651 if (bool_on_stack(cctx) == FAIL)
4652 {
4653 ga_clear(&end_ga);
4654 return FAIL;
4655 }
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 // Fill in the end label in all jumps.
4658 while (end_ga.ga_len > 0)
4659 {
4660 isn_T *isn;
4661
4662 --end_ga.ga_len;
4663 isn = ((isn_T *)instr->ga_data)
4664 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4665 isn->isn_arg.jump.jump_where = instr->ga_len;
4666 }
4667 ga_clear(&end_ga);
4668 }
4669
4670 return OK;
4671}
4672
4673/*
4674 * expr4a && expr4a && expr4a logical AND
4675 *
4676 * Produces instructions:
4677 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004678 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004679 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004681 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 * EVAL expr4c Push result of "expr4c"
4683 * end:
4684 */
4685 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004686compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004688 int ppconst_used = ppconst->pp_used;
4689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 return FAIL;
4693
4694 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696}
4697
4698/*
4699 * expr3a || expr3b || expr3c logical OR
4700 *
4701 * Produces instructions:
4702 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004703 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004704 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004706 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 * EVAL expr3c Push result of "expr3c"
4708 * end:
4709 */
4710 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004713 int ppconst_used = ppconst->pp_used;
4714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004716 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 return FAIL;
4718
4719 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721}
4722
4723/*
4724 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004726 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727 * JUMP_IF_FALSE alt jump if false
4728 * EVAL expr1a
4729 * JUMP_ALWAYS end
4730 * alt: EVAL expr1b
4731 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004732 *
4733 * Toplevel expression: expr2 ?? expr1
4734 * Produces instructions:
4735 * EVAL expr2 Push result of "expr2"
4736 * JUMP_AND_KEEP_IF_TRUE end jump if true
4737 * EVAL expr1
4738 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 */
4740 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004741compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742{
4743 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004744 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004745 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaar3988f642020-08-27 22:43:03 +02004747 // Ignore all kinds of errors when not producing code.
4748 if (cctx->ctx_skip == SKIP_YES)
4749 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004750 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004751 return OK;
4752 }
4753
Bram Moolenaar61a89812020-05-07 16:58:17 +02004754 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004755 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004756 return FAIL;
4757
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004758 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 if (*p == '?')
4760 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004761 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 garray_T *instr = &cctx->ctx_instr;
4763 garray_T *stack = &cctx->ctx_type_stack;
4764 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004765 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004767 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004768 int has_const_expr = FALSE;
4769 int const_value = FALSE;
4770 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004772 if (next != NULL)
4773 {
4774 *arg = next_line_from_context(cctx, TRUE);
4775 p = skipwhite(*arg);
4776 }
4777
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004778 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004779 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004780 semsg(_(e_white_space_required_before_and_after_str_at_str),
4781 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004782 return FAIL;
4783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784
Bram Moolenaara5565e42020-05-09 15:44:01 +02004785 if (ppconst->pp_used == ppconst_used + 1)
4786 {
4787 // the condition is a constant, we know whether the ? or the :
4788 // expression is to be evaluated.
4789 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004790 if (op_falsy)
4791 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4792 else
4793 {
4794 int error = FALSE;
4795
4796 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4797 &error);
4798 if (error)
4799 return FAIL;
4800 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004801 cctx->ctx_skip = save_skip == SKIP_YES ||
4802 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4803
4804 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4805 // "left ?? right" and "left" is truthy: produce "left"
4806 generate_ppconst(cctx, ppconst);
4807 else
4808 {
4809 clear_tv(&ppconst->pp_tv[ppconst_used]);
4810 --ppconst->pp_used;
4811 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004812 }
4813 else
4814 {
4815 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004816 if (op_falsy)
4817 end_idx = instr->ga_len;
4818 generate_JUMP(cctx, op_falsy
4819 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4820 if (op_falsy)
4821 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823
4824 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004825 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004826 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004827 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004828 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829
Bram Moolenaara5565e42020-05-09 15:44:01 +02004830 if (!has_const_expr)
4831 {
4832 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004834 if (!op_falsy)
4835 {
4836 // remember the type and drop it
4837 --stack->ga_len;
4838 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004839
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004840 end_idx = instr->ga_len;
4841 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004842
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004843 // jump here from JUMP_IF_FALSE
4844 isn = ((isn_T *)instr->ga_data) + alt_idx;
4845 isn->isn_arg.jump.jump_where = instr->ga_len;
4846 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004849 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004851 // Check for the ":".
4852 p = may_peek_next_line(cctx, *arg, &next);
4853 if (*p != ':')
4854 {
4855 emsg(_(e_missing_colon));
4856 return FAIL;
4857 }
4858 if (next != NULL)
4859 {
4860 *arg = next_line_from_context(cctx, TRUE);
4861 p = skipwhite(*arg);
4862 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004863
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004864 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4865 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004866 semsg(_(e_white_space_required_before_and_after_str_at_str),
4867 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004868 return FAIL;
4869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004871 // evaluate the third expression
4872 if (has_const_expr)
4873 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004874 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004875 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004876 return FAIL;
4877 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4878 return FAIL;
4879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880
Bram Moolenaara5565e42020-05-09 15:44:01 +02004881 if (!has_const_expr)
4882 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004883 type_T **typep;
4884
Bram Moolenaara5565e42020-05-09 15:44:01 +02004885 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886
Bram Moolenaara5565e42020-05-09 15:44:01 +02004887 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004888 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4889 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004890
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004891 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004892 isn = ((isn_T *)instr->ga_data) + end_idx;
4893 isn->isn_arg.jump.jump_where = instr->ga_len;
4894 }
4895
4896 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 }
4898 return OK;
4899}
4900
4901/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004902 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004903 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4904 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004905 */
4906 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004907compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004908{
4909 ppconst_T ppconst;
4910
4911 CLEAR_FIELD(ppconst);
4912 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4913 {
4914 clear_ppconst(&ppconst);
4915 return FAIL;
4916 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004917 if (is_const != NULL)
4918 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004919 if (generate_ppconst(cctx, &ppconst) == FAIL)
4920 return FAIL;
4921 return OK;
4922}
4923
4924/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004925 * Toplevel expression.
4926 */
4927 static int
4928compile_expr0(char_u **arg, cctx_T *cctx)
4929{
4930 return compile_expr0_ext(arg, cctx, NULL);
4931}
4932
4933/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 * compile "return [expr]"
4935 */
4936 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004937compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938{
4939 char_u *p = arg;
4940 garray_T *stack = &cctx->ctx_type_stack;
4941 type_T *stack_type;
4942
4943 if (*p != NUL && *p != '|' && *p != '\n')
4944 {
4945 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004946 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 return NULL;
4948
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004949 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004950 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004951 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004952 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004953 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4954 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004955 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004956 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004957 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004958 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004959 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004960 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4961 && stack_type->tt_type != VAR_VOID
4962 && stack_type->tt_type != VAR_UNKNOWN)
4963 {
4964 emsg(_(e_returning_value_in_function_without_return_type));
4965 return NULL;
4966 }
4967 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004968 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004969 return NULL;
4970 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004971 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 }
4973 else
4974 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004975 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004976 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004977 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4978 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004980 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 return NULL;
4982 }
4983
4984 // No argument, return zero.
4985 generate_PUSHNR(cctx, 0);
4986 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004987
4988 // Undo any command modifiers.
4989 generate_undo_cmdmods(cctx);
4990
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004991 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 return NULL;
4993
4994 // "return val | endif" is possible
4995 return skipwhite(p);
4996}
4997
4998/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004999 * Get a line from the compilation context, compatible with exarg_T getline().
5000 * Return a pointer to the line in allocated memory.
5001 * Return NULL for end-of-file or some error.
5002 */
5003 static char_u *
5004exarg_getline(
5005 int c UNUSED,
5006 void *cookie,
5007 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005008 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005009{
5010 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005011 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005012
Bram Moolenaar66250c92020-08-20 15:02:42 +02005013 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005014 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005015 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005016 return NULL;
5017 ++cctx->ctx_lnum;
5018 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5019 // Comment lines result in NULL pointers, skip them.
5020 if (p != NULL)
5021 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005022 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005023}
5024
5025/*
5026 * Compile a nested :def command.
5027 */
5028 static char_u *
5029compile_nested_function(exarg_T *eap, cctx_T *cctx)
5030{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005031 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005032 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005033 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005034 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005035 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005036 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005037
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005038 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005039 {
5040 emsg(_(e_cannot_use_bang_with_nested_def));
5041 return NULL;
5042 }
5043
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005044 if (*name_start == '/')
5045 {
5046 name_end = skip_regexp(name_start + 1, '/', TRUE);
5047 if (*name_end == '/')
5048 ++name_end;
5049 eap->nextcmd = check_nextcmd(name_end);
5050 }
5051 if (name_end == name_start || *skipwhite(name_end) != '(')
5052 {
5053 if (!ends_excmd2(name_start, name_end))
5054 {
5055 semsg(_(e_invalid_command_str), eap->cmd);
5056 return NULL;
5057 }
5058
5059 // "def" or "def Name": list functions
5060 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5061 return NULL;
5062 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5063 }
5064
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005065 // Only g:Func() can use a namespace.
5066 if (name_start[1] == ':' && !is_global)
5067 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005068 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005069 return NULL;
5070 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005071 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5072 return NULL;
5073
Bram Moolenaar04b12692020-05-04 23:24:44 +02005074 eap->arg = name_end;
5075 eap->getline = exarg_getline;
5076 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005077 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005078 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005079 lambda_name = vim_strsave(get_lambda_name());
5080 if (lambda_name == NULL)
5081 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005082 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005083
Bram Moolenaar822ba242020-05-24 23:00:18 +02005084 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005085 {
5086 r = eap->skip ? OK : FAIL;
5087 goto theend;
5088 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005089 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
5090 && compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx)
5091 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005092 {
5093 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005094 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005095 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005096
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005097 if (is_global)
5098 {
5099 char_u *func_name = vim_strnsave(name_start + 2,
5100 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005101
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005102 if (func_name == NULL)
5103 r = FAIL;
5104 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005105 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005106 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005107 lambda_name = NULL;
5108 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005109 }
5110 else
5111 {
5112 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005113 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005114 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005115 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005116
Bram Moolenaareef21022020-08-01 22:16:43 +02005117 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005118 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005119 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005120 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005121 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005122
5123 // copy over the block scope IDs
5124 if (block_depth > 0)
5125 {
5126 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5127 if (ufunc->uf_block_ids != NULL)
5128 {
5129 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5130 sizeof(int) * block_depth);
5131 ufunc->uf_block_depth = block_depth;
5132 }
5133 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005134 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005135 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005136 r = OK;
5137
5138theend:
5139 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005140 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005141}
5142
5143/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 * Return the length of an assignment operator, or zero if there isn't one.
5145 */
5146 int
5147assignment_len(char_u *p, int *heredoc)
5148{
5149 if (*p == '=')
5150 {
5151 if (p[1] == '<' && p[2] == '<')
5152 {
5153 *heredoc = TRUE;
5154 return 3;
5155 }
5156 return 1;
5157 }
5158 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5159 return 2;
5160 if (STRNCMP(p, "..=", 3) == 0)
5161 return 3;
5162 return 0;
5163}
5164
5165// words that cannot be used as a variable
5166static char *reserved[] = {
5167 "true",
5168 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005169 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 NULL
5171};
5172
Bram Moolenaar752fc692021-01-04 21:57:11 +01005173// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005174typedef enum {
5175 dest_local,
5176 dest_option,
5177 dest_env,
5178 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005179 dest_buffer,
5180 dest_window,
5181 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005182 dest_vimvar,
5183 dest_script,
5184 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005185 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005186} assign_dest_T;
5187
Bram Moolenaar752fc692021-01-04 21:57:11 +01005188// Used by compile_lhs() to store information about the LHS of an assignment
5189// and one argument of ":unlet" with an index.
5190typedef struct {
5191 assign_dest_T lhs_dest; // type of destination
5192
5193 char_u *lhs_name; // allocated name including
5194 // "[expr]" or ".name".
5195 size_t lhs_varlen; // length of the variable without
5196 // "[expr]" or ".name"
5197 char_u *lhs_dest_end; // end of the destination, including
5198 // "[expr]" or ".name".
5199
5200 int lhs_has_index; // has "[expr]" or ".name"
5201
5202 int lhs_new_local; // create new local variable
5203 int lhs_opt_flags; // for when destination is an option
5204 int lhs_vimvaridx; // for when destination is a v:var
5205
5206 lvar_T lhs_local_lvar; // used for existing local destination
5207 lvar_T lhs_arg_lvar; // used for argument destination
5208 lvar_T *lhs_lvar; // points to destination lvar
5209 int lhs_scriptvar_sid;
5210 int lhs_scriptvar_idx;
5211
5212 int lhs_has_type; // type was specified
5213 type_T *lhs_type;
5214 type_T *lhs_member_type;
5215} lhs_T;
5216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005218 * Generate the load instruction for "name".
5219 */
5220 static void
5221generate_loadvar(
5222 cctx_T *cctx,
5223 assign_dest_T dest,
5224 char_u *name,
5225 lvar_T *lvar,
5226 type_T *type)
5227{
5228 switch (dest)
5229 {
5230 case dest_option:
5231 // TODO: check the option exists
5232 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5233 break;
5234 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005235 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5236 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5237 else
5238 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005239 break;
5240 case dest_buffer:
5241 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5242 break;
5243 case dest_window:
5244 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5245 break;
5246 case dest_tab:
5247 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5248 break;
5249 case dest_script:
5250 compile_load_scriptvar(cctx,
5251 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5252 break;
5253 case dest_env:
5254 // Include $ in the name here
5255 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5256 break;
5257 case dest_reg:
5258 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5259 break;
5260 case dest_vimvar:
5261 generate_LOADV(cctx, name + 2, TRUE);
5262 break;
5263 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005264 if (lvar->lv_from_outer > 0)
5265 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5266 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005267 else
5268 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5269 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005270 case dest_expr:
5271 // list or dict value should already be on the stack.
5272 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005273 }
5274}
5275
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005276/*
5277 * Skip over "[expr]" or ".member".
5278 * Does not check for any errors.
5279 */
5280 static char_u *
5281skip_index(char_u *start)
5282{
5283 char_u *p = start;
5284
5285 if (*p == '[')
5286 {
5287 p = skipwhite(p + 1);
5288 (void)skip_expr(&p, NULL);
5289 p = skipwhite(p);
5290 if (*p == ']')
5291 return p + 1;
5292 return p;
5293 }
5294 // if (*p == '.')
5295 return to_name_end(p + 1, TRUE);
5296}
5297
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005298 void
5299vim9_declare_error(char_u *name)
5300{
5301 char *scope = "";
5302
5303 switch (*name)
5304 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005305 case 'g': scope = _("global"); break;
5306 case 'b': scope = _("buffer"); break;
5307 case 'w': scope = _("window"); break;
5308 case 't': scope = _("tab"); break;
5309 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005310 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005311 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005312 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005313 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005314 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005315 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005316 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005317 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005318 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005319}
5320
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005321/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005322 * For one assignment figure out the type of destination. Return it in "dest".
5323 * When not recognized "dest" is not set.
5324 * For an option "opt_flags" is set.
5325 * For a v:var "vimvaridx" is set.
5326 * "type" is set to the destination type if known, unchanted otherwise.
5327 * Return FAIL if an error message was given.
5328 */
5329 static int
5330get_var_dest(
5331 char_u *name,
5332 assign_dest_T *dest,
5333 int cmdidx,
5334 int *opt_flags,
5335 int *vimvaridx,
5336 type_T **type,
5337 cctx_T *cctx)
5338{
5339 char_u *p;
5340
5341 if (*name == '&')
5342 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005343 int cc;
5344 long numval;
5345 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005346
5347 *dest = dest_option;
5348 if (cmdidx == CMD_final || cmdidx == CMD_const)
5349 {
5350 emsg(_(e_const_option));
5351 return FAIL;
5352 }
5353 p = name;
5354 p = find_option_end(&p, opt_flags);
5355 if (p == NULL)
5356 {
5357 // cannot happen?
5358 emsg(_(e_letunexp));
5359 return FAIL;
5360 }
5361 cc = *p;
5362 *p = NUL;
5363 opt_type = get_option_value(skip_option_env_lead(name),
5364 &numval, NULL, *opt_flags);
5365 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005366 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005367 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005368 case gov_unknown:
5369 semsg(_(e_unknown_option), name);
5370 return FAIL;
5371 case gov_string:
5372 case gov_hidden_string:
5373 *type = &t_string;
5374 break;
5375 case gov_bool:
5376 case gov_hidden_bool:
5377 *type = &t_bool;
5378 break;
5379 case gov_number:
5380 case gov_hidden_number:
5381 *type = &t_number;
5382 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005383 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005384 }
5385 else if (*name == '$')
5386 {
5387 *dest = dest_env;
5388 *type = &t_string;
5389 }
5390 else if (*name == '@')
5391 {
5392 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5393 {
5394 emsg_invreg(name[1]);
5395 return FAIL;
5396 }
5397 *dest = dest_reg;
5398 *type = &t_string;
5399 }
5400 else if (STRNCMP(name, "g:", 2) == 0)
5401 {
5402 *dest = dest_global;
5403 }
5404 else if (STRNCMP(name, "b:", 2) == 0)
5405 {
5406 *dest = dest_buffer;
5407 }
5408 else if (STRNCMP(name, "w:", 2) == 0)
5409 {
5410 *dest = dest_window;
5411 }
5412 else if (STRNCMP(name, "t:", 2) == 0)
5413 {
5414 *dest = dest_tab;
5415 }
5416 else if (STRNCMP(name, "v:", 2) == 0)
5417 {
5418 typval_T *vtv;
5419 int di_flags;
5420
5421 *vimvaridx = find_vim_var(name + 2, &di_flags);
5422 if (*vimvaridx < 0)
5423 {
5424 semsg(_(e_variable_not_found_str), name);
5425 return FAIL;
5426 }
5427 // We use the current value of "sandbox" here, is that OK?
5428 if (var_check_ro(di_flags, name, FALSE))
5429 return FAIL;
5430 *dest = dest_vimvar;
5431 vtv = get_vim_var_tv(*vimvaridx);
5432 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5433 }
5434 return OK;
5435}
5436
5437/*
5438 * Generate a STORE instruction for "dest", not being "dest_local".
5439 * Return FAIL when out of memory.
5440 */
5441 static int
5442generate_store_var(
5443 cctx_T *cctx,
5444 assign_dest_T dest,
5445 int opt_flags,
5446 int vimvaridx,
5447 int scriptvar_idx,
5448 int scriptvar_sid,
5449 type_T *type,
5450 char_u *name)
5451{
5452 switch (dest)
5453 {
5454 case dest_option:
5455 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5456 opt_flags);
5457 case dest_global:
5458 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005459 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5460 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005461 case dest_buffer:
5462 // include b: with the name, easier to execute that way
5463 return generate_STORE(cctx, ISN_STOREB, 0, name);
5464 case dest_window:
5465 // include w: with the name, easier to execute that way
5466 return generate_STORE(cctx, ISN_STOREW, 0, name);
5467 case dest_tab:
5468 // include t: with the name, easier to execute that way
5469 return generate_STORE(cctx, ISN_STORET, 0, name);
5470 case dest_env:
5471 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5472 case dest_reg:
5473 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5474 case dest_vimvar:
5475 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5476 case dest_script:
5477 if (scriptvar_idx < 0)
5478 {
5479 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005480 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005481
Bram Moolenaar41d61962020-12-06 20:12:43 +01005482 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005483 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5484 scriptvar_sid, type);
5485 if (name_s != name)
5486 vim_free(name_s);
5487 return r;
5488 }
5489 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5490 scriptvar_sid, scriptvar_idx, type);
5491 case dest_local:
5492 case dest_expr:
5493 // cannot happen
5494 break;
5495 }
5496 return FAIL;
5497}
5498
Bram Moolenaar752fc692021-01-04 21:57:11 +01005499 static int
5500is_decl_command(int cmdidx)
5501{
5502 return cmdidx == CMD_let || cmdidx == CMD_var
5503 || cmdidx == CMD_final || cmdidx == CMD_const;
5504}
5505
5506/*
5507 * Figure out the LHS type and other properties for an assignment or one item
5508 * of ":unlet" with an index.
5509 * Returns OK or FAIL.
5510 */
5511 static int
5512compile_lhs(
5513 char_u *var_start,
5514 lhs_T *lhs,
5515 int cmdidx,
5516 int heredoc,
5517 int oplen,
5518 cctx_T *cctx)
5519{
5520 char_u *var_end;
5521 int is_decl = is_decl_command(cmdidx);
5522
5523 CLEAR_POINTER(lhs);
5524 lhs->lhs_dest = dest_local;
5525 lhs->lhs_vimvaridx = -1;
5526 lhs->lhs_scriptvar_idx = -1;
5527
5528 // "dest_end" is the end of the destination, including "[expr]" or
5529 // ".name".
5530 // "var_end" is the end of the variable/option/etc. name.
5531 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5532 if (*var_start == '@')
5533 var_end = var_start + 2;
5534 else
5535 {
5536 // skip over the leading "&", "&l:", "&g:" and "$"
5537 var_end = skip_option_env_lead(var_start);
5538 var_end = to_name_end(var_end, TRUE);
5539 }
5540
5541 // "a: type" is declaring variable "a" with a type, not dict "a:".
5542 if (is_decl && lhs->lhs_dest_end == var_start + 2
5543 && lhs->lhs_dest_end[-1] == ':')
5544 --lhs->lhs_dest_end;
5545 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5546 --var_end;
5547
5548 // compute the length of the destination without "[expr]" or ".name"
5549 lhs->lhs_varlen = var_end - var_start;
5550 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5551 if (lhs->lhs_name == NULL)
5552 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005553
5554 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5555 // Something follows after the variable: "var[idx]" or "var.key".
5556 lhs->lhs_has_index = TRUE;
5557
Bram Moolenaar752fc692021-01-04 21:57:11 +01005558 if (heredoc)
5559 lhs->lhs_type = &t_list_string;
5560 else
5561 lhs->lhs_type = &t_any;
5562
5563 if (cctx->ctx_skip != SKIP_YES)
5564 {
5565 int declare_error = FALSE;
5566
5567 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5568 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5569 &lhs->lhs_type, cctx) == FAIL)
5570 return FAIL;
5571 if (lhs->lhs_dest != dest_local)
5572 {
5573 // Specific kind of variable recognized.
5574 declare_error = is_decl;
5575 }
5576 else
5577 {
5578 int idx;
5579
5580 // No specific kind of variable recognized, just a name.
5581 for (idx = 0; reserved[idx] != NULL; ++idx)
5582 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5583 {
5584 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5585 return FAIL;
5586 }
5587
5588
5589 if (lookup_local(var_start, lhs->lhs_varlen,
5590 &lhs->lhs_local_lvar, cctx) == OK)
5591 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5592 else
5593 {
5594 CLEAR_FIELD(lhs->lhs_arg_lvar);
5595 if (arg_exists(var_start, lhs->lhs_varlen,
5596 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5597 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5598 {
5599 if (is_decl)
5600 {
5601 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5602 return FAIL;
5603 }
5604 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5605 }
5606 }
5607 if (lhs->lhs_lvar != NULL)
5608 {
5609 if (is_decl)
5610 {
5611 semsg(_(e_variable_already_declared), lhs->lhs_name);
5612 return FAIL;
5613 }
5614 }
5615 else
5616 {
5617 int script_namespace = lhs->lhs_varlen > 1
5618 && STRNCMP(var_start, "s:", 2) == 0;
5619 int script_var = (script_namespace
5620 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5621 FALSE, cctx)
5622 : script_var_exists(var_start, lhs->lhs_varlen,
5623 TRUE, cctx)) == OK;
5624 imported_T *import =
5625 find_imported(var_start, lhs->lhs_varlen, cctx);
5626
5627 if (script_namespace || script_var || import != NULL)
5628 {
5629 char_u *rawname = lhs->lhs_name
5630 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5631
5632 if (is_decl)
5633 {
5634 if (script_namespace)
5635 semsg(_(e_cannot_declare_script_variable_in_function),
5636 lhs->lhs_name);
5637 else
5638 semsg(_(e_variable_already_declared_in_script),
5639 lhs->lhs_name);
5640 return FAIL;
5641 }
5642 else if (cctx->ctx_ufunc->uf_script_ctx_version
5643 == SCRIPT_VERSION_VIM9
5644 && script_namespace
5645 && !script_var && import == NULL)
5646 {
5647 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5648 return FAIL;
5649 }
5650
5651 lhs->lhs_dest = dest_script;
5652
5653 // existing script-local variables should have a type
5654 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5655 if (import != NULL)
5656 lhs->lhs_scriptvar_sid = import->imp_sid;
5657 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5658 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005659 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005660 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005661 lhs->lhs_scriptvar_sid, rawname,
5662 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5663 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005664 if (lhs->lhs_scriptvar_idx >= 0)
5665 {
5666 scriptitem_T *si = SCRIPT_ITEM(
5667 lhs->lhs_scriptvar_sid);
5668 svar_T *sv =
5669 ((svar_T *)si->sn_var_vals.ga_data)
5670 + lhs->lhs_scriptvar_idx;
5671 lhs->lhs_type = sv->sv_type;
5672 }
5673 }
5674 }
5675 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5676 == FAIL)
5677 return FAIL;
5678 }
5679 }
5680
5681 if (declare_error)
5682 {
5683 vim9_declare_error(lhs->lhs_name);
5684 return FAIL;
5685 }
5686 }
5687
5688 // handle "a:name" as a name, not index "name" on "a"
5689 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5690 var_end = lhs->lhs_dest_end;
5691
5692 if (lhs->lhs_dest != dest_option)
5693 {
5694 if (is_decl && *var_end == ':')
5695 {
5696 char_u *p;
5697
5698 // parse optional type: "let var: type = expr"
5699 if (!VIM_ISWHITE(var_end[1]))
5700 {
5701 semsg(_(e_white_space_required_after_str), ":");
5702 return FAIL;
5703 }
5704 p = skipwhite(var_end + 1);
5705 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5706 if (lhs->lhs_type == NULL)
5707 return FAIL;
5708 lhs->lhs_has_type = TRUE;
5709 }
5710 else if (lhs->lhs_lvar != NULL)
5711 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5712 }
5713
5714 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5715 && lhs->lhs_type->tt_type != VAR_STRING
5716 && lhs->lhs_type->tt_type != VAR_ANY)
5717 {
5718 emsg(_(e_can_only_concatenate_to_string));
5719 return FAIL;
5720 }
5721
5722 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5723 && cctx->ctx_skip != SKIP_YES)
5724 {
5725 if (oplen > 1 && !heredoc)
5726 {
5727 // +=, /=, etc. require an existing variable
5728 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5729 return FAIL;
5730 }
5731 if (!is_decl)
5732 {
5733 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5734 return FAIL;
5735 }
5736
5737 // new local variable
5738 if ((lhs->lhs_type->tt_type == VAR_FUNC
5739 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5740 && var_wrong_func_name(lhs->lhs_name, TRUE))
5741 return FAIL;
5742 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5743 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5744 if (lhs->lhs_lvar == NULL)
5745 return FAIL;
5746 lhs->lhs_new_local = TRUE;
5747 }
5748
5749 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005750 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005751 {
5752 // Something follows after the variable: "var[idx]" or "var.key".
5753 // TODO: should we also handle "->func()" here?
5754 if (is_decl)
5755 {
5756 emsg(_(e_cannot_use_index_when_declaring_variable));
5757 return FAIL;
5758 }
5759
5760 if (var_start[lhs->lhs_varlen] == '['
5761 || var_start[lhs->lhs_varlen] == '.')
5762 {
5763 char_u *after = var_start + lhs->lhs_varlen;
5764 char_u *p;
5765
5766 // Only the last index is used below, if there are others
5767 // before it generate code for the expression. Thus for
5768 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5769 for (;;)
5770 {
5771 p = skip_index(after);
5772 if (*p != '[' && *p != '.')
5773 break;
5774 after = p;
5775 }
5776 if (after > var_start + lhs->lhs_varlen)
5777 {
5778 lhs->lhs_varlen = after - var_start;
5779 lhs->lhs_dest = dest_expr;
5780 // We don't know the type before evaluating the expression,
5781 // use "any" until then.
5782 lhs->lhs_type = &t_any;
5783 }
5784
Bram Moolenaar752fc692021-01-04 21:57:11 +01005785 if (lhs->lhs_type->tt_member == NULL)
5786 lhs->lhs_member_type = &t_any;
5787 else
5788 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5789 }
5790 else
5791 {
5792 semsg("Not supported yet: %s", var_start);
5793 return FAIL;
5794 }
5795 }
5796 return OK;
5797}
5798
5799/*
5800 * Assignment to a list or dict member, or ":unlet" for the item, using the
5801 * information in "lhs".
5802 * Returns OK or FAIL.
5803 */
5804 static int
5805compile_assign_unlet(
5806 char_u *var_start,
5807 lhs_T *lhs,
5808 int is_assign,
5809 type_T *rhs_type,
5810 cctx_T *cctx)
5811{
5812 char_u *p;
5813 int r;
5814 vartype_T dest_type;
5815 size_t varlen = lhs->lhs_varlen;
5816 garray_T *stack = &cctx->ctx_type_stack;
5817
5818 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5819 p = var_start + varlen;
5820 if (*p == '[')
5821 {
5822 p = skipwhite(p + 1);
5823 r = compile_expr0(&p, cctx);
5824 if (r == OK && *skipwhite(p) != ']')
5825 {
5826 // this should not happen
5827 emsg(_(e_missbrac));
5828 r = FAIL;
5829 }
5830 }
5831 else // if (*p == '.')
5832 {
5833 char_u *key_end = to_name_end(p + 1, TRUE);
5834 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5835
5836 r = generate_PUSHS(cctx, key);
5837 }
5838 if (r == FAIL)
5839 return FAIL;
5840
5841 if (lhs->lhs_type == &t_any)
5842 {
5843 // Index on variable of unknown type: check at runtime.
5844 dest_type = VAR_ANY;
5845 }
5846 else
5847 {
5848 dest_type = lhs->lhs_type->tt_type;
5849 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5850 return FAIL;
5851 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005852 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5853 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005854 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005855 }
5856
5857 // Load the dict or list. On the stack we then have:
5858 // - value (for assignment, not for :unlet)
5859 // - index
5860 // - variable
5861 if (lhs->lhs_dest == dest_expr)
5862 {
5863 int c = var_start[varlen];
5864
5865 // Evaluate "ll[expr]" of "ll[expr][idx]"
5866 p = var_start;
5867 var_start[varlen] = NUL;
5868 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5869 {
5870 // this should not happen
5871 emsg(_(e_missbrac));
5872 return FAIL;
5873 }
5874 var_start[varlen] = c;
5875
5876 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5877 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5878 // now we can properly check the type
5879 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005880 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005881 FALSE, FALSE) == FAIL)
5882 return FAIL;
5883 }
5884 else
5885 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5886 lhs->lhs_lvar, lhs->lhs_type);
5887
5888 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5889 {
5890 if (is_assign)
5891 {
5892 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5893
5894 if (isn == NULL)
5895 return FAIL;
5896 isn->isn_arg.vartype = dest_type;
5897 }
5898 else
5899 {
5900 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5901 return FAIL;
5902 }
5903 }
5904 else
5905 {
5906 emsg(_(e_indexable_type_required));
5907 return FAIL;
5908 }
5909
5910 return OK;
5911}
5912
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005913/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005914 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005915 * "let name"
5916 * "var name = expr"
5917 * "final name = expr"
5918 * "const name = expr"
5919 * "name = expr"
5920 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005921 * Return NULL for an error.
5922 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005923 */
5924 static char_u *
5925compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5926{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005927 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005928 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005929 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 char_u *ret = NULL;
5931 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005932 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005934 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005935 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937 int oplen = 0;
5938 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005939 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005941 int is_decl = is_decl_command(cmdidx);
5942 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005944 // Skip over the "var" or "[var, var]" to get to any "=".
5945 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5946 if (p == NULL)
5947 return *arg == '[' ? arg : NULL;
5948
5949 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005951 // TODO: should we allow this, and figure out type inference from list
5952 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005953 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005954 return NULL;
5955 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005956 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958 sp = p;
5959 p = skipwhite(p);
5960 op = p;
5961 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005962
5963 if (var_count > 0 && oplen == 0)
5964 // can be something like "[1, 2]->func()"
5965 return arg;
5966
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005967 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005969 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005970 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005971 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 if (heredoc)
5974 {
5975 list_T *l;
5976 listitem_T *li;
5977
5978 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005979 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005981 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005982 if (l == NULL)
5983 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984
Bram Moolenaar078269b2020-09-21 20:35:55 +02005985 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005986 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005987 // Push each line and the create the list.
5988 FOR_ALL_LIST_ITEMS(l, li)
5989 {
5990 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5991 li->li_tv.vval.v_string = NULL;
5992 }
5993 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 list_free(l);
5996 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005997 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005998 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005999 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006001 char_u *wp;
6002
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006003 // for "[var, var] = expr" evaluate the expression here, loop over the
6004 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006005 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006006
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006007 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006008 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006009 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006010 if (compile_expr0(&p, cctx) == FAIL)
6011 return NULL;
6012 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006014 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006016 type_T *stacktype;
6017
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006018 stacktype = stack->ga_len == 0 ? &t_void
6019 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006020 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006022 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006023 goto theend;
6024 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006025 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006026 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006028 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006029 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6030 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006031 if (stacktype->tt_member != NULL)
6032 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006033 }
6034 }
6035
6036 /*
6037 * Loop over variables in "[var, var] = expr".
6038 * For "var = expr" and "let var: type" this is done only once.
6039 */
6040 if (var_count > 0)
6041 var_start = skipwhite(arg + 1); // skip over the "["
6042 else
6043 var_start = arg;
6044 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6045 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006046 int instr_count = -1;
6047
Bram Moolenaar752fc692021-01-04 21:57:11 +01006048 vim_free(lhs.lhs_name);
6049
6050 /*
6051 * Figure out the LHS type and other properties.
6052 */
6053 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6054 goto theend;
6055
6056 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006057 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006058 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006059 goto theend;
6060 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006061 if (!is_decl && lhs.lhs_lvar != NULL
6062 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006063 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006064 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006065 goto theend;
6066 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006067
6068 if (!heredoc)
6069 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006070 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006071 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006072 if (oplen > 0 && var_count == 0)
6073 {
6074 // skip over the "=" and the expression
6075 p = skipwhite(op + oplen);
6076 compile_expr0(&p, cctx);
6077 }
6078 }
6079 else if (oplen > 0)
6080 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006081 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006082 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006083
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006084 // For "var = expr" evaluate the expression.
6085 if (var_count == 0)
6086 {
6087 int r;
6088
6089 // for "+=", "*=", "..=" etc. first load the current value
6090 if (*op != '=')
6091 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006092 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6093 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006094
Bram Moolenaar752fc692021-01-04 21:57:11 +01006095 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006096 {
6097 // TODO: get member from list or dict
6098 emsg("Index with operation not supported yet");
6099 goto theend;
6100 }
6101 }
6102
6103 // Compile the expression. Temporarily hide the new local
6104 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006105 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006106 --cctx->ctx_locals.ga_len;
6107 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006108 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006109 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006110 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006111 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006112 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006113 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006114 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006115 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006116 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006117 ++cctx->ctx_locals.ga_len;
6118 if (r == FAIL)
6119 goto theend;
6120 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006121 else if (semicolon && var_idx == var_count - 1)
6122 {
6123 // For "[var; var] = expr" get the rest of the list
6124 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6125 goto theend;
6126 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006127 else
6128 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006129 // For "[var, var] = expr" get the "var_idx" item from the
6130 // list.
6131 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006132 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006134
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006135 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006136 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006137 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006138 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006139 if ((rhs_type->tt_type == VAR_FUNC
6140 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006141 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006142 goto theend;
6143
Bram Moolenaar752fc692021-01-04 21:57:11 +01006144 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006145 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006146 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006148 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006149 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006150 }
6151 else
6152 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006153 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006154 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006155 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006156 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006157 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006158 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006159 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006160 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006161 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006162 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006163 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006164 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006165 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006166 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006168
Bram Moolenaar71abe482020-09-14 22:28:30 +02006169 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006170 if (lhs.lhs_has_index)
6171 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006172 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006173 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006174 goto theend;
6175 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006176 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006177 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006178 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006179 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006180 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006181 else if (cmdidx == CMD_final)
6182 {
6183 emsg(_(e_final_requires_a_value));
6184 goto theend;
6185 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006186 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006188 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006189 goto theend;
6190 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006191 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006192 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006193 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006194 goto theend;
6195 }
6196 else
6197 {
6198 // variables are always initialized
6199 if (ga_grow(instr, 1) == FAIL)
6200 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006201 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006202 {
6203 case VAR_BOOL:
6204 generate_PUSHBOOL(cctx, VVAL_FALSE);
6205 break;
6206 case VAR_FLOAT:
6207#ifdef FEAT_FLOAT
6208 generate_PUSHF(cctx, 0.0);
6209#endif
6210 break;
6211 case VAR_STRING:
6212 generate_PUSHS(cctx, NULL);
6213 break;
6214 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006215 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006216 break;
6217 case VAR_FUNC:
6218 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6219 break;
6220 case VAR_LIST:
6221 generate_NEWLIST(cctx, 0);
6222 break;
6223 case VAR_DICT:
6224 generate_NEWDICT(cctx, 0);
6225 break;
6226 case VAR_JOB:
6227 generate_PUSHJOB(cctx, NULL);
6228 break;
6229 case VAR_CHANNEL:
6230 generate_PUSHCHANNEL(cctx, NULL);
6231 break;
6232 case VAR_NUMBER:
6233 case VAR_UNKNOWN:
6234 case VAR_ANY:
6235 case VAR_PARTIAL:
6236 case VAR_VOID:
6237 case VAR_SPECIAL: // cannot happen
6238 generate_PUSHNR(cctx, 0);
6239 break;
6240 }
6241 }
6242 if (var_count == 0)
6243 end = p;
6244 }
6245
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006246 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006247 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006248 break;
6249
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006250 if (oplen > 0 && *op != '=')
6251 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006252 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006253 type_T *stacktype;
6254
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006255 if (*op == '.')
6256 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006257 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006258 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006259 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006260 if (
6261#ifdef FEAT_FLOAT
6262 // If variable is float operation with number is OK.
6263 !(expected == &t_float && stacktype == &t_number) &&
6264#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006265 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006266 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006267 goto theend;
6268
6269 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006270 {
6271 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6272 goto theend;
6273 }
6274 else if (*op == '+')
6275 {
6276 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006277 operator_type(lhs.lhs_member_type, stacktype),
6278 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006279 goto theend;
6280 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006281 else if (generate_two_op(cctx, op) == FAIL)
6282 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284
Bram Moolenaar752fc692021-01-04 21:57:11 +01006285 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006286 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006287 // Use the info in "lhs" to store the value at the index in the
6288 // list or dict.
6289 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6290 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006291 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006292 }
6293 else
6294 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006295 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6296 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006297 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006298 generate_LOCKCONST(cctx);
6299
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006300 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006301 && (lhs.lhs_type->tt_type == VAR_DICT
6302 || lhs.lhs_type->tt_type == VAR_LIST)
6303 && lhs.lhs_type->tt_member != NULL
6304 && lhs.lhs_type->tt_member != &t_any
6305 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006306 // Set the type in the list or dict, so that it can be checked,
6307 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006309
Bram Moolenaar752fc692021-01-04 21:57:11 +01006310 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006311 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006312 if (generate_store_var(cctx, lhs.lhs_dest,
6313 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6314 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6315 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006316 goto theend;
6317 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006318 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006319 {
6320 isn_T *isn = ((isn_T *)instr->ga_data)
6321 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006322
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006323 // optimization: turn "var = 123" from ISN_PUSHNR +
6324 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006325 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006326 && instr->ga_len == instr_count + 1
6327 && isn->isn_type == ISN_PUSHNR)
6328 {
6329 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006330
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006331 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006332 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006333 isn->isn_arg.storenr.stnr_val = val;
6334 if (stack->ga_len > 0)
6335 --stack->ga_len;
6336 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006337 else if (lhs.lhs_lvar->lv_from_outer > 0)
6338 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6339 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006340 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006341 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006342 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006343 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006344
6345 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006346 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006347 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006348
6349 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006350 if (var_count > 0 && !semicolon)
6351 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006352 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006353 goto theend;
6354 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006355
Bram Moolenaarb2097502020-07-19 17:17:02 +02006356 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
6358theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006359 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006360 return ret;
6361}
6362
6363/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006364 * Check for an assignment at "eap->cmd", compile it if found.
6365 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6366 */
6367 static int
6368may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6369{
6370 char_u *pskip;
6371 char_u *p;
6372
6373 // Assuming the command starts with a variable or function name,
6374 // find what follows.
6375 // Skip over "var.member", "var[idx]" and the like.
6376 // Also "&opt = val", "$ENV = val" and "@r = val".
6377 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6378 ? eap->cmd + 1 : eap->cmd;
6379 p = to_name_end(pskip, TRUE);
6380 if (p > eap->cmd && *p != NUL)
6381 {
6382 char_u *var_end;
6383 int oplen;
6384 int heredoc;
6385
6386 if (eap->cmd[0] == '@')
6387 var_end = eap->cmd + 2;
6388 else
6389 var_end = find_name_end(pskip, NULL, NULL,
6390 FNE_CHECK_START | FNE_INCL_BR);
6391 oplen = assignment_len(skipwhite(var_end), &heredoc);
6392 if (oplen > 0)
6393 {
6394 size_t len = p - eap->cmd;
6395
6396 // Recognize an assignment if we recognize the variable
6397 // name:
6398 // "g:var = expr"
6399 // "local = expr" where "local" is a local var.
6400 // "script = expr" where "script" is a script-local var.
6401 // "import = expr" where "import" is an imported var
6402 // "&opt = expr"
6403 // "$ENV = expr"
6404 // "@r = expr"
6405 if (*eap->cmd == '&'
6406 || *eap->cmd == '$'
6407 || *eap->cmd == '@'
6408 || ((len) > 2 && eap->cmd[1] == ':')
6409 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6410 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6411 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6412 || find_imported(eap->cmd, len, cctx) != NULL)
6413 {
6414 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6415 if (*line == NULL || *line == eap->cmd)
6416 return FAIL;
6417 return OK;
6418 }
6419 }
6420 }
6421
6422 if (*eap->cmd == '[')
6423 {
6424 // [var, var] = expr
6425 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6426 if (*line == NULL)
6427 return FAIL;
6428 if (*line != eap->cmd)
6429 return OK;
6430 }
6431 return NOTDONE;
6432}
6433
6434/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006435 * Check if "name" can be "unlet".
6436 */
6437 int
6438check_vim9_unlet(char_u *name)
6439{
6440 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6441 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006442 // "unlet s:var" is allowed in legacy script.
6443 if (*name == 's' && !script_is_vim9())
6444 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006445 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006446 return FAIL;
6447 }
6448 return OK;
6449}
6450
6451/*
6452 * Callback passed to ex_unletlock().
6453 */
6454 static int
6455compile_unlet(
6456 lval_T *lvp,
6457 char_u *name_end,
6458 exarg_T *eap,
6459 int deep UNUSED,
6460 void *coookie)
6461{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006462 cctx_T *cctx = coookie;
6463 char_u *p = lvp->ll_name;
6464 int cc = *name_end;
6465 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006466
Bram Moolenaar752fc692021-01-04 21:57:11 +01006467 if (cctx->ctx_skip == SKIP_YES)
6468 return OK;
6469
6470 *name_end = NUL;
6471 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006472 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006473 // :unlet $ENV_VAR
6474 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6475 }
6476 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6477 {
6478 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006479
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 // This is similar to assigning: lookup the list/dict, compile the
6481 // idx/key. Then instead of storing the value unlet the item.
6482 // unlet {list}[idx]
6483 // unlet {dict}[key] dict.key
6484 //
6485 // Figure out the LHS type and other properties.
6486 //
6487 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6488
6489 // : unlet an indexed item
6490 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006491 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006492 iemsg("called compile_lhs() without an index");
6493 ret = FAIL;
6494 }
6495 else
6496 {
6497 // Use the info in "lhs" to unlet the item at the index in the
6498 // list or dict.
6499 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006500 }
6501
Bram Moolenaar752fc692021-01-04 21:57:11 +01006502 vim_free(lhs.lhs_name);
6503 }
6504 else if (check_vim9_unlet(p) == FAIL)
6505 {
6506 ret = FAIL;
6507 }
6508 else
6509 {
6510 // Normal name. Only supports g:, w:, t: and b: namespaces.
6511 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006512 }
6513
Bram Moolenaar752fc692021-01-04 21:57:11 +01006514 *name_end = cc;
6515 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006516}
6517
6518/*
6519 * compile "unlet var", "lock var" and "unlock var"
6520 * "arg" points to "var".
6521 */
6522 static char_u *
6523compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6524{
6525 char_u *p = arg;
6526
6527 if (eap->cmdidx != CMD_unlet)
6528 {
6529 emsg("Sorry, :lock and unlock not implemented yet");
6530 return NULL;
6531 }
6532
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006533 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6534 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006535 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6536}
6537
6538/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539 * Compile an :import command.
6540 */
6541 static char_u *
6542compile_import(char_u *arg, cctx_T *cctx)
6543{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006544 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006545}
6546
6547/*
6548 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6549 */
6550 static int
6551compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6552{
6553 garray_T *instr = &cctx->ctx_instr;
6554 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6555
6556 if (endlabel == NULL)
6557 return FAIL;
6558 endlabel->el_next = *el;
6559 *el = endlabel;
6560 endlabel->el_end_label = instr->ga_len;
6561
6562 generate_JUMP(cctx, when, 0);
6563 return OK;
6564}
6565
6566 static void
6567compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6568{
6569 garray_T *instr = &cctx->ctx_instr;
6570
6571 while (*el != NULL)
6572 {
6573 endlabel_T *cur = (*el);
6574 isn_T *isn;
6575
6576 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6577 isn->isn_arg.jump.jump_where = instr->ga_len;
6578 *el = cur->el_next;
6579 vim_free(cur);
6580 }
6581}
6582
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006583 static void
6584compile_free_jump_to_end(endlabel_T **el)
6585{
6586 while (*el != NULL)
6587 {
6588 endlabel_T *cur = (*el);
6589
6590 *el = cur->el_next;
6591 vim_free(cur);
6592 }
6593}
6594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006595/*
6596 * Create a new scope and set up the generic items.
6597 */
6598 static scope_T *
6599new_scope(cctx_T *cctx, scopetype_T type)
6600{
6601 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6602
6603 if (scope == NULL)
6604 return NULL;
6605 scope->se_outer = cctx->ctx_scope;
6606 cctx->ctx_scope = scope;
6607 scope->se_type = type;
6608 scope->se_local_count = cctx->ctx_locals.ga_len;
6609 return scope;
6610}
6611
6612/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006613 * Free the current scope and go back to the outer scope.
6614 */
6615 static void
6616drop_scope(cctx_T *cctx)
6617{
6618 scope_T *scope = cctx->ctx_scope;
6619
6620 if (scope == NULL)
6621 {
6622 iemsg("calling drop_scope() without a scope");
6623 return;
6624 }
6625 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006626 switch (scope->se_type)
6627 {
6628 case IF_SCOPE:
6629 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6630 case FOR_SCOPE:
6631 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6632 case WHILE_SCOPE:
6633 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6634 case TRY_SCOPE:
6635 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6636 case NO_SCOPE:
6637 case BLOCK_SCOPE:
6638 break;
6639 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006640 vim_free(scope);
6641}
6642
6643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006644 * compile "if expr"
6645 *
6646 * "if expr" Produces instructions:
6647 * EVAL expr Push result of "expr"
6648 * JUMP_IF_FALSE end
6649 * ... body ...
6650 * end:
6651 *
6652 * "if expr | else" Produces instructions:
6653 * EVAL expr Push result of "expr"
6654 * JUMP_IF_FALSE else
6655 * ... body ...
6656 * JUMP_ALWAYS end
6657 * else:
6658 * ... body ...
6659 * end:
6660 *
6661 * "if expr1 | elseif expr2 | else" Produces instructions:
6662 * EVAL expr Push result of "expr"
6663 * JUMP_IF_FALSE elseif
6664 * ... body ...
6665 * JUMP_ALWAYS end
6666 * elseif:
6667 * EVAL expr Push result of "expr"
6668 * JUMP_IF_FALSE else
6669 * ... body ...
6670 * JUMP_ALWAYS end
6671 * else:
6672 * ... body ...
6673 * end:
6674 */
6675 static char_u *
6676compile_if(char_u *arg, cctx_T *cctx)
6677{
6678 char_u *p = arg;
6679 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006680 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006681 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006682 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006683 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006684
Bram Moolenaara5565e42020-05-09 15:44:01 +02006685 CLEAR_FIELD(ppconst);
6686 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006687 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006688 clear_ppconst(&ppconst);
6689 return NULL;
6690 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006691 if (cctx->ctx_skip == SKIP_YES)
6692 clear_ppconst(&ppconst);
6693 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006694 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006695 int error = FALSE;
6696 int v;
6697
Bram Moolenaara5565e42020-05-09 15:44:01 +02006698 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006699 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006700 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006701 if (error)
6702 return NULL;
6703 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006704 }
6705 else
6706 {
6707 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006708 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006709 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006710 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006711 if (bool_on_stack(cctx) == FAIL)
6712 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714
6715 scope = new_scope(cctx, IF_SCOPE);
6716 if (scope == NULL)
6717 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006718 scope->se_skip_save = skip_save;
6719 // "is_had_return" will be reset if any block does not end in :return
6720 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006722 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006723 {
6724 // "where" is set when ":elseif", "else" or ":endif" is found
6725 scope->se_u.se_if.is_if_label = instr->ga_len;
6726 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6727 }
6728 else
6729 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006730
6731 return p;
6732}
6733
6734 static char_u *
6735compile_elseif(char_u *arg, cctx_T *cctx)
6736{
6737 char_u *p = arg;
6738 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006739 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740 isn_T *isn;
6741 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006742 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006743 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
6745 if (scope == NULL || scope->se_type != IF_SCOPE)
6746 {
6747 emsg(_(e_elseif_without_if));
6748 return NULL;
6749 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006750 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006751 if (!cctx->ctx_had_return)
6752 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006753
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006754 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006755 {
6756 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006757 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006758 return NULL;
6759 // previous "if" or "elseif" jumps here
6760 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6761 isn->isn_arg.jump.jump_where = instr->ga_len;
6762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006763
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006764 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006765 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006766 if (cctx->ctx_skip == SKIP_YES)
6767 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006768 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006769 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006770 clear_ppconst(&ppconst);
6771 return NULL;
6772 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006773 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006774 if (scope->se_skip_save == SKIP_YES)
6775 clear_ppconst(&ppconst);
6776 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006777 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006778 int error = FALSE;
6779 int v;
6780
Bram Moolenaar7f141552020-05-09 17:35:53 +02006781 // The expression results in a constant.
6782 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006783 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6784 if (error)
6785 return NULL;
6786 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006787 clear_ppconst(&ppconst);
6788 scope->se_u.se_if.is_if_label = -1;
6789 }
6790 else
6791 {
6792 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006793 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006794 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006795 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006796 if (bool_on_stack(cctx) == FAIL)
6797 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006798
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006799 // "where" is set when ":elseif", "else" or ":endif" is found
6800 scope->se_u.se_if.is_if_label = instr->ga_len;
6801 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803
6804 return p;
6805}
6806
6807 static char_u *
6808compile_else(char_u *arg, cctx_T *cctx)
6809{
6810 char_u *p = arg;
6811 garray_T *instr = &cctx->ctx_instr;
6812 isn_T *isn;
6813 scope_T *scope = cctx->ctx_scope;
6814
6815 if (scope == NULL || scope->se_type != IF_SCOPE)
6816 {
6817 emsg(_(e_else_without_if));
6818 return NULL;
6819 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006820 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006821 if (!cctx->ctx_had_return)
6822 scope->se_u.se_if.is_had_return = FALSE;
6823 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006824
Bram Moolenaarefd88552020-06-18 20:50:10 +02006825 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006826 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006827 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006828 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006829 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006830 if (!cctx->ctx_had_return
6831 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6832 JUMP_ALWAYS, cctx) == FAIL)
6833 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006834 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006835
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006836 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006837 {
6838 if (scope->se_u.se_if.is_if_label >= 0)
6839 {
6840 // previous "if" or "elseif" jumps here
6841 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6842 isn->isn_arg.jump.jump_where = instr->ga_len;
6843 scope->se_u.se_if.is_if_label = -1;
6844 }
6845 }
6846
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006847 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006848 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850
6851 return p;
6852}
6853
6854 static char_u *
6855compile_endif(char_u *arg, cctx_T *cctx)
6856{
6857 scope_T *scope = cctx->ctx_scope;
6858 ifscope_T *ifscope;
6859 garray_T *instr = &cctx->ctx_instr;
6860 isn_T *isn;
6861
6862 if (scope == NULL || scope->se_type != IF_SCOPE)
6863 {
6864 emsg(_(e_endif_without_if));
6865 return NULL;
6866 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006867 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006868 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006869 if (!cctx->ctx_had_return)
6870 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006871
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006872 if (scope->se_u.se_if.is_if_label >= 0)
6873 {
6874 // previous "if" or "elseif" jumps here
6875 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6876 isn->isn_arg.jump.jump_where = instr->ga_len;
6877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878 // Fill in the "end" label in jumps at the end of the blocks.
6879 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006880 cctx->ctx_skip = scope->se_skip_save;
6881
6882 // If all the blocks end in :return and there is an :else then the
6883 // had_return flag is set.
6884 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006886 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006887 return arg;
6888}
6889
6890/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006891 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892 *
6893 * Produces instructions:
6894 * PUSHNR -1
6895 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006896 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006897 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6898 * - if beyond end, jump to "end"
6899 * - otherwise get item from list and push it
6900 * STORE var Store item in "var"
6901 * ... body ...
6902 * JUMP top Jump back to repeat
6903 * end: DROP Drop the result of "expr"
6904 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006905 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6906 * UNPACK 2 Split item in 2
6907 * STORE var1 Store item in "var1"
6908 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006909 */
6910 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006911compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006912{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006913 char_u *arg;
6914 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006915 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006917 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006918 int var_count = 0;
6919 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006920 size_t varlen;
6921 garray_T *instr = &cctx->ctx_instr;
6922 garray_T *stack = &cctx->ctx_type_stack;
6923 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006924 lvar_T *loop_lvar; // loop iteration variable
6925 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006926 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006927 type_T *item_type = &t_any;
6928 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006929
Bram Moolenaar792f7862020-11-23 08:31:18 +01006930 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006931 if (p == NULL)
6932 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006933 if (var_count == 0)
6934 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006935
6936 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006937 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006938 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6939 return NULL;
6940 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 {
6942 emsg(_(e_missing_in));
6943 return NULL;
6944 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006945 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006946 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6947 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006949 scope = new_scope(cctx, FOR_SCOPE);
6950 if (scope == NULL)
6951 return NULL;
6952
Bram Moolenaar792f7862020-11-23 08:31:18 +01006953 // Reserve a variable to store the loop iteration counter and initialize it
6954 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006955 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6956 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006957 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006958 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006959 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006960 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006961 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006962 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006963
6964 // compile "expr", it remains on the stack until "endfor"
6965 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006966 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006967 {
6968 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006969 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006970 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006971 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006973 // Now that we know the type of "var", check that it is a list, now or at
6974 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006975 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006976 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006977 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006978 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 return NULL;
6980 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006981
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006982 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006983 {
6984 if (var_count == 1)
6985 item_type = vartype->tt_member;
6986 else if (vartype->tt_member->tt_type == VAR_LIST
6987 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6988 item_type = vartype->tt_member->tt_member;
6989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006990
6991 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006992 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006993 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994
Bram Moolenaar792f7862020-11-23 08:31:18 +01006995 arg = arg_start;
6996 if (var_count > 1)
6997 {
6998 generate_UNPACK(cctx, var_count, semicolon);
6999 arg = skipwhite(arg + 1); // skip white after '['
7000
7001 // the list item is replaced by a number of items
7002 if (ga_grow(stack, var_count - 1) == FAIL)
7003 {
7004 drop_scope(cctx);
7005 return NULL;
7006 }
7007 --stack->ga_len;
7008 for (idx = 0; idx < var_count; ++idx)
7009 {
7010 ((type_T **)stack->ga_data)[stack->ga_len] =
7011 (semicolon && idx == 0) ? vartype : item_type;
7012 ++stack->ga_len;
7013 }
7014 }
7015
7016 for (idx = 0; idx < var_count; ++idx)
7017 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007018 assign_dest_T dest = dest_local;
7019 int opt_flags = 0;
7020 int vimvaridx = -1;
7021 type_T *type = &t_any;
7022
7023 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007024 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007025 name = vim_strnsave(arg, varlen);
7026 if (name == NULL)
7027 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007028
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007029 // TODO: script var not supported?
7030 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7031 &vimvaridx, &type, cctx) == FAIL)
7032 goto failed;
7033 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007034 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007035 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7036 0, 0, type, name) == FAIL)
7037 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007038 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007039 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007040 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007041 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007042 {
7043 semsg(_(e_variable_already_declared), arg);
7044 goto failed;
7045 }
7046
Bram Moolenaarea870692020-12-02 14:24:30 +01007047 if (STRNCMP(name, "s:", 2) == 0)
7048 {
7049 semsg(_(e_cannot_declare_script_variable_in_function), name);
7050 goto failed;
7051 }
7052
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007053 // Reserve a variable to store "var".
7054 // TODO: check for type
7055 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7056 if (var_lvar == NULL)
7057 // out of memory or used as an argument
7058 goto failed;
7059
7060 if (semicolon && idx == var_count - 1)
7061 var_lvar->lv_type = vartype;
7062 else
7063 var_lvar->lv_type = item_type;
7064 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7065 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007066
Bram Moolenaar036d0712021-01-17 20:23:38 +01007067 if (*p == ':')
7068 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007069 if (*p == ',' || *p == ';')
7070 ++p;
7071 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007072 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007073 }
7074
7075 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007076
7077failed:
7078 vim_free(name);
7079 drop_scope(cctx);
7080 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081}
7082
7083/*
7084 * compile "endfor"
7085 */
7086 static char_u *
7087compile_endfor(char_u *arg, cctx_T *cctx)
7088{
7089 garray_T *instr = &cctx->ctx_instr;
7090 scope_T *scope = cctx->ctx_scope;
7091 forscope_T *forscope;
7092 isn_T *isn;
7093
7094 if (scope == NULL || scope->se_type != FOR_SCOPE)
7095 {
7096 emsg(_(e_for));
7097 return NULL;
7098 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007099 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007100 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007101 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007102
7103 // At end of ":for" scope jump back to the FOR instruction.
7104 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7105
7106 // Fill in the "end" label in the FOR statement so it can jump here
7107 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7108 isn->isn_arg.forloop.for_end = instr->ga_len;
7109
7110 // Fill in the "end" label any BREAK statements
7111 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7112
7113 // Below the ":for" scope drop the "expr" list from the stack.
7114 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7115 return NULL;
7116
7117 vim_free(scope);
7118
7119 return arg;
7120}
7121
7122/*
7123 * compile "while expr"
7124 *
7125 * Produces instructions:
7126 * top: EVAL expr Push result of "expr"
7127 * JUMP_IF_FALSE end jump if false
7128 * ... body ...
7129 * JUMP top Jump back to repeat
7130 * end:
7131 *
7132 */
7133 static char_u *
7134compile_while(char_u *arg, cctx_T *cctx)
7135{
7136 char_u *p = arg;
7137 garray_T *instr = &cctx->ctx_instr;
7138 scope_T *scope;
7139
7140 scope = new_scope(cctx, WHILE_SCOPE);
7141 if (scope == NULL)
7142 return NULL;
7143
Bram Moolenaarb2049902021-01-24 12:53:53 +01007144 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007145 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarb2049902021-01-24 12:53:53 +01007146 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7147 .isn_type == ISN_PROF_START)
7148 --scope->se_u.se_while.ws_top_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007149
7150 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007151 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 return NULL;
7153
Bram Moolenaar13106602020-10-04 16:06:05 +02007154 if (bool_on_stack(cctx) == FAIL)
7155 return FAIL;
7156
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007157 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007158 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 JUMP_IF_FALSE, cctx) == FAIL)
7160 return FAIL;
7161
7162 return p;
7163}
7164
7165/*
7166 * compile "endwhile"
7167 */
7168 static char_u *
7169compile_endwhile(char_u *arg, cctx_T *cctx)
7170{
7171 scope_T *scope = cctx->ctx_scope;
7172
7173 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7174 {
7175 emsg(_(e_while));
7176 return NULL;
7177 }
7178 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007179 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007180
Bram Moolenaarb2049902021-01-24 12:53:53 +01007181 // count the endwhile before jumping
7182 may_generate_prof_end(cctx, cctx->ctx_lnum);
7183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007185 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186
7187 // Fill in the "end" label in the WHILE statement so it can jump here.
7188 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007189 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190
7191 vim_free(scope);
7192
7193 return arg;
7194}
7195
7196/*
7197 * compile "continue"
7198 */
7199 static char_u *
7200compile_continue(char_u *arg, cctx_T *cctx)
7201{
7202 scope_T *scope = cctx->ctx_scope;
7203
7204 for (;;)
7205 {
7206 if (scope == NULL)
7207 {
7208 emsg(_(e_continue));
7209 return NULL;
7210 }
7211 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7212 break;
7213 scope = scope->se_outer;
7214 }
7215
7216 // Jump back to the FOR or WHILE instruction.
7217 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007218 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7219 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007220 return arg;
7221}
7222
7223/*
7224 * compile "break"
7225 */
7226 static char_u *
7227compile_break(char_u *arg, cctx_T *cctx)
7228{
7229 scope_T *scope = cctx->ctx_scope;
7230 endlabel_T **el;
7231
7232 for (;;)
7233 {
7234 if (scope == NULL)
7235 {
7236 emsg(_(e_break));
7237 return NULL;
7238 }
7239 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7240 break;
7241 scope = scope->se_outer;
7242 }
7243
7244 // Jump to the end of the FOR or WHILE loop.
7245 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007246 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007247 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007248 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7250 return FAIL;
7251
7252 return arg;
7253}
7254
7255/*
7256 * compile "{" start of block
7257 */
7258 static char_u *
7259compile_block(char_u *arg, cctx_T *cctx)
7260{
7261 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7262 return NULL;
7263 return skipwhite(arg + 1);
7264}
7265
7266/*
7267 * compile end of block: drop one scope
7268 */
7269 static void
7270compile_endblock(cctx_T *cctx)
7271{
7272 scope_T *scope = cctx->ctx_scope;
7273
7274 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007275 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007276 vim_free(scope);
7277}
7278
7279/*
7280 * compile "try"
7281 * Creates a new scope for the try-endtry, pointing to the first catch and
7282 * finally.
7283 * Creates another scope for the "try" block itself.
7284 * TRY instruction sets up exception handling at runtime.
7285 *
7286 * "try"
7287 * TRY -> catch1, -> finally push trystack entry
7288 * ... try block
7289 * "throw {exception}"
7290 * EVAL {exception}
7291 * THROW create exception
7292 * ... try block
7293 * " catch {expr}"
7294 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007295 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007296 * EVAL {expr}
7297 * MATCH
7298 * JUMP nomatch -> catch2
7299 * CATCH remove exception
7300 * ... catch block
7301 * " catch"
7302 * JUMP -> finally
7303 * catch2: CATCH remove exception
7304 * ... catch block
7305 * " finally"
7306 * finally:
7307 * ... finally block
7308 * " endtry"
7309 * ENDTRY pop trystack entry, may rethrow
7310 */
7311 static char_u *
7312compile_try(char_u *arg, cctx_T *cctx)
7313{
7314 garray_T *instr = &cctx->ctx_instr;
7315 scope_T *try_scope;
7316 scope_T *scope;
7317
7318 // scope that holds the jumps that go to catch/finally/endtry
7319 try_scope = new_scope(cctx, TRY_SCOPE);
7320 if (try_scope == NULL)
7321 return NULL;
7322
Bram Moolenaar69f70502021-01-01 16:10:46 +01007323 if (cctx->ctx_skip != SKIP_YES)
7324 {
7325 // "catch" is set when the first ":catch" is found.
7326 // "finally" is set when ":finally" or ":endtry" is found
7327 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7328 if (generate_instr(cctx, ISN_TRY) == NULL)
7329 return NULL;
7330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331
7332 // scope for the try block itself
7333 scope = new_scope(cctx, BLOCK_SCOPE);
7334 if (scope == NULL)
7335 return NULL;
7336
7337 return arg;
7338}
7339
7340/*
7341 * compile "catch {expr}"
7342 */
7343 static char_u *
7344compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7345{
7346 scope_T *scope = cctx->ctx_scope;
7347 garray_T *instr = &cctx->ctx_instr;
7348 char_u *p;
7349 isn_T *isn;
7350
7351 // end block scope from :try or :catch
7352 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7353 compile_endblock(cctx);
7354 scope = cctx->ctx_scope;
7355
7356 // Error if not in a :try scope
7357 if (scope == NULL || scope->se_type != TRY_SCOPE)
7358 {
7359 emsg(_(e_catch));
7360 return NULL;
7361 }
7362
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007363 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007364 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007365 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007366 return NULL;
7367 }
7368
Bram Moolenaar69f70502021-01-01 16:10:46 +01007369 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007370 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007371 // Jump from end of previous block to :finally or :endtry
7372 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7373 JUMP_ALWAYS, cctx) == FAIL)
7374 return NULL;
7375
7376 // End :try or :catch scope: set value in ISN_TRY instruction
7377 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7378 if (isn->isn_arg.try.try_catch == 0)
7379 isn->isn_arg.try.try_catch = instr->ga_len;
7380 if (scope->se_u.se_try.ts_catch_label != 0)
7381 {
7382 // Previous catch without match jumps here
7383 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7384 isn->isn_arg.jump.jump_where = instr->ga_len;
7385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007386 }
7387
7388 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007389 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007391 scope->se_u.se_try.ts_caught_all = TRUE;
7392 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 }
7394 else
7395 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007396 char_u *end;
7397 char_u *pat;
7398 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007399 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007400 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 // Push v:exception, push {expr} and MATCH
7403 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7404
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007405 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007406 if (*end != *p)
7407 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007408 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007409 vim_free(tofree);
7410 return FAIL;
7411 }
7412 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007413 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007414 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007415 len = (int)(end - tofree);
7416 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007417 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007418 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007419 if (pat == NULL)
7420 return FAIL;
7421 if (generate_PUSHS(cctx, pat) == FAIL)
7422 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7425 return NULL;
7426
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007427 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7429 return NULL;
7430 }
7431
Bram Moolenaar69f70502021-01-01 16:10:46 +01007432 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 return NULL;
7434
7435 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7436 return NULL;
7437 return p;
7438}
7439
7440 static char_u *
7441compile_finally(char_u *arg, cctx_T *cctx)
7442{
7443 scope_T *scope = cctx->ctx_scope;
7444 garray_T *instr = &cctx->ctx_instr;
7445 isn_T *isn;
7446
7447 // end block scope from :try or :catch
7448 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7449 compile_endblock(cctx);
7450 scope = cctx->ctx_scope;
7451
7452 // Error if not in a :try scope
7453 if (scope == NULL || scope->se_type != TRY_SCOPE)
7454 {
7455 emsg(_(e_finally));
7456 return NULL;
7457 }
7458
7459 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007460 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 if (isn->isn_arg.try.try_finally != 0)
7462 {
7463 emsg(_(e_finally_dup));
7464 return NULL;
7465 }
7466
7467 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007468 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
Bram Moolenaar585fea72020-04-02 22:33:21 +02007470 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007471 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 {
7473 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007474 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007476 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 }
7478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 // TODO: set index in ts_finally_label jumps
7480
7481 return arg;
7482}
7483
7484 static char_u *
7485compile_endtry(char_u *arg, cctx_T *cctx)
7486{
7487 scope_T *scope = cctx->ctx_scope;
7488 garray_T *instr = &cctx->ctx_instr;
7489 isn_T *isn;
7490
7491 // end block scope from :catch or :finally
7492 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7493 compile_endblock(cctx);
7494 scope = cctx->ctx_scope;
7495
7496 // Error if not in a :try scope
7497 if (scope == NULL || scope->se_type != TRY_SCOPE)
7498 {
7499 if (scope == NULL)
7500 emsg(_(e_no_endtry));
7501 else if (scope->se_type == WHILE_SCOPE)
7502 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007503 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007504 emsg(_(e_endfor));
7505 else
7506 emsg(_(e_endif));
7507 return NULL;
7508 }
7509
Bram Moolenaar69f70502021-01-01 16:10:46 +01007510 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007511 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007512 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7513 if (isn->isn_arg.try.try_catch == 0
7514 && isn->isn_arg.try.try_finally == 0)
7515 {
7516 emsg(_(e_missing_catch_or_finally));
7517 return NULL;
7518 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007519
Bram Moolenaar69f70502021-01-01 16:10:46 +01007520 // Fill in the "end" label in jumps at the end of the blocks, if not
7521 // done by ":finally".
7522 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523
Bram Moolenaar69f70502021-01-01 16:10:46 +01007524 // End :catch or :finally scope: set value in ISN_TRY instruction
7525 if (isn->isn_arg.try.try_catch == 0)
7526 isn->isn_arg.try.try_catch = instr->ga_len;
7527 if (isn->isn_arg.try.try_finally == 0)
7528 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007529
Bram Moolenaar69f70502021-01-01 16:10:46 +01007530 if (scope->se_u.se_try.ts_catch_label != 0)
7531 {
7532 // Last catch without match jumps here
7533 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7534 isn->isn_arg.jump.jump_where = instr->ga_len;
7535 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007536 }
7537
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007538 compile_endblock(cctx);
7539
Bram Moolenaar69f70502021-01-01 16:10:46 +01007540 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 return NULL;
7542 return arg;
7543}
7544
7545/*
7546 * compile "throw {expr}"
7547 */
7548 static char_u *
7549compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7550{
7551 char_u *p = skipwhite(arg);
7552
Bram Moolenaara5565e42020-05-09 15:44:01 +02007553 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007555 if (cctx->ctx_skip == SKIP_YES)
7556 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557 if (may_generate_2STRING(-1, cctx) == FAIL)
7558 return NULL;
7559 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7560 return NULL;
7561
7562 return p;
7563}
7564
7565/*
7566 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007567 * compile "echomsg expr"
7568 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007569 * compile "execute expr"
7570 */
7571 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007572compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007573{
7574 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007575 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007576 int count = 0;
7577
7578 for (;;)
7579 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007580 if (ends_excmd2(prev, p))
7581 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007582 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007583 return NULL;
7584 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007585 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007586 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007587 }
7588
Bram Moolenaare4984292020-12-13 14:19:25 +01007589 if (count > 0)
7590 {
7591 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7592 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7593 else if (cmdidx == CMD_execute)
7594 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7595 else if (cmdidx == CMD_echomsg)
7596 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7597 else
7598 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 return p;
7601}
7602
7603/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007604 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007605 * instruction to compute it and return OK.
7606 * Otherwise return FAIL, the caller must deal with any range.
7607 */
7608 static int
7609compile_variable_range(exarg_T *eap, cctx_T *cctx)
7610{
7611 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7612 char_u *p = skipdigits(eap->cmd);
7613
7614 if (p == range_end)
7615 return FAIL;
7616 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7617}
7618
7619/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007620 * :put r
7621 * :put ={expr}
7622 */
7623 static char_u *
7624compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7625{
7626 char_u *line = arg;
7627 linenr_T lnum;
7628 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007629 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007630
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007631 eap->regname = *line;
7632
7633 if (eap->regname == '=')
7634 {
7635 char_u *p = line + 1;
7636
7637 if (compile_expr0(&p, cctx) == FAIL)
7638 return NULL;
7639 line = p;
7640 }
7641 else if (eap->regname != NUL)
7642 ++line;
7643
Bram Moolenaar08597872020-12-10 19:43:40 +01007644 if (compile_variable_range(eap, cctx) == OK)
7645 {
7646 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7647 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007648 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007649 {
7650 // Either no range or a number.
7651 // "errormsg" will not be set because the range is ADDR_LINES.
7652 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007653 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007654 return NULL;
7655 if (eap->addr_count == 0)
7656 lnum = -1;
7657 else
7658 lnum = eap->line2;
7659 if (above)
7660 --lnum;
7661 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007662
7663 generate_PUT(cctx, eap->regname, lnum);
7664 return line;
7665}
7666
7667/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007668 * A command that is not compiled, execute with legacy code.
7669 */
7670 static char_u *
7671compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7672{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007673 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007674 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007675 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007676
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007677 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007678 goto theend;
7679
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007680 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007681 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007682 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007683 int usefilter = FALSE;
7684
7685 has_expr = argt & (EX_XFILE | EX_EXPAND);
7686
7687 // If the command can be followed by a bar, find the bar and truncate
7688 // it, so that the following command can be compiled.
7689 // The '|' is overwritten with a NUL, it is put back below.
7690 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7691 && *eap->arg == '!')
7692 // :w !filter or :r !filter or :r! filter
7693 usefilter = TRUE;
7694 if ((argt & EX_TRLBAR) && !usefilter)
7695 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007696 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007697 separate_nextcmd(eap);
7698 if (eap->nextcmd != NULL)
7699 nextcmd = eap->nextcmd;
7700 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007701 else if (eap->cmdidx == CMD_wincmd)
7702 {
7703 p = eap->arg;
7704 if (*p != NUL)
7705 ++p;
7706 if (*p == 'g' || *p == Ctrl_G)
7707 ++p;
7708 p = skipwhite(p);
7709 if (*p == '|')
7710 {
7711 *p = NUL;
7712 nextcmd = p + 1;
7713 }
7714 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007715 }
7716
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007717 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7718 {
7719 // expand filename in "syntax include [@group] filename"
7720 has_expr = TRUE;
7721 eap->arg = skipwhite(eap->arg + 7);
7722 if (*eap->arg == '@')
7723 eap->arg = skiptowhite(eap->arg);
7724 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007725
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007726 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7727 && STRLEN(eap->arg) > 4)
7728 {
7729 int delim = *eap->arg;
7730
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007731 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007732 if (*p == delim)
7733 {
7734 eap->arg = p + 1;
7735 has_expr = TRUE;
7736 }
7737 }
7738
Bram Moolenaarecac5912021-01-05 19:23:28 +01007739 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7740 {
7741 // TODO: should only expand when appropriate for the command
7742 eap->arg = skiptowhite(eap->arg);
7743 has_expr = TRUE;
7744 }
7745
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007746 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007747 {
7748 int count = 0;
7749 char_u *start = skipwhite(line);
7750
7751 // :cmd xxx`=expr1`yyy`=expr2`zzz
7752 // PUSHS ":cmd xxx"
7753 // eval expr1
7754 // PUSHS "yyy"
7755 // eval expr2
7756 // PUSHS "zzz"
7757 // EXECCONCAT 5
7758 for (;;)
7759 {
7760 if (p > start)
7761 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007762 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007763 ++count;
7764 }
7765 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007766 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007767 return NULL;
7768 may_generate_2STRING(-1, cctx);
7769 ++count;
7770 p = skipwhite(p);
7771 if (*p != '`')
7772 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007773 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007774 return NULL;
7775 }
7776 start = p + 1;
7777
7778 p = (char_u *)strstr((char *)start, "`=");
7779 if (p == NULL)
7780 {
7781 if (*skipwhite(start) != NUL)
7782 {
7783 generate_PUSHS(cctx, vim_strsave(start));
7784 ++count;
7785 }
7786 break;
7787 }
7788 }
7789 generate_EXECCONCAT(cctx, count);
7790 }
7791 else
7792 generate_EXEC(cctx, line);
7793
7794theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007795 if (*nextcmd != NUL)
7796 {
7797 // the parser expects a pointer to the bar, put it back
7798 --nextcmd;
7799 *nextcmd = '|';
7800 }
7801
7802 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007803}
7804
7805/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007806 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007807 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007808 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007809 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007810add_def_function(ufunc_T *ufunc)
7811{
7812 dfunc_T *dfunc;
7813
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007814 if (def_functions.ga_len == 0)
7815 {
7816 // The first position is not used, so that a zero uf_dfunc_idx means it
7817 // wasn't set.
7818 if (ga_grow(&def_functions, 1) == FAIL)
7819 return FAIL;
7820 ++def_functions.ga_len;
7821 }
7822
Bram Moolenaar09689a02020-05-09 22:50:08 +02007823 // Add the function to "def_functions".
7824 if (ga_grow(&def_functions, 1) == FAIL)
7825 return FAIL;
7826 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7827 CLEAR_POINTER(dfunc);
7828 dfunc->df_idx = def_functions.ga_len;
7829 ufunc->uf_dfunc_idx = dfunc->df_idx;
7830 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007831 dfunc->df_name = vim_strsave(ufunc->uf_name);
7832 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007833 ++def_functions.ga_len;
7834 return OK;
7835}
7836
7837/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007838 * After ex_function() has collected all the function lines: parse and compile
7839 * the lines into instructions.
7840 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007841 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7842 * the return statement (used for lambda). When uf_ret_type is already set
7843 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01007844 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007845 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007846 * This can be used recursively through compile_lambda(), which may reallocate
7847 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007848 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007850 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01007851compile_def_function(
7852 ufunc_T *ufunc,
7853 int check_return_type,
7854 int profiling,
7855 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007856{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007857 char_u *line = NULL;
7858 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007859 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 cctx_T cctx;
7861 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007862 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 int ret = FAIL;
7864 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007865 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007866 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007867 int new_def_function = FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01007868 int prof_lnum = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007870 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007871 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007872 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007874 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7875 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007876 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007877 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007878 else
7879 {
7880 if (add_def_function(ufunc) == FAIL)
7881 return FAIL;
7882 new_def_function = TRUE;
7883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007884
Bram Moolenaar985116a2020-07-12 17:31:09 +02007885 ufunc->uf_def_status = UF_COMPILING;
7886
Bram Moolenaara80faa82020-04-12 19:37:17 +02007887 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01007888
7889 cctx.ctx_profiling = profiling;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 cctx.ctx_ufunc = ufunc;
7891 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007892 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007893 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7894 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7895 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7896 cctx.ctx_type_list = &ufunc->uf_type_list;
7897 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7898 instr = &cctx.ctx_instr;
7899
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007900 // Set the context to the function, it may be compiled when called from
7901 // another script. Set the script version to the most modern one.
7902 // The line number will be set in next_line_from_context().
7903 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007904 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7905
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007906 // Make sure error messages are OK.
7907 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7908 if (do_estack_push)
7909 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007910 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007911
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007912 if (ufunc->uf_def_args.ga_len > 0)
7913 {
7914 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007915 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007916 int i;
7917 char_u *arg;
7918 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007919 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007920
7921 // Produce instructions for the default values of optional arguments.
7922 // Store the instruction index in uf_def_arg_idx[] so that we know
7923 // where to start when the function is called, depending on the number
7924 // of arguments.
7925 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7926 if (ufunc->uf_def_arg_idx == NULL)
7927 goto erret;
7928 for (i = 0; i < count; ++i)
7929 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007930 garray_T *stack = &cctx.ctx_type_stack;
7931 type_T *val_type;
7932 int arg_idx = first_def_arg + i;
7933
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007934 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7935 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007936 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007937 goto erret;
7938
7939 // If no type specified use the type of the default value.
7940 // Otherwise check that the default value type matches the
7941 // specified type.
7942 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7943 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007944 {
7945 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007946 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007947 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007948 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7949 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007950 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007951
7952 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007953 goto erret;
7954 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007955 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007956
7957 if (did_set_arg_type)
7958 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007959 }
7960
7961 /*
7962 * Loop over all the lines of the function and generate instructions.
7963 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007964 for (;;)
7965 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007966 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007967 int starts_with_colon = FALSE;
7968 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007969 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007970
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007971 // Bail out on the first error to avoid a flood of errors and report
7972 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007973 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007974 goto erret;
7975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007976 if (line != NULL && *line == '|')
7977 // the line continues after a '|'
7978 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007979 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007980 && !(*line == '#' && (line == cctx.ctx_line_start
7981 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007982 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007983 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007984 goto erret;
7985 }
7986 else
7987 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007988 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007989 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01007990 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007991 // beyond the last line
Bram Moolenaarb2049902021-01-24 12:53:53 +01007992 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007993 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01007994 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007995 }
7996
Bram Moolenaara80faa82020-04-12 19:37:17 +02007997 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 ea.cmdlinep = &line;
7999 ea.cmd = skipwhite(line);
8000
Bram Moolenaarb2049902021-01-24 12:53:53 +01008001 if (*ea.cmd == '#')
8002 {
8003 // "#" starts a comment
8004 line = (char_u *)"";
8005 continue;
8006 }
8007
8008 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum)
8009 {
8010 may_generate_prof_end(&cctx, prof_lnum);
8011
8012 prof_lnum = cctx.ctx_lnum;
8013 generate_instr(&cctx, ISN_PROF_START);
8014 }
8015
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008016 // Some things can be recognized by the first character.
8017 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008018 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008019 case '}':
8020 {
8021 // "}" ends a block scope
8022 scopetype_T stype = cctx.ctx_scope == NULL
8023 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008025 if (stype == BLOCK_SCOPE)
8026 {
8027 compile_endblock(&cctx);
8028 line = ea.cmd;
8029 }
8030 else
8031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008032 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008033 goto erret;
8034 }
8035 if (line != NULL)
8036 line = skipwhite(ea.cmd + 1);
8037 continue;
8038 }
8039
8040 case '{':
8041 // "{" starts a block scope
8042 // "{'a': 1}->func() is something else
8043 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8044 {
8045 line = compile_block(ea.cmd, &cctx);
8046 continue;
8047 }
8048 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008049 }
8050
8051 /*
8052 * COMMAND MODIFIERS
8053 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008054 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008055 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8056 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008057 {
8058 if (errormsg != NULL)
8059 goto erret;
8060 // empty line or comment
8061 line = (char_u *)"";
8062 continue;
8063 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008064 generate_cmdmods(&cctx, &local_cmdmod);
8065 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008066
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008067 // Check if there was a colon after the last command modifier or before
8068 // the current position.
8069 for (p = ea.cmd; p >= line; --p)
8070 {
8071 if (*p == ':')
8072 starts_with_colon = TRUE;
8073 if (p < ea.cmd && !VIM_ISWHITE(*p))
8074 break;
8075 }
8076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008077 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008078 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008079 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008080 {
8081 if (*ea.cmd == '(')
8082 // not for "call()"
8083 ea.cmd = p;
8084 else
8085 ea.cmd = skipwhite(ea.cmd);
8086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008087
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008088 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008089 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008090 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008091
Bram Moolenaar17126b12021-01-07 22:03:02 +01008092 // Check for assignment after command modifiers.
8093 assign = may_compile_assignment(&ea, &line, &cctx);
8094 if (assign == OK)
8095 goto nextline;
8096 if (assign == FAIL)
8097 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098 }
8099
8100 /*
8101 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008102 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008103 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008104 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008105 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008106 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008107 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008108 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008109 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008110 if (!starts_with_colon)
8111 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008112 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008113 goto erret;
8114 }
8115 if (ends_excmd2(line, ea.cmd))
8116 {
8117 // A range without a command: jump to the line.
8118 // TODO: compile to a more efficient command, possibly
8119 // calling parse_cmd_address().
8120 ea.cmdidx = CMD_SIZE;
8121 line = compile_exec(line, &ea, &cctx);
8122 goto nextline;
8123 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008124 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008125 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008126 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008127 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008128 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008130 if (p == NULL)
8131 {
8132 if (cctx.ctx_skip != SKIP_YES)
8133 emsg(_(e_ambiguous_use_of_user_defined_command));
8134 goto erret;
8135 }
8136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8138 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008139 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008140 {
8141 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008142 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008143 }
8144
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008145 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008146 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008148 // CMD_var cannot happen, compile_assignment() above would be
8149 // used. Most likely an assignment to a non-existing variable.
8150 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008151 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008153 }
8154
Bram Moolenaar3988f642020-08-27 22:43:03 +02008155 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008156 && ea.cmdidx != CMD_elseif
8157 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008158 && ea.cmdidx != CMD_endif
8159 && ea.cmdidx != CMD_endfor
8160 && ea.cmdidx != CMD_endwhile
8161 && ea.cmdidx != CMD_catch
8162 && ea.cmdidx != CMD_finally
8163 && ea.cmdidx != CMD_endtry)
8164 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008165 emsg(_(e_unreachable_code_after_return));
8166 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008167 }
8168
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008169 p = skipwhite(p);
8170 if (ea.cmdidx != CMD_SIZE
8171 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8172 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008173 if (ea.cmdidx >= 0)
8174 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008175 if ((ea.argt & EX_BANG) && *p == '!')
8176 {
8177 ea.forceit = TRUE;
8178 p = skipwhite(p + 1);
8179 }
8180 }
8181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008182 switch (ea.cmdidx)
8183 {
8184 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008185 ea.arg = p;
8186 line = compile_nested_function(&ea, &cctx);
8187 break;
8188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008189 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008190 // TODO: should we allow this, e.g. to declare a global
8191 // function?
8192 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008193 goto erret;
8194
8195 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008196 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008197 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008198 break;
8199
8200 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008201 emsg(_(e_cannot_use_let_in_vim9_script));
8202 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008203 case CMD_var:
8204 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008205 case CMD_const:
8206 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008207 if (line == p)
8208 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008209 break;
8210
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008211 case CMD_unlet:
8212 case CMD_unlockvar:
8213 case CMD_lockvar:
8214 line = compile_unletlock(p, &ea, &cctx);
8215 break;
8216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 case CMD_import:
8218 line = compile_import(p, &cctx);
8219 break;
8220
8221 case CMD_if:
8222 line = compile_if(p, &cctx);
8223 break;
8224 case CMD_elseif:
8225 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008226 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008227 break;
8228 case CMD_else:
8229 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008230 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 break;
8232 case CMD_endif:
8233 line = compile_endif(p, &cctx);
8234 break;
8235
8236 case CMD_while:
8237 line = compile_while(p, &cctx);
8238 break;
8239 case CMD_endwhile:
8240 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008241 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242 break;
8243
8244 case CMD_for:
8245 line = compile_for(p, &cctx);
8246 break;
8247 case CMD_endfor:
8248 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008249 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 break;
8251 case CMD_continue:
8252 line = compile_continue(p, &cctx);
8253 break;
8254 case CMD_break:
8255 line = compile_break(p, &cctx);
8256 break;
8257
8258 case CMD_try:
8259 line = compile_try(p, &cctx);
8260 break;
8261 case CMD_catch:
8262 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008263 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008264 break;
8265 case CMD_finally:
8266 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008267 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008268 break;
8269 case CMD_endtry:
8270 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008271 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008272 break;
8273 case CMD_throw:
8274 line = compile_throw(p, &cctx);
8275 break;
8276
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008277 case CMD_eval:
8278 if (compile_expr0(&p, &cctx) == FAIL)
8279 goto erret;
8280
Bram Moolenaar3988f642020-08-27 22:43:03 +02008281 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008282 generate_instr_drop(&cctx, ISN_DROP, 1);
8283
8284 line = skipwhite(p);
8285 break;
8286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008288 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008289 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008290 case CMD_echomsg:
8291 case CMD_echoerr:
8292 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008294
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008295 case CMD_put:
8296 ea.cmd = cmd;
8297 line = compile_put(p, &ea, &cctx);
8298 break;
8299
Bram Moolenaar3988f642020-08-27 22:43:03 +02008300 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008301
Bram Moolenaarae616492020-07-28 20:07:27 +02008302 case CMD_append:
8303 case CMD_change:
8304 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008305 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008306 case CMD_xit:
8307 not_in_vim9(&ea);
8308 goto erret;
8309
Bram Moolenaar002262f2020-07-08 17:47:57 +02008310 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008311 if (cctx.ctx_skip != SKIP_YES)
8312 {
8313 semsg(_(e_invalid_command_str), ea.cmd);
8314 goto erret;
8315 }
8316 // We don't check for a next command here.
8317 line = (char_u *)"";
8318 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008320 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008321 if (cctx.ctx_skip == SKIP_YES)
8322 {
8323 // We don't check for a next command here.
8324 line = (char_u *)"";
8325 }
8326 else
8327 {
8328 // Not recognized, execute with do_cmdline_cmd().
8329 ea.arg = p;
8330 line = compile_exec(line, &ea, &cctx);
8331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008332 break;
8333 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008334nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008335 if (line == NULL)
8336 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008337 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008338
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008339 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008340 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008342 if (cctx.ctx_type_stack.ga_len < 0)
8343 {
8344 iemsg("Type stack underflow");
8345 goto erret;
8346 }
8347 }
8348
8349 if (cctx.ctx_scope != NULL)
8350 {
8351 if (cctx.ctx_scope->se_type == IF_SCOPE)
8352 emsg(_(e_endif));
8353 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8354 emsg(_(e_endwhile));
8355 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8356 emsg(_(e_endfor));
8357 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008358 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008359 goto erret;
8360 }
8361
Bram Moolenaarefd88552020-06-18 20:50:10 +02008362 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008363 {
8364 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8365 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008366 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008367 goto erret;
8368 }
8369
8370 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008371 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008372 }
8373
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008374 {
8375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8376 + ufunc->uf_dfunc_idx;
8377 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008378 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008379 if (cctx.ctx_profiling)
8380 {
8381 dfunc->df_instr_prof = instr->ga_data;
8382 dfunc->df_instr_prof_count = instr->ga_len;
8383 }
8384 else
8385 {
8386 dfunc->df_instr = instr->ga_data;
8387 dfunc->df_instr_count = instr->ga_len;
8388 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008389 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008390 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008391 if (cctx.ctx_outer_used)
8392 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008393 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008395
8396 ret = OK;
8397
8398erret:
8399 if (ret == FAIL)
8400 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008401 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8403 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008404
8405 for (idx = 0; idx < instr->ga_len; ++idx)
8406 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008407 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008408 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008409
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008410 // If using the last entry in the table and it was added above, we
8411 // might as well remove it.
8412 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008413 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008414 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008415 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008416 ufunc->uf_dfunc_idx = 0;
8417 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008418 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008419
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008420 while (cctx.ctx_scope != NULL)
8421 drop_scope(&cctx);
8422
Bram Moolenaar20431c92020-03-20 18:39:46 +01008423 // Don't execute this function body.
8424 ga_clear_strings(&ufunc->uf_lines);
8425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008426 if (errormsg != NULL)
8427 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008428 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008429 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008430 }
8431
8432 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008433 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008434 if (do_estack_push)
8435 estack_pop();
8436
Bram Moolenaar20431c92020-03-20 18:39:46 +01008437 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008438 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008439 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008440 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008441}
8442
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008443 void
8444set_function_type(ufunc_T *ufunc)
8445{
8446 int varargs = ufunc->uf_va_name != NULL;
8447 int argcount = ufunc->uf_args.ga_len;
8448
8449 // Create a type for the function, with the return type and any
8450 // argument types.
8451 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8452 // The type is included in "tt_args".
8453 if (argcount > 0 || varargs)
8454 {
8455 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8456 argcount, &ufunc->uf_type_list);
8457 // Add argument types to the function type.
8458 if (func_type_add_arg_types(ufunc->uf_func_type,
8459 argcount + varargs,
8460 &ufunc->uf_type_list) == FAIL)
8461 return;
8462 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8463 ufunc->uf_func_type->tt_min_argcount =
8464 argcount - ufunc->uf_def_args.ga_len;
8465 if (ufunc->uf_arg_types == NULL)
8466 {
8467 int i;
8468
8469 // lambda does not have argument types.
8470 for (i = 0; i < argcount; ++i)
8471 ufunc->uf_func_type->tt_args[i] = &t_any;
8472 }
8473 else
8474 mch_memmove(ufunc->uf_func_type->tt_args,
8475 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8476 if (varargs)
8477 {
8478 ufunc->uf_func_type->tt_args[argcount] =
8479 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8480 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8481 }
8482 }
8483 else
8484 // No arguments, can use a predefined type.
8485 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8486 argcount, &ufunc->uf_type_list);
8487}
8488
8489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008490/*
8491 * Delete an instruction, free what it contains.
8492 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008493 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008494delete_instr(isn_T *isn)
8495{
8496 switch (isn->isn_type)
8497 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008498 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008499 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008500 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008501 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008502 case ISN_LOADENV:
8503 case ISN_LOADG:
8504 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008505 case ISN_LOADT:
8506 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008507 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008508 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008509 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008510 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008511 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008512 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008513 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008514 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008515 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008516 case ISN_STOREW:
8517 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008518 vim_free(isn->isn_arg.string);
8519 break;
8520
8521 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008522 case ISN_STORES:
8523 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008524 break;
8525
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008526 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008527 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008528 vim_free(isn->isn_arg.unlet.ul_name);
8529 break;
8530
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008531 case ISN_STOREOPT:
8532 vim_free(isn->isn_arg.storeopt.so_name);
8533 break;
8534
8535 case ISN_PUSHBLOB: // push blob isn_arg.blob
8536 blob_unref(isn->isn_arg.blob);
8537 break;
8538
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008539 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008540#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008541 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008542#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008543 break;
8544
8545 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008547 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008548#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008549 break;
8550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008551 case ISN_UCALL:
8552 vim_free(isn->isn_arg.ufunc.cuf_name);
8553 break;
8554
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008555 case ISN_FUNCREF:
8556 {
8557 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8558 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008559 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008560
Bram Moolenaar077a4232020-12-22 18:33:27 +01008561 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8562 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008563 }
8564 break;
8565
8566 case ISN_DCALL:
8567 {
8568 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8569 + isn->isn_arg.dfunc.cdf_idx;
8570
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008571 if (dfunc->df_ufunc != NULL
8572 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008573 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008574 }
8575 break;
8576
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008577 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008578 {
8579 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8580 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8581
8582 if (ufunc != NULL)
8583 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008584 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008585 func_ptr_unref(ufunc);
8586 }
8587
8588 vim_free(lambda);
8589 vim_free(isn->isn_arg.newfunc.nf_global);
8590 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008591 break;
8592
Bram Moolenaar5e654232020-09-16 15:22:00 +02008593 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008594 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008595 free_type(isn->isn_arg.type.ct_type);
8596 break;
8597
Bram Moolenaar02194d22020-10-24 23:08:38 +02008598 case ISN_CMDMOD:
8599 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8600 ->cmod_filter_regmatch.regprog);
8601 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8602 break;
8603
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008604 case ISN_LOADSCRIPT:
8605 case ISN_STORESCRIPT:
8606 vim_free(isn->isn_arg.script.scriptref);
8607 break;
8608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008609 case ISN_2BOOL:
8610 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008611 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008612 case ISN_ADDBLOB:
8613 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008614 case ISN_ANYINDEX:
8615 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008616 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008617 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008618 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008619 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008620 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008621 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008622 case ISN_COMPAREANY:
8623 case ISN_COMPAREBLOB:
8624 case ISN_COMPAREBOOL:
8625 case ISN_COMPAREDICT:
8626 case ISN_COMPAREFLOAT:
8627 case ISN_COMPAREFUNC:
8628 case ISN_COMPARELIST:
8629 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008630 case ISN_COMPARESPECIAL:
8631 case ISN_COMPARESTRING:
8632 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008633 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008634 case ISN_DROP:
8635 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008636 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008637 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008638 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008639 case ISN_EXECCONCAT:
8640 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008642 case ISN_GETITEM:
8643 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008644 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008645 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008646 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008647 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008648 case ISN_LOADBDICT:
8649 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008650 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008652 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008653 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008654 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008655 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008656 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008657 case ISN_NEGATENR:
8658 case ISN_NEWDICT:
8659 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008660 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008661 case ISN_OPFLOAT:
8662 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008663 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008664 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008665 case ISN_PROF_END:
8666 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008667 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 case ISN_PUSHF:
8669 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008670 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008671 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008672 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008673 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008674 case ISN_SHUFFLE:
8675 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008676 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008677 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008678 case ISN_STORENR:
8679 case ISN_STOREOUTER:
8680 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008681 case ISN_STOREV:
8682 case ISN_STRINDEX:
8683 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008684 case ISN_THROW:
8685 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008686 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008687 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008688 // nothing allocated
8689 break;
8690 }
8691}
8692
8693/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008694 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008695 */
8696 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008697delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008698{
8699 int idx;
8700
8701 ga_clear(&dfunc->df_def_args_isn);
8702
8703 if (dfunc->df_instr != NULL)
8704 {
8705 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8706 delete_instr(dfunc->df_instr + idx);
8707 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008708 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008709 }
8710
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008711 if (mark_deleted)
8712 dfunc->df_deleted = TRUE;
8713 if (dfunc->df_ufunc != NULL)
8714 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008715}
8716
8717/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008718 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008719 * function, unless another user function still uses it.
8720 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008721 */
8722 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008723unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008724{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008725 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008726 {
8727 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8728 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008729
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008730 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008731 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008732 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008733 ufunc->uf_dfunc_idx = 0;
8734 if (dfunc->df_ufunc == ufunc)
8735 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008736 }
8737}
8738
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008739/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008740 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008741 */
8742 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008743link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008744{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008745 if (ufunc->uf_dfunc_idx > 0)
8746 {
8747 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8748 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008749
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008750 ++dfunc->df_refcount;
8751 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008752}
8753
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008754#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008755/*
8756 * Free all functions defined with ":def".
8757 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008758 void
8759free_def_functions(void)
8760{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008761 int idx;
8762
8763 for (idx = 0; idx < def_functions.ga_len; ++idx)
8764 {
8765 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8766
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008767 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008768 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008769 }
8770
8771 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008772}
8773#endif
8774
8775
8776#endif // FEAT_EVAL