blob: 32d9dea1eb1ae5f4451de557bb7873dc431d5aeb [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
Bram Moolenaarf002a412021-01-24 13:34:18 +01001702func_needs_compiling(ufunc_T *ufunc, int profile UNUSED)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001703{
1704 switch (ufunc->uf_def_status)
1705 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001706 case UF_NOT_COMPILED: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001707 case UF_TO_BE_COMPILED: return TRUE;
1708 case UF_COMPILED:
1709 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01001710#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001711 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1712 + ufunc->uf_dfunc_idx;
1713
1714 return profile ? dfunc->df_instr_prof == NULL
1715 : dfunc->df_instr == NULL;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001716#else
1717 break;
1718#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01001719 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001720 case UF_COMPILING: break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001721 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01001722 return FALSE;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001723}
1724
1725/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 * Generate an ISN_DCALL or ISN_UCALL instruction.
1727 * Return FAIL if the number of arguments is wrong.
1728 */
1729 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001730generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731{
1732 isn_T *isn;
1733 garray_T *stack = &cctx->ctx_type_stack;
1734 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001735 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736
Bram Moolenaar080457c2020-03-03 21:53:32 +01001737 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 if (argcount > regular_args && !has_varargs(ufunc))
1739 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001740 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 return FAIL;
1742 }
1743 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1744 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001745 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 return FAIL;
1747 }
1748
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001749 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001750 {
1751 int i;
1752
1753 for (i = 0; i < argcount; ++i)
1754 {
1755 type_T *expected;
1756 type_T *actual;
1757
1758 if (i < regular_args)
1759 {
1760 if (ufunc->uf_arg_types == NULL)
1761 continue;
1762 expected = ufunc->uf_arg_types[i];
1763 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001764 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1765 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001766 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001767 else
1768 expected = ufunc->uf_va_type->tt_member;
1769 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001770 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001771 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001772 {
1773 arg_type_mismatch(expected, actual, i + 1);
1774 return FAIL;
1775 }
1776 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01001777 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
1778 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1779 cctx->ctx_profiling, NULL) == FAIL)
1780 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001781 }
1782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001784 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001785 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001787 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 {
1789 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1790 isn->isn_arg.dfunc.cdf_argcount = argcount;
1791 }
1792 else
1793 {
1794 // A user function may be deleted and redefined later, can't use the
1795 // ufunc pointer, need to look it up again at runtime.
1796 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1797 isn->isn_arg.ufunc.cuf_argcount = argcount;
1798 }
1799
1800 stack->ga_len -= argcount; // drop the arguments
1801 if (ga_grow(stack, 1) == FAIL)
1802 return FAIL;
1803 // add return value
1804 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1805 ++stack->ga_len;
1806
1807 return OK;
1808}
1809
1810/*
1811 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1812 */
1813 static int
1814generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1815{
1816 isn_T *isn;
1817 garray_T *stack = &cctx->ctx_type_stack;
1818
Bram Moolenaar080457c2020-03-03 21:53:32 +01001819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1821 return FAIL;
1822 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1823 isn->isn_arg.ufunc.cuf_argcount = argcount;
1824
1825 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001826 if (ga_grow(stack, 1) == FAIL)
1827 return FAIL;
1828 // add return value
1829 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1830 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
1832 return OK;
1833}
1834
1835/*
1836 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001837 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 */
1839 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001840generate_PCALL(
1841 cctx_T *cctx,
1842 int argcount,
1843 char_u *name,
1844 type_T *type,
1845 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846{
1847 isn_T *isn;
1848 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001849 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850
Bram Moolenaar080457c2020-03-03 21:53:32 +01001851 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001852
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001853 if (type->tt_type == VAR_ANY)
1854 ret_type = &t_any;
1855 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001856 {
1857 if (type->tt_argcount != -1)
1858 {
1859 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1860
1861 if (argcount < type->tt_min_argcount - varargs)
1862 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001863 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001864 return FAIL;
1865 }
1866 if (!varargs && argcount > type->tt_argcount)
1867 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001868 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001869 return FAIL;
1870 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001871 if (type->tt_args != NULL)
1872 {
1873 int i;
1874
1875 for (i = 0; i < argcount; ++i)
1876 {
1877 int offset = -argcount + i - 1;
1878 type_T *actual = ((type_T **)stack->ga_data)[
1879 stack->ga_len + offset];
1880 type_T *expected;
1881
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001882 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001883 expected = type->tt_args[
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001884 type->tt_argcount - 1]->tt_member;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001885 else
1886 expected = type->tt_args[i];
Bram Moolenaare32e5162021-01-21 20:21:29 +01001887 if (need_type(actual, expected, offset, i + 1,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001888 cctx, TRUE, FALSE) == FAIL)
1889 {
1890 arg_type_mismatch(expected, actual, i + 1);
1891 return FAIL;
1892 }
1893 }
1894 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001895 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001896 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001897 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001898 else
1899 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001900 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001901 return FAIL;
1902 }
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1905 return FAIL;
1906 isn->isn_arg.pfunc.cpf_top = at_top;
1907 isn->isn_arg.pfunc.cpf_argcount = argcount;
1908
1909 stack->ga_len -= argcount; // drop the arguments
1910
1911 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001912 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001914 // If partial is above the arguments it must be cleared and replaced with
1915 // the return value.
1916 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1917 return FAIL;
1918
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 return OK;
1920}
1921
1922/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001923 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 */
1925 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001926generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927{
1928 isn_T *isn;
1929 garray_T *stack = &cctx->ctx_type_stack;
1930 type_T *type;
1931
Bram Moolenaar080457c2020-03-03 21:53:32 +01001932 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001933 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001935 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001937 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001939 if (type->tt_type != VAR_DICT && type != &t_any)
1940 {
1941 emsg(_(e_dictreq));
1942 return FAIL;
1943 }
1944 // change dict type to dict member type
1945 if (type->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01001946 {
1947 ((type_T **)stack->ga_data)[stack->ga_len - 1] =
1948 type->tt_member == &t_unknown ? &t_any : type->tt_member;
1949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950
1951 return OK;
1952}
1953
1954/*
1955 * Generate an ISN_ECHO instruction.
1956 */
1957 static int
1958generate_ECHO(cctx_T *cctx, int with_white, int count)
1959{
1960 isn_T *isn;
1961
Bram Moolenaar080457c2020-03-03 21:53:32 +01001962 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1964 return FAIL;
1965 isn->isn_arg.echo.echo_with_white = with_white;
1966 isn->isn_arg.echo.echo_count = count;
1967
1968 return OK;
1969}
1970
Bram Moolenaarad39c092020-02-26 18:23:43 +01001971/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001972 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001973 */
1974 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001975generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001976{
1977 isn_T *isn;
1978
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001979 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001980 return FAIL;
1981 isn->isn_arg.number = count;
1982
1983 return OK;
1984}
1985
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001986/*
1987 * Generate an ISN_PUT instruction.
1988 */
1989 static int
1990generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1991{
1992 isn_T *isn;
1993
1994 RETURN_OK_IF_SKIP(cctx);
1995 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1996 return FAIL;
1997 isn->isn_arg.put.put_regname = regname;
1998 isn->isn_arg.put.put_lnum = lnum;
1999 return OK;
2000}
2001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 static int
2003generate_EXEC(cctx_T *cctx, char_u *line)
2004{
2005 isn_T *isn;
2006
Bram Moolenaar080457c2020-03-03 21:53:32 +01002007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
2009 return FAIL;
2010 isn->isn_arg.string = vim_strsave(line);
2011 return OK;
2012}
2013
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002014 static int
2015generate_EXECCONCAT(cctx_T *cctx, int count)
2016{
2017 isn_T *isn;
2018
2019 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2020 return FAIL;
2021 isn->isn_arg.number = count;
2022 return OK;
2023}
2024
Bram Moolenaar08597872020-12-10 19:43:40 +01002025/*
2026 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2027 */
2028 static int
2029generate_RANGE(cctx_T *cctx, char_u *range)
2030{
2031 isn_T *isn;
2032 garray_T *stack = &cctx->ctx_type_stack;
2033
2034 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2035 return FAIL;
2036 isn->isn_arg.string = range;
2037
2038 if (ga_grow(stack, 1) == FAIL)
2039 return FAIL;
2040 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
2041 ++stack->ga_len;
2042 return OK;
2043}
2044
Bram Moolenaar792f7862020-11-23 08:31:18 +01002045 static int
2046generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2047{
2048 isn_T *isn;
2049
2050 RETURN_OK_IF_SKIP(cctx);
2051 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2052 return FAIL;
2053 isn->isn_arg.unpack.unp_count = var_count;
2054 isn->isn_arg.unpack.unp_semicolon = semicolon;
2055 return OK;
2056}
2057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02002059 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002060 */
2061 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02002062generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002063{
2064 isn_T *isn;
2065
Bram Moolenaar02194d22020-10-24 23:08:38 +02002066 if (cmod->cmod_flags != 0
2067 || cmod->cmod_split != 0
2068 || cmod->cmod_verbose != 0
2069 || cmod->cmod_tab != 0
2070 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002071 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02002072 cctx->ctx_has_cmdmod = TRUE;
2073
2074 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002075 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02002076 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2077 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2078 return FAIL;
2079 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002080 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02002081 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002082 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02002083
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002084 return OK;
2085}
2086
2087 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02002088generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002089{
Bram Moolenaarf665e972020-12-05 19:17:16 +01002090 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2091 return FAIL;
Bram Moolenaar7cd24222021-01-12 18:58:39 +01002092 cctx->ctx_has_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002093 return OK;
2094}
2095
Bram Moolenaarf002a412021-01-24 13:34:18 +01002096#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002097 static void
2098may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2099{
2100 if (cctx->ctx_profiling && prof_lnum >= 0)
2101 {
2102 int save_lnum = cctx->ctx_lnum;
2103
2104 cctx->ctx_lnum = prof_lnum;
2105 generate_instr(cctx, ISN_PROF_END);
2106 cctx->ctx_lnum = save_lnum;
2107 }
2108}
Bram Moolenaarf002a412021-01-24 13:34:18 +01002109#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01002110
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002111/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002113 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002115 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02002116reserve_local(
2117 cctx_T *cctx,
2118 char_u *name,
2119 size_t len,
2120 int isConst,
2121 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 lvar_T *lvar;
2124
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002125 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002127 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002128 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 }
2130
2131 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002132 return NULL;
2133 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002134 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002136 // Every local variable uses the next entry on the stack. We could re-use
2137 // the last ones when leaving a scope, but then variables used in a closure
2138 // might get overwritten. To keep things simple do not re-use stack
2139 // entries. This is less efficient, but memory is cheap these days.
2140 lvar->lv_idx = cctx->ctx_locals_count++;
2141
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002142 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 lvar->lv_const = isConst;
2144 lvar->lv_type = type;
2145
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002146 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002147}
2148
2149/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002150 * Remove local variables above "new_top".
2151 */
2152 static void
2153unwind_locals(cctx_T *cctx, int new_top)
2154{
2155 if (cctx->ctx_locals.ga_len > new_top)
2156 {
2157 int idx;
2158 lvar_T *lvar;
2159
2160 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2161 {
2162 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2163 vim_free(lvar->lv_name);
2164 }
2165 }
2166 cctx->ctx_locals.ga_len = new_top;
2167}
2168
2169/*
2170 * Free all local variables.
2171 */
2172 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002173free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002174{
2175 unwind_locals(cctx, 0);
2176 ga_clear(&cctx->ctx_locals);
2177}
2178
2179/*
Bram Moolenaar08251752021-01-11 21:20:18 +01002180 * If "check_writable" is ASSIGN_CONST give an error if the variable was
2181 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
2182 * error if the variable was defined with :const.
2183 */
2184 static int
2185check_item_writable(svar_T *sv, int check_writable, char_u *name)
2186{
2187 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
2188 || (check_writable == ASSIGN_FINAL
2189 && sv->sv_const == ASSIGN_CONST))
2190 {
2191 semsg(_(e_readonlyvar), name);
2192 return FAIL;
2193 }
2194 return OK;
2195}
2196
2197/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002198 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +01002199 * Pass "check_writable" to check_item_writable().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 * Returns the index in "sn_var_vals" if found.
2201 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +01002202 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 */
2204 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002205get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206{
2207 hashtab_T *ht;
2208 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002209 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002210 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 int idx;
2212
Bram Moolenaare3d46852020-08-29 13:39:17 +02002213 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002215 if (sid == current_sctx.sc_sid)
2216 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002217 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002218
2219 if (sav == NULL)
2220 return -2;
2221 idx = sav->sav_var_vals_idx;
2222 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +01002223 if (check_item_writable(sv, check_writable, name) == FAIL)
2224 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002225 return idx;
2226 }
2227
2228 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 ht = &SCRIPT_VARS(sid);
2230 di = find_var_in_ht(ht, 0, name, TRUE);
2231 if (di == NULL)
2232 return -2;
2233
2234 // Now find the svar_T index in sn_var_vals.
2235 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2236 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002237 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 if (sv->sv_tv == &di->di_tv)
2239 {
Bram Moolenaar08251752021-01-11 21:20:18 +01002240 if (check_item_writable(sv, check_writable, name) == FAIL)
2241 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 return idx;
2243 }
2244 }
2245 return -1;
2246}
2247
2248/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002249 * Find "name" in imported items of the current script or in "cctx" if not
2250 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 */
2252 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002253find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 int idx;
2256
Bram Moolenaare3d46852020-08-29 13:39:17 +02002257 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002258 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 if (cctx != NULL)
2260 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2261 {
2262 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2263 + idx;
2264
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002265 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2266 : STRLEN(import->imp_name) == len
2267 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 return import;
2269 }
2270
Bram Moolenaarefa94442020-08-08 22:16:00 +02002271 return find_imported_in_script(name, len, current_sctx.sc_sid);
2272}
2273
2274 imported_T *
2275find_imported_in_script(char_u *name, size_t len, int sid)
2276{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002277 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002278 int idx;
2279
Bram Moolenaare3d46852020-08-29 13:39:17 +02002280 if (!SCRIPT_ID_VALID(sid))
2281 return NULL;
2282 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2284 {
2285 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2286
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002287 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2288 : STRLEN(import->imp_name) == len
2289 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 return import;
2291 }
2292 return NULL;
2293}
2294
2295/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002296 * Free all imported variables.
2297 */
2298 static void
2299free_imported(cctx_T *cctx)
2300{
2301 int idx;
2302
2303 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2304 {
2305 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2306
2307 vim_free(import->imp_name);
2308 }
2309 ga_clear(&cctx->ctx_imports);
2310}
2311
2312/*
Bram Moolenaar23c55272020-06-21 16:58:13 +02002313 * Return a pointer to the next line that isn't empty or only contains a
2314 * comment. Skips over white space.
2315 * Returns NULL if there is none.
2316 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002317 char_u *
2318peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002319{
2320 int lnum = cctx->ctx_lnum;
2321
2322 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2323 {
2324 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002325 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002326
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002327 // ignore NULLs inserted for continuation lines
2328 if (line != NULL)
2329 {
2330 p = skipwhite(line);
2331 if (*p != NUL && !vim9_comment_start(p))
2332 return p;
2333 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002334 }
2335 return NULL;
2336}
2337
2338/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002339 * Called when checking for a following operator at "arg". When the rest of
2340 * the line is empty or only a comment, peek the next line. If there is a next
2341 * line return a pointer to it and set "nextp".
2342 * Otherwise skip over white space.
2343 */
2344 static char_u *
2345may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2346{
2347 char_u *p = skipwhite(arg);
2348
2349 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002350 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002351 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002352 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002353 if (*nextp != NULL)
2354 return *nextp;
2355 }
2356 return p;
2357}
2358
2359/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002360 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002361 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002362 * Returns NULL when at the end.
2363 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002364 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002365next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002366{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002367 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002368
2369 do
2370 {
2371 ++cctx->ctx_lnum;
2372 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002373 {
2374 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002375 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002376 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002377 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002378 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002379 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002380 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002381 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002382 return line;
2383}
2384
2385/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002386 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002387 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002388 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002389 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2390 */
2391 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002392may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002393{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01002394 *arg = skipwhite(whitep);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002395 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002396 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002397 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002398
2399 if (next == NULL)
2400 return FAIL;
2401 *arg = skipwhite(next);
2402 }
2403 return OK;
2404}
2405
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002406/*
2407 * Idem, and give an error when failed.
2408 */
2409 static int
2410may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2411{
2412 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2413 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002414 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002415 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002416 return FAIL;
2417 }
2418 return OK;
2419}
2420
2421
Bram Moolenaara5565e42020-05-09 15:44:01 +02002422// Structure passed between the compile_expr* functions to keep track of
2423// constants that have been parsed but for which no code was produced yet. If
2424// possible expressions on these constants are applied at compile time. If
2425// that is not possible, the code to push the constants needs to be generated
2426// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002427// Using 50 should be more than enough of 5 levels of ().
2428#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002429typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002430 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002431 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002432 int pp_is_const; // all generated code was constants, used for a
2433 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002434} ppconst_T;
2435
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002436static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002437static int compile_expr0(char_u **arg, cctx_T *cctx);
2438static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2439
Bram Moolenaara5565e42020-05-09 15:44:01 +02002440/*
2441 * Generate a PUSH instruction for "tv".
2442 * "tv" will be consumed or cleared.
2443 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2444 */
2445 static int
2446generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2447{
2448 if (tv != NULL)
2449 {
2450 switch (tv->v_type)
2451 {
2452 case VAR_UNKNOWN:
2453 break;
2454 case VAR_BOOL:
2455 generate_PUSHBOOL(cctx, tv->vval.v_number);
2456 break;
2457 case VAR_SPECIAL:
2458 generate_PUSHSPEC(cctx, tv->vval.v_number);
2459 break;
2460 case VAR_NUMBER:
2461 generate_PUSHNR(cctx, tv->vval.v_number);
2462 break;
2463#ifdef FEAT_FLOAT
2464 case VAR_FLOAT:
2465 generate_PUSHF(cctx, tv->vval.v_float);
2466 break;
2467#endif
2468 case VAR_BLOB:
2469 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2470 tv->vval.v_blob = NULL;
2471 break;
2472 case VAR_STRING:
2473 generate_PUSHS(cctx, tv->vval.v_string);
2474 tv->vval.v_string = NULL;
2475 break;
2476 default:
2477 iemsg("constant type not supported");
2478 clear_tv(tv);
2479 return FAIL;
2480 }
2481 tv->v_type = VAR_UNKNOWN;
2482 }
2483 return OK;
2484}
2485
2486/*
2487 * Generate code for any ppconst entries.
2488 */
2489 static int
2490generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2491{
2492 int i;
2493 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002494 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002495
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002496 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002497 for (i = 0; i < ppconst->pp_used; ++i)
2498 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2499 ret = FAIL;
2500 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002501 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002502 return ret;
2503}
2504
2505/*
2506 * Clear ppconst constants. Used when failing.
2507 */
2508 static void
2509clear_ppconst(ppconst_T *ppconst)
2510{
2511 int i;
2512
2513 for (i = 0; i < ppconst->pp_used; ++i)
2514 clear_tv(&ppconst->pp_tv[i]);
2515 ppconst->pp_used = 0;
2516}
2517
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002518/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002519 * Generate an instruction to load script-local variable "name", without the
2520 * leading "s:".
2521 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 */
2523 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002524compile_load_scriptvar(
2525 cctx_T *cctx,
2526 char_u *name, // variable NUL terminated
2527 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002528 char_u **end, // end of variable
2529 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002531 scriptitem_T *si;
2532 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 imported_T *import;
2534
Bram Moolenaare3d46852020-08-29 13:39:17 +02002535 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2536 return FAIL;
2537 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar08251752021-01-11 21:20:18 +01002538 idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002539 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002541 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002542 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2543 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
2545 if (idx >= 0)
2546 {
2547 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2548
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002549 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 current_sctx.sc_sid, idx, sv->sv_type);
2551 return OK;
2552 }
2553
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002554 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 if (import != NULL)
2556 {
Bram Moolenaara6294952020-12-27 13:39:50 +01002557 if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002558 {
2559 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002560 char_u *exp_name;
2561 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002562 ufunc_T *ufunc;
2563 type_T *type;
2564
2565 // Used "import * as Name", need to lookup the member.
2566 if (*p != '.')
2567 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002568 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002569 return FAIL;
2570 }
2571 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002572 if (VIM_ISWHITE(*p))
2573 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002574 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002575 return FAIL;
2576 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002577
Bram Moolenaar1c991142020-07-04 13:15:31 +02002578 // isolate one name
2579 exp_name = p;
2580 while (eval_isnamec(*p))
2581 ++p;
2582 cc = *p;
2583 *p = NUL;
2584
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002585 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002586 *p = cc;
2587 p = skipwhite(p);
2588
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002589 // TODO: what if it is a function?
2590 if (idx < 0)
2591 return FAIL;
2592 *end = p;
2593
2594 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2595 import->imp_sid,
2596 idx,
2597 type);
2598 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002599 else if (import->imp_funcname != NULL)
2600 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002601 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002602 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2603 import->imp_sid,
2604 import->imp_var_vals_idx,
2605 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 return OK;
2607 }
2608
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002609 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002610 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 return FAIL;
2612}
2613
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002614 static int
2615generate_funcref(cctx_T *cctx, char_u *name)
2616{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002617 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002618
2619 if (ufunc == NULL)
2620 return FAIL;
2621
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002622 // Need to compile any default values to get the argument types.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002623 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
2624 && compile_def_function(ufunc, TRUE, cctx->ctx_profiling, NULL)
2625 == FAIL)
2626 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002627 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002628}
2629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630/*
2631 * Compile a variable name into a load instruction.
2632 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002633 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 * When "error" is FALSE do not give an error when not found.
2635 */
2636 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002637compile_load(
2638 char_u **arg,
2639 char_u *end_arg,
2640 cctx_T *cctx,
2641 int is_expr,
2642 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643{
2644 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002645 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002646 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002648 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 if (*(*arg + 1) == ':')
2651 {
2652 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002653 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002654 {
2655 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002657 switch (**arg)
2658 {
2659 case 'g': isn_type = ISN_LOADGDICT; break;
2660 case 'w': isn_type = ISN_LOADWDICT; break;
2661 case 't': isn_type = ISN_LOADTDICT; break;
2662 case 'b': isn_type = ISN_LOADBDICT; break;
2663 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002664 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002665 goto theend;
2666 }
2667 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2668 goto theend;
2669 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 else
2672 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002673 isntype_T isn_type = ISN_DROP;
2674
2675 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2676 if (name == NULL)
2677 return FAIL;
2678
2679 switch (**arg)
2680 {
2681 case 'v': res = generate_LOADV(cctx, name, error);
2682 break;
2683 case 's': res = compile_load_scriptvar(cctx, name,
2684 NULL, NULL, error);
2685 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002686 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2687 isn_type = ISN_LOADG;
2688 else
2689 {
2690 isn_type = ISN_LOADAUTO;
2691 vim_free(name);
2692 name = vim_strnsave(*arg, end - *arg);
2693 if (name == NULL)
2694 return FAIL;
2695 }
2696 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002697 case 'w': isn_type = ISN_LOADW; break;
2698 case 't': isn_type = ISN_LOADT; break;
2699 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002700 default: // cannot happen, just in case
2701 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002702 goto theend;
2703 }
2704 if (isn_type != ISN_DROP)
2705 {
2706 // Global, Buffer-local, Window-local and Tabpage-local
2707 // variables can be defined later, thus we don't check if it
2708 // exists, give error at runtime.
2709 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 }
2712 }
2713 else
2714 {
2715 size_t len = end - *arg;
2716 int idx;
2717 int gen_load = FALSE;
Bram Moolenaarab360522021-01-10 14:02:28 +01002718 int gen_load_outer = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719
2720 name = vim_strnsave(*arg, end - *arg);
2721 if (name == NULL)
2722 return FAIL;
2723
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002724 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002726 if (gen_load_outer == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002727 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 }
2729 else
2730 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002731 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002732
Bram Moolenaar709664c2020-12-12 14:33:41 +01002733 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002735 type = lvar.lv_type;
2736 idx = lvar.lv_idx;
Bram Moolenaarab360522021-01-10 14:02:28 +01002737 if (lvar.lv_from_outer != 0)
2738 gen_load_outer = lvar.lv_from_outer;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002739 else
2740 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 }
2742 else
2743 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002744 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002745 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002746 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002747 || find_imported(name, 0, cctx) != NULL)
2748 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002749
Bram Moolenaar0f769812020-09-12 18:32:34 +02002750 // When evaluating an expression and the name starts with an
2751 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002752 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002753 if (res == FAIL && is_expr
2754 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002755 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 }
2757 }
2758 if (gen_load)
2759 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarab360522021-01-10 14:02:28 +01002760 if (gen_load_outer > 0)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002761 {
Bram Moolenaarab360522021-01-10 14:02:28 +01002762 res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002763 cctx->ctx_outer_used = TRUE;
2764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 }
2766
2767 *arg = end;
2768
2769theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002770 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002771 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 vim_free(name);
2773 return res;
2774}
2775
2776/*
2777 * Compile the argument expressions.
2778 * "arg" points to just after the "(" and is advanced to after the ")"
2779 */
2780 static int
2781compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2782{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002783 char_u *p = *arg;
2784 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002785 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
Bram Moolenaare6085c52020-04-12 20:19:16 +02002787 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002789 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2790 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002791 if (*p == ')')
2792 {
2793 *arg = p + 1;
2794 return OK;
2795 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002796 if (must_end)
2797 {
2798 semsg(_(e_missing_comma_before_argument_str), p);
2799 return FAIL;
2800 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002801
Bram Moolenaara5565e42020-05-09 15:44:01 +02002802 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 return FAIL;
2804 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002805
2806 if (*p != ',' && *skipwhite(p) == ',')
2807 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002808 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002809 p = skipwhite(p);
2810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002812 {
2813 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002814 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002815 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002816 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002817 else
2818 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002819 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002820 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002822failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002823 emsg(_(e_missing_close));
2824 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825}
2826
2827/*
2828 * Compile a function call: name(arg1, arg2)
2829 * "arg" points to "name", "arg + varlen" to the "(".
2830 * "argcount_init" is 1 for "value->method()"
2831 * Instructions:
2832 * EVAL arg1
2833 * EVAL arg2
2834 * BCALL / DCALL / UCALL
2835 */
2836 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002837compile_call(
2838 char_u **arg,
2839 size_t varlen,
2840 cctx_T *cctx,
2841 ppconst_T *ppconst,
2842 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843{
2844 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002845 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 int argcount = argcount_init;
2847 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002848 char_u fname_buf[FLEN_FIXED + 1];
2849 char_u *tofree = NULL;
2850 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002851 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002852 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002853 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854
Bram Moolenaara5565e42020-05-09 15:44:01 +02002855 // we can evaluate "has('name')" at compile time
2856 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2857 {
2858 char_u *s = skipwhite(*arg + varlen + 1);
2859 typval_T argvars[2];
2860
2861 argvars[0].v_type = VAR_UNKNOWN;
2862 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002863 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002864 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002865 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002866 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002867 if (*s == ')' && argvars[0].v_type == VAR_STRING
2868 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002869 {
2870 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2871
2872 *arg = s + 1;
2873 argvars[1].v_type = VAR_UNKNOWN;
2874 tv->v_type = VAR_NUMBER;
2875 tv->vval.v_number = 0;
2876 f_has(argvars, tv);
2877 clear_tv(&argvars[0]);
2878 ++ppconst->pp_used;
2879 return OK;
2880 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002881 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002882 }
2883
2884 if (generate_ppconst(cctx, ppconst) == FAIL)
2885 return FAIL;
2886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 if (varlen >= sizeof(namebuf))
2888 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002889 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 return FAIL;
2891 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002892 vim_strncpy(namebuf, *arg, varlen);
2893 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894
2895 *arg = skipwhite(*arg + varlen + 1);
2896 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002897 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
Bram Moolenaar03290b82020-12-19 16:30:44 +01002899 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002900 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 {
2902 int idx;
2903
2904 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002905 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002907 {
2908 if (STRCMP(name, "add") == 0 && argcount == 2)
2909 {
2910 garray_T *stack = &cctx->ctx_type_stack;
2911 type_T *type = ((type_T **)stack->ga_data)[
2912 stack->ga_len - 2];
2913
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002914 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002915 if (type->tt_type == VAR_LIST)
2916 {
2917 // inline "add(list, item)" so that the type can be checked
2918 res = generate_LISTAPPEND(cctx);
2919 idx = -1;
2920 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002921 else if (type->tt_type == VAR_BLOB)
2922 {
2923 // inline "add(blob, nr)" so that the type can be checked
2924 res = generate_BLOBAPPEND(cctx);
2925 idx = -1;
2926 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002927 }
2928
2929 if (idx >= 0)
2930 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2931 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002932 else
2933 semsg(_(e_unknownfunc), namebuf);
2934 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 }
2936
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002937 // An argument or local variable can be a function reference, this
2938 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002939 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002940 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002941 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002942 // If we can find the function by name generate the right call.
2943 // Skip global functions here, a local funcref takes precedence.
2944 ufunc = find_func(name, FALSE, cctx);
2945 if (ufunc != NULL && !func_is_global(ufunc))
2946 {
2947 res = generate_CALL(cctx, ufunc, argcount);
2948 goto theend;
2949 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002950 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951
2952 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002953 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002954 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002956 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002957 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002958 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002959 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar7e368202020-12-25 21:56:57 +01002960 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002961
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002962 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002963 goto theend;
2964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965
Bram Moolenaar0f769812020-09-12 18:32:34 +02002966 // If we can find a global function by name generate the right call.
2967 if (ufunc != NULL)
2968 {
2969 res = generate_CALL(cctx, ufunc, argcount);
2970 goto theend;
2971 }
2972
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002973 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002974 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002975 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002976 res = generate_UCALL(cctx, name, argcount);
2977 else
2978 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002979
2980theend:
2981 vim_free(tofree);
2982 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983}
2984
2985// like NAMESPACE_CHAR but with 'a' and 'l'.
2986#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2987
2988/*
2989 * Find the end of a variable or function name. Unlike find_name_end() this
2990 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002991 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 * Return a pointer to just after the name. Equal to "arg" if there is no
2993 * valid name.
2994 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002995 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002996to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997{
2998 char_u *p;
2999
3000 // Quick check for valid starting character.
3001 if (!eval_isnamec1(*arg))
3002 return arg;
3003
3004 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
3005 // Include a namespace such as "s:var" and "v:var". But "n:" is not
3006 // and can be used in slice "[n:]".
3007 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01003008 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
3010 break;
3011 return p;
3012}
3013
3014/*
3015 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02003016 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 */
3018 char_u *
3019to_name_const_end(char_u *arg)
3020{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01003021 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 typval_T rettv;
3023
3024 if (p == arg && *arg == '[')
3025 {
3026
3027 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003028 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 p = arg;
3030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 return p;
3032}
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034/*
3035 * parse a list: [expr, expr]
3036 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003037 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 */
3039 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003040compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041{
3042 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02003043 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003045 int is_const;
3046 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003048 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003050 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01003051 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003052 semsg(_(e_list_end), *arg);
3053 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003054 }
Bram Moolenaardb199212020-08-12 18:01:53 +02003055 if (*p == ',')
3056 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003057 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02003058 return FAIL;
3059 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 if (*p == ']')
3061 {
3062 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01003064 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003065 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003066 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003067 if (!is_const)
3068 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 ++count;
3070 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003071 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003073 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
3074 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003075 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02003076 return FAIL;
3077 }
3078 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003079 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 p = skipwhite(p);
3081 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003082 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003084 ppconst->pp_is_const = is_all_const;
3085 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086}
3087
3088/*
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003089 * Parse a lambda: "(arg, arg) => expr"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 * "*arg" points to the '{'.
Bram Moolenaare462f522020-12-27 14:43:30 +01003091 * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 */
3093 static int
3094compile_lambda(char_u **arg, cctx_T *cctx)
3095{
Bram Moolenaare462f522020-12-27 14:43:30 +01003096 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 typval_T rettv;
3098 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003099 evalarg_T evalarg;
3100
3101 CLEAR_FIELD(evalarg);
3102 evalarg.eval_flags = EVAL_EVALUATE;
3103 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104
3105 // Get the funcref in "rettv".
Bram Moolenaare462f522020-12-27 14:43:30 +01003106 r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
3107 if (r != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003108 {
3109 clear_evalarg(&evalarg, NULL);
Bram Moolenaare462f522020-12-27 14:43:30 +01003110 return r;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02003111 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01003112
Bram Moolenaar65c44152020-12-24 15:14:01 +01003113 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003115 ++ufunc->uf_refcount;
3116 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117
Bram Moolenaar65c44152020-12-24 15:14:01 +01003118 // Compile the function into instructions.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003119 compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02003121 clear_evalarg(&evalarg, NULL);
3122
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003123 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003124 {
3125 // The return type will now be known.
3126 set_function_type(ufunc);
3127
Bram Moolenaarfdeab652020-09-19 15:16:50 +02003128 // The function reference count will be 1. When the ISN_FUNCREF
3129 // instruction is deleted the reference count is decremented and the
3130 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02003131 return generate_FUNCREF(cctx, ufunc);
3132 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003133
3134 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 return FAIL;
3136}
3137
3138/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003139 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003141 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 */
3143 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003144compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145{
3146 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003147 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 int count = 0;
3149 dict_T *d = dict_alloc();
3150 dictitem_T *item;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003151 char_u *whitep = *arg + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003152 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003153 int is_const;
3154 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 if (d == NULL)
3157 return FAIL;
Bram Moolenaard62d87d2021-01-04 17:40:12 +01003158 if (generate_ppconst(cctx, ppconst) == FAIL)
3159 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003162 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163
Bram Moolenaar23c55272020-06-21 16:58:13 +02003164 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 *arg = NULL;
3167 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 }
3169
3170 if (**arg == '}')
3171 break;
3172
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003173 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003175 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003177 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003178 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003179 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3182 if (isn->isn_type == ISN_PUSHS)
3183 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003184 else
3185 {
3186 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003187 [stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003188 if (need_type(keytype, &t_string, -1, 0, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003189 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003190 return FAIL;
3191 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003192 *arg = skipwhite(*arg);
3193 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003194 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003195 emsg(_(e_missing_matching_bracket_after_dict_key));
3196 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003197 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003198 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003200 else
3201 {
3202 // {"name": value},
3203 // {'name': value},
3204 // {name: value} use "name" as a literal key
3205 key = get_literal_key(arg);
3206 if (key == NULL)
3207 return FAIL;
3208 if (generate_PUSHS(cctx, key) == FAIL)
3209 return FAIL;
3210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
3212 // Check for duplicate keys, if using string keys.
3213 if (key != NULL)
3214 {
3215 item = dict_find(d, key, -1);
3216 if (item != NULL)
3217 {
3218 semsg(_(e_duplicate_key), key);
3219 goto failret;
3220 }
3221 item = dictitem_alloc(key);
3222 if (item != NULL)
3223 {
3224 item->di_tv.v_type = VAR_UNKNOWN;
3225 item->di_tv.v_lock = 0;
3226 if (dict_add(d, item) == FAIL)
3227 dictitem_free(item);
3228 }
3229 }
3230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 if (**arg != ':')
3232 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003233 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003234 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003235 else
3236 semsg(_(e_missing_dict_colon), *arg);
3237 return FAIL;
3238 }
3239 whitep = *arg + 1;
3240 if (!IS_WHITE_OR_NUL(*whitep))
3241 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003242 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 return FAIL;
3244 }
3245
Bram Moolenaar23c55272020-06-21 16:58:13 +02003246 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003247 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003248 *arg = NULL;
3249 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003250 }
3251
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003252 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003254 if (!is_const)
3255 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 ++count;
3257
Bram Moolenaar2c330432020-04-13 14:41:35 +02003258 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003259 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003260 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003261 *arg = NULL;
3262 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 if (**arg == '}')
3265 break;
3266 if (**arg != ',')
3267 {
3268 semsg(_(e_missing_dict_comma), *arg);
3269 goto failret;
3270 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003271 if (IS_WHITE_OR_NUL(*whitep))
3272 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003273 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003274 return FAIL;
3275 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003276 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003277 if (!IS_WHITE_OR_NUL(*whitep))
3278 {
3279 semsg(_(e_white_space_required_after_str), ",");
3280 return FAIL;
3281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 *arg = skipwhite(*arg + 1);
3283 }
3284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 *arg = *arg + 1;
3286
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003287 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003288 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003289 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003290 *arg += STRLEN(*arg);
3291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003293 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 return generate_NEWDICT(cctx, count);
3295
3296failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003297 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003298 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003299 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003300 *arg = (char_u *)"";
3301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 dict_unref(d);
3303 return FAIL;
3304}
3305
3306/*
3307 * Compile "&option".
3308 */
3309 static int
3310compile_get_option(char_u **arg, cctx_T *cctx)
3311{
3312 typval_T rettv;
3313 char_u *start = *arg;
3314 int ret;
3315
3316 // parse the option and get the current value to get the type.
3317 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003318 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (ret == OK)
3320 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003321 // include the '&' in the name, eval_option() expects it.
Bram Moolenaard5ea8f02021-01-01 14:49:15 +01003322 char_u *name = vim_strnsave(start, *arg - start);
3323 type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
3324 : rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325
3326 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3327 vim_free(name);
3328 }
3329 clear_tv(&rettv);
3330
3331 return ret;
3332}
3333
3334/*
3335 * Compile "$VAR".
3336 */
3337 static int
3338compile_get_env(char_u **arg, cctx_T *cctx)
3339{
3340 char_u *start = *arg;
3341 int len;
3342 int ret;
3343 char_u *name;
3344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 ++*arg;
3346 len = get_env_len(arg);
3347 if (len == 0)
3348 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003349 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 return FAIL;
3351 }
3352
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 name = vim_strnsave(start, len + 1);
3355 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3356 vim_free(name);
3357 return ret;
3358}
3359
3360/*
3361 * Compile "@r".
3362 */
3363 static int
3364compile_get_register(char_u **arg, cctx_T *cctx)
3365{
3366 int ret;
3367
3368 ++*arg;
3369 if (**arg == NUL)
3370 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003371 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 return FAIL;
3373 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003374 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 {
3376 emsg_invreg(**arg);
3377 return FAIL;
3378 }
3379 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3380 ++*arg;
3381 return ret;
3382}
3383
3384/*
3385 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003386 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 */
3388 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003389apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003391 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392
3393 // this works from end to start
3394 while (p > start)
3395 {
3396 --p;
3397 if (*p == '-' || *p == '+')
3398 {
3399 // only '-' has an effect, for '+' we only check the type
3400#ifdef FEAT_FLOAT
3401 if (rettv->v_type == VAR_FLOAT)
3402 {
3403 if (*p == '-')
3404 rettv->vval.v_float = -rettv->vval.v_float;
3405 }
3406 else
3407#endif
3408 {
3409 varnumber_T val;
3410 int error = FALSE;
3411
3412 // tv_get_number_chk() accepts a string, but we don't want that
3413 // here
3414 if (check_not_string(rettv) == FAIL)
3415 return FAIL;
3416 val = tv_get_number_chk(rettv, &error);
3417 clear_tv(rettv);
3418 if (error)
3419 return FAIL;
3420 if (*p == '-')
3421 val = -val;
3422 rettv->v_type = VAR_NUMBER;
3423 rettv->vval.v_number = val;
3424 }
3425 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003426 else if (numeric_only)
3427 {
3428 ++p;
3429 break;
3430 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003431 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 {
3433 int v = tv2bool(rettv);
3434
3435 // '!' is permissive in the type.
3436 clear_tv(rettv);
3437 rettv->v_type = VAR_BOOL;
3438 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3439 }
3440 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003441 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 return OK;
3443}
3444
3445/*
3446 * Recognize v: variables that are constants and set "rettv".
3447 */
3448 static void
3449get_vim_constant(char_u **arg, typval_T *rettv)
3450{
3451 if (STRNCMP(*arg, "v:true", 6) == 0)
3452 {
3453 rettv->v_type = VAR_BOOL;
3454 rettv->vval.v_number = VVAL_TRUE;
3455 *arg += 6;
3456 }
3457 else if (STRNCMP(*arg, "v:false", 7) == 0)
3458 {
3459 rettv->v_type = VAR_BOOL;
3460 rettv->vval.v_number = VVAL_FALSE;
3461 *arg += 7;
3462 }
3463 else if (STRNCMP(*arg, "v:null", 6) == 0)
3464 {
3465 rettv->v_type = VAR_SPECIAL;
3466 rettv->vval.v_number = VVAL_NULL;
3467 *arg += 6;
3468 }
3469 else if (STRNCMP(*arg, "v:none", 6) == 0)
3470 {
3471 rettv->v_type = VAR_SPECIAL;
3472 rettv->vval.v_number = VVAL_NONE;
3473 *arg += 6;
3474 }
3475}
3476
Bram Moolenaar657137c2021-01-09 15:45:23 +01003477 exprtype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003478get_compare_type(char_u *p, int *len, int *type_is)
3479{
Bram Moolenaar657137c2021-01-09 15:45:23 +01003480 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar61a89812020-05-07 16:58:17 +02003481 int i;
3482
3483 switch (p[0])
3484 {
3485 case '=': if (p[1] == '=')
3486 type = EXPR_EQUAL;
3487 else if (p[1] == '~')
3488 type = EXPR_MATCH;
3489 break;
3490 case '!': if (p[1] == '=')
3491 type = EXPR_NEQUAL;
3492 else if (p[1] == '~')
3493 type = EXPR_NOMATCH;
3494 break;
3495 case '>': if (p[1] != '=')
3496 {
3497 type = EXPR_GREATER;
3498 *len = 1;
3499 }
3500 else
3501 type = EXPR_GEQUAL;
3502 break;
3503 case '<': if (p[1] != '=')
3504 {
3505 type = EXPR_SMALLER;
3506 *len = 1;
3507 }
3508 else
3509 type = EXPR_SEQUAL;
3510 break;
3511 case 'i': if (p[1] == 's')
3512 {
3513 // "is" and "isnot"; but not a prefix of a name
3514 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3515 *len = 5;
3516 i = p[*len];
3517 if (!isalnum(i) && i != '_')
3518 {
3519 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3520 *type_is = TRUE;
3521 }
3522 }
3523 break;
3524 }
3525 return type;
3526}
3527
3528/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003529 * Skip over an expression, ignoring most errors.
3530 */
3531 static void
3532skip_expr_cctx(char_u **arg, cctx_T *cctx)
3533{
3534 evalarg_T evalarg;
3535
3536 CLEAR_FIELD(evalarg);
3537 evalarg.eval_cctx = cctx;
3538 skip_expr(arg, &evalarg);
3539}
3540
3541/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003543 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 */
3545 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003546compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003548 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549
3550 // this works from end to start
3551 while (p > start)
3552 {
3553 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003554 while (VIM_ISWHITE(*p))
3555 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 if (*p == '-' || *p == '+')
3557 {
3558 int negate = *p == '-';
3559 isn_T *isn;
3560
3561 // TODO: check type
3562 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3563 {
3564 --p;
3565 if (*p == '-')
3566 negate = !negate;
3567 }
3568 // only '-' has an effect, for '+' we only check the type
3569 if (negate)
3570 isn = generate_instr(cctx, ISN_NEGATENR);
3571 else
3572 isn = generate_instr(cctx, ISN_CHECKNR);
3573 if (isn == NULL)
3574 return FAIL;
3575 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003576 else if (numeric_only)
3577 {
3578 ++p;
3579 break;
3580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 else
3582 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003583 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003585 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003587 if (p[-1] == '!')
3588 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 }
3591 if (generate_2BOOL(cctx, invert) == FAIL)
3592 return FAIL;
3593 }
3594 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003595 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 return OK;
3597}
3598
3599/*
Bram Moolenaar7e368202020-12-25 21:56:57 +01003600 * Compile "(expression)": recursive!
3601 * Return FAIL/OK.
3602 */
3603 static int
3604compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
3605{
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003606 int ret;
Bram Moolenaar24156692021-01-14 20:35:49 +01003607 char_u *p = *arg + 1;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003608
Bram Moolenaar24156692021-01-14 20:35:49 +01003609 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3610 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003611 if (ppconst->pp_used <= PPSIZE - 10)
3612 {
3613 ret = compile_expr1(arg, cctx, ppconst);
3614 }
3615 else
3616 {
3617 // Not enough space in ppconst, flush constants.
3618 if (generate_ppconst(cctx, ppconst) == FAIL)
3619 return FAIL;
3620 ret = compile_expr0(arg, cctx);
3621 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003622 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
3623 return FAIL;
Bram Moolenaar7e368202020-12-25 21:56:57 +01003624 if (**arg == ')')
3625 ++*arg;
3626 else if (ret == OK)
3627 {
3628 emsg(_(e_missing_close));
3629 ret = FAIL;
3630 }
3631 return ret;
3632}
3633
3634/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003636 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 */
3638 static int
3639compile_subscript(
3640 char_u **arg,
3641 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003642 char_u *start_leader,
3643 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003644 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003646 char_u *name_start = *end_leader;
3647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 for (;;)
3649 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003650 char_u *p = skipwhite(*arg);
3651
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003652 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003653 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003654 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003655
3656 // If a following line starts with "->{" or "->X" advance to that
3657 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003658 // Also if a following line starts with ".x".
3659 if (next != NULL &&
3660 ((next[0] == '-' && next[1] == '>'
3661 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003662 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003663 {
3664 next = next_line_from_context(cctx, TRUE);
3665 if (next == NULL)
3666 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003667 *arg = next;
3668 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003669 }
3670 }
3671
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003672 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003673 // not a function call.
3674 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003676 garray_T *stack = &cctx->ctx_type_stack;
3677 type_T *type;
3678 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003680 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003681 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003682 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003685 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3686
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003687 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3689 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003690 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 return FAIL;
3692 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003693 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003695 char_u *pstart = p;
3696
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003697 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003698 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003699 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 // something->method()
3702 // Apply the '!', '-' and '+' first:
3703 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003704 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003707 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003708 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003709 // No line break supported right after "->".
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003710 if (**arg == '(')
Bram Moolenaar65c44152020-12-24 15:14:01 +01003711 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01003712 int argcount = 1;
3713 char_u *expr;
3714 garray_T *stack;
3715 type_T *type;
3716
3717 // Funcref call: list->(Refs[2])(arg)
3718 // or lambda: list->((arg) => expr)(arg)
3719 // Fist compile the arguments.
3720 expr = *arg;
3721 *arg = skipwhite(*arg + 1);
3722 skip_expr_cctx(arg, cctx);
3723 *arg = skipwhite(*arg);
3724 if (**arg != ')')
3725 {
3726 semsg(_(e_missing_paren), *arg);
3727 return FAIL;
3728 }
3729 ++*arg;
3730 if (**arg != '(')
3731 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003732 if (*skipwhite(*arg) == '(')
3733 emsg(_(e_nowhitespace));
3734 else
3735 semsg(_(e_missing_paren), *arg);
Bram Moolenaar7e368202020-12-25 21:56:57 +01003736 return FAIL;
3737 }
3738
3739 *arg = skipwhite(*arg + 1);
3740 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3741 return FAIL;
3742
3743 // Compile the function expression.
3744 if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
3745 return FAIL;
3746
3747 stack = &cctx->ctx_type_stack;
3748 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3749 if (generate_PCALL(cctx, argcount,
3750 (char_u *)"[expression]", type, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 return FAIL;
3752 }
3753 else
3754 {
3755 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003756 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003757 if (!eval_isnamec1(*p))
3758 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003759 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003760 return FAIL;
3761 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003762 if (ASCII_ISALPHA(*p) && p[1] == ':')
3763 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003764 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 ;
3766 if (*p != '(')
3767 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003768 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 return FAIL;
3770 }
3771 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003772 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 return FAIL;
3774 }
3775 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003776 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003778 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003779 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003780 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003781 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003782 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003783
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003784 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003785 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003786 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 // TODO: blob index
3788 // TODO: more arguments
3789 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003790 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003791 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003792 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003793
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003794 ++p;
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003795 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003796 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003797 if (**arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003798 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003799 // missing first index is equal to zero
3800 generate_PUSHNR(cctx, 0);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003801 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003802 else
3803 {
3804 if (compile_expr0(arg, cctx) == FAIL)
3805 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003806 if (**arg == ':')
3807 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003808 semsg(_(e_white_space_required_before_and_after_str_at_str),
3809 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003810 return FAIL;
3811 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003812 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003813 return FAIL;
3814 *arg = skipwhite(*arg);
3815 }
3816 if (**arg == ':')
3817 {
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003818 is_slice = TRUE;
3819 ++*arg;
3820 if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
3821 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003822 semsg(_(e_white_space_required_before_and_after_str_at_str),
3823 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003824 return FAIL;
3825 }
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003826 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003827 return FAIL;
3828 if (**arg == ']')
3829 // missing second index is equal to end of string
3830 generate_PUSHNR(cctx, -1);
3831 else
3832 {
3833 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003834 return FAIL;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01003835 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003836 return FAIL;
3837 *arg = skipwhite(*arg);
3838 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840
3841 if (**arg != ']')
3842 {
3843 emsg(_(e_missbrac));
3844 return FAIL;
3845 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003846 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003848 // We can index a list and a dict. If we don't know the type
3849 // we can use the index value type.
3850 // TODO: If we don't know use an instruction to figure it out at
3851 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003852 typep = ((type_T **)stack->ga_data) + stack->ga_len
3853 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003854 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003855 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3856 // If the index is a string, the variable must be a Dict.
3857 if (*typep == &t_any && valtype == &t_string)
3858 vtype = VAR_DICT;
3859 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003860 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003861 if (need_type(valtype, &t_number, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003862 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003863 return FAIL;
3864 if (is_slice)
3865 {
3866 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar351ead02021-01-16 16:07:01 +01003867 if (need_type(valtype, &t_number, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003868 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003869 return FAIL;
3870 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003871 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003872
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003873 if (vtype == VAR_DICT)
3874 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003875 if (is_slice)
3876 {
3877 emsg(_(e_cannot_slice_dictionary));
3878 return FAIL;
3879 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003880 if ((*typep)->tt_type == VAR_DICT)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003881 {
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003882 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003883 if (*typep == &t_unknown)
3884 // empty dict was used
3885 *typep = &t_any;
3886 }
Bram Moolenaar7892b952020-07-20 22:09:34 +02003887 else
3888 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01003889 if (need_type(*typep, &t_dict_any, -2, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003890 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003891 return FAIL;
3892 *typep = &t_any;
3893 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003894 if (may_generate_2STRING(-1, cctx) == FAIL)
3895 return FAIL;
3896 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3897 return FAIL;
3898 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003899 else if (vtype == VAR_STRING)
3900 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003901 *typep = &t_string;
3902 if ((is_slice
3903 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3904 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003905 return FAIL;
3906 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003907 else if (vtype == VAR_BLOB)
3908 {
3909 emsg("Sorry, blob index and slice not implemented yet");
3910 return FAIL;
3911 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003912 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003913 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003914 if (is_slice)
3915 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003916 if (generate_instr_drop(cctx,
3917 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3918 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003919 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003920 }
Bram Moolenaared591872020-08-15 22:14:53 +02003921 else
3922 {
3923 if ((*typep)->tt_type == VAR_LIST)
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003924 {
Bram Moolenaared591872020-08-15 22:14:53 +02003925 *typep = (*typep)->tt_member;
Bram Moolenaar31a11b92021-01-10 19:23:27 +01003926 if (*typep == &t_unknown)
3927 // empty list was used
3928 *typep = &t_any;
3929 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003930 if (generate_instr_drop(cctx,
3931 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3932 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003933 return FAIL;
3934 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003935 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003936 else
3937 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003938 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003939 return FAIL;
3940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003942 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003944 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003945 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003946 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003947 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003948
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003949 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003950 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003951 {
3952 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003953 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003954 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003955 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003956 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 while (eval_isnamec(*p))
3958 MB_PTR_ADV(p);
3959 if (p == *arg)
3960 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003961 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 return FAIL;
3963 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003964 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 return FAIL;
3966 *arg = p;
3967 }
3968 else
3969 break;
3970 }
3971
3972 // TODO - see handle_subscript():
3973 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3974 // Don't do this when "Func" is already a partial that was bound
3975 // explicitly (pt_auto is FALSE).
3976
3977 return OK;
3978}
3979
3980/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003981 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3982 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003984 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003985 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003986 *
3987 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 */
3989
3990/*
3991 * number number constant
3992 * 0zFFFFFFFF Blob constant
3993 * "string" string constant
3994 * 'string' literal string constant
3995 * &option-name option value
3996 * @r register contents
3997 * identifier variable value
3998 * function() function call
3999 * $VAR environment variable
4000 * (expression) nested expression
4001 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01004002 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 *
4004 * Also handle:
4005 * ! in front logical NOT
4006 * - in front unary minus
4007 * + in front unary plus (ignored)
4008 * trailing (arg) funcref/partial call
4009 * trailing [] subscript in String or List
4010 * trailing .name entry in Dictionary
4011 * trailing ->name() method call
4012 */
4013 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004014compile_expr7(
4015 char_u **arg,
4016 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004017 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 char_u *start_leader, *end_leader;
4020 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004021 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02004022 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004024 ppconst->pp_is_const = FALSE;
4025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 /*
4027 * Skip '!', '-' and '+' characters. They are handled later.
4028 */
4029 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004030 if (eval_leader(arg, TRUE) == FAIL)
4031 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 end_leader = *arg;
4033
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004034 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 switch (**arg)
4036 {
4037 /*
4038 * Number constant.
4039 */
4040 case '0': // also for blob starting with 0z
4041 case '1':
4042 case '2':
4043 case '3':
4044 case '4':
4045 case '5':
4046 case '6':
4047 case '7':
4048 case '8':
4049 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004050 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02004052 // Apply "-" and "+" just before the number now, right to
4053 // left. Matters especially when "->" follows. Stops at
4054 // '!'.
4055 if (apply_leader(rettv, TRUE,
4056 start_leader, &end_leader) == FAIL)
4057 {
4058 clear_tv(rettv);
4059 return FAIL;
4060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 break;
4062
4063 /*
4064 * String constant: "string".
4065 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004066 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 return FAIL;
4068 break;
4069
4070 /*
4071 * Literal string constant: 'str''ing'.
4072 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004073 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 return FAIL;
4075 break;
4076
4077 /*
4078 * Constant Vim variable.
4079 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004080 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 ret = NOTDONE;
4082 break;
4083
4084 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004085 * "true" constant
4086 */
4087 case 't': if (STRNCMP(*arg, "true", 4) == 0
4088 && !eval_isnamec((*arg)[4]))
4089 {
4090 *arg += 4;
4091 rettv->v_type = VAR_BOOL;
4092 rettv->vval.v_number = VVAL_TRUE;
4093 }
4094 else
4095 ret = NOTDONE;
4096 break;
4097
4098 /*
4099 * "false" constant
4100 */
4101 case 'f': if (STRNCMP(*arg, "false", 5) == 0
4102 && !eval_isnamec((*arg)[5]))
4103 {
4104 *arg += 5;
4105 rettv->v_type = VAR_BOOL;
4106 rettv->vval.v_number = VVAL_FALSE;
4107 }
4108 else
4109 ret = NOTDONE;
4110 break;
4111
4112 /*
Bram Moolenaar67977822021-01-03 21:53:53 +01004113 * "null" constant
4114 */
4115 case 'n': if (STRNCMP(*arg, "null", 4) == 0
4116 && !eval_isnamec((*arg)[5]))
4117 {
4118 *arg += 4;
4119 rettv->v_type = VAR_SPECIAL;
4120 rettv->vval.v_number = VVAL_NULL;
4121 }
4122 else
4123 ret = NOTDONE;
4124 break;
4125
4126 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 * List: [expr, expr]
4128 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004129 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 break;
4131
4132 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 * Dictionary: {'key': val, 'key': val}
4134 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004135 case '{': ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 break;
4137
4138 /*
4139 * Option value: &name
4140 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004141 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
4142 return FAIL;
4143 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 break;
4145
4146 /*
4147 * Environment variable: $VAR.
4148 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004149 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
4150 return FAIL;
4151 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 break;
4153
4154 /*
4155 * Register contents: @r.
4156 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02004157 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
4158 return FAIL;
4159 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 break;
4161 /*
4162 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01004163 * lambda: (arg, arg) => expr
4164 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 */
Bram Moolenaare462f522020-12-27 14:43:30 +01004166 case '(': // if compile_lambda returns NOTDONE then it must be (expr)
4167 ret = compile_lambda(arg, cctx);
4168 if (ret == NOTDONE)
Bram Moolenaar7e368202020-12-25 21:56:57 +01004169 ret = compile_parenthesis(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 break;
4171
4172 default: ret = NOTDONE;
4173 break;
4174 }
4175 if (ret == FAIL)
4176 return FAIL;
4177
Bram Moolenaar1c747212020-05-09 18:28:34 +02004178 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004180 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181 clear_tv(rettv);
4182 else
4183 // A constant expression can possibly be handled compile time,
4184 // return the value instead of generating code.
4185 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187 else if (ret == NOTDONE)
4188 {
4189 char_u *p;
4190 int r;
4191
4192 if (!eval_isnamec1(**arg))
4193 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004194 if (ends_excmd(*skipwhite(*arg)))
4195 semsg(_(e_empty_expression_str), *arg);
4196 else
4197 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 return FAIL;
4199 }
4200
4201 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004202 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004204 {
4205 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004207 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004208 {
4209 if (generate_ppconst(cctx, ppconst) == FAIL)
4210 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004211 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004212 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 if (r == FAIL)
4214 return FAIL;
4215 }
4216
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004217 // Handle following "[]", ".member", etc.
4218 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004219 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004220 ppconst) == FAIL)
4221 return FAIL;
4222 if (ppconst->pp_used > 0)
4223 {
4224 // apply the '!', '-' and '+' before the constant
4225 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004226 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004227 return FAIL;
4228 return OK;
4229 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004230 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004232 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233}
4234
4235/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004236 * Give the "white on both sides" error, taking the operator from "p[len]".
4237 */
4238 void
4239error_white_both(char_u *op, int len)
4240{
4241 char_u buf[10];
4242
4243 vim_strncpy(buf, op, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004244 semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004245}
4246
4247/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004248 * <type>expr7: runtime type check / conversion
4249 */
4250 static int
4251compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4252{
4253 type_T *want_type = NULL;
4254
4255 // Recognize <type>
4256 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4257 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004258 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004259 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4260 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004261 return FAIL;
4262
4263 if (**arg != '>')
4264 {
4265 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004266 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004267 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004268 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004269 return FAIL;
4270 }
4271 ++*arg;
Bram Moolenaar5afd0812021-01-03 18:33:13 +01004272 if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004273 return FAIL;
4274 }
4275
4276 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4277 return FAIL;
4278
4279 if (want_type != NULL)
4280 {
4281 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004282 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004283
Bram Moolenaard1103582020-08-14 22:44:25 +02004284 generate_ppconst(cctx, ppconst);
4285 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004286 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004287 {
Bram Moolenaar351ead02021-01-16 16:07:01 +01004288 if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004289 return FAIL;
4290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 * * number multiplication
4298 * / number division
4299 * % number modulo
4300 */
4301 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004302compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303{
4304 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004305 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004306 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004309 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 return FAIL;
4311
4312 /*
4313 * Repeat computing, until no "*", "/" or "%" is following.
4314 */
4315 for (;;)
4316 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004317 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 if (*op != '*' && *op != '/' && *op != '%')
4319 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004320 if (next != NULL)
4321 {
4322 *arg = next_line_from_context(cctx, TRUE);
4323 op = skipwhite(*arg);
4324 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004325
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004326 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004328 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004329 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 }
Bram Moolenaar918a4242020-12-06 14:37:08 +01004331 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004334 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004335 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004337
4338 if (ppconst->pp_used == ppconst_used + 2
4339 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4340 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004341 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004342 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4343 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
4344 varnumber_T res = 0;
4345 int failed = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004347 // both are numbers: compute the result
4348 switch (*op)
4349 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004350 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004351 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004352 case '/': res = num_divide(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004353 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004354 break;
Bram Moolenaare64f83c2021-01-19 22:16:41 +01004355 case '%': res = num_modulus(tv1->vval.v_number,
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004356 tv2->vval.v_number, &failed);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004357 break;
4358 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01004359 if (failed)
4360 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004361 tv1->vval.v_number = res;
4362 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004363 }
4364 else
4365 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004366 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004367 generate_two_op(cctx, op);
4368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * + number addition
4376 * - number subtraction
4377 * .. string concatenation
4378 */
4379 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004380compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381{
4382 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004383 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004385 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
4387 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004388 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 return FAIL;
4390
4391 /*
4392 * Repeat computing, until no "+", "-" or ".." is following.
4393 */
4394 for (;;)
4395 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004396 op = may_peek_next_line(cctx, *arg, &next);
4397 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 break;
4399 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004400 if (next != NULL)
4401 {
4402 *arg = next_line_from_context(cctx, TRUE);
4403 op = skipwhite(*arg);
4404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004406 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004408 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004409 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 }
4411
Bram Moolenaare0de1712020-12-02 17:36:54 +01004412 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004413 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004415 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004416 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 return FAIL;
4418
Bram Moolenaara5565e42020-05-09 15:44:01 +02004419 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004420 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004421 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4422 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4423 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4424 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004426 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4427 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004428
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004429 // concat/subtract/add constant numbers
4430 if (*op == '+')
4431 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4432 else if (*op == '-')
4433 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4434 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004435 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004436 // concatenate constant strings
4437 char_u *s1 = tv1->vval.v_string;
4438 char_u *s2 = tv2->vval.v_string;
4439 size_t len1 = STRLEN(s1);
4440
4441 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4442 if (tv1->vval.v_string == NULL)
4443 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004444 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004445 return FAIL;
4446 }
4447 mch_memmove(tv1->vval.v_string, s1, len1);
4448 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004449 vim_free(s1);
4450 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004451 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004452 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 }
4454 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004455 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004456 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004457 if (*op == '.')
4458 {
4459 if (may_generate_2STRING(-2, cctx) == FAIL
4460 || may_generate_2STRING(-1, cctx) == FAIL)
4461 return FAIL;
4462 generate_instr_drop(cctx, ISN_CONCAT, 1);
4463 }
4464 else
4465 generate_two_op(cctx, op);
4466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 }
4468
4469 return OK;
4470}
4471
4472/*
4473 * expr5a == expr5b
4474 * expr5a =~ expr5b
4475 * expr5a != expr5b
4476 * expr5a !~ expr5b
4477 * expr5a > expr5b
4478 * expr5a >= expr5b
4479 * expr5a < expr5b
4480 * expr5a <= expr5b
4481 * expr5a is expr5b
4482 * expr5a isnot expr5b
4483 *
4484 * Produces instructions:
4485 * EVAL expr5a Push result of "expr5a"
4486 * EVAL expr5b Push result of "expr5b"
4487 * COMPARE one of the compare instructions
4488 */
4489 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004490compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491{
Bram Moolenaar657137c2021-01-09 15:45:23 +01004492 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004494 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004497 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
4499 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004500 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 return FAIL;
4502
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004503 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004504 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505
4506 /*
4507 * If there is a comparative operator, use it.
4508 */
4509 if (type != EXPR_UNKNOWN)
4510 {
4511 int ic = FALSE; // Default: do not ignore case
4512
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004513 if (next != NULL)
4514 {
4515 *arg = next_line_from_context(cctx, TRUE);
4516 p = skipwhite(*arg);
4517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 if (type_is && (p[len] == '?' || p[len] == '#'))
4519 {
4520 semsg(_(e_invexpr2), *arg);
4521 return FAIL;
4522 }
4523 // extra question mark appended: ignore case
4524 if (p[len] == '?')
4525 {
4526 ic = TRUE;
4527 ++len;
4528 }
4529 // extra '#' appended: match case (ignored)
4530 else if (p[len] == '#')
4531 ++len;
4532 // nothing appended: match case
4533
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004534 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004536 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004537 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004538 }
4539
4540 // get the second variable
Bram Moolenaar918a4242020-12-06 14:37:08 +01004541 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004542 return FAIL;
4543
Bram Moolenaara5565e42020-05-09 15:44:01 +02004544 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 return FAIL;
4546
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 if (ppconst->pp_used == ppconst_used + 2)
4548 {
4549 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4550 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4551 int ret;
4552
4553 // Both sides are a constant, compute the result now.
4554 // First check for a valid combination of types, this is more
4555 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004556 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004557 ret = FAIL;
4558 else
4559 {
4560 ret = typval_compare(tv1, tv2, type, ic);
4561 tv1->v_type = VAR_BOOL;
4562 tv1->vval.v_number = tv1->vval.v_number
4563 ? VVAL_TRUE : VVAL_FALSE;
4564 clear_tv(tv2);
4565 --ppconst->pp_used;
4566 }
4567 return ret;
4568 }
4569
4570 generate_ppconst(cctx, ppconst);
4571 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 }
4573
4574 return OK;
4575}
4576
Bram Moolenaar7f141552020-05-09 17:35:53 +02004577static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4578
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579/*
4580 * Compile || or &&.
4581 */
4582 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004583compile_and_or(
4584 char_u **arg,
4585 cctx_T *cctx,
4586 char *op,
4587 ppconst_T *ppconst,
4588 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004590 char_u *next;
4591 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 int opchar = *op;
4593
4594 if (p[0] == opchar && p[1] == opchar)
4595 {
4596 garray_T *instr = &cctx->ctx_instr;
4597 garray_T end_ga;
4598
4599 /*
4600 * Repeat until there is no following "||" or "&&"
4601 */
4602 ga_init2(&end_ga, sizeof(int), 10);
4603 while (p[0] == opchar && p[1] == opchar)
4604 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004605 if (next != NULL)
4606 {
4607 *arg = next_line_from_context(cctx, TRUE);
4608 p = skipwhite(*arg);
4609 }
4610
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004611 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4612 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004613 semsg(_(e_white_space_required_before_and_after_str_at_str),
4614 op, *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004615 return FAIL;
4616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004618 // TODO: use ppconst if the value is a constant and check
4619 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004620 generate_ppconst(cctx, ppconst);
4621
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004622 // Every part must evaluate to a bool.
4623 if (bool_on_stack(cctx) == FAIL)
4624 {
4625 ga_clear(&end_ga);
4626 return FAIL;
4627 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 if (ga_grow(&end_ga, 1) == FAIL)
4630 {
4631 ga_clear(&end_ga);
4632 return FAIL;
4633 }
4634 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4635 ++end_ga.ga_len;
4636 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004637 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
4639 // eval the next expression
Bram Moolenaar918a4242020-12-06 14:37:08 +01004640 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004641 {
4642 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004643 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004644 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004645
Bram Moolenaara5565e42020-05-09 15:44:01 +02004646 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4647 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 {
4649 ga_clear(&end_ga);
4650 return FAIL;
4651 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004652
4653 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004655 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004657 // Every part must evaluate to a bool.
4658 if (bool_on_stack(cctx) == FAIL)
4659 {
4660 ga_clear(&end_ga);
4661 return FAIL;
4662 }
4663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 // Fill in the end label in all jumps.
4665 while (end_ga.ga_len > 0)
4666 {
4667 isn_T *isn;
4668
4669 --end_ga.ga_len;
4670 isn = ((isn_T *)instr->ga_data)
4671 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4672 isn->isn_arg.jump.jump_where = instr->ga_len;
4673 }
4674 ga_clear(&end_ga);
4675 }
4676
4677 return OK;
4678}
4679
4680/*
4681 * expr4a && expr4a && expr4a logical AND
4682 *
4683 * Produces instructions:
4684 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004685 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004686 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004688 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 * EVAL expr4c Push result of "expr4c"
4690 * end:
4691 */
4692 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004693compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004695 int ppconst_used = ppconst->pp_used;
4696
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004698 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 return FAIL;
4700
4701 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703}
4704
4705/*
4706 * expr3a || expr3b || expr3c logical OR
4707 *
4708 * Produces instructions:
4709 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004710 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004711 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004713 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 * EVAL expr3c Push result of "expr3c"
4715 * end:
4716 */
4717 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004718compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004719{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720 int ppconst_used = ppconst->pp_used;
4721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004723 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 return FAIL;
4725
4726 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004727 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004728}
4729
4730/*
4731 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004733 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 * JUMP_IF_FALSE alt jump if false
4735 * EVAL expr1a
4736 * JUMP_ALWAYS end
4737 * alt: EVAL expr1b
4738 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004739 *
4740 * Toplevel expression: expr2 ?? expr1
4741 * Produces instructions:
4742 * EVAL expr2 Push result of "expr2"
4743 * JUMP_AND_KEEP_IF_TRUE end jump if true
4744 * EVAL expr1
4745 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 */
4747 static int
Bram Moolenaar7e368202020-12-25 21:56:57 +01004748compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749{
4750 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004751 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004752 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaar3988f642020-08-27 22:43:03 +02004754 // Ignore all kinds of errors when not producing code.
4755 if (cctx->ctx_skip == SKIP_YES)
4756 {
Bram Moolenaar7e368202020-12-25 21:56:57 +01004757 skip_expr_cctx(arg, cctx);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004758 return OK;
4759 }
4760
Bram Moolenaar61a89812020-05-07 16:58:17 +02004761 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004762 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 return FAIL;
4764
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004765 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004766 if (*p == '?')
4767 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004768 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 garray_T *instr = &cctx->ctx_instr;
4770 garray_T *stack = &cctx->ctx_type_stack;
4771 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004772 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004774 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004775 int has_const_expr = FALSE;
4776 int const_value = FALSE;
4777 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004779 if (next != NULL)
4780 {
4781 *arg = next_line_from_context(cctx, TRUE);
4782 p = skipwhite(*arg);
4783 }
4784
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004785 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004786 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004787 semsg(_(e_white_space_required_before_and_after_str_at_str),
4788 op_falsy ? "??" : "?", *arg);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004789 return FAIL;
4790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791
Bram Moolenaara5565e42020-05-09 15:44:01 +02004792 if (ppconst->pp_used == ppconst_used + 1)
4793 {
4794 // the condition is a constant, we know whether the ? or the :
4795 // expression is to be evaluated.
4796 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004797 if (op_falsy)
4798 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4799 else
4800 {
4801 int error = FALSE;
4802
4803 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4804 &error);
4805 if (error)
4806 return FAIL;
4807 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004808 cctx->ctx_skip = save_skip == SKIP_YES ||
4809 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4810
4811 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4812 // "left ?? right" and "left" is truthy: produce "left"
4813 generate_ppconst(cctx, ppconst);
4814 else
4815 {
4816 clear_tv(&ppconst->pp_tv[ppconst_used]);
4817 --ppconst->pp_used;
4818 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004819 }
4820 else
4821 {
4822 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004823 if (op_falsy)
4824 end_idx = instr->ga_len;
4825 generate_JUMP(cctx, op_falsy
4826 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4827 if (op_falsy)
4828 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004829 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830
4831 // evaluate the second expression; any type is accepted
Bram Moolenaar918a4242020-12-06 14:37:08 +01004832 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004833 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004834 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004835 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836
Bram Moolenaara5565e42020-05-09 15:44:01 +02004837 if (!has_const_expr)
4838 {
4839 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004841 if (!op_falsy)
4842 {
4843 // remember the type and drop it
4844 --stack->ga_len;
4845 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004847 end_idx = instr->ga_len;
4848 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004849
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004850 // jump here from JUMP_IF_FALSE
4851 isn = ((isn_T *)instr->ga_data) + alt_idx;
4852 isn->isn_arg.jump.jump_where = instr->ga_len;
4853 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004856 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004858 // Check for the ":".
4859 p = may_peek_next_line(cctx, *arg, &next);
4860 if (*p != ':')
4861 {
4862 emsg(_(e_missing_colon));
4863 return FAIL;
4864 }
4865 if (next != NULL)
4866 {
4867 *arg = next_line_from_context(cctx, TRUE);
4868 p = skipwhite(*arg);
4869 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004870
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004871 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4872 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004873 semsg(_(e_white_space_required_before_and_after_str_at_str),
4874 ":", p);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004875 return FAIL;
4876 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004878 // evaluate the third expression
4879 if (has_const_expr)
4880 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004881 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar918a4242020-12-06 14:37:08 +01004882 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004883 return FAIL;
4884 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4885 return FAIL;
4886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887
Bram Moolenaara5565e42020-05-09 15:44:01 +02004888 if (!has_const_expr)
4889 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004890 type_T **typep;
4891
Bram Moolenaara5565e42020-05-09 15:44:01 +02004892 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
Bram Moolenaara5565e42020-05-09 15:44:01 +02004894 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004895 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4896 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004897
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004898 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004899 isn = ((isn_T *)instr->ga_data) + end_idx;
4900 isn->isn_arg.jump.jump_where = instr->ga_len;
4901 }
4902
4903 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 }
4905 return OK;
4906}
4907
4908/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004909 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004910 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4911 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004912 */
4913 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004914compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004915{
4916 ppconst_T ppconst;
4917
4918 CLEAR_FIELD(ppconst);
4919 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4920 {
4921 clear_ppconst(&ppconst);
4922 return FAIL;
4923 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004924 if (is_const != NULL)
4925 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004926 if (generate_ppconst(cctx, &ppconst) == FAIL)
4927 return FAIL;
4928 return OK;
4929}
4930
4931/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004932 * Toplevel expression.
4933 */
4934 static int
4935compile_expr0(char_u **arg, cctx_T *cctx)
4936{
4937 return compile_expr0_ext(arg, cctx, NULL);
4938}
4939
4940/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 * compile "return [expr]"
4942 */
4943 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004944compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945{
4946 char_u *p = arg;
4947 garray_T *stack = &cctx->ctx_type_stack;
4948 type_T *stack_type;
4949
4950 if (*p != NUL && *p != '|' && *p != '\n')
4951 {
4952 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004953 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 return NULL;
4955
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004956 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004957 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004958 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar6b553772020-12-31 13:31:23 +01004959 if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
Bram Moolenaar328eac22021-01-07 19:23:08 +01004960 || cctx->ctx_ufunc->uf_ret_type == &t_unknown
4961 || cctx->ctx_ufunc->uf_ret_type == &t_any))
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004962 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004963 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004964 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004965 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004966 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004967 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4968 && stack_type->tt_type != VAR_VOID
4969 && stack_type->tt_type != VAR_UNKNOWN)
4970 {
4971 emsg(_(e_returning_value_in_function_without_return_type));
4972 return NULL;
4973 }
4974 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar351ead02021-01-16 16:07:01 +01004975 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004976 return NULL;
4977 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 }
4980 else
4981 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004982 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004983 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004984 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4985 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004987 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 return NULL;
4989 }
4990
4991 // No argument, return zero.
4992 generate_PUSHNR(cctx, 0);
4993 }
Bram Moolenaar7cd24222021-01-12 18:58:39 +01004994
4995 // Undo any command modifiers.
4996 generate_undo_cmdmods(cctx);
4997
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004998 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 return NULL;
5000
5001 // "return val | endif" is possible
5002 return skipwhite(p);
5003}
5004
5005/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02005006 * Get a line from the compilation context, compatible with exarg_T getline().
5007 * Return a pointer to the line in allocated memory.
5008 * Return NULL for end-of-file or some error.
5009 */
5010 static char_u *
5011exarg_getline(
5012 int c UNUSED,
5013 void *cookie,
5014 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005015 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005016{
5017 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02005018 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005019
Bram Moolenaar66250c92020-08-20 15:02:42 +02005020 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02005021 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02005022 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02005023 return NULL;
5024 ++cctx->ctx_lnum;
5025 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
5026 // Comment lines result in NULL pointers, skip them.
5027 if (p != NULL)
5028 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005029 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005030}
5031
5032/*
5033 * Compile a nested :def command.
5034 */
5035 static char_u *
5036compile_nested_function(exarg_T *eap, cctx_T *cctx)
5037{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005038 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02005039 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005040 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005041 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005042 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005043 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005044
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005045 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02005046 {
5047 emsg(_(e_cannot_use_bang_with_nested_def));
5048 return NULL;
5049 }
5050
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01005051 if (*name_start == '/')
5052 {
5053 name_end = skip_regexp(name_start + 1, '/', TRUE);
5054 if (*name_end == '/')
5055 ++name_end;
5056 eap->nextcmd = check_nextcmd(name_end);
5057 }
5058 if (name_end == name_start || *skipwhite(name_end) != '(')
5059 {
5060 if (!ends_excmd2(name_start, name_end))
5061 {
5062 semsg(_(e_invalid_command_str), eap->cmd);
5063 return NULL;
5064 }
5065
5066 // "def" or "def Name": list functions
5067 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
5068 return NULL;
5069 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
5070 }
5071
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005072 // Only g:Func() can use a namespace.
5073 if (name_start[1] == ':' && !is_global)
5074 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005075 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02005076 return NULL;
5077 }
Bram Moolenaareef21022020-08-01 22:16:43 +02005078 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
5079 return NULL;
5080
Bram Moolenaar04b12692020-05-04 23:24:44 +02005081 eap->arg = name_end;
5082 eap->getline = exarg_getline;
5083 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005084 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005085 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005086 lambda_name = vim_strsave(get_lambda_name());
5087 if (lambda_name == NULL)
5088 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005089 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005090
Bram Moolenaar822ba242020-05-24 23:00:18 +02005091 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005092 {
5093 r = eap->skip ? OK : FAIL;
5094 goto theend;
5095 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01005096 if (func_needs_compiling(ufunc, cctx->ctx_profiling)
5097 && compile_def_function(ufunc, TRUE, cctx->ctx_profiling, cctx)
5098 == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005099 {
5100 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005101 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02005102 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02005103
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005104 if (is_global)
5105 {
5106 char_u *func_name = vim_strnsave(name_start + 2,
5107 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02005108
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005109 if (func_name == NULL)
5110 r = FAIL;
5111 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005112 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005113 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005114 lambda_name = NULL;
5115 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005116 }
5117 else
5118 {
5119 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02005120 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005121 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005122 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02005123
Bram Moolenaareef21022020-08-01 22:16:43 +02005124 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005125 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02005126 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005127 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005128 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005129
5130 // copy over the block scope IDs
5131 if (block_depth > 0)
5132 {
5133 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
5134 if (ufunc->uf_block_ids != NULL)
5135 {
5136 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
5137 sizeof(int) * block_depth);
5138 ufunc->uf_block_depth = block_depth;
5139 }
5140 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005141 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02005142 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01005143 r = OK;
5144
5145theend:
5146 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02005147 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02005148}
5149
5150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 * Return the length of an assignment operator, or zero if there isn't one.
5152 */
5153 int
5154assignment_len(char_u *p, int *heredoc)
5155{
5156 if (*p == '=')
5157 {
5158 if (p[1] == '<' && p[2] == '<')
5159 {
5160 *heredoc = TRUE;
5161 return 3;
5162 }
5163 return 1;
5164 }
5165 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5166 return 2;
5167 if (STRNCMP(p, "..=", 3) == 0)
5168 return 3;
5169 return 0;
5170}
5171
5172// words that cannot be used as a variable
5173static char *reserved[] = {
5174 "true",
5175 "false",
Bram Moolenaar67977822021-01-03 21:53:53 +01005176 "null",
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 NULL
5178};
5179
Bram Moolenaar752fc692021-01-04 21:57:11 +01005180// Destination for an assignment or ":unlet" with an index.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005181typedef enum {
5182 dest_local,
5183 dest_option,
5184 dest_env,
5185 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005186 dest_buffer,
5187 dest_window,
5188 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005189 dest_vimvar,
5190 dest_script,
5191 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005192 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005193} assign_dest_T;
5194
Bram Moolenaar752fc692021-01-04 21:57:11 +01005195// Used by compile_lhs() to store information about the LHS of an assignment
5196// and one argument of ":unlet" with an index.
5197typedef struct {
5198 assign_dest_T lhs_dest; // type of destination
5199
5200 char_u *lhs_name; // allocated name including
5201 // "[expr]" or ".name".
5202 size_t lhs_varlen; // length of the variable without
5203 // "[expr]" or ".name"
5204 char_u *lhs_dest_end; // end of the destination, including
5205 // "[expr]" or ".name".
5206
5207 int lhs_has_index; // has "[expr]" or ".name"
5208
5209 int lhs_new_local; // create new local variable
5210 int lhs_opt_flags; // for when destination is an option
5211 int lhs_vimvaridx; // for when destination is a v:var
5212
5213 lvar_T lhs_local_lvar; // used for existing local destination
5214 lvar_T lhs_arg_lvar; // used for argument destination
5215 lvar_T *lhs_lvar; // points to destination lvar
5216 int lhs_scriptvar_sid;
5217 int lhs_scriptvar_idx;
5218
5219 int lhs_has_type; // type was specified
5220 type_T *lhs_type;
5221 type_T *lhs_member_type;
5222} lhs_T;
5223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005225 * Generate the load instruction for "name".
5226 */
5227 static void
5228generate_loadvar(
5229 cctx_T *cctx,
5230 assign_dest_T dest,
5231 char_u *name,
5232 lvar_T *lvar,
5233 type_T *type)
5234{
5235 switch (dest)
5236 {
5237 case dest_option:
5238 // TODO: check the option exists
5239 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5240 break;
5241 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005242 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5243 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5244 else
5245 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005246 break;
5247 case dest_buffer:
5248 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5249 break;
5250 case dest_window:
5251 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5252 break;
5253 case dest_tab:
5254 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5255 break;
5256 case dest_script:
5257 compile_load_scriptvar(cctx,
5258 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5259 break;
5260 case dest_env:
5261 // Include $ in the name here
5262 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5263 break;
5264 case dest_reg:
5265 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5266 break;
5267 case dest_vimvar:
5268 generate_LOADV(cctx, name + 2, TRUE);
5269 break;
5270 case dest_local:
Bram Moolenaarab360522021-01-10 14:02:28 +01005271 if (lvar->lv_from_outer > 0)
5272 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
5273 type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005274 else
5275 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5276 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005277 case dest_expr:
5278 // list or dict value should already be on the stack.
5279 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005280 }
5281}
5282
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005283/*
5284 * Skip over "[expr]" or ".member".
5285 * Does not check for any errors.
5286 */
5287 static char_u *
5288skip_index(char_u *start)
5289{
5290 char_u *p = start;
5291
5292 if (*p == '[')
5293 {
5294 p = skipwhite(p + 1);
5295 (void)skip_expr(&p, NULL);
5296 p = skipwhite(p);
5297 if (*p == ']')
5298 return p + 1;
5299 return p;
5300 }
5301 // if (*p == '.')
5302 return to_name_end(p + 1, TRUE);
5303}
5304
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005305 void
5306vim9_declare_error(char_u *name)
5307{
5308 char *scope = "";
5309
5310 switch (*name)
5311 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005312 case 'g': scope = _("global"); break;
5313 case 'b': scope = _("buffer"); break;
5314 case 'w': scope = _("window"); break;
5315 case 't': scope = _("tab"); break;
5316 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005317 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005318 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005319 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005320 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005321 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005322 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005323 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005324 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005325 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005326}
5327
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005328/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005329 * For one assignment figure out the type of destination. Return it in "dest".
5330 * When not recognized "dest" is not set.
5331 * For an option "opt_flags" is set.
5332 * For a v:var "vimvaridx" is set.
5333 * "type" is set to the destination type if known, unchanted otherwise.
5334 * Return FAIL if an error message was given.
5335 */
5336 static int
5337get_var_dest(
5338 char_u *name,
5339 assign_dest_T *dest,
5340 int cmdidx,
5341 int *opt_flags,
5342 int *vimvaridx,
5343 type_T **type,
5344 cctx_T *cctx)
5345{
5346 char_u *p;
5347
5348 if (*name == '&')
5349 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005350 int cc;
5351 long numval;
5352 getoption_T opt_type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005353
5354 *dest = dest_option;
5355 if (cmdidx == CMD_final || cmdidx == CMD_const)
5356 {
5357 emsg(_(e_const_option));
5358 return FAIL;
5359 }
5360 p = name;
5361 p = find_option_end(&p, opt_flags);
5362 if (p == NULL)
5363 {
5364 // cannot happen?
5365 emsg(_(e_letunexp));
5366 return FAIL;
5367 }
5368 cc = *p;
5369 *p = NUL;
5370 opt_type = get_option_value(skip_option_env_lead(name),
5371 &numval, NULL, *opt_flags);
5372 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005373 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005374 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005375 case gov_unknown:
5376 semsg(_(e_unknown_option), name);
5377 return FAIL;
5378 case gov_string:
5379 case gov_hidden_string:
5380 *type = &t_string;
5381 break;
5382 case gov_bool:
5383 case gov_hidden_bool:
5384 *type = &t_bool;
5385 break;
5386 case gov_number:
5387 case gov_hidden_number:
5388 *type = &t_number;
5389 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005390 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005391 }
5392 else if (*name == '$')
5393 {
5394 *dest = dest_env;
5395 *type = &t_string;
5396 }
5397 else if (*name == '@')
5398 {
5399 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5400 {
5401 emsg_invreg(name[1]);
5402 return FAIL;
5403 }
5404 *dest = dest_reg;
5405 *type = &t_string;
5406 }
5407 else if (STRNCMP(name, "g:", 2) == 0)
5408 {
5409 *dest = dest_global;
5410 }
5411 else if (STRNCMP(name, "b:", 2) == 0)
5412 {
5413 *dest = dest_buffer;
5414 }
5415 else if (STRNCMP(name, "w:", 2) == 0)
5416 {
5417 *dest = dest_window;
5418 }
5419 else if (STRNCMP(name, "t:", 2) == 0)
5420 {
5421 *dest = dest_tab;
5422 }
5423 else if (STRNCMP(name, "v:", 2) == 0)
5424 {
5425 typval_T *vtv;
5426 int di_flags;
5427
5428 *vimvaridx = find_vim_var(name + 2, &di_flags);
5429 if (*vimvaridx < 0)
5430 {
5431 semsg(_(e_variable_not_found_str), name);
5432 return FAIL;
5433 }
5434 // We use the current value of "sandbox" here, is that OK?
5435 if (var_check_ro(di_flags, name, FALSE))
5436 return FAIL;
5437 *dest = dest_vimvar;
5438 vtv = get_vim_var_tv(*vimvaridx);
5439 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5440 }
5441 return OK;
5442}
5443
5444/*
5445 * Generate a STORE instruction for "dest", not being "dest_local".
5446 * Return FAIL when out of memory.
5447 */
5448 static int
5449generate_store_var(
5450 cctx_T *cctx,
5451 assign_dest_T dest,
5452 int opt_flags,
5453 int vimvaridx,
5454 int scriptvar_idx,
5455 int scriptvar_sid,
5456 type_T *type,
5457 char_u *name)
5458{
5459 switch (dest)
5460 {
5461 case dest_option:
5462 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5463 opt_flags);
5464 case dest_global:
5465 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005466 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5467 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005468 case dest_buffer:
5469 // include b: with the name, easier to execute that way
5470 return generate_STORE(cctx, ISN_STOREB, 0, name);
5471 case dest_window:
5472 // include w: with the name, easier to execute that way
5473 return generate_STORE(cctx, ISN_STOREW, 0, name);
5474 case dest_tab:
5475 // include t: with the name, easier to execute that way
5476 return generate_STORE(cctx, ISN_STORET, 0, name);
5477 case dest_env:
5478 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5479 case dest_reg:
5480 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5481 case dest_vimvar:
5482 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5483 case dest_script:
5484 if (scriptvar_idx < 0)
5485 {
5486 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005487 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005488
Bram Moolenaar41d61962020-12-06 20:12:43 +01005489 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005490 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5491 scriptvar_sid, type);
5492 if (name_s != name)
5493 vim_free(name_s);
5494 return r;
5495 }
5496 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5497 scriptvar_sid, scriptvar_idx, type);
5498 case dest_local:
5499 case dest_expr:
5500 // cannot happen
5501 break;
5502 }
5503 return FAIL;
5504}
5505
Bram Moolenaar752fc692021-01-04 21:57:11 +01005506 static int
5507is_decl_command(int cmdidx)
5508{
5509 return cmdidx == CMD_let || cmdidx == CMD_var
5510 || cmdidx == CMD_final || cmdidx == CMD_const;
5511}
5512
5513/*
5514 * Figure out the LHS type and other properties for an assignment or one item
5515 * of ":unlet" with an index.
5516 * Returns OK or FAIL.
5517 */
5518 static int
5519compile_lhs(
5520 char_u *var_start,
5521 lhs_T *lhs,
5522 int cmdidx,
5523 int heredoc,
5524 int oplen,
5525 cctx_T *cctx)
5526{
5527 char_u *var_end;
5528 int is_decl = is_decl_command(cmdidx);
5529
5530 CLEAR_POINTER(lhs);
5531 lhs->lhs_dest = dest_local;
5532 lhs->lhs_vimvaridx = -1;
5533 lhs->lhs_scriptvar_idx = -1;
5534
5535 // "dest_end" is the end of the destination, including "[expr]" or
5536 // ".name".
5537 // "var_end" is the end of the variable/option/etc. name.
5538 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
5539 if (*var_start == '@')
5540 var_end = var_start + 2;
5541 else
5542 {
5543 // skip over the leading "&", "&l:", "&g:" and "$"
5544 var_end = skip_option_env_lead(var_start);
5545 var_end = to_name_end(var_end, TRUE);
5546 }
5547
5548 // "a: type" is declaring variable "a" with a type, not dict "a:".
5549 if (is_decl && lhs->lhs_dest_end == var_start + 2
5550 && lhs->lhs_dest_end[-1] == ':')
5551 --lhs->lhs_dest_end;
5552 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5553 --var_end;
5554
5555 // compute the length of the destination without "[expr]" or ".name"
5556 lhs->lhs_varlen = var_end - var_start;
5557 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
5558 if (lhs->lhs_name == NULL)
5559 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01005560
5561 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
5562 // Something follows after the variable: "var[idx]" or "var.key".
5563 lhs->lhs_has_index = TRUE;
5564
Bram Moolenaar752fc692021-01-04 21:57:11 +01005565 if (heredoc)
5566 lhs->lhs_type = &t_list_string;
5567 else
5568 lhs->lhs_type = &t_any;
5569
5570 if (cctx->ctx_skip != SKIP_YES)
5571 {
5572 int declare_error = FALSE;
5573
5574 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
5575 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
5576 &lhs->lhs_type, cctx) == FAIL)
5577 return FAIL;
5578 if (lhs->lhs_dest != dest_local)
5579 {
5580 // Specific kind of variable recognized.
5581 declare_error = is_decl;
5582 }
5583 else
5584 {
5585 int idx;
5586
5587 // No specific kind of variable recognized, just a name.
5588 for (idx = 0; reserved[idx] != NULL; ++idx)
5589 if (STRCMP(reserved[idx], lhs->lhs_name) == 0)
5590 {
5591 semsg(_(e_cannot_use_reserved_name), lhs->lhs_name);
5592 return FAIL;
5593 }
5594
5595
5596 if (lookup_local(var_start, lhs->lhs_varlen,
5597 &lhs->lhs_local_lvar, cctx) == OK)
5598 lhs->lhs_lvar = &lhs->lhs_local_lvar;
5599 else
5600 {
5601 CLEAR_FIELD(lhs->lhs_arg_lvar);
5602 if (arg_exists(var_start, lhs->lhs_varlen,
5603 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
5604 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
5605 {
5606 if (is_decl)
5607 {
5608 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
5609 return FAIL;
5610 }
5611 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
5612 }
5613 }
5614 if (lhs->lhs_lvar != NULL)
5615 {
5616 if (is_decl)
5617 {
5618 semsg(_(e_variable_already_declared), lhs->lhs_name);
5619 return FAIL;
5620 }
5621 }
5622 else
5623 {
5624 int script_namespace = lhs->lhs_varlen > 1
5625 && STRNCMP(var_start, "s:", 2) == 0;
5626 int script_var = (script_namespace
5627 ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
5628 FALSE, cctx)
5629 : script_var_exists(var_start, lhs->lhs_varlen,
5630 TRUE, cctx)) == OK;
5631 imported_T *import =
5632 find_imported(var_start, lhs->lhs_varlen, cctx);
5633
5634 if (script_namespace || script_var || import != NULL)
5635 {
5636 char_u *rawname = lhs->lhs_name
5637 + (lhs->lhs_name[1] == ':' ? 2 : 0);
5638
5639 if (is_decl)
5640 {
5641 if (script_namespace)
5642 semsg(_(e_cannot_declare_script_variable_in_function),
5643 lhs->lhs_name);
5644 else
5645 semsg(_(e_variable_already_declared_in_script),
5646 lhs->lhs_name);
5647 return FAIL;
5648 }
5649 else if (cctx->ctx_ufunc->uf_script_ctx_version
5650 == SCRIPT_VERSION_VIM9
5651 && script_namespace
5652 && !script_var && import == NULL)
5653 {
5654 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5655 return FAIL;
5656 }
5657
5658 lhs->lhs_dest = dest_script;
5659
5660 // existing script-local variables should have a type
5661 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
5662 if (import != NULL)
5663 lhs->lhs_scriptvar_sid = import->imp_sid;
5664 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
5665 {
Bram Moolenaar08251752021-01-11 21:20:18 +01005666 // Check writable only when no index follows.
Bram Moolenaar752fc692021-01-04 21:57:11 +01005667 lhs->lhs_scriptvar_idx = get_script_item_idx(
Bram Moolenaar08251752021-01-11 21:20:18 +01005668 lhs->lhs_scriptvar_sid, rawname,
5669 lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
5670 cctx);
Bram Moolenaar752fc692021-01-04 21:57:11 +01005671 if (lhs->lhs_scriptvar_idx >= 0)
5672 {
5673 scriptitem_T *si = SCRIPT_ITEM(
5674 lhs->lhs_scriptvar_sid);
5675 svar_T *sv =
5676 ((svar_T *)si->sn_var_vals.ga_data)
5677 + lhs->lhs_scriptvar_idx;
5678 lhs->lhs_type = sv->sv_type;
5679 }
5680 }
5681 }
5682 else if (check_defined(var_start, lhs->lhs_varlen, cctx)
5683 == FAIL)
5684 return FAIL;
5685 }
5686 }
5687
5688 if (declare_error)
5689 {
5690 vim9_declare_error(lhs->lhs_name);
5691 return FAIL;
5692 }
5693 }
5694
5695 // handle "a:name" as a name, not index "name" on "a"
5696 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
5697 var_end = lhs->lhs_dest_end;
5698
5699 if (lhs->lhs_dest != dest_option)
5700 {
5701 if (is_decl && *var_end == ':')
5702 {
5703 char_u *p;
5704
5705 // parse optional type: "let var: type = expr"
5706 if (!VIM_ISWHITE(var_end[1]))
5707 {
5708 semsg(_(e_white_space_required_after_str), ":");
5709 return FAIL;
5710 }
5711 p = skipwhite(var_end + 1);
5712 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
5713 if (lhs->lhs_type == NULL)
5714 return FAIL;
5715 lhs->lhs_has_type = TRUE;
5716 }
5717 else if (lhs->lhs_lvar != NULL)
5718 lhs->lhs_type = lhs->lhs_lvar->lv_type;
5719 }
5720
5721 if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global
5722 && lhs->lhs_type->tt_type != VAR_STRING
5723 && lhs->lhs_type->tt_type != VAR_ANY)
5724 {
5725 emsg(_(e_can_only_concatenate_to_string));
5726 return FAIL;
5727 }
5728
5729 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
5730 && cctx->ctx_skip != SKIP_YES)
5731 {
5732 if (oplen > 1 && !heredoc)
5733 {
5734 // +=, /=, etc. require an existing variable
5735 semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
5736 return FAIL;
5737 }
5738 if (!is_decl)
5739 {
5740 semsg(_(e_unknown_variable_str), lhs->lhs_name);
5741 return FAIL;
5742 }
5743
5744 // new local variable
5745 if ((lhs->lhs_type->tt_type == VAR_FUNC
5746 || lhs->lhs_type->tt_type == VAR_PARTIAL)
5747 && var_wrong_func_name(lhs->lhs_name, TRUE))
5748 return FAIL;
5749 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
5750 cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
5751 if (lhs->lhs_lvar == NULL)
5752 return FAIL;
5753 lhs->lhs_new_local = TRUE;
5754 }
5755
5756 lhs->lhs_member_type = lhs->lhs_type;
Bram Moolenaar08251752021-01-11 21:20:18 +01005757 if (lhs->lhs_has_index)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005758 {
5759 // Something follows after the variable: "var[idx]" or "var.key".
5760 // TODO: should we also handle "->func()" here?
5761 if (is_decl)
5762 {
5763 emsg(_(e_cannot_use_index_when_declaring_variable));
5764 return FAIL;
5765 }
5766
5767 if (var_start[lhs->lhs_varlen] == '['
5768 || var_start[lhs->lhs_varlen] == '.')
5769 {
5770 char_u *after = var_start + lhs->lhs_varlen;
5771 char_u *p;
5772
5773 // Only the last index is used below, if there are others
5774 // before it generate code for the expression. Thus for
5775 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5776 for (;;)
5777 {
5778 p = skip_index(after);
5779 if (*p != '[' && *p != '.')
5780 break;
5781 after = p;
5782 }
5783 if (after > var_start + lhs->lhs_varlen)
5784 {
5785 lhs->lhs_varlen = after - var_start;
5786 lhs->lhs_dest = dest_expr;
5787 // We don't know the type before evaluating the expression,
5788 // use "any" until then.
5789 lhs->lhs_type = &t_any;
5790 }
5791
Bram Moolenaar752fc692021-01-04 21:57:11 +01005792 if (lhs->lhs_type->tt_member == NULL)
5793 lhs->lhs_member_type = &t_any;
5794 else
5795 lhs->lhs_member_type = lhs->lhs_type->tt_member;
5796 }
5797 else
5798 {
5799 semsg("Not supported yet: %s", var_start);
5800 return FAIL;
5801 }
5802 }
5803 return OK;
5804}
5805
5806/*
5807 * Assignment to a list or dict member, or ":unlet" for the item, using the
5808 * information in "lhs".
5809 * Returns OK or FAIL.
5810 */
5811 static int
5812compile_assign_unlet(
5813 char_u *var_start,
5814 lhs_T *lhs,
5815 int is_assign,
5816 type_T *rhs_type,
5817 cctx_T *cctx)
5818{
5819 char_u *p;
5820 int r;
5821 vartype_T dest_type;
5822 size_t varlen = lhs->lhs_varlen;
5823 garray_T *stack = &cctx->ctx_type_stack;
5824
5825 // Compile the "idx" in "var[idx]" or "key" in "var.key".
5826 p = var_start + varlen;
5827 if (*p == '[')
5828 {
5829 p = skipwhite(p + 1);
5830 r = compile_expr0(&p, cctx);
5831 if (r == OK && *skipwhite(p) != ']')
5832 {
5833 // this should not happen
5834 emsg(_(e_missbrac));
5835 r = FAIL;
5836 }
5837 }
5838 else // if (*p == '.')
5839 {
5840 char_u *key_end = to_name_end(p + 1, TRUE);
5841 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5842
5843 r = generate_PUSHS(cctx, key);
5844 }
5845 if (r == FAIL)
5846 return FAIL;
5847
5848 if (lhs->lhs_type == &t_any)
5849 {
5850 // Index on variable of unknown type: check at runtime.
5851 dest_type = VAR_ANY;
5852 }
5853 else
5854 {
5855 dest_type = lhs->lhs_type->tt_type;
5856 if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
5857 return FAIL;
5858 if (dest_type == VAR_LIST
Bram Moolenaarf30a14d2021-01-17 21:51:24 +01005859 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
5860 &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01005861 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005862 }
5863
5864 // Load the dict or list. On the stack we then have:
5865 // - value (for assignment, not for :unlet)
5866 // - index
5867 // - variable
5868 if (lhs->lhs_dest == dest_expr)
5869 {
5870 int c = var_start[varlen];
5871
5872 // Evaluate "ll[expr]" of "ll[expr][idx]"
5873 p = var_start;
5874 var_start[varlen] = NUL;
5875 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5876 {
5877 // this should not happen
5878 emsg(_(e_missbrac));
5879 return FAIL;
5880 }
5881 var_start[varlen] = c;
5882
5883 lhs->lhs_type = stack->ga_len == 0 ? &t_void
5884 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5885 // now we can properly check the type
5886 if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void
Bram Moolenaar351ead02021-01-16 16:07:01 +01005887 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01005888 FALSE, FALSE) == FAIL)
5889 return FAIL;
5890 }
5891 else
5892 generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
5893 lhs->lhs_lvar, lhs->lhs_type);
5894
5895 if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY)
5896 {
5897 if (is_assign)
5898 {
5899 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5900
5901 if (isn == NULL)
5902 return FAIL;
5903 isn->isn_arg.vartype = dest_type;
5904 }
5905 else
5906 {
5907 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
5908 return FAIL;
5909 }
5910 }
5911 else
5912 {
5913 emsg(_(e_indexable_type_required));
5914 return FAIL;
5915 }
5916
5917 return OK;
5918}
5919
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005920/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005921 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005922 * "let name"
5923 * "var name = expr"
5924 * "final name = expr"
5925 * "const name = expr"
5926 * "name = expr"
5927 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005928 * Return NULL for an error.
5929 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005930 */
5931 static char_u *
5932compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5933{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005934 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005935 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005936 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005937 char_u *ret = NULL;
5938 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005939 int var_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005942 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005943 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005944 int oplen = 0;
5945 int heredoc = FALSE;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005946 type_T *rhs_type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 char_u *sp;
Bram Moolenaar752fc692021-01-04 21:57:11 +01005948 int is_decl = is_decl_command(cmdidx);
5949 lhs_T lhs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005950
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005951 // Skip over the "var" or "[var, var]" to get to any "=".
5952 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5953 if (p == NULL)
5954 return *arg == '[' ? arg : NULL;
5955
5956 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005957 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005958 // TODO: should we allow this, and figure out type inference from list
5959 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005960 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 return NULL;
5962 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01005963 lhs.lhs_name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005965 sp = p;
5966 p = skipwhite(p);
5967 op = p;
5968 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005969
5970 if (var_count > 0 && oplen == 0)
5971 // can be something like "[1, 2]->func()"
5972 return arg;
5973
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005974 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005976 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005977 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005978 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 if (heredoc)
5981 {
5982 list_T *l;
5983 listitem_T *li;
5984
5985 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005986 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005988 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005989 if (l == NULL)
5990 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991
Bram Moolenaar078269b2020-09-21 20:35:55 +02005992 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005993 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005994 // Push each line and the create the list.
5995 FOR_ALL_LIST_ITEMS(l, li)
5996 {
5997 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5998 li->li_tv.vval.v_string = NULL;
5999 }
6000 generate_NEWLIST(cctx, l->lv_len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 list_free(l);
6003 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006004 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006006 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006007 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006008 char_u *wp;
6009
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006010 // for "[var, var] = expr" evaluate the expression here, loop over the
6011 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006012 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02006013
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006014 wp = op + oplen;
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01006015 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01006016 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006017 if (compile_expr0(&p, cctx) == FAIL)
6018 return NULL;
6019 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006021 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006023 type_T *stacktype;
6024
Bram Moolenaarec5929d2020-04-07 20:53:39 +02006025 stacktype = stack->ga_len == 0 ? &t_void
6026 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006029 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006030 goto theend;
6031 }
Bram Moolenaar351ead02021-01-16 16:07:01 +01006032 if (need_type(stacktype, &t_list_any, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006033 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006034 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02006035 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02006036 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
6037 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006038 if (stacktype->tt_member != NULL)
6039 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006040 }
6041 }
6042
6043 /*
6044 * Loop over variables in "[var, var] = expr".
6045 * For "var = expr" and "let var: type" this is done only once.
6046 */
6047 if (var_count > 0)
6048 var_start = skipwhite(arg + 1); // skip over the "["
6049 else
6050 var_start = arg;
6051 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
6052 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006053 int instr_count = -1;
6054
Bram Moolenaar752fc692021-01-04 21:57:11 +01006055 vim_free(lhs.lhs_name);
6056
6057 /*
6058 * Figure out the LHS type and other properties.
6059 */
6060 if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
6061 goto theend;
6062
6063 if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar)
Bram Moolenaar65821722020-08-02 18:58:54 +02006064 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006065 semsg(_(e_cannot_assign_to_argument), lhs.lhs_name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006066 goto theend;
6067 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006068 if (!is_decl && lhs.lhs_lvar != NULL
6069 && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006070 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006071 semsg(_(e_cannot_assign_to_constant), lhs.lhs_name);
Bram Moolenaardbeecb22020-09-14 18:15:09 +02006072 goto theend;
6073 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006074
6075 if (!heredoc)
6076 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006077 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006078 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006079 if (oplen > 0 && var_count == 0)
6080 {
6081 // skip over the "=" and the expression
6082 p = skipwhite(op + oplen);
6083 compile_expr0(&p, cctx);
6084 }
6085 }
6086 else if (oplen > 0)
6087 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006088 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006089 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006090
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006091 // For "var = expr" evaluate the expression.
6092 if (var_count == 0)
6093 {
6094 int r;
6095
6096 // for "+=", "*=", "..=" etc. first load the current value
6097 if (*op != '=')
6098 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006099 generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name,
6100 lhs.lhs_lvar, lhs.lhs_type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006101
Bram Moolenaar752fc692021-01-04 21:57:11 +01006102 if (lhs.lhs_has_index)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006103 {
6104 // TODO: get member from list or dict
6105 emsg("Index with operation not supported yet");
6106 goto theend;
6107 }
6108 }
6109
6110 // Compile the expression. Temporarily hide the new local
6111 // variable here, it is not available to this expression.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006112 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006113 --cctx->ctx_locals.ga_len;
6114 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006115 wp = op + oplen;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006116 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006117 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006118 if (lhs.lhs_new_local)
Bram Moolenaar21e51222020-12-04 12:43:29 +01006119 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01006120 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01006121 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006122 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar752fc692021-01-04 21:57:11 +01006123 if (lhs.lhs_new_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006124 ++cctx->ctx_locals.ga_len;
6125 if (r == FAIL)
6126 goto theend;
6127 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02006128 else if (semicolon && var_idx == var_count - 1)
6129 {
6130 // For "[var; var] = expr" get the rest of the list
6131 if (generate_SLICE(cctx, var_count - 1) == FAIL)
6132 goto theend;
6133 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006134 else
6135 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006136 // For "[var, var] = expr" get the "var_idx" item from the
6137 // list.
6138 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01006139 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006140 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006141
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006142 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02006143 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar752fc692021-01-04 21:57:11 +01006144 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006145 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006146 if ((rhs_type->tt_type == VAR_FUNC
6147 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006148 && var_wrong_func_name(lhs.lhs_name, TRUE))
Bram Moolenaar0f769812020-09-12 18:32:34 +02006149 goto theend;
6150
Bram Moolenaar752fc692021-01-04 21:57:11 +01006151 if (lhs.lhs_new_local && !lhs.lhs_has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006152 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006153 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006154 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006155 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006156 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006157 }
6158 else
6159 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02006160 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006161 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006162 if (rhs_type == &t_list_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006163 lhs.lhs_lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006164 else if (rhs_type == &t_dict_empty)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006165 lhs.lhs_lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006166 else if (rhs_type == &t_unknown)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006167 lhs.lhs_lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006168 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006169 lhs.lhs_lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006170 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006171 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006172 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006173 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006174 type_T *use_type = lhs.lhs_lvar->lv_type;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006175
Bram Moolenaar71abe482020-09-14 22:28:30 +02006176 // without operator check type here, otherwise below
Bram Moolenaar752fc692021-01-04 21:57:11 +01006177 if (lhs.lhs_has_index)
6178 use_type = lhs.lhs_member_type;
Bram Moolenaar351ead02021-01-16 16:07:01 +01006179 if (need_type(rhs_type, use_type, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006180 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006181 goto theend;
6182 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006183 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006184 else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type,
Bram Moolenaar351ead02021-01-16 16:07:01 +01006185 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006186 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006187 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006188 else if (cmdidx == CMD_final)
6189 {
6190 emsg(_(e_final_requires_a_value));
6191 goto theend;
6192 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006193 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006194 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006195 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006196 goto theend;
6197 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006198 else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006199 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02006200 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006201 goto theend;
6202 }
6203 else
6204 {
6205 // variables are always initialized
6206 if (ga_grow(instr, 1) == FAIL)
6207 goto theend;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006208 switch (lhs.lhs_member_type->tt_type)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006209 {
6210 case VAR_BOOL:
6211 generate_PUSHBOOL(cctx, VVAL_FALSE);
6212 break;
6213 case VAR_FLOAT:
6214#ifdef FEAT_FLOAT
6215 generate_PUSHF(cctx, 0.0);
6216#endif
6217 break;
6218 case VAR_STRING:
6219 generate_PUSHS(cctx, NULL);
6220 break;
6221 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02006222 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006223 break;
6224 case VAR_FUNC:
6225 generate_PUSHFUNC(cctx, NULL, &t_func_void);
6226 break;
6227 case VAR_LIST:
6228 generate_NEWLIST(cctx, 0);
6229 break;
6230 case VAR_DICT:
6231 generate_NEWDICT(cctx, 0);
6232 break;
6233 case VAR_JOB:
6234 generate_PUSHJOB(cctx, NULL);
6235 break;
6236 case VAR_CHANNEL:
6237 generate_PUSHCHANNEL(cctx, NULL);
6238 break;
6239 case VAR_NUMBER:
6240 case VAR_UNKNOWN:
6241 case VAR_ANY:
6242 case VAR_PARTIAL:
6243 case VAR_VOID:
6244 case VAR_SPECIAL: // cannot happen
6245 generate_PUSHNR(cctx, 0);
6246 break;
6247 }
6248 }
6249 if (var_count == 0)
6250 end = p;
6251 }
6252
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006253 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02006254 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02006255 break;
6256
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006257 if (oplen > 0 && *op != '=')
6258 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006259 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006260 type_T *stacktype;
6261
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006262 if (*op == '.')
6263 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006264 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006265 expected = lhs.lhs_member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006266 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006267 if (
6268#ifdef FEAT_FLOAT
6269 // If variable is float operation with number is OK.
6270 !(expected == &t_float && stacktype == &t_number) &&
6271#endif
Bram Moolenaar351ead02021-01-16 16:07:01 +01006272 need_type(stacktype, expected, -1, 0, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006273 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006274 goto theend;
6275
6276 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006277 {
6278 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
6279 goto theend;
6280 }
6281 else if (*op == '+')
6282 {
6283 if (generate_add_instr(cctx,
Bram Moolenaar752fc692021-01-04 21:57:11 +01006284 operator_type(lhs.lhs_member_type, stacktype),
6285 lhs.lhs_member_type, stacktype) == FAIL)
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02006286 goto theend;
6287 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02006288 else if (generate_two_op(cctx, op) == FAIL)
6289 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291
Bram Moolenaar752fc692021-01-04 21:57:11 +01006292 if (lhs.lhs_has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006293 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006294 // Use the info in "lhs" to store the value at the index in the
6295 // list or dict.
6296 if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
6297 == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006298 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006299 }
6300 else
6301 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006302 if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
6303 || lhs.lhs_dest == dest_local))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006304 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006305 generate_LOCKCONST(cctx);
6306
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006307 if (is_decl
Bram Moolenaar752fc692021-01-04 21:57:11 +01006308 && (lhs.lhs_type->tt_type == VAR_DICT
6309 || lhs.lhs_type->tt_type == VAR_LIST)
6310 && lhs.lhs_type->tt_member != NULL
6311 && lhs.lhs_type->tt_member != &t_any
6312 && lhs.lhs_type->tt_member != &t_unknown)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006313 // Set the type in the list or dict, so that it can be checked,
6314 // also in legacy script.
Bram Moolenaar752fc692021-01-04 21:57:11 +01006315 generate_SETTYPE(cctx, lhs.lhs_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01006316
Bram Moolenaar752fc692021-01-04 21:57:11 +01006317 if (lhs.lhs_dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006318 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006319 if (generate_store_var(cctx, lhs.lhs_dest,
6320 lhs.lhs_opt_flags, lhs.lhs_vimvaridx,
6321 lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid,
6322 lhs.lhs_type, lhs.lhs_name) == FAIL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006323 goto theend;
6324 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01006325 else if (lhs.lhs_lvar != NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006326 {
6327 isn_T *isn = ((isn_T *)instr->ga_data)
6328 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006329
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006330 // optimization: turn "var = 123" from ISN_PUSHNR +
6331 // ISN_STORE into ISN_STORENR
Bram Moolenaarab360522021-01-10 14:02:28 +01006332 if (lhs.lhs_lvar->lv_from_outer == 0
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006333 && instr->ga_len == instr_count + 1
6334 && isn->isn_type == ISN_PUSHNR)
6335 {
6336 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006337
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006338 isn->isn_type = ISN_STORENR;
Bram Moolenaar752fc692021-01-04 21:57:11 +01006339 isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006340 isn->isn_arg.storenr.stnr_val = val;
6341 if (stack->ga_len > 0)
6342 --stack->ga_len;
6343 }
Bram Moolenaarab360522021-01-10 14:02:28 +01006344 else if (lhs.lhs_lvar->lv_from_outer > 0)
6345 generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx,
6346 lhs.lhs_lvar->lv_from_outer);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006347 else
Bram Moolenaar752fc692021-01-04 21:57:11 +01006348 generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006349 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006350 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006351
6352 if (var_idx + 1 < var_count)
Bram Moolenaar752fc692021-01-04 21:57:11 +01006353 var_start = skipwhite(lhs.lhs_dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006354 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006355
6356 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006357 if (var_count > 0 && !semicolon)
6358 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006359 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006360 goto theend;
6361 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006362
Bram Moolenaarb2097502020-07-19 17:17:02 +02006363 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006364
6365theend:
Bram Moolenaar752fc692021-01-04 21:57:11 +01006366 vim_free(lhs.lhs_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 return ret;
6368}
6369
6370/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01006371 * Check for an assignment at "eap->cmd", compile it if found.
6372 * Return NOTDONE if there is none, FAIL for failure, OK if done.
6373 */
6374 static int
6375may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
6376{
6377 char_u *pskip;
6378 char_u *p;
6379
6380 // Assuming the command starts with a variable or function name,
6381 // find what follows.
6382 // Skip over "var.member", "var[idx]" and the like.
6383 // Also "&opt = val", "$ENV = val" and "@r = val".
6384 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
6385 ? eap->cmd + 1 : eap->cmd;
6386 p = to_name_end(pskip, TRUE);
6387 if (p > eap->cmd && *p != NUL)
6388 {
6389 char_u *var_end;
6390 int oplen;
6391 int heredoc;
6392
6393 if (eap->cmd[0] == '@')
6394 var_end = eap->cmd + 2;
6395 else
6396 var_end = find_name_end(pskip, NULL, NULL,
6397 FNE_CHECK_START | FNE_INCL_BR);
6398 oplen = assignment_len(skipwhite(var_end), &heredoc);
6399 if (oplen > 0)
6400 {
6401 size_t len = p - eap->cmd;
6402
6403 // Recognize an assignment if we recognize the variable
6404 // name:
6405 // "g:var = expr"
6406 // "local = expr" where "local" is a local var.
6407 // "script = expr" where "script" is a script-local var.
6408 // "import = expr" where "import" is an imported var
6409 // "&opt = expr"
6410 // "$ENV = expr"
6411 // "@r = expr"
6412 if (*eap->cmd == '&'
6413 || *eap->cmd == '$'
6414 || *eap->cmd == '@'
6415 || ((len) > 2 && eap->cmd[1] == ':')
6416 || lookup_local(eap->cmd, len, NULL, cctx) == OK
6417 || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK
6418 || script_var_exists(eap->cmd, len, FALSE, cctx) == OK
6419 || find_imported(eap->cmd, len, cctx) != NULL)
6420 {
6421 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6422 if (*line == NULL || *line == eap->cmd)
6423 return FAIL;
6424 return OK;
6425 }
6426 }
6427 }
6428
6429 if (*eap->cmd == '[')
6430 {
6431 // [var, var] = expr
6432 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
6433 if (*line == NULL)
6434 return FAIL;
6435 if (*line != eap->cmd)
6436 return OK;
6437 }
6438 return NOTDONE;
6439}
6440
6441/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006442 * Check if "name" can be "unlet".
6443 */
6444 int
6445check_vim9_unlet(char_u *name)
6446{
6447 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6448 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006449 // "unlet s:var" is allowed in legacy script.
6450 if (*name == 's' && !script_is_vim9())
6451 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006452 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006453 return FAIL;
6454 }
6455 return OK;
6456}
6457
6458/*
6459 * Callback passed to ex_unletlock().
6460 */
6461 static int
6462compile_unlet(
6463 lval_T *lvp,
6464 char_u *name_end,
6465 exarg_T *eap,
6466 int deep UNUSED,
6467 void *coookie)
6468{
Bram Moolenaar752fc692021-01-04 21:57:11 +01006469 cctx_T *cctx = coookie;
6470 char_u *p = lvp->ll_name;
6471 int cc = *name_end;
6472 int ret = OK;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006473
Bram Moolenaar752fc692021-01-04 21:57:11 +01006474 if (cctx->ctx_skip == SKIP_YES)
6475 return OK;
6476
6477 *name_end = NUL;
6478 if (*p == '$')
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006479 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006480 // :unlet $ENV_VAR
6481 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6482 }
6483 else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
6484 {
6485 lhs_T lhs;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006486
Bram Moolenaar752fc692021-01-04 21:57:11 +01006487 // This is similar to assigning: lookup the list/dict, compile the
6488 // idx/key. Then instead of storing the value unlet the item.
6489 // unlet {list}[idx]
6490 // unlet {dict}[key] dict.key
6491 //
6492 // Figure out the LHS type and other properties.
6493 //
6494 ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
6495
6496 // : unlet an indexed item
6497 if (!lhs.lhs_has_index)
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006498 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01006499 iemsg("called compile_lhs() without an index");
6500 ret = FAIL;
6501 }
6502 else
6503 {
6504 // Use the info in "lhs" to unlet the item at the index in the
6505 // list or dict.
6506 ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006507 }
6508
Bram Moolenaar752fc692021-01-04 21:57:11 +01006509 vim_free(lhs.lhs_name);
6510 }
6511 else if (check_vim9_unlet(p) == FAIL)
6512 {
6513 ret = FAIL;
6514 }
6515 else
6516 {
6517 // Normal name. Only supports g:, w:, t: and b: namespaces.
6518 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006519 }
6520
Bram Moolenaar752fc692021-01-04 21:57:11 +01006521 *name_end = cc;
6522 return ret;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006523}
6524
6525/*
6526 * compile "unlet var", "lock var" and "unlock var"
6527 * "arg" points to "var".
6528 */
6529 static char_u *
6530compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6531{
6532 char_u *p = arg;
6533
6534 if (eap->cmdidx != CMD_unlet)
6535 {
6536 emsg("Sorry, :lock and unlock not implemented yet");
6537 return NULL;
6538 }
6539
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01006540 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
6541 compile_unlet, cctx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006542 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6543}
6544
6545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 * Compile an :import command.
6547 */
6548 static char_u *
6549compile_import(char_u *arg, cctx_T *cctx)
6550{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006551 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006552}
6553
6554/*
6555 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6556 */
6557 static int
6558compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6559{
6560 garray_T *instr = &cctx->ctx_instr;
6561 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6562
6563 if (endlabel == NULL)
6564 return FAIL;
6565 endlabel->el_next = *el;
6566 *el = endlabel;
6567 endlabel->el_end_label = instr->ga_len;
6568
6569 generate_JUMP(cctx, when, 0);
6570 return OK;
6571}
6572
6573 static void
6574compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6575{
6576 garray_T *instr = &cctx->ctx_instr;
6577
6578 while (*el != NULL)
6579 {
6580 endlabel_T *cur = (*el);
6581 isn_T *isn;
6582
6583 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6584 isn->isn_arg.jump.jump_where = instr->ga_len;
6585 *el = cur->el_next;
6586 vim_free(cur);
6587 }
6588}
6589
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006590 static void
6591compile_free_jump_to_end(endlabel_T **el)
6592{
6593 while (*el != NULL)
6594 {
6595 endlabel_T *cur = (*el);
6596
6597 *el = cur->el_next;
6598 vim_free(cur);
6599 }
6600}
6601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602/*
6603 * Create a new scope and set up the generic items.
6604 */
6605 static scope_T *
6606new_scope(cctx_T *cctx, scopetype_T type)
6607{
6608 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6609
6610 if (scope == NULL)
6611 return NULL;
6612 scope->se_outer = cctx->ctx_scope;
6613 cctx->ctx_scope = scope;
6614 scope->se_type = type;
6615 scope->se_local_count = cctx->ctx_locals.ga_len;
6616 return scope;
6617}
6618
6619/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006620 * Free the current scope and go back to the outer scope.
6621 */
6622 static void
6623drop_scope(cctx_T *cctx)
6624{
6625 scope_T *scope = cctx->ctx_scope;
6626
6627 if (scope == NULL)
6628 {
6629 iemsg("calling drop_scope() without a scope");
6630 return;
6631 }
6632 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006633 switch (scope->se_type)
6634 {
6635 case IF_SCOPE:
6636 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6637 case FOR_SCOPE:
6638 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6639 case WHILE_SCOPE:
6640 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6641 case TRY_SCOPE:
6642 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6643 case NO_SCOPE:
6644 case BLOCK_SCOPE:
6645 break;
6646 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006647 vim_free(scope);
6648}
6649
6650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006651 * compile "if expr"
6652 *
6653 * "if expr" Produces instructions:
6654 * EVAL expr Push result of "expr"
6655 * JUMP_IF_FALSE end
6656 * ... body ...
6657 * end:
6658 *
6659 * "if expr | else" Produces instructions:
6660 * EVAL expr Push result of "expr"
6661 * JUMP_IF_FALSE else
6662 * ... body ...
6663 * JUMP_ALWAYS end
6664 * else:
6665 * ... body ...
6666 * end:
6667 *
6668 * "if expr1 | elseif expr2 | else" Produces instructions:
6669 * EVAL expr Push result of "expr"
6670 * JUMP_IF_FALSE elseif
6671 * ... body ...
6672 * JUMP_ALWAYS end
6673 * elseif:
6674 * EVAL expr Push result of "expr"
6675 * JUMP_IF_FALSE else
6676 * ... body ...
6677 * JUMP_ALWAYS end
6678 * else:
6679 * ... body ...
6680 * end:
6681 */
6682 static char_u *
6683compile_if(char_u *arg, cctx_T *cctx)
6684{
6685 char_u *p = arg;
6686 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006687 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006688 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006689 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006690 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006691
Bram Moolenaara5565e42020-05-09 15:44:01 +02006692 CLEAR_FIELD(ppconst);
6693 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006694 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006695 clear_ppconst(&ppconst);
6696 return NULL;
6697 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006698 if (cctx->ctx_skip == SKIP_YES)
6699 clear_ppconst(&ppconst);
6700 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006701 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006702 int error = FALSE;
6703 int v;
6704
Bram Moolenaara5565e42020-05-09 15:44:01 +02006705 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006706 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006707 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006708 if (error)
6709 return NULL;
6710 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006711 }
6712 else
6713 {
6714 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006715 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006716 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006717 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006718 if (bool_on_stack(cctx) == FAIL)
6719 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006720 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006721
6722 scope = new_scope(cctx, IF_SCOPE);
6723 if (scope == NULL)
6724 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006725 scope->se_skip_save = skip_save;
6726 // "is_had_return" will be reset if any block does not end in :return
6727 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006728
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006729 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006730 {
6731 // "where" is set when ":elseif", "else" or ":endif" is found
6732 scope->se_u.se_if.is_if_label = instr->ga_len;
6733 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6734 }
6735 else
6736 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737
6738 return p;
6739}
6740
6741 static char_u *
6742compile_elseif(char_u *arg, cctx_T *cctx)
6743{
6744 char_u *p = arg;
6745 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006746 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006747 isn_T *isn;
6748 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006749 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006750 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006751
6752 if (scope == NULL || scope->se_type != IF_SCOPE)
6753 {
6754 emsg(_(e_elseif_without_if));
6755 return NULL;
6756 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006757 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006758 if (!cctx->ctx_had_return)
6759 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006760
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006761 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006762 {
6763 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006764 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006765 return NULL;
6766 // previous "if" or "elseif" jumps here
6767 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6768 isn->isn_arg.jump.jump_where = instr->ga_len;
6769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006771 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006772 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006773 if (cctx->ctx_skip == SKIP_YES)
6774 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006775 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006776 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006777 clear_ppconst(&ppconst);
6778 return NULL;
6779 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006780 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006781 if (scope->se_skip_save == SKIP_YES)
6782 clear_ppconst(&ppconst);
6783 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006784 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006785 int error = FALSE;
6786 int v;
6787
Bram Moolenaar7f141552020-05-09 17:35:53 +02006788 // The expression results in a constant.
6789 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006790 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6791 if (error)
6792 return NULL;
6793 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006794 clear_ppconst(&ppconst);
6795 scope->se_u.se_if.is_if_label = -1;
6796 }
6797 else
6798 {
6799 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006800 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006801 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006802 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006803 if (bool_on_stack(cctx) == FAIL)
6804 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006805
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006806 // "where" is set when ":elseif", "else" or ":endif" is found
6807 scope->se_u.se_if.is_if_label = instr->ga_len;
6808 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006810
6811 return p;
6812}
6813
6814 static char_u *
6815compile_else(char_u *arg, cctx_T *cctx)
6816{
6817 char_u *p = arg;
6818 garray_T *instr = &cctx->ctx_instr;
6819 isn_T *isn;
6820 scope_T *scope = cctx->ctx_scope;
6821
6822 if (scope == NULL || scope->se_type != IF_SCOPE)
6823 {
6824 emsg(_(e_else_without_if));
6825 return NULL;
6826 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006827 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006828 if (!cctx->ctx_had_return)
6829 scope->se_u.se_if.is_had_return = FALSE;
6830 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006831
Bram Moolenaarefd88552020-06-18 20:50:10 +02006832 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006833 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006834 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006835 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006836 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006837 if (!cctx->ctx_had_return
6838 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6839 JUMP_ALWAYS, cctx) == FAIL)
6840 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006841 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006842
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006843 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006844 {
6845 if (scope->se_u.se_if.is_if_label >= 0)
6846 {
6847 // previous "if" or "elseif" jumps here
6848 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6849 isn->isn_arg.jump.jump_where = instr->ga_len;
6850 scope->se_u.se_if.is_if_label = -1;
6851 }
6852 }
6853
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006854 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006855 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006857
6858 return p;
6859}
6860
6861 static char_u *
6862compile_endif(char_u *arg, cctx_T *cctx)
6863{
6864 scope_T *scope = cctx->ctx_scope;
6865 ifscope_T *ifscope;
6866 garray_T *instr = &cctx->ctx_instr;
6867 isn_T *isn;
6868
6869 if (scope == NULL || scope->se_type != IF_SCOPE)
6870 {
6871 emsg(_(e_endif_without_if));
6872 return NULL;
6873 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006874 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006875 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006876 if (!cctx->ctx_had_return)
6877 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006878
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006879 if (scope->se_u.se_if.is_if_label >= 0)
6880 {
6881 // previous "if" or "elseif" jumps here
6882 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6883 isn->isn_arg.jump.jump_where = instr->ga_len;
6884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006885 // Fill in the "end" label in jumps at the end of the blocks.
6886 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006887 cctx->ctx_skip = scope->se_skip_save;
6888
6889 // If all the blocks end in :return and there is an :else then the
6890 // had_return flag is set.
6891 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006892
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006893 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894 return arg;
6895}
6896
6897/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006898 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006899 *
6900 * Produces instructions:
6901 * PUSHNR -1
6902 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006903 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006904 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6905 * - if beyond end, jump to "end"
6906 * - otherwise get item from list and push it
6907 * STORE var Store item in "var"
6908 * ... body ...
6909 * JUMP top Jump back to repeat
6910 * end: DROP Drop the result of "expr"
6911 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006912 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6913 * UNPACK 2 Split item in 2
6914 * STORE var1 Store item in "var1"
6915 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006916 */
6917 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006918compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006919{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006920 char_u *arg;
6921 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006922 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006923 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006924 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006925 int var_count = 0;
6926 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 size_t varlen;
6928 garray_T *instr = &cctx->ctx_instr;
6929 garray_T *stack = &cctx->ctx_type_stack;
6930 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006931 lvar_T *loop_lvar; // loop iteration variable
6932 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006934 type_T *item_type = &t_any;
6935 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006936
Bram Moolenaar792f7862020-11-23 08:31:18 +01006937 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar036d0712021-01-17 20:23:38 +01006938 if (p == NULL)
6939 return NULL;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006940 if (var_count == 0)
6941 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006942
6943 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006944 wp = p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006945 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6946 return NULL;
6947 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006948 {
6949 emsg(_(e_missing_in));
6950 return NULL;
6951 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006952 wp = p + 2;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006953 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6954 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006956 scope = new_scope(cctx, FOR_SCOPE);
6957 if (scope == NULL)
6958 return NULL;
6959
Bram Moolenaar792f7862020-11-23 08:31:18 +01006960 // Reserve a variable to store the loop iteration counter and initialize it
6961 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006962 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6963 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006964 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006965 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006966 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006967 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006968 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006969 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006970
6971 // compile "expr", it remains on the stack until "endfor"
6972 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006973 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006974 {
6975 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006977 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006978 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006980 // Now that we know the type of "var", check that it is a list, now or at
6981 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006982 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar351ead02021-01-16 16:07:01 +01006983 if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006984 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006985 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 return NULL;
6987 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006988
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006989 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006990 {
6991 if (var_count == 1)
6992 item_type = vartype->tt_member;
6993 else if (vartype->tt_member->tt_type == VAR_LIST
6994 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6995 item_type = vartype->tt_member->tt_member;
6996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997
6998 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006999 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007000 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007001
Bram Moolenaar792f7862020-11-23 08:31:18 +01007002 arg = arg_start;
7003 if (var_count > 1)
7004 {
7005 generate_UNPACK(cctx, var_count, semicolon);
7006 arg = skipwhite(arg + 1); // skip white after '['
7007
7008 // the list item is replaced by a number of items
7009 if (ga_grow(stack, var_count - 1) == FAIL)
7010 {
7011 drop_scope(cctx);
7012 return NULL;
7013 }
7014 --stack->ga_len;
7015 for (idx = 0; idx < var_count; ++idx)
7016 {
7017 ((type_T **)stack->ga_data)[stack->ga_len] =
7018 (semicolon && idx == 0) ? vartype : item_type;
7019 ++stack->ga_len;
7020 }
7021 }
7022
7023 for (idx = 0; idx < var_count; ++idx)
7024 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007025 assign_dest_T dest = dest_local;
7026 int opt_flags = 0;
7027 int vimvaridx = -1;
7028 type_T *type = &t_any;
7029
7030 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007031 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007032 name = vim_strnsave(arg, varlen);
7033 if (name == NULL)
7034 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007035
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007036 // TODO: script var not supported?
7037 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
7038 &vimvaridx, &type, cctx) == FAIL)
7039 goto failed;
7040 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01007041 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007042 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
7043 0, 0, type, name) == FAIL)
7044 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01007045 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007046 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007047 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01007048 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007049 {
7050 semsg(_(e_variable_already_declared), arg);
7051 goto failed;
7052 }
7053
Bram Moolenaarea870692020-12-02 14:24:30 +01007054 if (STRNCMP(name, "s:", 2) == 0)
7055 {
7056 semsg(_(e_cannot_declare_script_variable_in_function), name);
7057 goto failed;
7058 }
7059
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007060 // Reserve a variable to store "var".
7061 // TODO: check for type
7062 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
7063 if (var_lvar == NULL)
7064 // out of memory or used as an argument
7065 goto failed;
7066
7067 if (semicolon && idx == var_count - 1)
7068 var_lvar->lv_type = vartype;
7069 else
7070 var_lvar->lv_type = item_type;
7071 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
7072 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01007073
Bram Moolenaar036d0712021-01-17 20:23:38 +01007074 if (*p == ':')
7075 p = skip_type(skipwhite(p + 1), FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007076 if (*p == ',' || *p == ';')
7077 ++p;
7078 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007079 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01007080 }
7081
7082 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01007083
7084failed:
7085 vim_free(name);
7086 drop_scope(cctx);
7087 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088}
7089
7090/*
7091 * compile "endfor"
7092 */
7093 static char_u *
7094compile_endfor(char_u *arg, cctx_T *cctx)
7095{
7096 garray_T *instr = &cctx->ctx_instr;
7097 scope_T *scope = cctx->ctx_scope;
7098 forscope_T *forscope;
7099 isn_T *isn;
7100
7101 if (scope == NULL || scope->se_type != FOR_SCOPE)
7102 {
7103 emsg(_(e_for));
7104 return NULL;
7105 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007106 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007107 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007108 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007109
7110 // At end of ":for" scope jump back to the FOR instruction.
7111 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
7112
7113 // Fill in the "end" label in the FOR statement so it can jump here
7114 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
7115 isn->isn_arg.forloop.for_end = instr->ga_len;
7116
7117 // Fill in the "end" label any BREAK statements
7118 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
7119
7120 // Below the ":for" scope drop the "expr" list from the stack.
7121 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
7122 return NULL;
7123
7124 vim_free(scope);
7125
7126 return arg;
7127}
7128
7129/*
7130 * compile "while expr"
7131 *
7132 * Produces instructions:
7133 * top: EVAL expr Push result of "expr"
7134 * JUMP_IF_FALSE end jump if false
7135 * ... body ...
7136 * JUMP top Jump back to repeat
7137 * end:
7138 *
7139 */
7140 static char_u *
7141compile_while(char_u *arg, cctx_T *cctx)
7142{
7143 char_u *p = arg;
7144 garray_T *instr = &cctx->ctx_instr;
7145 scope_T *scope;
7146
7147 scope = new_scope(cctx, WHILE_SCOPE);
7148 if (scope == NULL)
7149 return NULL;
7150
Bram Moolenaarb2049902021-01-24 12:53:53 +01007151 // "endwhile" jumps back here, one before when profiling
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007152 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007153#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007154 if (cctx->ctx_profiling && ((isn_T *)instr->ga_data)[instr->ga_len - 1]
7155 .isn_type == ISN_PROF_START)
7156 --scope->se_u.se_while.ws_top_label;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007157#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
7159 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02007160 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 return NULL;
7162
Bram Moolenaar13106602020-10-04 16:06:05 +02007163 if (bool_on_stack(cctx) == FAIL)
7164 return FAIL;
7165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007166 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007167 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007168 JUMP_IF_FALSE, cctx) == FAIL)
7169 return FAIL;
7170
7171 return p;
7172}
7173
7174/*
7175 * compile "endwhile"
7176 */
7177 static char_u *
7178compile_endwhile(char_u *arg, cctx_T *cctx)
7179{
7180 scope_T *scope = cctx->ctx_scope;
7181
7182 if (scope == NULL || scope->se_type != WHILE_SCOPE)
7183 {
7184 emsg(_(e_while));
7185 return NULL;
7186 }
7187 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007188 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007189
Bram Moolenaarf002a412021-01-24 13:34:18 +01007190#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007191 // count the endwhile before jumping
7192 may_generate_prof_end(cctx, cctx->ctx_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01007193#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01007194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007195 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007196 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007197
7198 // Fill in the "end" label in the WHILE statement so it can jump here.
7199 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007200 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201
7202 vim_free(scope);
7203
7204 return arg;
7205}
7206
7207/*
7208 * compile "continue"
7209 */
7210 static char_u *
7211compile_continue(char_u *arg, cctx_T *cctx)
7212{
7213 scope_T *scope = cctx->ctx_scope;
7214
7215 for (;;)
7216 {
7217 if (scope == NULL)
7218 {
7219 emsg(_(e_continue));
7220 return NULL;
7221 }
7222 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7223 break;
7224 scope = scope->se_outer;
7225 }
7226
7227 // Jump back to the FOR or WHILE instruction.
7228 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007229 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
7230 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 return arg;
7232}
7233
7234/*
7235 * compile "break"
7236 */
7237 static char_u *
7238compile_break(char_u *arg, cctx_T *cctx)
7239{
7240 scope_T *scope = cctx->ctx_scope;
7241 endlabel_T **el;
7242
7243 for (;;)
7244 {
7245 if (scope == NULL)
7246 {
7247 emsg(_(e_break));
7248 return NULL;
7249 }
7250 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
7251 break;
7252 scope = scope->se_outer;
7253 }
7254
7255 // Jump to the end of the FOR or WHILE loop.
7256 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007257 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007259 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
7261 return FAIL;
7262
7263 return arg;
7264}
7265
7266/*
7267 * compile "{" start of block
7268 */
7269 static char_u *
7270compile_block(char_u *arg, cctx_T *cctx)
7271{
7272 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7273 return NULL;
7274 return skipwhite(arg + 1);
7275}
7276
7277/*
7278 * compile end of block: drop one scope
7279 */
7280 static void
7281compile_endblock(cctx_T *cctx)
7282{
7283 scope_T *scope = cctx->ctx_scope;
7284
7285 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007286 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007287 vim_free(scope);
7288}
7289
7290/*
7291 * compile "try"
7292 * Creates a new scope for the try-endtry, pointing to the first catch and
7293 * finally.
7294 * Creates another scope for the "try" block itself.
7295 * TRY instruction sets up exception handling at runtime.
7296 *
7297 * "try"
7298 * TRY -> catch1, -> finally push trystack entry
7299 * ... try block
7300 * "throw {exception}"
7301 * EVAL {exception}
7302 * THROW create exception
7303 * ... try block
7304 * " catch {expr}"
7305 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007306 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007307 * EVAL {expr}
7308 * MATCH
7309 * JUMP nomatch -> catch2
7310 * CATCH remove exception
7311 * ... catch block
7312 * " catch"
7313 * JUMP -> finally
7314 * catch2: CATCH remove exception
7315 * ... catch block
7316 * " finally"
7317 * finally:
7318 * ... finally block
7319 * " endtry"
7320 * ENDTRY pop trystack entry, may rethrow
7321 */
7322 static char_u *
7323compile_try(char_u *arg, cctx_T *cctx)
7324{
7325 garray_T *instr = &cctx->ctx_instr;
7326 scope_T *try_scope;
7327 scope_T *scope;
7328
7329 // scope that holds the jumps that go to catch/finally/endtry
7330 try_scope = new_scope(cctx, TRY_SCOPE);
7331 if (try_scope == NULL)
7332 return NULL;
7333
Bram Moolenaar69f70502021-01-01 16:10:46 +01007334 if (cctx->ctx_skip != SKIP_YES)
7335 {
7336 // "catch" is set when the first ":catch" is found.
7337 // "finally" is set when ":finally" or ":endtry" is found
7338 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
7339 if (generate_instr(cctx, ISN_TRY) == NULL)
7340 return NULL;
7341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342
7343 // scope for the try block itself
7344 scope = new_scope(cctx, BLOCK_SCOPE);
7345 if (scope == NULL)
7346 return NULL;
7347
7348 return arg;
7349}
7350
7351/*
7352 * compile "catch {expr}"
7353 */
7354 static char_u *
7355compile_catch(char_u *arg, cctx_T *cctx UNUSED)
7356{
7357 scope_T *scope = cctx->ctx_scope;
7358 garray_T *instr = &cctx->ctx_instr;
7359 char_u *p;
7360 isn_T *isn;
7361
7362 // end block scope from :try or :catch
7363 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7364 compile_endblock(cctx);
7365 scope = cctx->ctx_scope;
7366
7367 // Error if not in a :try scope
7368 if (scope == NULL || scope->se_type != TRY_SCOPE)
7369 {
7370 emsg(_(e_catch));
7371 return NULL;
7372 }
7373
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007374 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007375 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007376 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007377 return NULL;
7378 }
7379
Bram Moolenaar69f70502021-01-01 16:10:46 +01007380 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007382 // Jump from end of previous block to :finally or :endtry
7383 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
7384 JUMP_ALWAYS, cctx) == FAIL)
7385 return NULL;
7386
7387 // End :try or :catch scope: set value in ISN_TRY instruction
7388 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7389 if (isn->isn_arg.try.try_catch == 0)
7390 isn->isn_arg.try.try_catch = instr->ga_len;
7391 if (scope->se_u.se_try.ts_catch_label != 0)
7392 {
7393 // Previous catch without match jumps here
7394 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7395 isn->isn_arg.jump.jump_where = instr->ga_len;
7396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 }
7398
7399 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007400 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007402 scope->se_u.se_try.ts_caught_all = TRUE;
7403 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007404 }
7405 else
7406 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007407 char_u *end;
7408 char_u *pat;
7409 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007410 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007411 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007413 // Push v:exception, push {expr} and MATCH
7414 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7415
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007416 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007417 if (*end != *p)
7418 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007419 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007420 vim_free(tofree);
7421 return FAIL;
7422 }
7423 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007424 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007425 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007426 len = (int)(end - tofree);
7427 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007428 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007429 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007430 if (pat == NULL)
7431 return FAIL;
7432 if (generate_PUSHS(cctx, pat) == FAIL)
7433 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7436 return NULL;
7437
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007438 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7440 return NULL;
7441 }
7442
Bram Moolenaar69f70502021-01-01 16:10:46 +01007443 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 return NULL;
7445
7446 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7447 return NULL;
7448 return p;
7449}
7450
7451 static char_u *
7452compile_finally(char_u *arg, cctx_T *cctx)
7453{
7454 scope_T *scope = cctx->ctx_scope;
7455 garray_T *instr = &cctx->ctx_instr;
7456 isn_T *isn;
7457
7458 // end block scope from :try or :catch
7459 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7460 compile_endblock(cctx);
7461 scope = cctx->ctx_scope;
7462
7463 // Error if not in a :try scope
7464 if (scope == NULL || scope->se_type != TRY_SCOPE)
7465 {
7466 emsg(_(e_finally));
7467 return NULL;
7468 }
7469
7470 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007471 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007472 if (isn->isn_arg.try.try_finally != 0)
7473 {
7474 emsg(_(e_finally_dup));
7475 return NULL;
7476 }
7477
7478 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007479 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480
Bram Moolenaar585fea72020-04-02 22:33:21 +02007481 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007482 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007483 {
7484 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007485 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007487 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 }
7489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007490 // TODO: set index in ts_finally_label jumps
7491
7492 return arg;
7493}
7494
7495 static char_u *
7496compile_endtry(char_u *arg, cctx_T *cctx)
7497{
7498 scope_T *scope = cctx->ctx_scope;
7499 garray_T *instr = &cctx->ctx_instr;
7500 isn_T *isn;
7501
7502 // end block scope from :catch or :finally
7503 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7504 compile_endblock(cctx);
7505 scope = cctx->ctx_scope;
7506
7507 // Error if not in a :try scope
7508 if (scope == NULL || scope->se_type != TRY_SCOPE)
7509 {
7510 if (scope == NULL)
7511 emsg(_(e_no_endtry));
7512 else if (scope->se_type == WHILE_SCOPE)
7513 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007514 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007515 emsg(_(e_endfor));
7516 else
7517 emsg(_(e_endif));
7518 return NULL;
7519 }
7520
Bram Moolenaar69f70502021-01-01 16:10:46 +01007521 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007522 {
Bram Moolenaar69f70502021-01-01 16:10:46 +01007523 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
7524 if (isn->isn_arg.try.try_catch == 0
7525 && isn->isn_arg.try.try_finally == 0)
7526 {
7527 emsg(_(e_missing_catch_or_finally));
7528 return NULL;
7529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007530
Bram Moolenaar69f70502021-01-01 16:10:46 +01007531 // Fill in the "end" label in jumps at the end of the blocks, if not
7532 // done by ":finally".
7533 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534
Bram Moolenaar69f70502021-01-01 16:10:46 +01007535 // End :catch or :finally scope: set value in ISN_TRY instruction
7536 if (isn->isn_arg.try.try_catch == 0)
7537 isn->isn_arg.try.try_catch = instr->ga_len;
7538 if (isn->isn_arg.try.try_finally == 0)
7539 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007540
Bram Moolenaar69f70502021-01-01 16:10:46 +01007541 if (scope->se_u.se_try.ts_catch_label != 0)
7542 {
7543 // Last catch without match jumps here
7544 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7545 isn->isn_arg.jump.jump_where = instr->ga_len;
7546 }
Bram Moolenaare8593122020-07-18 15:17:02 +02007547 }
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549 compile_endblock(cctx);
7550
Bram Moolenaar69f70502021-01-01 16:10:46 +01007551 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007552 return NULL;
7553 return arg;
7554}
7555
7556/*
7557 * compile "throw {expr}"
7558 */
7559 static char_u *
7560compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7561{
7562 char_u *p = skipwhite(arg);
7563
Bram Moolenaara5565e42020-05-09 15:44:01 +02007564 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007565 return NULL;
Bram Moolenaar9e1d9e32021-01-11 20:17:34 +01007566 if (cctx->ctx_skip == SKIP_YES)
7567 return p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007568 if (may_generate_2STRING(-1, cctx) == FAIL)
7569 return NULL;
7570 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7571 return NULL;
7572
7573 return p;
7574}
7575
7576/*
7577 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007578 * compile "echomsg expr"
7579 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007580 * compile "execute expr"
7581 */
7582 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007583compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007584{
7585 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007586 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007587 int count = 0;
7588
7589 for (;;)
7590 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007591 if (ends_excmd2(prev, p))
7592 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007593 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007594 return NULL;
7595 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007596 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007597 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007598 }
7599
Bram Moolenaare4984292020-12-13 14:19:25 +01007600 if (count > 0)
7601 {
7602 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7603 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7604 else if (cmdidx == CMD_execute)
7605 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7606 else if (cmdidx == CMD_echomsg)
7607 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7608 else
7609 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007611 return p;
7612}
7613
7614/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007615 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007616 * instruction to compute it and return OK.
7617 * Otherwise return FAIL, the caller must deal with any range.
7618 */
7619 static int
7620compile_variable_range(exarg_T *eap, cctx_T *cctx)
7621{
7622 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7623 char_u *p = skipdigits(eap->cmd);
7624
7625 if (p == range_end)
7626 return FAIL;
7627 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7628}
7629
7630/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007631 * :put r
7632 * :put ={expr}
7633 */
7634 static char_u *
7635compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7636{
7637 char_u *line = arg;
7638 linenr_T lnum;
7639 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007640 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007641
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007642 eap->regname = *line;
7643
7644 if (eap->regname == '=')
7645 {
7646 char_u *p = line + 1;
7647
7648 if (compile_expr0(&p, cctx) == FAIL)
7649 return NULL;
7650 line = p;
7651 }
7652 else if (eap->regname != NUL)
7653 ++line;
7654
Bram Moolenaar08597872020-12-10 19:43:40 +01007655 if (compile_variable_range(eap, cctx) == OK)
7656 {
7657 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7658 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007659 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007660 {
7661 // Either no range or a number.
7662 // "errormsg" will not be set because the range is ADDR_LINES.
7663 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007664 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007665 return NULL;
7666 if (eap->addr_count == 0)
7667 lnum = -1;
7668 else
7669 lnum = eap->line2;
7670 if (above)
7671 --lnum;
7672 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007673
7674 generate_PUT(cctx, eap->regname, lnum);
7675 return line;
7676}
7677
7678/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007679 * A command that is not compiled, execute with legacy code.
7680 */
7681 static char_u *
7682compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7683{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007684 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007685 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007686 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007687
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007688 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007689 goto theend;
7690
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007691 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007692 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007693 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007694 int usefilter = FALSE;
7695
7696 has_expr = argt & (EX_XFILE | EX_EXPAND);
7697
7698 // If the command can be followed by a bar, find the bar and truncate
7699 // it, so that the following command can be compiled.
7700 // The '|' is overwritten with a NUL, it is put back below.
7701 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7702 && *eap->arg == '!')
7703 // :w !filter or :r !filter or :r! filter
7704 usefilter = TRUE;
7705 if ((argt & EX_TRLBAR) && !usefilter)
7706 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007707 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007708 separate_nextcmd(eap);
7709 if (eap->nextcmd != NULL)
7710 nextcmd = eap->nextcmd;
7711 }
Bram Moolenaara11919f2021-01-02 19:44:56 +01007712 else if (eap->cmdidx == CMD_wincmd)
7713 {
7714 p = eap->arg;
7715 if (*p != NUL)
7716 ++p;
7717 if (*p == 'g' || *p == Ctrl_G)
7718 ++p;
7719 p = skipwhite(p);
7720 if (*p == '|')
7721 {
7722 *p = NUL;
7723 nextcmd = p + 1;
7724 }
7725 }
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007726 }
7727
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007728 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7729 {
7730 // expand filename in "syntax include [@group] filename"
7731 has_expr = TRUE;
7732 eap->arg = skipwhite(eap->arg + 7);
7733 if (*eap->arg == '@')
7734 eap->arg = skiptowhite(eap->arg);
7735 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007736
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007737 if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
7738 && STRLEN(eap->arg) > 4)
7739 {
7740 int delim = *eap->arg;
7741
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007742 p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
Bram Moolenaar56ce9ea2020-12-25 18:35:29 +01007743 if (*p == delim)
7744 {
7745 eap->arg = p + 1;
7746 has_expr = TRUE;
7747 }
7748 }
7749
Bram Moolenaarecac5912021-01-05 19:23:28 +01007750 if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
7751 {
7752 // TODO: should only expand when appropriate for the command
7753 eap->arg = skiptowhite(eap->arg);
7754 has_expr = TRUE;
7755 }
7756
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007757 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007758 {
7759 int count = 0;
7760 char_u *start = skipwhite(line);
7761
7762 // :cmd xxx`=expr1`yyy`=expr2`zzz
7763 // PUSHS ":cmd xxx"
7764 // eval expr1
7765 // PUSHS "yyy"
7766 // eval expr2
7767 // PUSHS "zzz"
7768 // EXECCONCAT 5
7769 for (;;)
7770 {
7771 if (p > start)
7772 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007773 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007774 ++count;
7775 }
7776 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007777 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007778 return NULL;
7779 may_generate_2STRING(-1, cctx);
7780 ++count;
7781 p = skipwhite(p);
7782 if (*p != '`')
7783 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007784 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007785 return NULL;
7786 }
7787 start = p + 1;
7788
7789 p = (char_u *)strstr((char *)start, "`=");
7790 if (p == NULL)
7791 {
7792 if (*skipwhite(start) != NUL)
7793 {
7794 generate_PUSHS(cctx, vim_strsave(start));
7795 ++count;
7796 }
7797 break;
7798 }
7799 }
7800 generate_EXECCONCAT(cctx, count);
7801 }
7802 else
7803 generate_EXEC(cctx, line);
7804
7805theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007806 if (*nextcmd != NUL)
7807 {
7808 // the parser expects a pointer to the bar, put it back
7809 --nextcmd;
7810 *nextcmd = '|';
7811 }
7812
7813 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007814}
7815
7816/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007817 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007818 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007819 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007820 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007821add_def_function(ufunc_T *ufunc)
7822{
7823 dfunc_T *dfunc;
7824
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007825 if (def_functions.ga_len == 0)
7826 {
7827 // The first position is not used, so that a zero uf_dfunc_idx means it
7828 // wasn't set.
7829 if (ga_grow(&def_functions, 1) == FAIL)
7830 return FAIL;
7831 ++def_functions.ga_len;
7832 }
7833
Bram Moolenaar09689a02020-05-09 22:50:08 +02007834 // Add the function to "def_functions".
7835 if (ga_grow(&def_functions, 1) == FAIL)
7836 return FAIL;
7837 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7838 CLEAR_POINTER(dfunc);
7839 dfunc->df_idx = def_functions.ga_len;
7840 ufunc->uf_dfunc_idx = dfunc->df_idx;
7841 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007842 dfunc->df_name = vim_strsave(ufunc->uf_name);
7843 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007844 ++def_functions.ga_len;
7845 return OK;
7846}
7847
7848/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007849 * After ex_function() has collected all the function lines: parse and compile
7850 * the lines into instructions.
7851 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007852 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7853 * the return statement (used for lambda). When uf_ret_type is already set
7854 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01007855 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007856 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007857 * This can be used recursively through compile_lambda(), which may reallocate
7858 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007859 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007860 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007861 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01007862compile_def_function(
7863 ufunc_T *ufunc,
7864 int check_return_type,
Bram Moolenaarf002a412021-01-24 13:34:18 +01007865 int profiling UNUSED,
Bram Moolenaarb2049902021-01-24 12:53:53 +01007866 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007867{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007868 char_u *line = NULL;
7869 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007871 cctx_T cctx;
7872 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007873 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007874 int ret = FAIL;
7875 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007876 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007877 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007878 int new_def_function = FALSE;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007879#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007880 int prof_lnum = -1;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007881#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007882
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007883 // When using a function that was compiled before: Free old instructions.
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007884 // The index is reused. Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007885 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007887 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7888 + ufunc->uf_dfunc_idx;
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01007889 delete_def_function_contents(dfunc, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007891 else
7892 {
7893 if (add_def_function(ufunc) == FAIL)
7894 return FAIL;
7895 new_def_function = TRUE;
7896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007897
Bram Moolenaar985116a2020-07-12 17:31:09 +02007898 ufunc->uf_def_status = UF_COMPILING;
7899
Bram Moolenaara80faa82020-04-12 19:37:17 +02007900 CLEAR_FIELD(cctx);
Bram Moolenaarb2049902021-01-24 12:53:53 +01007901
Bram Moolenaarf002a412021-01-24 13:34:18 +01007902#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01007903 cctx.ctx_profiling = profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007904#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007905 cctx.ctx_ufunc = ufunc;
7906 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007907 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007908 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7909 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7910 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7911 cctx.ctx_type_list = &ufunc->uf_type_list;
7912 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7913 instr = &cctx.ctx_instr;
7914
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007915 // Set the context to the function, it may be compiled when called from
7916 // another script. Set the script version to the most modern one.
7917 // The line number will be set in next_line_from_context().
7918 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007919 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7920
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007921 // Make sure error messages are OK.
7922 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7923 if (do_estack_push)
7924 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007925 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007926
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007927 if (ufunc->uf_def_args.ga_len > 0)
7928 {
7929 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007930 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007931 int i;
7932 char_u *arg;
7933 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007934 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007935
7936 // Produce instructions for the default values of optional arguments.
7937 // Store the instruction index in uf_def_arg_idx[] so that we know
7938 // where to start when the function is called, depending on the number
7939 // of arguments.
7940 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7941 if (ufunc->uf_def_arg_idx == NULL)
7942 goto erret;
7943 for (i = 0; i < count; ++i)
7944 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007945 garray_T *stack = &cctx.ctx_type_stack;
7946 type_T *val_type;
7947 int arg_idx = first_def_arg + i;
7948
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007949 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7950 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007951 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007952 goto erret;
7953
7954 // If no type specified use the type of the default value.
7955 // Otherwise check that the default value type matches the
7956 // specified type.
7957 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7958 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007959 {
7960 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007961 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007962 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007963 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7964 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007965 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007966
7967 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007968 goto erret;
7969 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007970 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007971
7972 if (did_set_arg_type)
7973 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007974 }
7975
7976 /*
7977 * Loop over all the lines of the function and generate instructions.
7978 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007979 for (;;)
7980 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007981 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007982 int starts_with_colon = FALSE;
7983 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007984 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007985
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007986 // Bail out on the first error to avoid a flood of errors and report
7987 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007988 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007989 goto erret;
7990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007991 if (line != NULL && *line == '|')
7992 // the line continues after a '|'
7993 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007994 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007995 && !(*line == '#' && (line == cctx.ctx_line_start
7996 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007997 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007998 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007999 goto erret;
8000 }
8001 else
8002 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02008003 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02008004 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarb2049902021-01-24 12:53:53 +01008005 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008006 // beyond the last line
Bram Moolenaarf002a412021-01-24 13:34:18 +01008007#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008008 may_generate_prof_end(&cctx, prof_lnum);
Bram Moolenaarf002a412021-01-24 13:34:18 +01008009#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008010 break;
Bram Moolenaarb2049902021-01-24 12:53:53 +01008011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012 }
8013
Bram Moolenaara80faa82020-04-12 19:37:17 +02008014 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008015 ea.cmdlinep = &line;
8016 ea.cmd = skipwhite(line);
8017
Bram Moolenaarb2049902021-01-24 12:53:53 +01008018 if (*ea.cmd == '#')
8019 {
8020 // "#" starts a comment
8021 line = (char_u *)"";
8022 continue;
8023 }
8024
Bram Moolenaarf002a412021-01-24 13:34:18 +01008025#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008026 if (cctx.ctx_profiling && cctx.ctx_lnum != prof_lnum)
8027 {
8028 may_generate_prof_end(&cctx, prof_lnum);
8029
8030 prof_lnum = cctx.ctx_lnum;
8031 generate_instr(&cctx, ISN_PROF_START);
8032 }
Bram Moolenaarf002a412021-01-24 13:34:18 +01008033#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008034
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008035 // Some things can be recognized by the first character.
8036 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008037 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008038 case '}':
8039 {
8040 // "}" ends a block scope
8041 scopetype_T stype = cctx.ctx_scope == NULL
8042 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008043
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008044 if (stype == BLOCK_SCOPE)
8045 {
8046 compile_endblock(&cctx);
8047 line = ea.cmd;
8048 }
8049 else
8050 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008051 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02008052 goto erret;
8053 }
8054 if (line != NULL)
8055 line = skipwhite(ea.cmd + 1);
8056 continue;
8057 }
8058
8059 case '{':
8060 // "{" starts a block scope
8061 // "{'a': 1}->func() is something else
8062 if (ends_excmd(*skipwhite(ea.cmd + 1)))
8063 {
8064 line = compile_block(ea.cmd, &cctx);
8065 continue;
8066 }
8067 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008068 }
8069
8070 /*
8071 * COMMAND MODIFIERS
8072 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02008073 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02008074 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
8075 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008076 {
8077 if (errormsg != NULL)
8078 goto erret;
8079 // empty line or comment
8080 line = (char_u *)"";
8081 continue;
8082 }
Bram Moolenaare1004402020-10-24 20:49:43 +02008083 generate_cmdmods(&cctx, &local_cmdmod);
8084 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085
Bram Moolenaare88c8e82020-11-01 17:03:37 +01008086 // Check if there was a colon after the last command modifier or before
8087 // the current position.
8088 for (p = ea.cmd; p >= line; --p)
8089 {
8090 if (*p == ':')
8091 starts_with_colon = TRUE;
8092 if (p < ea.cmd && !VIM_ISWHITE(*p))
8093 break;
8094 }
8095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008096 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008097 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02008099 {
8100 if (*ea.cmd == '(')
8101 // not for "call()"
8102 ea.cmd = p;
8103 else
8104 ea.cmd = skipwhite(ea.cmd);
8105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008106
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008107 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008108 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01008109 int assign;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008110
Bram Moolenaar17126b12021-01-07 22:03:02 +01008111 // Check for assignment after command modifiers.
8112 assign = may_compile_assignment(&ea, &line, &cctx);
8113 if (assign == OK)
8114 goto nextline;
8115 if (assign == FAIL)
8116 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008117 }
8118
8119 /*
8120 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008121 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008123 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02008124 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008125 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02008126 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008127 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008128 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008129 if (!starts_with_colon)
8130 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01008131 semsg(_(e_colon_required_before_range_str), cmd);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008132 goto erret;
8133 }
8134 if (ends_excmd2(line, ea.cmd))
8135 {
8136 // A range without a command: jump to the line.
8137 // TODO: compile to a more efficient command, possibly
8138 // calling parse_cmd_address().
8139 ea.cmdidx = CMD_SIZE;
8140 line = compile_exec(line, &ea, &cctx);
8141 goto nextline;
8142 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02008143 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008144 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02008145 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01008146 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01008147 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008148
Bram Moolenaard1510ee2021-01-04 16:15:58 +01008149 if (p == NULL)
8150 {
8151 if (cctx.ctx_skip != SKIP_YES)
8152 emsg(_(e_ambiguous_use_of_user_defined_command));
8153 goto erret;
8154 }
8155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008156 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
8157 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02008158 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008159 {
8160 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01008161 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008162 }
8163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008165 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008166 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01008167 // CMD_var cannot happen, compile_assignment() above would be
8168 // used. Most likely an assignment to a non-existing variable.
8169 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008170 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008172 }
8173
Bram Moolenaar3988f642020-08-27 22:43:03 +02008174 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01008175 && ea.cmdidx != CMD_elseif
8176 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02008177 && ea.cmdidx != CMD_endif
8178 && ea.cmdidx != CMD_endfor
8179 && ea.cmdidx != CMD_endwhile
8180 && ea.cmdidx != CMD_catch
8181 && ea.cmdidx != CMD_finally
8182 && ea.cmdidx != CMD_endtry)
8183 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02008184 emsg(_(e_unreachable_code_after_return));
8185 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02008186 }
8187
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008188 p = skipwhite(p);
8189 if (ea.cmdidx != CMD_SIZE
8190 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
8191 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02008192 if (ea.cmdidx >= 0)
8193 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008194 if ((ea.argt & EX_BANG) && *p == '!')
8195 {
8196 ea.forceit = TRUE;
8197 p = skipwhite(p + 1);
8198 }
8199 }
8200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008201 switch (ea.cmdidx)
8202 {
8203 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02008204 ea.arg = p;
8205 line = compile_nested_function(&ea, &cctx);
8206 break;
8207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008209 // TODO: should we allow this, e.g. to declare a global
8210 // function?
8211 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 goto erret;
8213
8214 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01008215 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008216 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008217 break;
8218
8219 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02008220 emsg(_(e_cannot_use_let_in_vim9_script));
8221 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02008222 case CMD_var:
8223 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 case CMD_const:
8225 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02008226 if (line == p)
8227 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 break;
8229
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008230 case CMD_unlet:
8231 case CMD_unlockvar:
8232 case CMD_lockvar:
8233 line = compile_unletlock(p, &ea, &cctx);
8234 break;
8235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 case CMD_import:
8237 line = compile_import(p, &cctx);
8238 break;
8239
8240 case CMD_if:
8241 line = compile_if(p, &cctx);
8242 break;
8243 case CMD_elseif:
8244 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008245 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008246 break;
8247 case CMD_else:
8248 line = compile_else(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_endif:
8252 line = compile_endif(p, &cctx);
8253 break;
8254
8255 case CMD_while:
8256 line = compile_while(p, &cctx);
8257 break;
8258 case CMD_endwhile:
8259 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008260 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008261 break;
8262
8263 case CMD_for:
8264 line = compile_for(p, &cctx);
8265 break;
8266 case CMD_endfor:
8267 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008268 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008269 break;
8270 case CMD_continue:
8271 line = compile_continue(p, &cctx);
8272 break;
8273 case CMD_break:
8274 line = compile_break(p, &cctx);
8275 break;
8276
8277 case CMD_try:
8278 line = compile_try(p, &cctx);
8279 break;
8280 case CMD_catch:
8281 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008282 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008283 break;
8284 case CMD_finally:
8285 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008286 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008287 break;
8288 case CMD_endtry:
8289 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02008290 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008291 break;
8292 case CMD_throw:
8293 line = compile_throw(p, &cctx);
8294 break;
8295
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008296 case CMD_eval:
8297 if (compile_expr0(&p, &cctx) == FAIL)
8298 goto erret;
8299
Bram Moolenaar3988f642020-08-27 22:43:03 +02008300 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02008301 generate_instr_drop(&cctx, ISN_DROP, 1);
8302
8303 line = skipwhite(p);
8304 break;
8305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008307 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01008308 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008309 case CMD_echomsg:
8310 case CMD_echoerr:
8311 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01008312 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008313
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008314 case CMD_put:
8315 ea.cmd = cmd;
8316 line = compile_put(p, &ea, &cctx);
8317 break;
8318
Bram Moolenaar3988f642020-08-27 22:43:03 +02008319 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02008320
Bram Moolenaarae616492020-07-28 20:07:27 +02008321 case CMD_append:
8322 case CMD_change:
8323 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02008324 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02008325 case CMD_xit:
8326 not_in_vim9(&ea);
8327 goto erret;
8328
Bram Moolenaar002262f2020-07-08 17:47:57 +02008329 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02008330 if (cctx.ctx_skip != SKIP_YES)
8331 {
8332 semsg(_(e_invalid_command_str), ea.cmd);
8333 goto erret;
8334 }
8335 // We don't check for a next command here.
8336 line = (char_u *)"";
8337 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02008338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008339 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02008340 if (cctx.ctx_skip == SKIP_YES)
8341 {
8342 // We don't check for a next command here.
8343 line = (char_u *)"";
8344 }
8345 else
8346 {
8347 // Not recognized, execute with do_cmdline_cmd().
8348 ea.arg = p;
8349 line = compile_exec(line, &ea, &cctx);
8350 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008351 break;
8352 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02008353nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354 if (line == NULL)
8355 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02008356 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008357
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008358 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02008359 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02008360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008361 if (cctx.ctx_type_stack.ga_len < 0)
8362 {
8363 iemsg("Type stack underflow");
8364 goto erret;
8365 }
8366 }
8367
8368 if (cctx.ctx_scope != NULL)
8369 {
8370 if (cctx.ctx_scope->se_type == IF_SCOPE)
8371 emsg(_(e_endif));
8372 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
8373 emsg(_(e_endwhile));
8374 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
8375 emsg(_(e_endfor));
8376 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008377 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008378 goto erret;
8379 }
8380
Bram Moolenaarefd88552020-06-18 20:50:10 +02008381 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008382 {
8383 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
8384 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02008385 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008386 goto erret;
8387 }
8388
8389 // Return zero if there is no return at the end.
Bram Moolenaar299f3032021-01-08 20:53:09 +01008390 generate_instr(&cctx, ISN_RETURN_ZERO);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008391 }
8392
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008393 {
8394 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8395 + ufunc->uf_dfunc_idx;
8396 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008397 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaarf002a412021-01-24 13:34:18 +01008398#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01008399 if (cctx.ctx_profiling)
8400 {
8401 dfunc->df_instr_prof = instr->ga_data;
8402 dfunc->df_instr_prof_count = instr->ga_len;
8403 }
8404 else
Bram Moolenaarf002a412021-01-24 13:34:18 +01008405#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01008406 {
8407 dfunc->df_instr = instr->ga_data;
8408 dfunc->df_instr_count = instr->ga_len;
8409 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008410 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008411 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008412 if (cctx.ctx_outer_used)
8413 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008414 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008416
8417 ret = OK;
8418
8419erret:
8420 if (ret == FAIL)
8421 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008422 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02008423 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8424 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008425
8426 for (idx = 0; idx < instr->ga_len; ++idx)
8427 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008428 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008429 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008430
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008431 // If using the last entry in the table and it was added above, we
8432 // might as well remove it.
8433 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008434 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008435 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008436 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008437 ufunc->uf_dfunc_idx = 0;
8438 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008439 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008440
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008441 while (cctx.ctx_scope != NULL)
8442 drop_scope(&cctx);
8443
Bram Moolenaar20431c92020-03-20 18:39:46 +01008444 // Don't execute this function body.
8445 ga_clear_strings(&ufunc->uf_lines);
8446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008447 if (errormsg != NULL)
8448 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008449 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008450 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008451 }
8452
8453 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008454 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008455 if (do_estack_push)
8456 estack_pop();
8457
Bram Moolenaar20431c92020-03-20 18:39:46 +01008458 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008459 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008460 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008461 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008462}
8463
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008464 void
8465set_function_type(ufunc_T *ufunc)
8466{
8467 int varargs = ufunc->uf_va_name != NULL;
8468 int argcount = ufunc->uf_args.ga_len;
8469
8470 // Create a type for the function, with the return type and any
8471 // argument types.
8472 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8473 // The type is included in "tt_args".
8474 if (argcount > 0 || varargs)
8475 {
8476 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8477 argcount, &ufunc->uf_type_list);
8478 // Add argument types to the function type.
8479 if (func_type_add_arg_types(ufunc->uf_func_type,
8480 argcount + varargs,
8481 &ufunc->uf_type_list) == FAIL)
8482 return;
8483 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8484 ufunc->uf_func_type->tt_min_argcount =
8485 argcount - ufunc->uf_def_args.ga_len;
8486 if (ufunc->uf_arg_types == NULL)
8487 {
8488 int i;
8489
8490 // lambda does not have argument types.
8491 for (i = 0; i < argcount; ++i)
8492 ufunc->uf_func_type->tt_args[i] = &t_any;
8493 }
8494 else
8495 mch_memmove(ufunc->uf_func_type->tt_args,
8496 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8497 if (varargs)
8498 {
8499 ufunc->uf_func_type->tt_args[argcount] =
8500 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8501 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8502 }
8503 }
8504 else
8505 // No arguments, can use a predefined type.
8506 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8507 argcount, &ufunc->uf_type_list);
8508}
8509
8510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008511/*
8512 * Delete an instruction, free what it contains.
8513 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008514 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008515delete_instr(isn_T *isn)
8516{
8517 switch (isn->isn_type)
8518 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008519 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008520 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008521 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008522 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008523 case ISN_LOADENV:
8524 case ISN_LOADG:
8525 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008526 case ISN_LOADT:
8527 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008528 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008529 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008530 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008531 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008532 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008533 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008534 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008535 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008536 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008537 case ISN_STOREW:
8538 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008539 vim_free(isn->isn_arg.string);
8540 break;
8541
8542 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008543 case ISN_STORES:
8544 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008545 break;
8546
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008547 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008548 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008549 vim_free(isn->isn_arg.unlet.ul_name);
8550 break;
8551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008552 case ISN_STOREOPT:
8553 vim_free(isn->isn_arg.storeopt.so_name);
8554 break;
8555
8556 case ISN_PUSHBLOB: // push blob isn_arg.blob
8557 blob_unref(isn->isn_arg.blob);
8558 break;
8559
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008560 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008561#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008562 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008563#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008564 break;
8565
8566 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008567#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008568 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008569#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008570 break;
8571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008572 case ISN_UCALL:
8573 vim_free(isn->isn_arg.ufunc.cuf_name);
8574 break;
8575
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008576 case ISN_FUNCREF:
8577 {
8578 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8579 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008580 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008581
Bram Moolenaar077a4232020-12-22 18:33:27 +01008582 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8583 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008584 }
8585 break;
8586
8587 case ISN_DCALL:
8588 {
8589 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8590 + isn->isn_arg.dfunc.cdf_idx;
8591
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008592 if (dfunc->df_ufunc != NULL
8593 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008594 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008595 }
8596 break;
8597
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008598 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008599 {
8600 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8601 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8602
8603 if (ufunc != NULL)
8604 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008605 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008606 func_ptr_unref(ufunc);
8607 }
8608
8609 vim_free(lambda);
8610 vim_free(isn->isn_arg.newfunc.nf_global);
8611 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008612 break;
8613
Bram Moolenaar5e654232020-09-16 15:22:00 +02008614 case ISN_CHECKTYPE:
Bram Moolenaaraa210a32021-01-02 15:41:03 +01008615 case ISN_SETTYPE:
Bram Moolenaar5e654232020-09-16 15:22:00 +02008616 free_type(isn->isn_arg.type.ct_type);
8617 break;
8618
Bram Moolenaar02194d22020-10-24 23:08:38 +02008619 case ISN_CMDMOD:
8620 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8621 ->cmod_filter_regmatch.regprog);
8622 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8623 break;
8624
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008625 case ISN_LOADSCRIPT:
8626 case ISN_STORESCRIPT:
8627 vim_free(isn->isn_arg.script.scriptref);
8628 break;
8629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008630 case ISN_2BOOL:
8631 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008632 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008633 case ISN_ADDBLOB:
8634 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008635 case ISN_ANYINDEX:
8636 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008638 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008639 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008640 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008641 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008642 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008643 case ISN_COMPAREANY:
8644 case ISN_COMPAREBLOB:
8645 case ISN_COMPAREBOOL:
8646 case ISN_COMPAREDICT:
8647 case ISN_COMPAREFLOAT:
8648 case ISN_COMPAREFUNC:
8649 case ISN_COMPARELIST:
8650 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008651 case ISN_COMPARESPECIAL:
8652 case ISN_COMPARESTRING:
8653 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008654 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008655 case ISN_DROP:
8656 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008657 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008658 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008659 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008660 case ISN_EXECCONCAT:
8661 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008662 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008663 case ISN_GETITEM:
8664 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008665 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008666 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008667 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008668 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008669 case ISN_LOADBDICT:
8670 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008671 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008672 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008673 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008674 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008675 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008676 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008677 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008678 case ISN_NEGATENR:
8679 case ISN_NEWDICT:
8680 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008681 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008682 case ISN_OPFLOAT:
8683 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008684 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008685 case ISN_PCALL_END:
Bram Moolenaarb2049902021-01-24 12:53:53 +01008686 case ISN_PROF_END:
8687 case ISN_PROF_START:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008688 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008689 case ISN_PUSHF:
8690 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008691 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008692 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008693 case ISN_RETURN:
Bram Moolenaar299f3032021-01-08 20:53:09 +01008694 case ISN_RETURN_ZERO:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008695 case ISN_SHUFFLE:
8696 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008697 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008698 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008699 case ISN_STORENR:
8700 case ISN_STOREOUTER:
8701 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008702 case ISN_STOREV:
8703 case ISN_STRINDEX:
8704 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705 case ISN_THROW:
8706 case ISN_TRY:
Bram Moolenaar752fc692021-01-04 21:57:11 +01008707 case ISN_UNLETINDEX:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008708 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008709 // nothing allocated
8710 break;
8711 }
8712}
8713
8714/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008715 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008716 */
8717 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008718delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008719{
8720 int idx;
8721
8722 ga_clear(&dfunc->df_def_args_isn);
8723
8724 if (dfunc->df_instr != NULL)
8725 {
8726 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8727 delete_instr(dfunc->df_instr + idx);
8728 VIM_CLEAR(dfunc->df_instr);
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008729 dfunc->df_instr = NULL;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008730 }
8731
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008732 if (mark_deleted)
8733 dfunc->df_deleted = TRUE;
8734 if (dfunc->df_ufunc != NULL)
8735 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008736}
8737
8738/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008739 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008740 * function, unless another user function still uses it.
8741 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008742 */
8743 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008744unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008745{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008746 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008747 {
8748 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8749 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008750
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008751 if (--dfunc->df_refcount <= 0)
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008752 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008753 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008754 ufunc->uf_dfunc_idx = 0;
8755 if (dfunc->df_ufunc == ufunc)
8756 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008757 }
8758}
8759
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008760/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008761 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008762 */
8763 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008764link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008765{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008766 if (ufunc->uf_dfunc_idx > 0)
8767 {
8768 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8769 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008770
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008771 ++dfunc->df_refcount;
8772 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008773}
8774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008775#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008776/*
8777 * Free all functions defined with ":def".
8778 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008779 void
8780free_def_functions(void)
8781{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008782 int idx;
8783
8784 for (idx = 0; idx < def_functions.ga_len; ++idx)
8785 {
8786 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8787
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01008788 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008789 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008790 }
8791
8792 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008793}
8794#endif
8795
8796
8797#endif // FEAT_EVAL