blob: 2a305ee12ada508d7e5c9c0a022786e9a40e0a04 [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
111 int lv_from_outer; // when TRUE using ctx_outer scope
112 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
126 garray_T ctx_locals; // currently visible local variables
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200127 int ctx_locals_count; // total number of local variables
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200129 int ctx_has_closure; // set to one if a closures was created in
130 // the function
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100132 garray_T ctx_imports; // imported items
133
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200134 skip_T ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 scope_T *ctx_scope; // current scope, NULL at toplevel
Bram Moolenaarefd88552020-06-18 20:50:10 +0200136 int ctx_had_return; // last seen statement was "return"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200138 cctx_T *ctx_outer; // outer scope for lambda or nested
139 // function
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200140 int ctx_outer_used; // var in ctx_outer was used
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 garray_T ctx_type_stack; // type of each item on the stack
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200143 garray_T *ctx_type_list; // list of pointers to allocated types
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200144
Bram Moolenaar02194d22020-10-24 23:08:38 +0200145 int ctx_has_cmdmod; // ISN_CMDMOD was generated
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100146};
147
Bram Moolenaar20431c92020-03-20 18:39:46 +0100148static void delete_def_function_contents(dfunc_T *dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149
150/*
Bram Moolenaar709664c2020-12-12 14:33:41 +0100151 * Lookup variable "name" in the local scope and return it in "lvar".
152 * "lvar->lv_from_outer" is set accordingly.
153 * If "lvar" is NULL only check if the variable can be found.
154 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 */
Bram Moolenaar709664c2020-12-12 14:33:41 +0100156 static int
157lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158{
159 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100160 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100161
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100162 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +0100163 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200164
165 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
167 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100168 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
169 if (STRNCMP(name, lvp->lv_name, len) == 0
170 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200171 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100172 if (lvar != NULL)
173 {
174 *lvar = *lvp;
175 lvar->lv_from_outer = FALSE;
176 }
177 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200180
181 // Find local in outer function scope.
182 if (cctx->ctx_outer != NULL)
183 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100184 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200185 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100186 if (lvar != NULL)
187 {
188 cctx->ctx_outer_used = TRUE;
189 lvar->lv_from_outer = TRUE;
190 }
191 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200192 }
193 }
194
Bram Moolenaar709664c2020-12-12 14:33:41 +0100195 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196}
197
198/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200199 * Lookup an argument in the current function and an enclosing function.
200 * Returns the argument index in "idxp"
201 * Returns the argument type in "type"
202 * Sets "gen_load_outer" to TRUE if found in outer scope.
203 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100204 */
205 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200206arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200207 char_u *name,
208 size_t len,
209 int *idxp,
210 type_T **type,
211 int *gen_load_outer,
212 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213{
214 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200215 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100217 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200218 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
220 {
221 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
222
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200223 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
224 {
225 if (idxp != NULL)
226 {
227 // Arguments are located above the frame pointer. One further
228 // if there is a vararg argument
229 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
230 + STACK_FRAME_SIZE)
231 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
232
233 if (cctx->ctx_ufunc->uf_arg_types != NULL)
234 *type = cctx->ctx_ufunc->uf_arg_types[idx];
235 else
236 *type = &t_any;
237 }
238 return OK;
239 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200242 va_name = cctx->ctx_ufunc->uf_va_name;
243 if (va_name != NULL
244 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
245 {
246 if (idxp != NULL)
247 {
248 // varargs is always the last argument
249 *idxp = -STACK_FRAME_SIZE - 1;
250 *type = cctx->ctx_ufunc->uf_va_type;
251 }
252 return OK;
253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200255 if (cctx->ctx_outer != NULL)
256 {
257 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200258 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200259 == OK)
260 {
261 *gen_load_outer = TRUE;
262 return OK;
263 }
264 }
265
266 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267}
268
269/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200270 * Lookup a script-local variable in the current script, possibly defined in a
271 * block that contains the function "cctx->ctx_ufunc".
272 * "cctx" is NULL at the script level.
273 * if "len" is <= 0 "name" must be NUL terminated.
274 * Return NULL when not found.
275 */
276 static sallvar_T *
277find_script_var(char_u *name, size_t len, cctx_T *cctx)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 int cc;
282 sallvar_T *sav;
283 ufunc_T *ufunc;
284
285 // Find the list of all script variables with the right name.
286 if (len > 0)
287 {
288 cc = name[len];
289 name[len] = NUL;
290 }
291 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
292 if (len > 0)
293 name[len] = cc;
294 if (HASHITEM_EMPTY(hi))
295 return NULL;
296
297 sav = HI2SAV(hi);
298 if (sav->sav_block_id == 0 || cctx == NULL)
299 // variable defined in the script scope or not in a function.
300 return sav;
301
302 // Go over the variables with this name and find one that was visible
303 // from the function.
304 ufunc = cctx->ctx_ufunc;
305 while (sav != NULL)
306 {
307 int idx;
308
309 // Go over the blocks that this function was defined in. If the
310 // variable block ID matches it was visible to the function.
311 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
312 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
313 return sav;
314 sav = sav->sav_next;
315 }
316
317 return NULL;
318}
319
320/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100321 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200322 */
323 static int
324script_is_vim9()
325{
326 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
327}
328
329/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200330 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar53900992020-08-22 19:02:02 +0200331 * If "vim9script" is TRUE the script must be Vim9 script. Used for "var"
332 * without "s:".
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200333 * "cctx" is NULL at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 * Returns OK or FAIL.
335 */
336 static int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200337script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200339 int is_vim9_script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200341 if (current_sctx.sc_sid <= 0)
342 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200343 is_vim9_script = script_is_vim9();
344 if (vim9script && !is_vim9_script)
Bram Moolenaar53900992020-08-22 19:02:02 +0200345 return FAIL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200346 if (is_vim9_script)
347 {
348 // Check script variables that were visible where the function was
349 // defined.
350 if (find_script_var(name, len, cctx) != NULL)
351 return OK;
352 }
353 else
354 {
355 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
356 dictitem_T *di;
357 int cc;
358
359 // Check script variables that are currently visible
360 cc = name[len];
361 name[len] = NUL;
362 di = find_var_in_ht(ht, 0, name, TRUE);
363 name[len] = cc;
364 if (di != NULL)
365 return OK;
366 }
367
368 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369}
370
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100371/*
372 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200373 * compilation context "cctx". "cctx" is NULL at the script level.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200374 * Does not check the global namespace.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100375 * Return FAIL and give an error if it defined.
376 */
377 int
Bram Moolenaarcbb6bdc2020-07-06 21:53:17 +0200378check_defined(char_u *p, size_t len, cctx_T *cctx)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100379{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200380 int c = p[len];
381 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200382
383 p[len] = NUL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200384 if (script_var_exists(p, len, FALSE, cctx) == OK
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100385 || (cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100386 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200387 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaarad486a02020-08-01 23:22:18 +0200388 || find_imported(p, len, cctx) != NULL
Bram Moolenaar0f769812020-09-12 18:32:34 +0200389 || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100390 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200391 // A local or script-local function can shadow a global function.
392 if (ufunc == NULL || !func_is_global(ufunc)
393 || (p[0] == 'g' && p[1] == ':'))
394 {
395 p[len] = c;
396 semsg(_(e_name_already_defined_str), p);
397 return FAIL;
398 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100399 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200400 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100401 return OK;
402}
403
Bram Moolenaar65b95452020-07-19 14:03:09 +0200404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405/////////////////////////////////////////////////////////////////////
406// Following generate_ functions expect the caller to call ga_grow().
407
Bram Moolenaar9b68c822020-06-18 19:31:08 +0200408#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
409#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
Bram Moolenaar080457c2020-03-03 21:53:32 +0100410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411/*
412 * Generate an instruction without arguments.
413 * Returns a pointer to the new instruction, NULL if failed.
414 */
415 static isn_T *
416generate_instr(cctx_T *cctx, isntype_T isn_type)
417{
418 garray_T *instr = &cctx->ctx_instr;
419 isn_T *isn;
420
Bram Moolenaar080457c2020-03-03 21:53:32 +0100421 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 if (ga_grow(instr, 1) == FAIL)
423 return NULL;
424 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
425 isn->isn_type = isn_type;
426 isn->isn_lnum = cctx->ctx_lnum + 1;
427 ++instr->ga_len;
428
429 return isn;
430}
431
432/*
433 * Generate an instruction without arguments.
434 * "drop" will be removed from the stack.
435 * Returns a pointer to the new instruction, NULL if failed.
436 */
437 static isn_T *
438generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
439{
440 garray_T *stack = &cctx->ctx_type_stack;
441
Bram Moolenaar080457c2020-03-03 21:53:32 +0100442 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100443 stack->ga_len -= drop;
444 return generate_instr(cctx, isn_type);
445}
446
447/*
448 * Generate instruction "isn_type" and put "type" on the type stack.
449 */
450 static isn_T *
451generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
452{
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
455
456 if ((isn = generate_instr(cctx, isn_type)) == NULL)
457 return NULL;
458
459 if (ga_grow(stack, 1) == FAIL)
460 return NULL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +0200461 ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 ++stack->ga_len;
463
464 return isn;
465}
466
467/*
468 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200469 * But only for simple types.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 */
471 static int
472may_generate_2STRING(int offset, cctx_T *cctx)
473{
474 isn_T *isn;
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200475 isntype_T isntype = ISN_2STRING;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 garray_T *stack = &cctx->ctx_type_stack;
477 type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
478
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200479 switch ((*type)->tt_type)
480 {
481 // nothing to be done
482 case VAR_STRING: return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200484 // conversion possible
485 case VAR_SPECIAL:
486 case VAR_BOOL:
487 case VAR_NUMBER:
488 case VAR_FLOAT:
489 break;
490
491 // conversion possible (with runtime check)
492 case VAR_ANY:
493 case VAR_UNKNOWN:
494 isntype = ISN_2STRING_ANY;
495 break;
496
497 // conversion not possible
498 case VAR_VOID:
499 case VAR_BLOB:
500 case VAR_FUNC:
501 case VAR_PARTIAL:
502 case VAR_LIST:
503 case VAR_DICT:
504 case VAR_JOB:
505 case VAR_CHANNEL:
506 to_string_error((*type)->tt_type);
507 return FAIL;
508 }
509
510 *type = &t_string;
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 return FAIL;
513 isn->isn_arg.number = offset;
514
515 return OK;
516}
517
518 static int
519check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
520{
Bram Moolenaar4c683752020-04-05 21:38:23 +0200521 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar4c683752020-04-05 21:38:23 +0200523 || type2 == VAR_ANY)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (*op == '+')
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200526 emsg(_(e_wrong_argument_type_for_plus));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100527 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200528 semsg(_(e_char_requires_number_or_float_arguments), *op);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 }
531 return OK;
532}
533
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200534 static int
535generate_add_instr(
536 cctx_T *cctx,
537 vartype_T vartype,
538 type_T *type1,
539 type_T *type2)
540{
Bram Moolenaar399ea812020-12-15 21:28:57 +0100541 garray_T *stack = &cctx->ctx_type_stack;
542 isn_T *isn = generate_instr_drop(cctx,
543 vartype == VAR_NUMBER ? ISN_OPNR
544 : vartype == VAR_LIST ? ISN_ADDLIST
545 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar399ea812020-12-15 21:28:57 +0100547 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200548#endif
Bram Moolenaar399ea812020-12-15 21:28:57 +0100549 : ISN_OPANY, 1);
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200550
551 if (vartype != VAR_LIST && vartype != VAR_BLOB
552 && type1->tt_type != VAR_ANY
553 && type2->tt_type != VAR_ANY
554 && check_number_or_float(
555 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
556 return FAIL;
557
558 if (isn != NULL)
559 isn->isn_arg.op.op_type = EXPR_ADD;
Bram Moolenaar399ea812020-12-15 21:28:57 +0100560
561 // When concatenating two lists with different member types the member type
562 // becomes "any".
563 if (vartype == VAR_LIST
564 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
565 && type1->tt_member != type2->tt_member)
566 (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
567
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200568 return isn == NULL ? FAIL : OK;
569}
570
571/*
572 * Get the type to use for an instruction for an operation on "type1" and
573 * "type2". If they are matching use a type-specific instruction. Otherwise
574 * fall back to runtime type checking.
575 */
576 static vartype_T
577operator_type(type_T *type1, type_T *type2)
578{
579 if (type1->tt_type == type2->tt_type
580 && (type1->tt_type == VAR_NUMBER
581 || type1->tt_type == VAR_LIST
582#ifdef FEAT_FLOAT
583 || type1->tt_type == VAR_FLOAT
584#endif
585 || type1->tt_type == VAR_BLOB))
586 return type1->tt_type;
587 return VAR_ANY;
588}
589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100590/*
591 * Generate an instruction with two arguments. The instruction depends on the
592 * type of the arguments.
593 */
594 static int
595generate_two_op(cctx_T *cctx, char_u *op)
596{
597 garray_T *stack = &cctx->ctx_type_stack;
598 type_T *type1;
599 type_T *type2;
600 vartype_T vartype;
601 isn_T *isn;
602
Bram Moolenaar080457c2020-03-03 21:53:32 +0100603 RETURN_OK_IF_SKIP(cctx);
604
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200605 // Get the known type of the two items on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
607 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200608 vartype = operator_type(type1, type2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609
610 switch (*op)
611 {
Bram Moolenaardd29f1b2020-08-07 20:46:20 +0200612 case '+':
613 if (generate_add_instr(cctx, vartype, type1, type2) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 break;
616
617 case '-':
618 case '*':
619 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
620 op) == FAIL)
621 return FAIL;
622 if (vartype == VAR_NUMBER)
623 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
624#ifdef FEAT_FLOAT
625 else if (vartype == VAR_FLOAT)
626 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
627#endif
628 else
629 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
630 if (isn != NULL)
631 isn->isn_arg.op.op_type = *op == '*'
632 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
633 break;
634
Bram Moolenaar4c683752020-04-05 21:38:23 +0200635 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 && type1->tt_type != VAR_NUMBER)
Bram Moolenaar4c683752020-04-05 21:38:23 +0200637 || (type2->tt_type != VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 && type2->tt_type != VAR_NUMBER))
639 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200640 emsg(_(e_percent_requires_number_arguments));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 return FAIL;
642 }
643 isn = generate_instr_drop(cctx,
644 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
645 if (isn != NULL)
646 isn->isn_arg.op.op_type = EXPR_REM;
647 break;
648 }
649
650 // correct type of result
Bram Moolenaar4c683752020-04-05 21:38:23 +0200651 if (vartype == VAR_ANY)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 {
653 type_T *type = &t_any;
654
655#ifdef FEAT_FLOAT
656 // float+number and number+float results in float
657 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
658 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
659 type = &t_float;
660#endif
661 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
662 }
663
664 return OK;
665}
666
667/*
Bram Moolenaara5565e42020-05-09 15:44:01 +0200668 * Get the instruction to use for comparing "type1" with "type2"
669 * Return ISN_DROP when failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 */
Bram Moolenaara5565e42020-05-09 15:44:01 +0200671 static isntype_T
672get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 isntype_T isntype = ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar4c683752020-04-05 21:38:23 +0200676 if (type1 == VAR_UNKNOWN)
677 type1 = VAR_ANY;
678 if (type2 == VAR_UNKNOWN)
679 type2 = VAR_ANY;
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (type1 == type2)
682 {
683 switch (type1)
684 {
685 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
686 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
687 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
688 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
689 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
690 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
691 case VAR_LIST: isntype = ISN_COMPARELIST; break;
692 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
693 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 default: isntype = ISN_COMPAREANY; break;
695 }
696 }
Bram Moolenaar4c683752020-04-05 21:38:23 +0200697 else if (type1 == VAR_ANY || type2 == VAR_ANY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
699 && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
700 isntype = ISN_COMPAREANY;
701
702 if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
703 && (isntype == ISN_COMPAREBOOL
704 || isntype == ISN_COMPARESPECIAL
705 || isntype == ISN_COMPARENR
706 || isntype == ISN_COMPAREFLOAT))
707 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200708 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200710 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 }
712 if (isntype == ISN_DROP
713 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
714 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
715 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
716 || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
717 && exptype != EXPR_IS && exptype != EXPR_ISNOT
718 && (type1 == VAR_BLOB || type2 == VAR_BLOB
719 || type1 == VAR_LIST || type2 == VAR_LIST))))
720 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200721 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 vartype_name(type1), vartype_name(type2));
Bram Moolenaara5565e42020-05-09 15:44:01 +0200723 return ISN_DROP;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724 }
Bram Moolenaara5565e42020-05-09 15:44:01 +0200725 return isntype;
726}
727
Bram Moolenaar543e6f32020-07-10 22:45:38 +0200728 int
729check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
730{
731 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
732 return FAIL;
733 return OK;
734}
735
Bram Moolenaara5565e42020-05-09 15:44:01 +0200736/*
737 * Generate an ISN_COMPARE* instruction with a boolean result.
738 */
739 static int
740generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
741{
742 isntype_T isntype;
743 isn_T *isn;
744 garray_T *stack = &cctx->ctx_type_stack;
745 vartype_T type1;
746 vartype_T type2;
747
748 RETURN_OK_IF_SKIP(cctx);
749
750 // Get the known type of the two items on the stack. If they are matching
751 // use a type-specific instruction. Otherwise fall back to runtime type
752 // checking.
753 type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
754 type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
755 isntype = get_compare_isn(exptype, type1, type2);
756 if (isntype == ISN_DROP)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if ((isn = generate_instr(cctx, isntype)) == NULL)
760 return FAIL;
761 isn->isn_arg.op.op_type = exptype;
762 isn->isn_arg.op.op_ic = ic;
763
764 // takes two arguments, puts one bool back
765 if (stack->ga_len >= 2)
766 {
767 --stack->ga_len;
768 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
769 }
770
771 return OK;
772}
773
774/*
775 * Generate an ISN_2BOOL instruction.
776 */
777 static int
778generate_2BOOL(cctx_T *cctx, int invert)
779{
780 isn_T *isn;
781 garray_T *stack = &cctx->ctx_type_stack;
782
Bram Moolenaar080457c2020-03-03 21:53:32 +0100783 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = invert;
787
788 // type becomes bool
789 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
790
791 return OK;
792}
793
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200794/*
795 * Generate an ISN_COND2BOOL instruction.
796 */
797 static int
798generate_COND2BOOL(cctx_T *cctx)
799{
800 isn_T *isn;
801 garray_T *stack = &cctx->ctx_type_stack;
802
803 RETURN_OK_IF_SKIP(cctx);
804 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
805 return FAIL;
806
807 // type becomes bool
808 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
809
810 return OK;
811}
812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 static int
Bram Moolenaar5e654232020-09-16 15:22:00 +0200814generate_TYPECHECK(
815 cctx_T *cctx,
816 type_T *expected,
817 int offset)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818{
819 isn_T *isn;
820 garray_T *stack = &cctx->ctx_type_stack;
821
Bram Moolenaar080457c2020-03-03 21:53:32 +0100822 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
824 return FAIL;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200825 isn->isn_arg.type.ct_type = alloc_type(expected);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 isn->isn_arg.type.ct_off = offset;
827
Bram Moolenaar5e654232020-09-16 15:22:00 +0200828 // type becomes expected
829 ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 return OK;
832}
833
834/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200835 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
836 * used. Return FALSE if the types will never match.
837 */
Bram Moolenaar193f6202020-11-16 20:08:35 +0100838 int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200839use_typecheck(type_T *actual, type_T *expected)
840{
841 if (actual->tt_type == VAR_ANY
842 || actual->tt_type == VAR_UNKNOWN
843 || (actual->tt_type == VAR_FUNC
844 && (expected->tt_type == VAR_FUNC
845 || expected->tt_type == VAR_PARTIAL)
846 && (actual->tt_member == &t_any || actual->tt_argcount < 0)))
847 return TRUE;
848 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
849 && actual->tt_type == expected->tt_type)
850 // This takes care of a nested list or dict.
851 return use_typecheck(actual->tt_member, expected->tt_member);
852 return FALSE;
853}
854
855/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200856 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200857 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200858 * - "actual" is a type that can be "expected" type: add a runtime check; or
859 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200860 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
861 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200862 */
863 static int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200864need_type(
865 type_T *actual,
866 type_T *expected,
867 int offset,
868 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200869 int silent,
870 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200871{
Bram Moolenaar4ed124c2020-09-09 20:03:46 +0200872 if (expected == &t_bool && actual != &t_bool
873 && (actual->tt_flags & TTFLAG_BOOL_OK))
874 {
875 // Using "0", "1" or the result of an expression with "&&" or "||" as a
876 // boolean is OK but requires a conversion.
877 generate_2BOOL(cctx, FALSE);
878 return OK;
879 }
880
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200881 if (check_type(expected, actual, FALSE, 0) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200882 return OK;
Bram Moolenaar5e654232020-09-16 15:22:00 +0200883
884 // If the actual type can be the expected type add a runtime check.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200885 // If it's a constant a runtime check makes no sense.
886 if (!actual_is_const && use_typecheck(actual, expected))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200887 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200888 generate_TYPECHECK(cctx, expected, offset);
889 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200890 }
Bram Moolenaar5e654232020-09-16 15:22:00 +0200891
892 if (!silent)
893 type_mismatch(expected, actual);
894 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200895}
896
897/*
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100898 * Check that the top of the type stack has a type that can be used as a
899 * condition. Give an error and return FAIL if not.
900 */
901 static int
902bool_on_stack(cctx_T *cctx)
903{
904 garray_T *stack = &cctx->ctx_type_stack;
905 type_T *type;
906
907 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
908 if (type == &t_bool)
909 return OK;
910
911 if (type == &t_any || type == &t_number)
912 // Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
913 // This requires a runtime type check.
914 return generate_COND2BOOL(cctx);
915
916 return need_type(type, &t_bool, -1, cctx, FALSE, FALSE);
917}
918
919/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 * Generate an ISN_PUSHNR instruction.
921 */
922 static int
923generate_PUSHNR(cctx_T *cctx, varnumber_T number)
924{
925 isn_T *isn;
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200926 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
Bram Moolenaar080457c2020-03-03 21:53:32 +0100928 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
930 return FAIL;
931 isn->isn_arg.number = number;
932
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200933 if (number == 0 || number == 1)
934 {
Bram Moolenaar5e654232020-09-16 15:22:00 +0200935 type_T *type = get_type_ptr(cctx->ctx_type_list);
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200936
937 // A 0 or 1 number can also be used as a bool.
938 if (type != NULL)
939 {
940 type->tt_type = VAR_NUMBER;
941 type->tt_flags = TTFLAG_BOOL_OK;
942 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
943 }
944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return OK;
946}
947
948/*
949 * Generate an ISN_PUSHBOOL instruction.
950 */
951 static int
952generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
953{
954 isn_T *isn;
955
Bram Moolenaar080457c2020-03-03 21:53:32 +0100956 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
958 return FAIL;
959 isn->isn_arg.number = number;
960
961 return OK;
962}
963
964/*
965 * Generate an ISN_PUSHSPEC instruction.
966 */
967 static int
968generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
969{
970 isn_T *isn;
971
Bram Moolenaar080457c2020-03-03 21:53:32 +0100972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
974 return FAIL;
975 isn->isn_arg.number = number;
976
977 return OK;
978}
979
980#ifdef FEAT_FLOAT
981/*
982 * Generate an ISN_PUSHF instruction.
983 */
984 static int
985generate_PUSHF(cctx_T *cctx, float_T fnumber)
986{
987 isn_T *isn;
988
Bram Moolenaar080457c2020-03-03 21:53:32 +0100989 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
991 return FAIL;
992 isn->isn_arg.fnumber = fnumber;
993
994 return OK;
995}
996#endif
997
998/*
999 * Generate an ISN_PUSHS instruction.
1000 * Consumes "str".
1001 */
1002 static int
1003generate_PUSHS(cctx_T *cctx, char_u *str)
1004{
1005 isn_T *isn;
1006
Bram Moolenaar080457c2020-03-03 21:53:32 +01001007 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1009 return FAIL;
1010 isn->isn_arg.string = str;
1011
1012 return OK;
1013}
1014
1015/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 * Generate an ISN_PUSHCHANNEL instruction.
1017 * Consumes "channel".
1018 */
1019 static int
1020generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1021{
1022 isn_T *isn;
1023
Bram Moolenaar080457c2020-03-03 21:53:32 +01001024 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001025 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.channel = channel;
1028
1029 return OK;
1030}
1031
1032/*
1033 * Generate an ISN_PUSHJOB instruction.
1034 * Consumes "job".
1035 */
1036 static int
1037generate_PUSHJOB(cctx_T *cctx, job_T *job)
1038{
1039 isn_T *isn;
1040
Bram Moolenaar080457c2020-03-03 21:53:32 +01001041 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001042 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001043 return FAIL;
1044 isn->isn_arg.job = job;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 * Generate an ISN_PUSHBLOB instruction.
1051 * Consumes "blob".
1052 */
1053 static int
1054generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1055{
1056 isn_T *isn;
1057
Bram Moolenaar080457c2020-03-03 21:53:32 +01001058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1060 return FAIL;
1061 isn->isn_arg.blob = blob;
1062
1063 return OK;
1064}
1065
1066/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001067 * Generate an ISN_PUSHFUNC instruction with name "name".
1068 * Consumes "name".
1069 */
1070 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001071generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001072{
1073 isn_T *isn;
1074
Bram Moolenaar080457c2020-03-03 21:53:32 +01001075 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001076 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001077 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001078 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001079
1080 return OK;
1081}
1082
1083/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 * Generate an ISN_GETITEM instruction with "index".
1085 */
1086 static int
1087generate_GETITEM(cctx_T *cctx, int index)
1088{
1089 isn_T *isn;
1090 garray_T *stack = &cctx->ctx_type_stack;
1091 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1092 type_T *item_type = &t_any;
1093
1094 RETURN_OK_IF_SKIP(cctx);
1095
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001096 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001097 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001098 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001099 emsg(_(e_listreq));
1100 return FAIL;
1101 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001102 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001103 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = index;
1106
1107 // add the item type to the type stack
1108 if (ga_grow(stack, 1) == FAIL)
1109 return FAIL;
1110 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1111 ++stack->ga_len;
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001116 * Generate an ISN_SLICE instruction with "count".
1117 */
1118 static int
1119generate_SLICE(cctx_T *cctx, int count)
1120{
1121 isn_T *isn;
1122
1123 RETURN_OK_IF_SKIP(cctx);
1124 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1125 return FAIL;
1126 isn->isn_arg.number = count;
1127 return OK;
1128}
1129
1130/*
1131 * Generate an ISN_CHECKLEN instruction with "min_len".
1132 */
1133 static int
1134generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1135{
1136 isn_T *isn;
1137
1138 RETURN_OK_IF_SKIP(cctx);
1139
1140 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1141 return FAIL;
1142 isn->isn_arg.checklen.cl_min_len = min_len;
1143 isn->isn_arg.checklen.cl_more_OK = more_OK;
1144
1145 return OK;
1146}
1147
1148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 * Generate an ISN_STORE instruction.
1150 */
1151 static int
1152generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1153{
1154 isn_T *isn;
1155
Bram Moolenaar080457c2020-03-03 21:53:32 +01001156 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1158 return FAIL;
1159 if (name != NULL)
1160 isn->isn_arg.string = vim_strsave(name);
1161 else
1162 isn->isn_arg.number = idx;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1169 */
1170 static int
1171generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1172{
1173 isn_T *isn;
1174
Bram Moolenaar080457c2020-03-03 21:53:32 +01001175 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1177 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001178 isn->isn_arg.storenr.stnr_idx = idx;
1179 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180
1181 return OK;
1182}
1183
1184/*
1185 * Generate an ISN_STOREOPT instruction
1186 */
1187 static int
1188generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1189{
1190 isn_T *isn;
1191
Bram Moolenaar080457c2020-03-03 21:53:32 +01001192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001193 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 return FAIL;
1195 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1196 isn->isn_arg.storeopt.so_flags = opt_flags;
1197
1198 return OK;
1199}
1200
1201/*
1202 * Generate an ISN_LOAD or similar instruction.
1203 */
1204 static int
1205generate_LOAD(
1206 cctx_T *cctx,
1207 isntype_T isn_type,
1208 int idx,
1209 char_u *name,
1210 type_T *type)
1211{
1212 isn_T *isn;
1213
Bram Moolenaar080457c2020-03-03 21:53:32 +01001214 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1216 return FAIL;
1217 if (name != NULL)
1218 isn->isn_arg.string = vim_strsave(name);
1219 else
1220 isn->isn_arg.number = idx;
1221
1222 return OK;
1223}
1224
1225/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001226 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 */
1228 static int
1229generate_LOADV(
1230 cctx_T *cctx,
1231 char_u *name,
1232 int error)
1233{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001234 int di_flags;
1235 int vidx = find_vim_var(name, &di_flags);
1236 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar080457c2020-03-03 21:53:32 +01001238 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239 if (vidx < 0)
1240 {
1241 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001242 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001243 return FAIL;
1244 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001245 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001247 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248}
1249
1250/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 * Generate an ISN_UNLET instruction.
1252 */
1253 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001254generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001255{
1256 isn_T *isn;
1257
1258 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001259 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001260 return FAIL;
1261 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1262 isn->isn_arg.unlet.ul_forceit = forceit;
1263
1264 return OK;
1265}
1266
1267/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001268 * Generate an ISN_LOCKCONST instruction.
1269 */
1270 static int
1271generate_LOCKCONST(cctx_T *cctx)
1272{
1273 isn_T *isn;
1274
1275 RETURN_OK_IF_SKIP(cctx);
1276 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1277 return FAIL;
1278 return OK;
1279}
1280
1281/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 * Generate an ISN_LOADS instruction.
1283 */
1284 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001285generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 int sid,
1290 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291{
1292 isn_T *isn;
1293
Bram Moolenaar080457c2020-03-03 21:53:32 +01001294 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 if (isn_type == ISN_LOADS)
1296 isn = generate_instr_type(cctx, isn_type, type);
1297 else
1298 isn = generate_instr_drop(cctx, isn_type, 1);
1299 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001301 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1302 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303
1304 return OK;
1305}
1306
1307/*
1308 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1309 */
1310 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001311generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 cctx_T *cctx,
1313 isntype_T isn_type,
1314 int sid,
1315 int idx,
1316 type_T *type)
1317{
1318 isn_T *isn;
1319
Bram Moolenaar080457c2020-03-03 21:53:32 +01001320 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 if (isn_type == ISN_LOADSCRIPT)
1322 isn = generate_instr_type(cctx, isn_type, type);
1323 else
1324 isn = generate_instr_drop(cctx, isn_type, 1);
1325 if (isn == NULL)
1326 return FAIL;
1327 isn->isn_arg.script.script_sid = sid;
1328 isn->isn_arg.script.script_idx = idx;
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_NEWLIST instruction.
1334 */
1335 static int
1336generate_NEWLIST(cctx_T *cctx, int count)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 type_T *type;
1341 type_T *member;
1342
Bram Moolenaar080457c2020-03-03 21:53:32 +01001343 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1345 return FAIL;
1346 isn->isn_arg.number = count;
1347
Bram Moolenaar127542b2020-08-09 17:22:04 +02001348 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001349 if (count == 0)
1350 member = &t_void;
1351 else
1352 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001353 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1354 cctx->ctx_type_list);
1355 type = get_list_type(member, cctx->ctx_type_list);
1356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 // drop the value types
1358 stack->ga_len -= count;
1359
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 // add the list type to the type stack
1361 if (ga_grow(stack, 1) == FAIL)
1362 return FAIL;
1363 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1364 ++stack->ga_len;
1365
1366 return OK;
1367}
1368
1369/*
1370 * Generate an ISN_NEWDICT instruction.
1371 */
1372 static int
1373generate_NEWDICT(cctx_T *cctx, int count)
1374{
1375 isn_T *isn;
1376 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 type_T *type;
1378 type_T *member;
1379
Bram Moolenaar080457c2020-03-03 21:53:32 +01001380 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1382 return FAIL;
1383 isn->isn_arg.number = count;
1384
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001385 if (count == 0)
1386 member = &t_void;
1387 else
1388 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001389 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1390 cctx->ctx_type_list);
1391 type = get_dict_type(member, cctx->ctx_type_list);
1392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 // drop the key and value types
1394 stack->ga_len -= 2 * count;
1395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 // add the dict type to the type stack
1397 if (ga_grow(stack, 1) == FAIL)
1398 return FAIL;
1399 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1400 ++stack->ga_len;
1401
1402 return OK;
1403}
1404
1405/*
1406 * Generate an ISN_FUNCREF instruction.
1407 */
1408 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001409generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410{
1411 isn_T *isn;
1412 garray_T *stack = &cctx->ctx_type_stack;
1413
Bram Moolenaar080457c2020-03-03 21:53:32 +01001414 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1416 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001417 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001418 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419
1420 if (ga_grow(stack, 1) == FAIL)
1421 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001422 ((type_T **)stack->ga_data)[stack->ga_len] =
1423 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 ++stack->ga_len;
1425
1426 return OK;
1427}
1428
1429/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001430 * Generate an ISN_NEWFUNC instruction.
1431 */
1432 static int
1433generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1434{
1435 isn_T *isn;
1436 char_u *name;
1437
1438 RETURN_OK_IF_SKIP(cctx);
1439 name = vim_strsave(lambda_name);
1440 if (name == NULL)
1441 return FAIL;
1442 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1443 return FAIL;
1444 isn->isn_arg.newfunc.nf_lambda = name;
1445 isn->isn_arg.newfunc.nf_global = func_name;
1446
1447 return OK;
1448}
1449
1450/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001451 * Generate an ISN_DEF instruction: list functions
1452 */
1453 static int
1454generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1455{
1456 isn_T *isn;
1457
1458 RETURN_OK_IF_SKIP(cctx);
1459 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1460 return FAIL;
1461 if (len > 0)
1462 {
1463 isn->isn_arg.string = vim_strnsave(name, len);
1464 if (isn->isn_arg.string == NULL)
1465 return FAIL;
1466 }
1467 return OK;
1468}
1469
1470/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 * Generate an ISN_JUMP instruction.
1472 */
1473 static int
1474generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1475{
1476 isn_T *isn;
1477 garray_T *stack = &cctx->ctx_type_stack;
1478
Bram Moolenaar080457c2020-03-03 21:53:32 +01001479 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1481 return FAIL;
1482 isn->isn_arg.jump.jump_when = when;
1483 isn->isn_arg.jump.jump_where = where;
1484
1485 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1486 --stack->ga_len;
1487
1488 return OK;
1489}
1490
1491 static int
1492generate_FOR(cctx_T *cctx, int loop_idx)
1493{
1494 isn_T *isn;
1495 garray_T *stack = &cctx->ctx_type_stack;
1496
Bram Moolenaar080457c2020-03-03 21:53:32 +01001497 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1499 return FAIL;
1500 isn->isn_arg.forloop.for_idx = loop_idx;
1501
1502 if (ga_grow(stack, 1) == FAIL)
1503 return FAIL;
1504 // type doesn't matter, will be stored next
1505 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1506 ++stack->ga_len;
1507
1508 return OK;
1509}
1510
1511/*
1512 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001513 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 * Return FAIL if the number of arguments is wrong.
1515 */
1516 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001517generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518{
1519 isn_T *isn;
1520 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001521 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001522 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523
Bram Moolenaar080457c2020-03-03 21:53:32 +01001524 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001525 argoff = check_internal_func(func_idx, argcount);
1526 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 return FAIL;
1528
Bram Moolenaar389df252020-07-09 21:20:47 +02001529 if (method_call && argoff > 1)
1530 {
1531 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1532 return FAIL;
1533 isn->isn_arg.shuffle.shfl_item = argcount;
1534 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1535 }
1536
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001537 if (argcount > 0)
1538 {
1539 // Check the types of the arguments.
1540 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1541 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001542 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001543 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.bfunc.cbf_idx = func_idx;
1548 isn->isn_arg.bfunc.cbf_argcount = argcount;
1549
Bram Moolenaar94738d82020-10-21 14:25:07 +02001550 // Drop the argument types and push the return type.
1551 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 if (ga_grow(stack, 1) == FAIL)
1553 return FAIL;
1554 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001555 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001556 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
1558 return OK;
1559}
1560
1561/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001562 * Generate an ISN_LISTAPPEND instruction. Works like add().
1563 * Argument count is already checked.
1564 */
1565 static int
1566generate_LISTAPPEND(cctx_T *cctx)
1567{
1568 garray_T *stack = &cctx->ctx_type_stack;
1569 type_T *list_type;
1570 type_T *item_type;
1571 type_T *expected;
1572
1573 // Caller already checked that list_type is a list.
1574 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1575 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1576 expected = list_type->tt_member;
1577 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1578 return FAIL;
1579
1580 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1581 return FAIL;
1582
1583 --stack->ga_len; // drop the argument
1584 return OK;
1585}
1586
1587/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001588 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1589 * Argument count is already checked.
1590 */
1591 static int
1592generate_BLOBAPPEND(cctx_T *cctx)
1593{
1594 garray_T *stack = &cctx->ctx_type_stack;
1595 type_T *item_type;
1596
1597 // Caller already checked that blob_type is a blob.
1598 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1599 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1600 return FAIL;
1601
1602 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1603 return FAIL;
1604
1605 --stack->ga_len; // drop the argument
1606 return OK;
1607}
1608
1609/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 * Generate an ISN_DCALL or ISN_UCALL instruction.
1611 * Return FAIL if the number of arguments is wrong.
1612 */
1613 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001614generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615{
1616 isn_T *isn;
1617 garray_T *stack = &cctx->ctx_type_stack;
1618 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001619 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620
Bram Moolenaar080457c2020-03-03 21:53:32 +01001621 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 if (argcount > regular_args && !has_varargs(ufunc))
1623 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001624 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 return FAIL;
1626 }
1627 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1628 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001629 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 return FAIL;
1631 }
1632
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001634 {
1635 int i;
1636
1637 for (i = 0; i < argcount; ++i)
1638 {
1639 type_T *expected;
1640 type_T *actual;
1641
1642 if (i < regular_args)
1643 {
1644 if (ufunc->uf_arg_types == NULL)
1645 continue;
1646 expected = ufunc->uf_arg_types[i];
1647 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001648 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1649 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001650 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001651 else
1652 expected = ufunc->uf_va_type->tt_member;
1653 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001654 if (need_type(actual, expected, -argcount + i, cctx,
1655 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001656 {
1657 arg_type_mismatch(expected, actual, i + 1);
1658 return FAIL;
1659 }
1660 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001661 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001662 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1663 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001664 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001665 }
1666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001668 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001669 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001671 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 {
1673 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1674 isn->isn_arg.dfunc.cdf_argcount = argcount;
1675 }
1676 else
1677 {
1678 // A user function may be deleted and redefined later, can't use the
1679 // ufunc pointer, need to look it up again at runtime.
1680 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1681 isn->isn_arg.ufunc.cuf_argcount = argcount;
1682 }
1683
1684 stack->ga_len -= argcount; // drop the arguments
1685 if (ga_grow(stack, 1) == FAIL)
1686 return FAIL;
1687 // add return value
1688 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1689 ++stack->ga_len;
1690
1691 return OK;
1692}
1693
1694/*
1695 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1696 */
1697 static int
1698generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1699{
1700 isn_T *isn;
1701 garray_T *stack = &cctx->ctx_type_stack;
1702
Bram Moolenaar080457c2020-03-03 21:53:32 +01001703 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1705 return FAIL;
1706 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1707 isn->isn_arg.ufunc.cuf_argcount = argcount;
1708
1709 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001710 if (ga_grow(stack, 1) == FAIL)
1711 return FAIL;
1712 // add return value
1713 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1714 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715
1716 return OK;
1717}
1718
1719/*
1720 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001721 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 */
1723 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001724generate_PCALL(
1725 cctx_T *cctx,
1726 int argcount,
1727 char_u *name,
1728 type_T *type,
1729 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730{
1731 isn_T *isn;
1732 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001733 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734
Bram Moolenaar080457c2020-03-03 21:53:32 +01001735 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001736
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001737 if (type->tt_type == VAR_ANY)
1738 ret_type = &t_any;
1739 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001740 {
1741 if (type->tt_argcount != -1)
1742 {
1743 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1744
1745 if (argcount < type->tt_min_argcount - varargs)
1746 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001747 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001748 return FAIL;
1749 }
1750 if (!varargs && argcount > type->tt_argcount)
1751 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001752 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001753 return FAIL;
1754 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001755 if (type->tt_args != NULL)
1756 {
1757 int i;
1758
1759 for (i = 0; i < argcount; ++i)
1760 {
1761 int offset = -argcount + i - 1;
1762 type_T *actual = ((type_T **)stack->ga_data)[
1763 stack->ga_len + offset];
1764 type_T *expected;
1765
1766 if (varargs && i >= type->tt_min_argcount - 1)
1767 expected = type->tt_args[
1768 type->tt_min_argcount - 1]->tt_member;
1769 else
1770 expected = type->tt_args[i];
1771 if (need_type(actual, expected, offset,
1772 cctx, TRUE, FALSE) == FAIL)
1773 {
1774 arg_type_mismatch(expected, actual, i + 1);
1775 return FAIL;
1776 }
1777 }
1778 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001779 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001780 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001781 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001782 else
1783 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001784 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001785 return FAIL;
1786 }
1787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1789 return FAIL;
1790 isn->isn_arg.pfunc.cpf_top = at_top;
1791 isn->isn_arg.pfunc.cpf_argcount = argcount;
1792
1793 stack->ga_len -= argcount; // drop the arguments
1794
1795 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001796 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001798 // If partial is above the arguments it must be cleared and replaced with
1799 // the return value.
1800 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1801 return FAIL;
1802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 return OK;
1804}
1805
1806/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001807 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 */
1809 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001810generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811{
1812 isn_T *isn;
1813 garray_T *stack = &cctx->ctx_type_stack;
1814 type_T *type;
1815
Bram Moolenaar080457c2020-03-03 21:53:32 +01001816 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001817 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001819 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001821 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001823 if (type->tt_type != VAR_DICT && type != &t_any)
1824 {
1825 emsg(_(e_dictreq));
1826 return FAIL;
1827 }
1828 // change dict type to dict member type
1829 if (type->tt_type == VAR_DICT)
1830 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831
1832 return OK;
1833}
1834
1835/*
1836 * Generate an ISN_ECHO instruction.
1837 */
1838 static int
1839generate_ECHO(cctx_T *cctx, int with_white, int count)
1840{
1841 isn_T *isn;
1842
Bram Moolenaar080457c2020-03-03 21:53:32 +01001843 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1845 return FAIL;
1846 isn->isn_arg.echo.echo_with_white = with_white;
1847 isn->isn_arg.echo.echo_count = count;
1848
1849 return OK;
1850}
1851
Bram Moolenaarad39c092020-02-26 18:23:43 +01001852/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001853 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001854 */
1855 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001856generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001857{
1858 isn_T *isn;
1859
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001860 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001861 return FAIL;
1862 isn->isn_arg.number = count;
1863
1864 return OK;
1865}
1866
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001867/*
1868 * Generate an ISN_PUT instruction.
1869 */
1870 static int
1871generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1872{
1873 isn_T *isn;
1874
1875 RETURN_OK_IF_SKIP(cctx);
1876 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1877 return FAIL;
1878 isn->isn_arg.put.put_regname = regname;
1879 isn->isn_arg.put.put_lnum = lnum;
1880 return OK;
1881}
1882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 static int
1884generate_EXEC(cctx_T *cctx, char_u *line)
1885{
1886 isn_T *isn;
1887
Bram Moolenaar080457c2020-03-03 21:53:32 +01001888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1890 return FAIL;
1891 isn->isn_arg.string = vim_strsave(line);
1892 return OK;
1893}
1894
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001895 static int
1896generate_EXECCONCAT(cctx_T *cctx, int count)
1897{
1898 isn_T *isn;
1899
1900 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1901 return FAIL;
1902 isn->isn_arg.number = count;
1903 return OK;
1904}
1905
Bram Moolenaar08597872020-12-10 19:43:40 +01001906/*
1907 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1908 */
1909 static int
1910generate_RANGE(cctx_T *cctx, char_u *range)
1911{
1912 isn_T *isn;
1913 garray_T *stack = &cctx->ctx_type_stack;
1914
1915 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1916 return FAIL;
1917 isn->isn_arg.string = range;
1918
1919 if (ga_grow(stack, 1) == FAIL)
1920 return FAIL;
1921 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1922 ++stack->ga_len;
1923 return OK;
1924}
1925
Bram Moolenaar792f7862020-11-23 08:31:18 +01001926 static int
1927generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1928{
1929 isn_T *isn;
1930
1931 RETURN_OK_IF_SKIP(cctx);
1932 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1933 return FAIL;
1934 isn->isn_arg.unpack.unp_count = var_count;
1935 isn->isn_arg.unpack.unp_semicolon = semicolon;
1936 return OK;
1937}
1938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001940 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001941 */
1942 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001943generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001944{
1945 isn_T *isn;
1946
Bram Moolenaar02194d22020-10-24 23:08:38 +02001947 if (cmod->cmod_flags != 0
1948 || cmod->cmod_split != 0
1949 || cmod->cmod_verbose != 0
1950 || cmod->cmod_tab != 0
1951 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001952 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001953 cctx->ctx_has_cmdmod = TRUE;
1954
1955 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001956 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001957 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1958 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1959 return FAIL;
1960 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001961 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001962 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001963 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001964
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001965 return OK;
1966}
1967
1968 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001969generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001970{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001971 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1972 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001973 return OK;
1974}
1975
1976/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001978 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001980 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001981reserve_local(
1982 cctx_T *cctx,
1983 char_u *name,
1984 size_t len,
1985 int isConst,
1986 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 lvar_T *lvar;
1989
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001990 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001991 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001992 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001993 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 }
1995
1996 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001997 return NULL;
1998 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02001999 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002001 // Every local variable uses the next entry on the stack. We could re-use
2002 // the last ones when leaving a scope, but then variables used in a closure
2003 // might get overwritten. To keep things simple do not re-use stack
2004 // entries. This is less efficient, but memory is cheap these days.
2005 lvar->lv_idx = cctx->ctx_locals_count++;
2006
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002007 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 lvar->lv_const = isConst;
2009 lvar->lv_type = type;
2010
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002011 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012}
2013
2014/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002015 * Remove local variables above "new_top".
2016 */
2017 static void
2018unwind_locals(cctx_T *cctx, int new_top)
2019{
2020 if (cctx->ctx_locals.ga_len > new_top)
2021 {
2022 int idx;
2023 lvar_T *lvar;
2024
2025 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2026 {
2027 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2028 vim_free(lvar->lv_name);
2029 }
2030 }
2031 cctx->ctx_locals.ga_len = new_top;
2032}
2033
2034/*
2035 * Free all local variables.
2036 */
2037 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002038free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002039{
2040 unwind_locals(cctx, 0);
2041 ga_clear(&cctx->ctx_locals);
2042}
2043
2044/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 * Find "name" in script-local items of script "sid".
2046 * Returns the index in "sn_var_vals" if found.
2047 * If found but not in "sn_var_vals" returns -1.
2048 * If not found returns -2.
2049 */
2050 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002051get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052{
2053 hashtab_T *ht;
2054 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002055 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002056 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 int idx;
2058
Bram Moolenaare3d46852020-08-29 13:39:17 +02002059 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002061 if (sid == current_sctx.sc_sid)
2062 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002063 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002064
2065 if (sav == NULL)
2066 return -2;
2067 idx = sav->sav_var_vals_idx;
2068 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2069 if (check_writable && sv->sv_const)
2070 semsg(_(e_readonlyvar), name);
2071 return idx;
2072 }
2073
2074 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 ht = &SCRIPT_VARS(sid);
2076 di = find_var_in_ht(ht, 0, name, TRUE);
2077 if (di == NULL)
2078 return -2;
2079
2080 // Now find the svar_T index in sn_var_vals.
2081 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2082 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002083 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 if (sv->sv_tv == &di->di_tv)
2085 {
2086 if (check_writable && sv->sv_const)
2087 semsg(_(e_readonlyvar), name);
2088 return idx;
2089 }
2090 }
2091 return -1;
2092}
2093
2094/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002095 * Find "name" in imported items of the current script or in "cctx" if not
2096 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 */
2098 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002099find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 int idx;
2102
Bram Moolenaare3d46852020-08-29 13:39:17 +02002103 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002104 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 if (cctx != NULL)
2106 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2107 {
2108 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2109 + idx;
2110
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002111 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2112 : STRLEN(import->imp_name) == len
2113 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 return import;
2115 }
2116
Bram Moolenaarefa94442020-08-08 22:16:00 +02002117 return find_imported_in_script(name, len, current_sctx.sc_sid);
2118}
2119
2120 imported_T *
2121find_imported_in_script(char_u *name, size_t len, int sid)
2122{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002123 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002124 int idx;
2125
Bram Moolenaare3d46852020-08-29 13:39:17 +02002126 if (!SCRIPT_ID_VALID(sid))
2127 return NULL;
2128 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2130 {
2131 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2132
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002133 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2134 : STRLEN(import->imp_name) == len
2135 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 return import;
2137 }
2138 return NULL;
2139}
2140
2141/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002142 * Free all imported variables.
2143 */
2144 static void
2145free_imported(cctx_T *cctx)
2146{
2147 int idx;
2148
2149 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2150 {
2151 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2152
2153 vim_free(import->imp_name);
2154 }
2155 ga_clear(&cctx->ctx_imports);
2156}
2157
2158/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002159 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002160 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002161 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002162vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002163{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002164 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002165}
2166
2167/*
2168 * Return a pointer to the next line that isn't empty or only contains a
2169 * comment. Skips over white space.
2170 * Returns NULL if there is none.
2171 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002172 char_u *
2173peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002174{
2175 int lnum = cctx->ctx_lnum;
2176
2177 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2178 {
2179 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002180 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002181
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002182 // ignore NULLs inserted for continuation lines
2183 if (line != NULL)
2184 {
2185 p = skipwhite(line);
2186 if (*p != NUL && !vim9_comment_start(p))
2187 return p;
2188 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002189 }
2190 return NULL;
2191}
2192
2193/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002194 * Called when checking for a following operator at "arg". When the rest of
2195 * the line is empty or only a comment, peek the next line. If there is a next
2196 * line return a pointer to it and set "nextp".
2197 * Otherwise skip over white space.
2198 */
2199 static char_u *
2200may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2201{
2202 char_u *p = skipwhite(arg);
2203
2204 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002205 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002206 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002207 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002208 if (*nextp != NULL)
2209 return *nextp;
2210 }
2211 return p;
2212}
2213
2214/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002215 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002216 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002217 * Returns NULL when at the end.
2218 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002219 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002220next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002221{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002222 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002223
2224 do
2225 {
2226 ++cctx->ctx_lnum;
2227 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002228 {
2229 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002230 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002231 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002232 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002233 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002234 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002235 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002236 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002237 return line;
2238}
2239
2240/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002241 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002242 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002243 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2244 */
2245 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002246may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002247{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002248 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002249 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002250 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002251
2252 if (next == NULL)
2253 return FAIL;
2254 *arg = skipwhite(next);
2255 }
2256 return OK;
2257}
2258
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002259/*
2260 * Idem, and give an error when failed.
2261 */
2262 static int
2263may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2264{
2265 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2266 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002267 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002268 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002269 return FAIL;
2270 }
2271 return OK;
2272}
2273
2274
Bram Moolenaara5565e42020-05-09 15:44:01 +02002275// Structure passed between the compile_expr* functions to keep track of
2276// constants that have been parsed but for which no code was produced yet. If
2277// possible expressions on these constants are applied at compile time. If
2278// that is not possible, the code to push the constants needs to be generated
2279// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002280// Using 50 should be more than enough of 5 levels of ().
2281#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002282typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002283 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002284 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002285 int pp_is_const; // all generated code was constants, used for a
2286 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002287} ppconst_T;
2288
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002289static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002290static int compile_expr0(char_u **arg, cctx_T *cctx);
2291static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2292
Bram Moolenaara5565e42020-05-09 15:44:01 +02002293/*
2294 * Generate a PUSH instruction for "tv".
2295 * "tv" will be consumed or cleared.
2296 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2297 */
2298 static int
2299generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2300{
2301 if (tv != NULL)
2302 {
2303 switch (tv->v_type)
2304 {
2305 case VAR_UNKNOWN:
2306 break;
2307 case VAR_BOOL:
2308 generate_PUSHBOOL(cctx, tv->vval.v_number);
2309 break;
2310 case VAR_SPECIAL:
2311 generate_PUSHSPEC(cctx, tv->vval.v_number);
2312 break;
2313 case VAR_NUMBER:
2314 generate_PUSHNR(cctx, tv->vval.v_number);
2315 break;
2316#ifdef FEAT_FLOAT
2317 case VAR_FLOAT:
2318 generate_PUSHF(cctx, tv->vval.v_float);
2319 break;
2320#endif
2321 case VAR_BLOB:
2322 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2323 tv->vval.v_blob = NULL;
2324 break;
2325 case VAR_STRING:
2326 generate_PUSHS(cctx, tv->vval.v_string);
2327 tv->vval.v_string = NULL;
2328 break;
2329 default:
2330 iemsg("constant type not supported");
2331 clear_tv(tv);
2332 return FAIL;
2333 }
2334 tv->v_type = VAR_UNKNOWN;
2335 }
2336 return OK;
2337}
2338
2339/*
2340 * Generate code for any ppconst entries.
2341 */
2342 static int
2343generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2344{
2345 int i;
2346 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002347 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002348
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002349 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002350 for (i = 0; i < ppconst->pp_used; ++i)
2351 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2352 ret = FAIL;
2353 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002354 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002355 return ret;
2356}
2357
2358/*
2359 * Clear ppconst constants. Used when failing.
2360 */
2361 static void
2362clear_ppconst(ppconst_T *ppconst)
2363{
2364 int i;
2365
2366 for (i = 0; i < ppconst->pp_used; ++i)
2367 clear_tv(&ppconst->pp_tv[i]);
2368 ppconst->pp_used = 0;
2369}
2370
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002371/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002372 * Generate an instruction to load script-local variable "name", without the
2373 * leading "s:".
2374 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 */
2376 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002377compile_load_scriptvar(
2378 cctx_T *cctx,
2379 char_u *name, // variable NUL terminated
2380 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002381 char_u **end, // end of variable
2382 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002384 scriptitem_T *si;
2385 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 imported_T *import;
2387
Bram Moolenaare3d46852020-08-29 13:39:17 +02002388 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2389 return FAIL;
2390 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002391 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002392 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002394 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002395 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2396 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398 if (idx >= 0)
2399 {
2400 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2401
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002402 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 current_sctx.sc_sid, idx, sv->sv_type);
2404 return OK;
2405 }
2406
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002407 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 if (import != NULL)
2409 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002410 if (import->imp_all)
2411 {
2412 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002413 char_u *exp_name;
2414 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002415 ufunc_T *ufunc;
2416 type_T *type;
2417
2418 // Used "import * as Name", need to lookup the member.
2419 if (*p != '.')
2420 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002421 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002422 return FAIL;
2423 }
2424 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002425 if (VIM_ISWHITE(*p))
2426 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002427 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002428 return FAIL;
2429 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002430
Bram Moolenaar1c991142020-07-04 13:15:31 +02002431 // isolate one name
2432 exp_name = p;
2433 while (eval_isnamec(*p))
2434 ++p;
2435 cc = *p;
2436 *p = NUL;
2437
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002438 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002439 *p = cc;
2440 p = skipwhite(p);
2441
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002442 // TODO: what if it is a function?
2443 if (idx < 0)
2444 return FAIL;
2445 *end = p;
2446
2447 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2448 import->imp_sid,
2449 idx,
2450 type);
2451 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002452 else if (import->imp_funcname != NULL)
2453 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002454 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002455 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2456 import->imp_sid,
2457 import->imp_var_vals_idx,
2458 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 return OK;
2460 }
2461
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002462 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002463 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 return FAIL;
2465}
2466
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002467 static int
2468generate_funcref(cctx_T *cctx, char_u *name)
2469{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002470 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002471
2472 if (ufunc == NULL)
2473 return FAIL;
2474
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002475 // Need to compile any default values to get the argument types.
2476 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2477 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2478 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002479 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002480}
2481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482/*
2483 * Compile a variable name into a load instruction.
2484 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002485 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 * When "error" is FALSE do not give an error when not found.
2487 */
2488 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002489compile_load(
2490 char_u **arg,
2491 char_u *end_arg,
2492 cctx_T *cctx,
2493 int is_expr,
2494 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495{
2496 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002497 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002498 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002500 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501
2502 if (*(*arg + 1) == ':')
2503 {
2504 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002505 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002506 {
2507 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002509 switch (**arg)
2510 {
2511 case 'g': isn_type = ISN_LOADGDICT; break;
2512 case 'w': isn_type = ISN_LOADWDICT; break;
2513 case 't': isn_type = ISN_LOADTDICT; break;
2514 case 'b': isn_type = ISN_LOADBDICT; break;
2515 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002516 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002517 goto theend;
2518 }
2519 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2520 goto theend;
2521 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 else
2524 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002525 isntype_T isn_type = ISN_DROP;
2526
2527 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2528 if (name == NULL)
2529 return FAIL;
2530
2531 switch (**arg)
2532 {
2533 case 'v': res = generate_LOADV(cctx, name, error);
2534 break;
2535 case 's': res = compile_load_scriptvar(cctx, name,
2536 NULL, NULL, error);
2537 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002538 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2539 isn_type = ISN_LOADG;
2540 else
2541 {
2542 isn_type = ISN_LOADAUTO;
2543 vim_free(name);
2544 name = vim_strnsave(*arg, end - *arg);
2545 if (name == NULL)
2546 return FAIL;
2547 }
2548 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002549 case 'w': isn_type = ISN_LOADW; break;
2550 case 't': isn_type = ISN_LOADT; break;
2551 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002552 default: // cannot happen, just in case
2553 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002554 goto theend;
2555 }
2556 if (isn_type != ISN_DROP)
2557 {
2558 // Global, Buffer-local, Window-local and Tabpage-local
2559 // variables can be defined later, thus we don't check if it
2560 // exists, give error at runtime.
2561 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 }
2564 }
2565 else
2566 {
2567 size_t len = end - *arg;
2568 int idx;
2569 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002570 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571
2572 name = vim_strnsave(*arg, end - *arg);
2573 if (name == NULL)
2574 return FAIL;
2575
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002576 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002578 if (!gen_load_outer)
2579 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 }
2581 else
2582 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002583 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002584
Bram Moolenaar709664c2020-12-12 14:33:41 +01002585 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002587 type = lvar.lv_type;
2588 idx = lvar.lv_idx;
2589 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002590 gen_load_outer = TRUE;
2591 else
2592 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 else
2595 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002596 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002597 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002598 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002599 || find_imported(name, 0, cctx) != NULL)
2600 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002601
Bram Moolenaar0f769812020-09-12 18:32:34 +02002602 // When evaluating an expression and the name starts with an
2603 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002604 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002605 if (res == FAIL && is_expr
2606 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002607 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 }
2609 }
2610 if (gen_load)
2611 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002612 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002613 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002614 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002615 cctx->ctx_outer_used = TRUE;
2616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 }
2618
2619 *arg = end;
2620
2621theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002622 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002623 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 vim_free(name);
2625 return res;
2626}
2627
2628/*
2629 * Compile the argument expressions.
2630 * "arg" points to just after the "(" and is advanced to after the ")"
2631 */
2632 static int
2633compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2634{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002635 char_u *p = *arg;
2636 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002637 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638
Bram Moolenaare6085c52020-04-12 20:19:16 +02002639 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002641 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2642 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002643 if (*p == ')')
2644 {
2645 *arg = p + 1;
2646 return OK;
2647 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002648 if (must_end)
2649 {
2650 semsg(_(e_missing_comma_before_argument_str), p);
2651 return FAIL;
2652 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002653
Bram Moolenaara5565e42020-05-09 15:44:01 +02002654 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 return FAIL;
2656 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002657
2658 if (*p != ',' && *skipwhite(p) == ',')
2659 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002660 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002661 p = skipwhite(p);
2662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002664 {
2665 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002666 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002667 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002668 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002669 else
2670 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002671 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002672 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002674failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002675 emsg(_(e_missing_close));
2676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677}
2678
2679/*
2680 * Compile a function call: name(arg1, arg2)
2681 * "arg" points to "name", "arg + varlen" to the "(".
2682 * "argcount_init" is 1 for "value->method()"
2683 * Instructions:
2684 * EVAL arg1
2685 * EVAL arg2
2686 * BCALL / DCALL / UCALL
2687 */
2688 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002689compile_call(
2690 char_u **arg,
2691 size_t varlen,
2692 cctx_T *cctx,
2693 ppconst_T *ppconst,
2694 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695{
2696 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002697 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 int argcount = argcount_init;
2699 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002700 char_u fname_buf[FLEN_FIXED + 1];
2701 char_u *tofree = NULL;
2702 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002703 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002704 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002705 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706
Bram Moolenaara5565e42020-05-09 15:44:01 +02002707 // we can evaluate "has('name')" at compile time
2708 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2709 {
2710 char_u *s = skipwhite(*arg + varlen + 1);
2711 typval_T argvars[2];
2712
2713 argvars[0].v_type = VAR_UNKNOWN;
2714 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002715 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002716 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002717 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002718 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002719 if (*s == ')' && argvars[0].v_type == VAR_STRING
2720 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002721 {
2722 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2723
2724 *arg = s + 1;
2725 argvars[1].v_type = VAR_UNKNOWN;
2726 tv->v_type = VAR_NUMBER;
2727 tv->vval.v_number = 0;
2728 f_has(argvars, tv);
2729 clear_tv(&argvars[0]);
2730 ++ppconst->pp_used;
2731 return OK;
2732 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002733 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002734 }
2735
2736 if (generate_ppconst(cctx, ppconst) == FAIL)
2737 return FAIL;
2738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 if (varlen >= sizeof(namebuf))
2740 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002741 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 return FAIL;
2743 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002744 vim_strncpy(namebuf, *arg, varlen);
2745 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746
2747 *arg = skipwhite(*arg + varlen + 1);
2748 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002749 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750
Bram Moolenaar03290b82020-12-19 16:30:44 +01002751 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002752 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 {
2754 int idx;
2755
2756 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002757 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002759 {
2760 if (STRCMP(name, "add") == 0 && argcount == 2)
2761 {
2762 garray_T *stack = &cctx->ctx_type_stack;
2763 type_T *type = ((type_T **)stack->ga_data)[
2764 stack->ga_len - 2];
2765
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002766 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002767 if (type->tt_type == VAR_LIST)
2768 {
2769 // inline "add(list, item)" so that the type can be checked
2770 res = generate_LISTAPPEND(cctx);
2771 idx = -1;
2772 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002773 else if (type->tt_type == VAR_BLOB)
2774 {
2775 // inline "add(blob, nr)" so that the type can be checked
2776 res = generate_BLOBAPPEND(cctx);
2777 idx = -1;
2778 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002779 }
2780
2781 if (idx >= 0)
2782 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2783 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002784 else
2785 semsg(_(e_unknownfunc), namebuf);
2786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 }
2788
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002789 // An argument or local variable can be a function reference, this
2790 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002791 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002792 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002793 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002794 // If we can find the function by name generate the right call.
2795 // Skip global functions here, a local funcref takes precedence.
2796 ufunc = find_func(name, FALSE, cctx);
2797 if (ufunc != NULL && !func_is_global(ufunc))
2798 {
2799 res = generate_CALL(cctx, ufunc, argcount);
2800 goto theend;
2801 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
2804 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002805 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002806 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002808 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002809 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002810 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002811 garray_T *stack = &cctx->ctx_type_stack;
2812 type_T *type;
2813
2814 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2815 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002816 goto theend;
2817 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818
Bram Moolenaar0f769812020-09-12 18:32:34 +02002819 // If we can find a global function by name generate the right call.
2820 if (ufunc != NULL)
2821 {
2822 res = generate_CALL(cctx, ufunc, argcount);
2823 goto theend;
2824 }
2825
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002826 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002827 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002828 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002829 res = generate_UCALL(cctx, name, argcount);
2830 else
2831 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002832
2833theend:
2834 vim_free(tofree);
2835 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836}
2837
2838// like NAMESPACE_CHAR but with 'a' and 'l'.
2839#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2840
2841/*
2842 * Find the end of a variable or function name. Unlike find_name_end() this
2843 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002844 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 * Return a pointer to just after the name. Equal to "arg" if there is no
2846 * valid name.
2847 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002848 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002849to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850{
2851 char_u *p;
2852
2853 // Quick check for valid starting character.
2854 if (!eval_isnamec1(*arg))
2855 return arg;
2856
2857 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2858 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2859 // and can be used in slice "[n:]".
2860 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002861 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2863 break;
2864 return p;
2865}
2866
2867/*
2868 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002869 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 */
2871 char_u *
2872to_name_const_end(char_u *arg)
2873{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002874 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 typval_T rettv;
2876
2877 if (p == arg && *arg == '[')
2878 {
2879
2880 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002881 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 p = arg;
2883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 return p;
2885}
2886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887/*
2888 * parse a list: [expr, expr]
2889 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002890 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 */
2892 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002893compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894{
2895 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002896 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002898 int is_const;
2899 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002901 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002903 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002904 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002905 semsg(_(e_list_end), *arg);
2906 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002907 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002908 if (*p == ',')
2909 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002910 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002911 return FAIL;
2912 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002913 if (*p == ']')
2914 {
2915 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002916 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002917 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002918 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002919 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002920 if (!is_const)
2921 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 ++count;
2923 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002926 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2927 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002928 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002929 return FAIL;
2930 }
2931 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002932 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 p = skipwhite(p);
2934 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002935 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002937 ppconst->pp_is_const = is_all_const;
2938 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939}
2940
2941/*
2942 * parse a lambda: {arg, arg -> expr}
2943 * "*arg" points to the '{'.
2944 */
2945 static int
2946compile_lambda(char_u **arg, cctx_T *cctx)
2947{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 typval_T rettv;
2949 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002950 evalarg_T evalarg;
2951
2952 CLEAR_FIELD(evalarg);
2953 evalarg.eval_flags = EVAL_EVALUATE;
2954 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955
2956 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002957 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002958 {
2959 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002961 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002964 ++ufunc->uf_refcount;
2965 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966
2967 // The function will have one line: "return {expr}".
2968 // Compile it into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002969 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002971 clear_evalarg(&evalarg, NULL);
2972
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002973 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002974 {
2975 // The return type will now be known.
2976 set_function_type(ufunc);
2977
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002978 // The function reference count will be 1. When the ISN_FUNCREF
2979 // instruction is deleted the reference count is decremented and the
2980 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002981 return generate_FUNCREF(cctx, ufunc);
2982 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002983
2984 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 return FAIL;
2986}
2987
2988/*
2989 * Compile a lamda call: expr->{lambda}(args)
2990 * "arg" points to the "{".
2991 */
2992 static int
2993compile_lambda_call(char_u **arg, cctx_T *cctx)
2994{
2995 ufunc_T *ufunc;
2996 typval_T rettv;
2997 int argcount = 1;
2998 int ret = FAIL;
2999
3000 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003001 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 return FAIL;
3003
3004 if (**arg != '(')
3005 {
3006 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003007 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 else
3009 semsg(_(e_missing_paren), "lambda");
3010 clear_tv(&rettv);
3011 return FAIL;
3012 }
3013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 ufunc = rettv.vval.v_partial->pt_func;
3015 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003016 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003017 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003018
Bram Moolenaara05e5242020-09-19 18:19:19 +02003019 // The function will have one line: "return {expr}". Compile it into
3020 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003021 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022
3023 // compile the arguments
3024 *arg = skipwhite(*arg + 1);
3025 if (compile_arguments(arg, cctx, &argcount) == OK)
3026 // call the compiled function
3027 ret = generate_CALL(cctx, ufunc, argcount);
3028
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003029 if (ret == FAIL)
3030 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 return ret;
3032}
3033
3034/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003035 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003037 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 */
3039 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003040compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041{
3042 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003043 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 int count = 0;
3045 dict_T *d = dict_alloc();
3046 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003047 char_u *whitep = *arg;
3048 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003049 int is_const;
3050 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051
3052 if (d == NULL)
3053 return FAIL;
3054 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003055 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003057 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058
Bram Moolenaar23c55272020-06-21 16:58:13 +02003059 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003060 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003061 *arg = NULL;
3062 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 }
3064
3065 if (**arg == '}')
3066 break;
3067
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003068 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003070 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003072 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003073 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003074 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3077 if (isn->isn_type == ISN_PUSHS)
3078 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003079 else
3080 {
3081 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003082 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003083 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003084 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003085 return FAIL;
3086 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003087 *arg = skipwhite(*arg);
3088 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003089 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003090 emsg(_(e_missing_matching_bracket_after_dict_key));
3091 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003092 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003093 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003095 else
3096 {
3097 // {"name": value},
3098 // {'name': value},
3099 // {name: value} use "name" as a literal key
3100 key = get_literal_key(arg);
3101 if (key == NULL)
3102 return FAIL;
3103 if (generate_PUSHS(cctx, key) == FAIL)
3104 return FAIL;
3105 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106
3107 // Check for duplicate keys, if using string keys.
3108 if (key != NULL)
3109 {
3110 item = dict_find(d, key, -1);
3111 if (item != NULL)
3112 {
3113 semsg(_(e_duplicate_key), key);
3114 goto failret;
3115 }
3116 item = dictitem_alloc(key);
3117 if (item != NULL)
3118 {
3119 item->di_tv.v_type = VAR_UNKNOWN;
3120 item->di_tv.v_lock = 0;
3121 if (dict_add(d, item) == FAIL)
3122 dictitem_free(item);
3123 }
3124 }
3125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 if (**arg != ':')
3127 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003128 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003129 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003130 else
3131 semsg(_(e_missing_dict_colon), *arg);
3132 return FAIL;
3133 }
3134 whitep = *arg + 1;
3135 if (!IS_WHITE_OR_NUL(*whitep))
3136 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003137 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 return FAIL;
3139 }
3140
3141 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003142 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003143 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003144 *arg = NULL;
3145 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003146 }
3147
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003148 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003150 if (!is_const)
3151 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 ++count;
3153
Bram Moolenaar2c330432020-04-13 14:41:35 +02003154 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003155 *arg = skipwhite(*arg);
3156 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003157 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003158 *arg = NULL;
3159 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (**arg == '}')
3162 break;
3163 if (**arg != ',')
3164 {
3165 semsg(_(e_missing_dict_comma), *arg);
3166 goto failret;
3167 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003168 if (IS_WHITE_OR_NUL(*whitep))
3169 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003170 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003171 return FAIL;
3172 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003173 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003174 if (!IS_WHITE_OR_NUL(*whitep))
3175 {
3176 semsg(_(e_white_space_required_after_str), ",");
3177 return FAIL;
3178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 *arg = skipwhite(*arg + 1);
3180 }
3181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 *arg = *arg + 1;
3183
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003184 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003185 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003186 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003187 *arg += STRLEN(*arg);
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003190 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 return generate_NEWDICT(cctx, count);
3192
3193failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003194 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003195 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003196 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003197 *arg = (char_u *)"";
3198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 dict_unref(d);
3200 return FAIL;
3201}
3202
3203/*
3204 * Compile "&option".
3205 */
3206 static int
3207compile_get_option(char_u **arg, cctx_T *cctx)
3208{
3209 typval_T rettv;
3210 char_u *start = *arg;
3211 int ret;
3212
3213 // parse the option and get the current value to get the type.
3214 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003215 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 if (ret == OK)
3217 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003218 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 char_u *name = vim_strnsave(start, *arg - start);
3220 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3221
3222 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3223 vim_free(name);
3224 }
3225 clear_tv(&rettv);
3226
3227 return ret;
3228}
3229
3230/*
3231 * Compile "$VAR".
3232 */
3233 static int
3234compile_get_env(char_u **arg, cctx_T *cctx)
3235{
3236 char_u *start = *arg;
3237 int len;
3238 int ret;
3239 char_u *name;
3240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 ++*arg;
3242 len = get_env_len(arg);
3243 if (len == 0)
3244 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003245 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 return FAIL;
3247 }
3248
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003249 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 name = vim_strnsave(start, len + 1);
3251 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3252 vim_free(name);
3253 return ret;
3254}
3255
3256/*
3257 * Compile "@r".
3258 */
3259 static int
3260compile_get_register(char_u **arg, cctx_T *cctx)
3261{
3262 int ret;
3263
3264 ++*arg;
3265 if (**arg == NUL)
3266 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003267 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 return FAIL;
3269 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003270 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 {
3272 emsg_invreg(**arg);
3273 return FAIL;
3274 }
3275 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3276 ++*arg;
3277 return ret;
3278}
3279
3280/*
3281 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003282 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 */
3284 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003285apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003287 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288
3289 // this works from end to start
3290 while (p > start)
3291 {
3292 --p;
3293 if (*p == '-' || *p == '+')
3294 {
3295 // only '-' has an effect, for '+' we only check the type
3296#ifdef FEAT_FLOAT
3297 if (rettv->v_type == VAR_FLOAT)
3298 {
3299 if (*p == '-')
3300 rettv->vval.v_float = -rettv->vval.v_float;
3301 }
3302 else
3303#endif
3304 {
3305 varnumber_T val;
3306 int error = FALSE;
3307
3308 // tv_get_number_chk() accepts a string, but we don't want that
3309 // here
3310 if (check_not_string(rettv) == FAIL)
3311 return FAIL;
3312 val = tv_get_number_chk(rettv, &error);
3313 clear_tv(rettv);
3314 if (error)
3315 return FAIL;
3316 if (*p == '-')
3317 val = -val;
3318 rettv->v_type = VAR_NUMBER;
3319 rettv->vval.v_number = val;
3320 }
3321 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003322 else if (numeric_only)
3323 {
3324 ++p;
3325 break;
3326 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003327 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 {
3329 int v = tv2bool(rettv);
3330
3331 // '!' is permissive in the type.
3332 clear_tv(rettv);
3333 rettv->v_type = VAR_BOOL;
3334 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3335 }
3336 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003337 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 return OK;
3339}
3340
3341/*
3342 * Recognize v: variables that are constants and set "rettv".
3343 */
3344 static void
3345get_vim_constant(char_u **arg, typval_T *rettv)
3346{
3347 if (STRNCMP(*arg, "v:true", 6) == 0)
3348 {
3349 rettv->v_type = VAR_BOOL;
3350 rettv->vval.v_number = VVAL_TRUE;
3351 *arg += 6;
3352 }
3353 else if (STRNCMP(*arg, "v:false", 7) == 0)
3354 {
3355 rettv->v_type = VAR_BOOL;
3356 rettv->vval.v_number = VVAL_FALSE;
3357 *arg += 7;
3358 }
3359 else if (STRNCMP(*arg, "v:null", 6) == 0)
3360 {
3361 rettv->v_type = VAR_SPECIAL;
3362 rettv->vval.v_number = VVAL_NULL;
3363 *arg += 6;
3364 }
3365 else if (STRNCMP(*arg, "v:none", 6) == 0)
3366 {
3367 rettv->v_type = VAR_SPECIAL;
3368 rettv->vval.v_number = VVAL_NONE;
3369 *arg += 6;
3370 }
3371}
3372
Bram Moolenaar696ba232020-07-29 21:20:41 +02003373 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003374get_compare_type(char_u *p, int *len, int *type_is)
3375{
3376 exptype_T type = EXPR_UNKNOWN;
3377 int i;
3378
3379 switch (p[0])
3380 {
3381 case '=': if (p[1] == '=')
3382 type = EXPR_EQUAL;
3383 else if (p[1] == '~')
3384 type = EXPR_MATCH;
3385 break;
3386 case '!': if (p[1] == '=')
3387 type = EXPR_NEQUAL;
3388 else if (p[1] == '~')
3389 type = EXPR_NOMATCH;
3390 break;
3391 case '>': if (p[1] != '=')
3392 {
3393 type = EXPR_GREATER;
3394 *len = 1;
3395 }
3396 else
3397 type = EXPR_GEQUAL;
3398 break;
3399 case '<': if (p[1] != '=')
3400 {
3401 type = EXPR_SMALLER;
3402 *len = 1;
3403 }
3404 else
3405 type = EXPR_SEQUAL;
3406 break;
3407 case 'i': if (p[1] == 's')
3408 {
3409 // "is" and "isnot"; but not a prefix of a name
3410 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3411 *len = 5;
3412 i = p[*len];
3413 if (!isalnum(i) && i != '_')
3414 {
3415 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3416 *type_is = TRUE;
3417 }
3418 }
3419 break;
3420 }
3421 return type;
3422}
3423
3424/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003426 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 */
3428 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003429compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003431 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432
3433 // this works from end to start
3434 while (p > start)
3435 {
3436 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003437 while (VIM_ISWHITE(*p))
3438 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 if (*p == '-' || *p == '+')
3440 {
3441 int negate = *p == '-';
3442 isn_T *isn;
3443
3444 // TODO: check type
3445 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3446 {
3447 --p;
3448 if (*p == '-')
3449 negate = !negate;
3450 }
3451 // only '-' has an effect, for '+' we only check the type
3452 if (negate)
3453 isn = generate_instr(cctx, ISN_NEGATENR);
3454 else
3455 isn = generate_instr(cctx, ISN_CHECKNR);
3456 if (isn == NULL)
3457 return FAIL;
3458 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003459 else if (numeric_only)
3460 {
3461 ++p;
3462 break;
3463 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 else
3465 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003466 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003468 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003470 if (p[-1] == '!')
3471 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 }
3474 if (generate_2BOOL(cctx, invert) == FAIL)
3475 return FAIL;
3476 }
3477 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003478 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 return OK;
3480}
3481
3482/*
3483 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003484 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 */
3486 static int
3487compile_subscript(
3488 char_u **arg,
3489 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003490 char_u *start_leader,
3491 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003492 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003494 char_u *name_start = *end_leader;
3495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 for (;;)
3497 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003498 char_u *p = skipwhite(*arg);
3499
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003500 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003501 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003502 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003503
3504 // If a following line starts with "->{" or "->X" advance to that
3505 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003506 // Also if a following line starts with ".x".
3507 if (next != NULL &&
3508 ((next[0] == '-' && next[1] == '>'
3509 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003510 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003511 {
3512 next = next_line_from_context(cctx, TRUE);
3513 if (next == NULL)
3514 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003515 *arg = next;
3516 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003517 }
3518 }
3519
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003520 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003521 // not a function call.
3522 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003524 garray_T *stack = &cctx->ctx_type_stack;
3525 type_T *type;
3526 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003528 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003529 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003530 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003533 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3534
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003535 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3537 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003538 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 return FAIL;
3540 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003541 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003543 char_u *pstart = p;
3544
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003545 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003546 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003547 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 // something->method()
3550 // Apply the '!', '-' and '+' first:
3551 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003552 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003555 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003556 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003557 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 if (**arg == '{')
3559 {
3560 // lambda call: list->{lambda}
3561 if (compile_lambda_call(arg, cctx) == FAIL)
3562 return FAIL;
3563 }
3564 else
3565 {
3566 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003567 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003568 if (!eval_isnamec1(*p))
3569 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003570 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003571 return FAIL;
3572 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003573 if (ASCII_ISALPHA(*p) && p[1] == ':')
3574 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003575 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 ;
3577 if (*p != '(')
3578 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003579 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 return FAIL;
3581 }
3582 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003583 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 return FAIL;
3585 }
3586 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003587 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003589 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003590 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003591 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003592 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003593 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003594
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003595 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003596 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003597 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003598 // TODO: blob index
3599 // TODO: more arguments
3600 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003601 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003602 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003603 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003604
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003605 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003606 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003607 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003608 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003609 if (**arg == ':')
3610 // missing first index is equal to zero
3611 generate_PUSHNR(cctx, 0);
3612 else
3613 {
3614 if (compile_expr0(arg, cctx) == FAIL)
3615 return FAIL;
3616 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3617 return FAIL;
3618 *arg = skipwhite(*arg);
3619 }
3620 if (**arg == ':')
3621 {
3622 *arg = skipwhite(*arg + 1);
3623 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3624 return FAIL;
3625 if (**arg == ']')
3626 // missing second index is equal to end of string
3627 generate_PUSHNR(cctx, -1);
3628 else
3629 {
3630 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003631 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003632 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3633 return FAIL;
3634 *arg = skipwhite(*arg);
3635 }
3636 is_slice = TRUE;
3637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638
3639 if (**arg != ']')
3640 {
3641 emsg(_(e_missbrac));
3642 return FAIL;
3643 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003644 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003646 // We can index a list and a dict. If we don't know the type
3647 // we can use the index value type.
3648 // TODO: If we don't know use an instruction to figure it out at
3649 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003650 typep = ((type_T **)stack->ga_data) + stack->ga_len
3651 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003652 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003653 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3654 // If the index is a string, the variable must be a Dict.
3655 if (*typep == &t_any && valtype == &t_string)
3656 vtype = VAR_DICT;
3657 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003658 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003659 if (need_type(valtype, &t_number, -1, cctx,
3660 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003661 return FAIL;
3662 if (is_slice)
3663 {
3664 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003665 if (need_type(valtype, &t_number, -2, cctx,
3666 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003667 return FAIL;
3668 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003669 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003670
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003671 if (vtype == VAR_DICT)
3672 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003673 if (is_slice)
3674 {
3675 emsg(_(e_cannot_slice_dictionary));
3676 return FAIL;
3677 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003678 if ((*typep)->tt_type == VAR_DICT)
3679 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003680 else
3681 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003682 if (need_type(*typep, &t_dict_any, -2, cctx,
3683 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003684 return FAIL;
3685 *typep = &t_any;
3686 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003687 if (may_generate_2STRING(-1, cctx) == FAIL)
3688 return FAIL;
3689 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3690 return FAIL;
3691 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003692 else if (vtype == VAR_STRING)
3693 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003694 *typep = &t_string;
3695 if ((is_slice
3696 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3697 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003698 return FAIL;
3699 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003700 else if (vtype == VAR_BLOB)
3701 {
3702 emsg("Sorry, blob index and slice not implemented yet");
3703 return FAIL;
3704 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003705 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003706 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003707 if (is_slice)
3708 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003709 if (generate_instr_drop(cctx,
3710 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3711 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003712 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003713 }
Bram Moolenaared591872020-08-15 22:14:53 +02003714 else
3715 {
3716 if ((*typep)->tt_type == VAR_LIST)
3717 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003718 if (generate_instr_drop(cctx,
3719 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3720 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003721 return FAIL;
3722 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003723 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003724 else
3725 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003726 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003727 return FAIL;
3728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003730 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003732 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003733 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003734 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003735 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003736
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003737 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003738 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003739 {
3740 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003741 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003742 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003743 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003744 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 while (eval_isnamec(*p))
3746 MB_PTR_ADV(p);
3747 if (p == *arg)
3748 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003749 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 return FAIL;
3751 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003752 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 return FAIL;
3754 *arg = p;
3755 }
3756 else
3757 break;
3758 }
3759
3760 // TODO - see handle_subscript():
3761 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3762 // Don't do this when "Func" is already a partial that was bound
3763 // explicitly (pt_auto is FALSE).
3764
3765 return OK;
3766}
3767
3768/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003769 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3770 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003772 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003773 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003774 *
3775 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 */
3777
3778/*
3779 * number number constant
3780 * 0zFFFFFFFF Blob constant
3781 * "string" string constant
3782 * 'string' literal string constant
3783 * &option-name option value
3784 * @r register contents
3785 * identifier variable value
3786 * function() function call
3787 * $VAR environment variable
3788 * (expression) nested expression
3789 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003790 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 *
3792 * Also handle:
3793 * ! in front logical NOT
3794 * - in front unary minus
3795 * + in front unary plus (ignored)
3796 * trailing (arg) funcref/partial call
3797 * trailing [] subscript in String or List
3798 * trailing .name entry in Dictionary
3799 * trailing ->name() method call
3800 */
3801 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003802compile_expr7(
3803 char_u **arg,
3804 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003805 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 char_u *start_leader, *end_leader;
3808 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003809 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003810 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003812 ppconst->pp_is_const = FALSE;
3813
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 /*
3815 * Skip '!', '-' and '+' characters. They are handled later.
3816 */
3817 start_leader = *arg;
3818 while (**arg == '!' || **arg == '-' || **arg == '+')
3819 *arg = skipwhite(*arg + 1);
3820 end_leader = *arg;
3821
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003822 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 switch (**arg)
3824 {
3825 /*
3826 * Number constant.
3827 */
3828 case '0': // also for blob starting with 0z
3829 case '1':
3830 case '2':
3831 case '3':
3832 case '4':
3833 case '5':
3834 case '6':
3835 case '7':
3836 case '8':
3837 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003838 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003840 // Apply "-" and "+" just before the number now, right to
3841 // left. Matters especially when "->" follows. Stops at
3842 // '!'.
3843 if (apply_leader(rettv, TRUE,
3844 start_leader, &end_leader) == FAIL)
3845 {
3846 clear_tv(rettv);
3847 return FAIL;
3848 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 break;
3850
3851 /*
3852 * String constant: "string".
3853 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003854 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 return FAIL;
3856 break;
3857
3858 /*
3859 * Literal string constant: 'str''ing'.
3860 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003861 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 return FAIL;
3863 break;
3864
3865 /*
3866 * Constant Vim variable.
3867 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003868 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 ret = NOTDONE;
3870 break;
3871
3872 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003873 * "true" constant
3874 */
3875 case 't': if (STRNCMP(*arg, "true", 4) == 0
3876 && !eval_isnamec((*arg)[4]))
3877 {
3878 *arg += 4;
3879 rettv->v_type = VAR_BOOL;
3880 rettv->vval.v_number = VVAL_TRUE;
3881 }
3882 else
3883 ret = NOTDONE;
3884 break;
3885
3886 /*
3887 * "false" constant
3888 */
3889 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3890 && !eval_isnamec((*arg)[5]))
3891 {
3892 *arg += 5;
3893 rettv->v_type = VAR_BOOL;
3894 rettv->vval.v_number = VVAL_FALSE;
3895 }
3896 else
3897 ret = NOTDONE;
3898 break;
3899
3900 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 * List: [expr, expr]
3902 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003903 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 break;
3905
3906 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 * Lambda: {arg, arg -> expr}
3908 * Dictionary: {'key': val, 'key': val}
3909 */
3910 case '{': {
3911 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003912 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913
3914 // Find out what comes after the arguments.
3915 ret = get_function_args(&start, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003916 &ga_arg, TRUE, NULL, NULL,
3917 TRUE, NULL, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 if (ret != FAIL && *start == '>')
3919 ret = compile_lambda(arg, cctx);
3920 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003921 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 }
3923 break;
3924
3925 /*
3926 * Option value: &name
3927 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003928 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3929 return FAIL;
3930 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 break;
3932
3933 /*
3934 * Environment variable: $VAR.
3935 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003936 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3937 return FAIL;
3938 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 break;
3940
3941 /*
3942 * Register contents: @r.
3943 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003944 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3945 return FAIL;
3946 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 break;
3948 /*
3949 * nested expression: (expression).
3950 */
3951 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar1c747212020-05-09 18:28:34 +02003952
3953 // recursive!
3954 if (ppconst->pp_used <= PPSIZE - 10)
3955 {
3956 ret = compile_expr1(arg, cctx, ppconst);
3957 }
3958 else
3959 {
3960 // Not enough space in ppconst, flush constants.
3961 if (generate_ppconst(cctx, ppconst) == FAIL)
3962 return FAIL;
3963 ret = compile_expr0(arg, cctx);
3964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 *arg = skipwhite(*arg);
3966 if (**arg == ')')
3967 ++*arg;
3968 else if (ret == OK)
3969 {
3970 emsg(_(e_missing_close));
3971 ret = FAIL;
3972 }
3973 break;
3974
3975 default: ret = NOTDONE;
3976 break;
3977 }
3978 if (ret == FAIL)
3979 return FAIL;
3980
Bram Moolenaar1c747212020-05-09 18:28:34 +02003981 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02003983 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02003984 clear_tv(rettv);
3985 else
3986 // A constant expression can possibly be handled compile time,
3987 // return the value instead of generating code.
3988 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990 else if (ret == NOTDONE)
3991 {
3992 char_u *p;
3993 int r;
3994
3995 if (!eval_isnamec1(**arg))
3996 {
Bram Moolenaare4984292020-12-13 14:19:25 +01003997 if (ends_excmd(*skipwhite(*arg)))
3998 semsg(_(e_empty_expression_str), *arg);
3999 else
4000 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 return FAIL;
4002 }
4003
4004 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004005 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004007 {
4008 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004011 {
4012 if (generate_ppconst(cctx, ppconst) == FAIL)
4013 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004014 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 if (r == FAIL)
4017 return FAIL;
4018 }
4019
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004020 // Handle following "[]", ".member", etc.
4021 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004022 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004023 ppconst) == FAIL)
4024 return FAIL;
4025 if (ppconst->pp_used > 0)
4026 {
4027 // apply the '!', '-' and '+' before the constant
4028 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004029 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004030 return FAIL;
4031 return OK;
4032 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004033 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004035 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036}
4037
4038/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004039 * Give the "white on both sides" error, taking the operator from "p[len]".
4040 */
4041 void
4042error_white_both(char_u *op, int len)
4043{
4044 char_u buf[10];
4045
4046 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004047 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004048}
4049
4050/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004051 * <type>expr7: runtime type check / conversion
4052 */
4053 static int
4054compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4055{
4056 type_T *want_type = NULL;
4057
4058 // Recognize <type>
4059 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4060 {
4061 int called_emsg_before = called_emsg;
4062
4063 ++*arg;
4064 want_type = parse_type(arg, cctx->ctx_type_list);
4065 if (called_emsg != called_emsg_before)
4066 return FAIL;
4067
4068 if (**arg != '>')
4069 {
4070 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004071 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004072 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004073 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004074 return FAIL;
4075 }
4076 ++*arg;
4077 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4078 return FAIL;
4079 }
4080
4081 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4082 return FAIL;
4083
4084 if (want_type != NULL)
4085 {
4086 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004087 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004088
Bram Moolenaard1103582020-08-14 22:44:25 +02004089 generate_ppconst(cctx, ppconst);
4090 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004091 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004092 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004093 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004094 return FAIL;
4095 }
4096 }
4097
4098 return OK;
4099}
4100
4101/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 * * number multiplication
4103 * / number division
4104 * % number modulo
4105 */
4106 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004107compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108{
4109 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004110 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004111 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004113 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004114 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 return FAIL;
4116
4117 /*
4118 * Repeat computing, until no "*", "/" or "%" is following.
4119 */
4120 for (;;)
4121 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004122 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 if (*op != '*' && *op != '/' && *op != '%')
4124 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004125 if (next != NULL)
4126 {
4127 *arg = next_line_from_context(cctx, TRUE);
4128 op = skipwhite(*arg);
4129 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004130
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004131 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004133 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004134 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 }
4136 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004137 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004138 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004140 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004141 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004143
4144 if (ppconst->pp_used == ppconst_used + 2
4145 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4146 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004147 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004148 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4149 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004150 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004152 // both are numbers: compute the result
4153 switch (*op)
4154 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004155 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004156 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004157 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004158 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004159 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004160 break;
4161 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004162 tv1->vval.v_number = res;
4163 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004164 }
4165 else
4166 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004167 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004168 generate_two_op(cctx, op);
4169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 }
4171
4172 return OK;
4173}
4174
4175/*
4176 * + number addition
4177 * - number subtraction
4178 * .. string concatenation
4179 */
4180 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004181compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182{
4183 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004184 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004186 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187
4188 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004189 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 return FAIL;
4191
4192 /*
4193 * Repeat computing, until no "+", "-" or ".." is following.
4194 */
4195 for (;;)
4196 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004197 op = may_peek_next_line(cctx, *arg, &next);
4198 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 break;
4200 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004201 if (next != NULL)
4202 {
4203 *arg = next_line_from_context(cctx, TRUE);
4204 op = skipwhite(*arg);
4205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004207 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004209 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004210 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 }
4212
4213 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004214 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004215 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004218 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 return FAIL;
4220
Bram Moolenaara5565e42020-05-09 15:44:01 +02004221 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004222 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004223 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4224 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4225 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4226 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004228 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4229 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004230
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004231 // concat/subtract/add constant numbers
4232 if (*op == '+')
4233 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4234 else if (*op == '-')
4235 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4236 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004237 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004238 // concatenate constant strings
4239 char_u *s1 = tv1->vval.v_string;
4240 char_u *s2 = tv2->vval.v_string;
4241 size_t len1 = STRLEN(s1);
4242
4243 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4244 if (tv1->vval.v_string == NULL)
4245 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004246 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004247 return FAIL;
4248 }
4249 mch_memmove(tv1->vval.v_string, s1, len1);
4250 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004251 vim_free(s1);
4252 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004253 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004254 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 }
4256 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004257 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004258 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004259 if (*op == '.')
4260 {
4261 if (may_generate_2STRING(-2, cctx) == FAIL
4262 || may_generate_2STRING(-1, cctx) == FAIL)
4263 return FAIL;
4264 generate_instr_drop(cctx, ISN_CONCAT, 1);
4265 }
4266 else
4267 generate_two_op(cctx, op);
4268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * expr5a == expr5b
4276 * expr5a =~ expr5b
4277 * expr5a != expr5b
4278 * expr5a !~ expr5b
4279 * expr5a > expr5b
4280 * expr5a >= expr5b
4281 * expr5a < expr5b
4282 * expr5a <= expr5b
4283 * expr5a is expr5b
4284 * expr5a isnot expr5b
4285 *
4286 * Produces instructions:
4287 * EVAL expr5a Push result of "expr5a"
4288 * EVAL expr5b Push result of "expr5b"
4289 * COMPARE one of the compare instructions
4290 */
4291 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004292compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293{
4294 exptype_T type = EXPR_UNKNOWN;
4295 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004296 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004299 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300
4301 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004302 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 return FAIL;
4304
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004305 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004306 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307
4308 /*
4309 * If there is a comparative operator, use it.
4310 */
4311 if (type != EXPR_UNKNOWN)
4312 {
4313 int ic = FALSE; // Default: do not ignore case
4314
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004315 if (next != NULL)
4316 {
4317 *arg = next_line_from_context(cctx, TRUE);
4318 p = skipwhite(*arg);
4319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 if (type_is && (p[len] == '?' || p[len] == '#'))
4321 {
4322 semsg(_(e_invexpr2), *arg);
4323 return FAIL;
4324 }
4325 // extra question mark appended: ignore case
4326 if (p[len] == '?')
4327 {
4328 ic = TRUE;
4329 ++len;
4330 }
4331 // extra '#' appended: match case (ignored)
4332 else if (p[len] == '#')
4333 ++len;
4334 // nothing appended: match case
4335
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004336 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004338 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004339 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 }
4341
4342 // get the second variable
4343 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004344 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004345 return FAIL;
4346
Bram Moolenaara5565e42020-05-09 15:44:01 +02004347 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 return FAIL;
4349
Bram Moolenaara5565e42020-05-09 15:44:01 +02004350 if (ppconst->pp_used == ppconst_used + 2)
4351 {
4352 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4353 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4354 int ret;
4355
4356 // Both sides are a constant, compute the result now.
4357 // First check for a valid combination of types, this is more
4358 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004359 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004360 ret = FAIL;
4361 else
4362 {
4363 ret = typval_compare(tv1, tv2, type, ic);
4364 tv1->v_type = VAR_BOOL;
4365 tv1->vval.v_number = tv1->vval.v_number
4366 ? VVAL_TRUE : VVAL_FALSE;
4367 clear_tv(tv2);
4368 --ppconst->pp_used;
4369 }
4370 return ret;
4371 }
4372
4373 generate_ppconst(cctx, ppconst);
4374 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 }
4376
4377 return OK;
4378}
4379
Bram Moolenaar7f141552020-05-09 17:35:53 +02004380static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382/*
4383 * Compile || or &&.
4384 */
4385 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004386compile_and_or(
4387 char_u **arg,
4388 cctx_T *cctx,
4389 char *op,
4390 ppconst_T *ppconst,
4391 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004393 char_u *next;
4394 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 int opchar = *op;
4396
4397 if (p[0] == opchar && p[1] == opchar)
4398 {
4399 garray_T *instr = &cctx->ctx_instr;
4400 garray_T end_ga;
4401
4402 /*
4403 * Repeat until there is no following "||" or "&&"
4404 */
4405 ga_init2(&end_ga, sizeof(int), 10);
4406 while (p[0] == opchar && p[1] == opchar)
4407 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004408 if (next != NULL)
4409 {
4410 *arg = next_line_from_context(cctx, TRUE);
4411 p = skipwhite(*arg);
4412 }
4413
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004414 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4415 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004416 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004417 return FAIL;
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004420 // TODO: use ppconst if the value is a constant and check
4421 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004422 generate_ppconst(cctx, ppconst);
4423
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004424 // Every part must evaluate to a bool.
4425 if (bool_on_stack(cctx) == FAIL)
4426 {
4427 ga_clear(&end_ga);
4428 return FAIL;
4429 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 if (ga_grow(&end_ga, 1) == FAIL)
4432 {
4433 ga_clear(&end_ga);
4434 return FAIL;
4435 }
4436 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4437 ++end_ga.ga_len;
4438 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004439 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440
4441 // eval the next expression
4442 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004443 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004444 {
4445 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004446 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004447 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004448
Bram Moolenaara5565e42020-05-09 15:44:01 +02004449 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4450 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 {
4452 ga_clear(&end_ga);
4453 return FAIL;
4454 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004455
4456 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004458 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004460 // Every part must evaluate to a bool.
4461 if (bool_on_stack(cctx) == FAIL)
4462 {
4463 ga_clear(&end_ga);
4464 return FAIL;
4465 }
4466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 // Fill in the end label in all jumps.
4468 while (end_ga.ga_len > 0)
4469 {
4470 isn_T *isn;
4471
4472 --end_ga.ga_len;
4473 isn = ((isn_T *)instr->ga_data)
4474 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4475 isn->isn_arg.jump.jump_where = instr->ga_len;
4476 }
4477 ga_clear(&end_ga);
4478 }
4479
4480 return OK;
4481}
4482
4483/*
4484 * expr4a && expr4a && expr4a logical AND
4485 *
4486 * Produces instructions:
4487 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004488 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004489 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004491 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492 * EVAL expr4c Push result of "expr4c"
4493 * end:
4494 */
4495 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004496compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 int ppconst_used = ppconst->pp_used;
4499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004501 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 return FAIL;
4503
4504 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004505 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506}
4507
4508/*
4509 * expr3a || expr3b || expr3c logical OR
4510 *
4511 * Produces instructions:
4512 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004513 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004514 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004516 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 * EVAL expr3c Push result of "expr3c"
4518 * end:
4519 */
4520 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004521compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004523 int ppconst_used = ppconst->pp_used;
4524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004526 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 return FAIL;
4528
4529 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004530 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531}
4532
4533/*
4534 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004536 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 * JUMP_IF_FALSE alt jump if false
4538 * EVAL expr1a
4539 * JUMP_ALWAYS end
4540 * alt: EVAL expr1b
4541 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004542 *
4543 * Toplevel expression: expr2 ?? expr1
4544 * Produces instructions:
4545 * EVAL expr2 Push result of "expr2"
4546 * JUMP_AND_KEEP_IF_TRUE end jump if true
4547 * EVAL expr1
4548 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 */
4550 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004551compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552{
4553 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004555 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556
Bram Moolenaar3988f642020-08-27 22:43:03 +02004557 // Ignore all kinds of errors when not producing code.
4558 if (cctx->ctx_skip == SKIP_YES)
4559 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004560 evalarg_T evalarg;
4561
4562 CLEAR_FIELD(evalarg);
4563 evalarg.eval_cctx = cctx;
4564 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004565 return OK;
4566 }
4567
Bram Moolenaar61a89812020-05-07 16:58:17 +02004568 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004569 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 return FAIL;
4571
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004572 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 if (*p == '?')
4574 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004575 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 garray_T *instr = &cctx->ctx_instr;
4577 garray_T *stack = &cctx->ctx_type_stack;
4578 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004579 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004581 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004582 int has_const_expr = FALSE;
4583 int const_value = FALSE;
4584 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004586 if (next != NULL)
4587 {
4588 *arg = next_line_from_context(cctx, TRUE);
4589 p = skipwhite(*arg);
4590 }
4591
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004592 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004593 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004594 semsg(_(e_white_space_required_before_and_after_str),
4595 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004596 return FAIL;
4597 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598
Bram Moolenaara5565e42020-05-09 15:44:01 +02004599 if (ppconst->pp_used == ppconst_used + 1)
4600 {
4601 // the condition is a constant, we know whether the ? or the :
4602 // expression is to be evaluated.
4603 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004604 if (op_falsy)
4605 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4606 else
4607 {
4608 int error = FALSE;
4609
4610 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4611 &error);
4612 if (error)
4613 return FAIL;
4614 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004615 cctx->ctx_skip = save_skip == SKIP_YES ||
4616 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4617
4618 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4619 // "left ?? right" and "left" is truthy: produce "left"
4620 generate_ppconst(cctx, ppconst);
4621 else
4622 {
4623 clear_tv(&ppconst->pp_tv[ppconst_used]);
4624 --ppconst->pp_used;
4625 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004626 }
4627 else
4628 {
4629 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004630 if (op_falsy)
4631 end_idx = instr->ga_len;
4632 generate_JUMP(cctx, op_falsy
4633 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4634 if (op_falsy)
4635 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637
4638 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004639 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004640 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004641 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004642 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004643 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
Bram Moolenaara5565e42020-05-09 15:44:01 +02004645 if (!has_const_expr)
4646 {
4647 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004649 if (!op_falsy)
4650 {
4651 // remember the type and drop it
4652 --stack->ga_len;
4653 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004655 end_idx = instr->ga_len;
4656 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004657
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004658 // jump here from JUMP_IF_FALSE
4659 isn = ((isn_T *)instr->ga_data) + alt_idx;
4660 isn->isn_arg.jump.jump_where = instr->ga_len;
4661 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004662 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004663
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004664 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004666 // Check for the ":".
4667 p = may_peek_next_line(cctx, *arg, &next);
4668 if (*p != ':')
4669 {
4670 emsg(_(e_missing_colon));
4671 return FAIL;
4672 }
4673 if (next != NULL)
4674 {
4675 *arg = next_line_from_context(cctx, TRUE);
4676 p = skipwhite(*arg);
4677 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004678
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004679 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4680 {
4681 semsg(_(e_white_space_required_before_and_after_str), ":");
4682 return FAIL;
4683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004685 // evaluate the third expression
4686 if (has_const_expr)
4687 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004688 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004689 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004690 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004691 return FAIL;
4692 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4693 return FAIL;
4694 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004695
Bram Moolenaara5565e42020-05-09 15:44:01 +02004696 if (!has_const_expr)
4697 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004698 type_T **typep;
4699
Bram Moolenaara5565e42020-05-09 15:44:01 +02004700 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaara5565e42020-05-09 15:44:01 +02004702 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004703 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4704 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004705
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004706 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004707 isn = ((isn_T *)instr->ga_data) + end_idx;
4708 isn->isn_arg.jump.jump_where = instr->ga_len;
4709 }
4710
4711 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 }
4713 return OK;
4714}
4715
4716/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004717 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004718 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4719 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004720 */
4721 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004722compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004723{
4724 ppconst_T ppconst;
4725
4726 CLEAR_FIELD(ppconst);
4727 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4728 {
4729 clear_ppconst(&ppconst);
4730 return FAIL;
4731 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004732 if (is_const != NULL)
4733 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004734 if (generate_ppconst(cctx, &ppconst) == FAIL)
4735 return FAIL;
4736 return OK;
4737}
4738
4739/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004740 * Toplevel expression.
4741 */
4742 static int
4743compile_expr0(char_u **arg, cctx_T *cctx)
4744{
4745 return compile_expr0_ext(arg, cctx, NULL);
4746}
4747
4748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 * compile "return [expr]"
4750 */
4751 static char_u *
4752compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
4753{
4754 char_u *p = arg;
4755 garray_T *stack = &cctx->ctx_type_stack;
4756 type_T *stack_type;
4757
4758 if (*p != NUL && *p != '|' && *p != '\n')
4759 {
4760 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004761 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 return NULL;
4763
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004764 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004765 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004766 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4767 if (set_return_type)
4768 cctx->ctx_ufunc->uf_ret_type = stack_type;
4769 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004770 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004771 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4772 && stack_type->tt_type != VAR_VOID
4773 && stack_type->tt_type != VAR_UNKNOWN)
4774 {
4775 emsg(_(e_returning_value_in_function_without_return_type));
4776 return NULL;
4777 }
4778 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004779 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004780 return NULL;
4781 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004782 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 }
4784 else
4785 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004786 // "set_return_type" cannot be TRUE, only used for a lambda which
4787 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004788 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4789 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004790 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004791 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 return NULL;
4793 }
4794
4795 // No argument, return zero.
4796 generate_PUSHNR(cctx, 0);
4797 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004798 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799 return NULL;
4800
4801 // "return val | endif" is possible
4802 return skipwhite(p);
4803}
4804
4805/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004806 * Get a line from the compilation context, compatible with exarg_T getline().
4807 * Return a pointer to the line in allocated memory.
4808 * Return NULL for end-of-file or some error.
4809 */
4810 static char_u *
4811exarg_getline(
4812 int c UNUSED,
4813 void *cookie,
4814 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004815 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004816{
4817 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004818 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004819
Bram Moolenaar66250c92020-08-20 15:02:42 +02004820 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004821 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004822 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004823 return NULL;
4824 ++cctx->ctx_lnum;
4825 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4826 // Comment lines result in NULL pointers, skip them.
4827 if (p != NULL)
4828 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004829 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004830}
4831
4832/*
4833 * Compile a nested :def command.
4834 */
4835 static char_u *
4836compile_nested_function(exarg_T *eap, cctx_T *cctx)
4837{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004838 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004839 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004840 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004841 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004842 ufunc_T *ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004843 int r;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004844
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004845 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004846 {
4847 emsg(_(e_cannot_use_bang_with_nested_def));
4848 return NULL;
4849 }
4850
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004851 if (*name_start == '/')
4852 {
4853 name_end = skip_regexp(name_start + 1, '/', TRUE);
4854 if (*name_end == '/')
4855 ++name_end;
4856 eap->nextcmd = check_nextcmd(name_end);
4857 }
4858 if (name_end == name_start || *skipwhite(name_end) != '(')
4859 {
4860 if (!ends_excmd2(name_start, name_end))
4861 {
4862 semsg(_(e_invalid_command_str), eap->cmd);
4863 return NULL;
4864 }
4865
4866 // "def" or "def Name": list functions
4867 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4868 return NULL;
4869 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4870 }
4871
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004872 // Only g:Func() can use a namespace.
4873 if (name_start[1] == ':' && !is_global)
4874 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004875 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004876 return NULL;
4877 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004878 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4879 return NULL;
4880
Bram Moolenaar04b12692020-05-04 23:24:44 +02004881 eap->arg = name_end;
4882 eap->getline = exarg_getline;
4883 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004884 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004885 eap->forceit = FALSE;
Bram Moolenaareef21022020-08-01 22:16:43 +02004886 lambda_name = get_lambda_name();
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004887 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004888
Bram Moolenaar822ba242020-05-24 23:00:18 +02004889 if (ufunc == NULL)
Bram Moolenaar7a3330f2020-08-27 23:57:57 +02004890 return eap->skip ? (char_u *)"" : NULL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004891 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004892 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004893 {
4894 func_ptr_unref(ufunc);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004895 return NULL;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004896 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004897
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004898 if (is_global)
4899 {
4900 char_u *func_name = vim_strnsave(name_start + 2,
4901 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004902
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004903 if (func_name == NULL)
4904 r = FAIL;
4905 else
Bram Moolenaareef21022020-08-01 22:16:43 +02004906 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004907 }
4908 else
4909 {
4910 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004911 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004912 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004913 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004914
Bram Moolenaareef21022020-08-01 22:16:43 +02004915 if (lvar == NULL)
4916 return NULL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004917 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004918 return NULL;
4919 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004920
4921 // copy over the block scope IDs
4922 if (block_depth > 0)
4923 {
4924 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4925 if (ufunc->uf_block_ids != NULL)
4926 {
4927 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4928 sizeof(int) * block_depth);
4929 ufunc->uf_block_depth = block_depth;
4930 }
4931 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004932 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004933
Bram Moolenaar61a89812020-05-07 16:58:17 +02004934 // TODO: warning for trailing text?
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004935 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004936}
4937
4938/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 * Return the length of an assignment operator, or zero if there isn't one.
4940 */
4941 int
4942assignment_len(char_u *p, int *heredoc)
4943{
4944 if (*p == '=')
4945 {
4946 if (p[1] == '<' && p[2] == '<')
4947 {
4948 *heredoc = TRUE;
4949 return 3;
4950 }
4951 return 1;
4952 }
4953 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
4954 return 2;
4955 if (STRNCMP(p, "..=", 3) == 0)
4956 return 3;
4957 return 0;
4958}
4959
4960// words that cannot be used as a variable
4961static char *reserved[] = {
4962 "true",
4963 "false",
4964 NULL
4965};
4966
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004967typedef enum {
4968 dest_local,
4969 dest_option,
4970 dest_env,
4971 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02004972 dest_buffer,
4973 dest_window,
4974 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004975 dest_vimvar,
4976 dest_script,
4977 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01004978 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01004979} assign_dest_T;
4980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004982 * Generate the load instruction for "name".
4983 */
4984 static void
4985generate_loadvar(
4986 cctx_T *cctx,
4987 assign_dest_T dest,
4988 char_u *name,
4989 lvar_T *lvar,
4990 type_T *type)
4991{
4992 switch (dest)
4993 {
4994 case dest_option:
4995 // TODO: check the option exists
4996 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
4997 break;
4998 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01004999 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5000 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5001 else
5002 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005003 break;
5004 case dest_buffer:
5005 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5006 break;
5007 case dest_window:
5008 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5009 break;
5010 case dest_tab:
5011 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5012 break;
5013 case dest_script:
5014 compile_load_scriptvar(cctx,
5015 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5016 break;
5017 case dest_env:
5018 // Include $ in the name here
5019 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5020 break;
5021 case dest_reg:
5022 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5023 break;
5024 case dest_vimvar:
5025 generate_LOADV(cctx, name + 2, TRUE);
5026 break;
5027 case dest_local:
5028 if (lvar->lv_from_outer)
5029 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5030 NULL, type);
5031 else
5032 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5033 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005034 case dest_expr:
5035 // list or dict value should already be on the stack.
5036 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005037 }
5038}
5039
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005040/*
5041 * Skip over "[expr]" or ".member".
5042 * Does not check for any errors.
5043 */
5044 static char_u *
5045skip_index(char_u *start)
5046{
5047 char_u *p = start;
5048
5049 if (*p == '[')
5050 {
5051 p = skipwhite(p + 1);
5052 (void)skip_expr(&p, NULL);
5053 p = skipwhite(p);
5054 if (*p == ']')
5055 return p + 1;
5056 return p;
5057 }
5058 // if (*p == '.')
5059 return to_name_end(p + 1, TRUE);
5060}
5061
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005062 void
5063vim9_declare_error(char_u *name)
5064{
5065 char *scope = "";
5066
5067 switch (*name)
5068 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005069 case 'g': scope = _("global"); break;
5070 case 'b': scope = _("buffer"); break;
5071 case 'w': scope = _("window"); break;
5072 case 't': scope = _("tab"); break;
5073 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005074 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005075 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005076 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005077 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005078 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005079 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005080 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005081 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005082 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005083}
5084
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005085/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005086 * For one assignment figure out the type of destination. Return it in "dest".
5087 * When not recognized "dest" is not set.
5088 * For an option "opt_flags" is set.
5089 * For a v:var "vimvaridx" is set.
5090 * "type" is set to the destination type if known, unchanted otherwise.
5091 * Return FAIL if an error message was given.
5092 */
5093 static int
5094get_var_dest(
5095 char_u *name,
5096 assign_dest_T *dest,
5097 int cmdidx,
5098 int *opt_flags,
5099 int *vimvaridx,
5100 type_T **type,
5101 cctx_T *cctx)
5102{
5103 char_u *p;
5104
5105 if (*name == '&')
5106 {
5107 int cc;
5108 long numval;
5109 int opt_type;
5110
5111 *dest = dest_option;
5112 if (cmdidx == CMD_final || cmdidx == CMD_const)
5113 {
5114 emsg(_(e_const_option));
5115 return FAIL;
5116 }
5117 p = name;
5118 p = find_option_end(&p, opt_flags);
5119 if (p == NULL)
5120 {
5121 // cannot happen?
5122 emsg(_(e_letunexp));
5123 return FAIL;
5124 }
5125 cc = *p;
5126 *p = NUL;
5127 opt_type = get_option_value(skip_option_env_lead(name),
5128 &numval, NULL, *opt_flags);
5129 *p = cc;
5130 if (opt_type == -3)
5131 {
5132 semsg(_(e_unknown_option), name);
5133 return FAIL;
5134 }
5135 if (opt_type == -2 || opt_type == 0)
5136 *type = &t_string;
5137 else
5138 *type = &t_number; // both number and boolean option
5139 }
5140 else if (*name == '$')
5141 {
5142 *dest = dest_env;
5143 *type = &t_string;
5144 }
5145 else if (*name == '@')
5146 {
5147 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5148 {
5149 emsg_invreg(name[1]);
5150 return FAIL;
5151 }
5152 *dest = dest_reg;
5153 *type = &t_string;
5154 }
5155 else if (STRNCMP(name, "g:", 2) == 0)
5156 {
5157 *dest = dest_global;
5158 }
5159 else if (STRNCMP(name, "b:", 2) == 0)
5160 {
5161 *dest = dest_buffer;
5162 }
5163 else if (STRNCMP(name, "w:", 2) == 0)
5164 {
5165 *dest = dest_window;
5166 }
5167 else if (STRNCMP(name, "t:", 2) == 0)
5168 {
5169 *dest = dest_tab;
5170 }
5171 else if (STRNCMP(name, "v:", 2) == 0)
5172 {
5173 typval_T *vtv;
5174 int di_flags;
5175
5176 *vimvaridx = find_vim_var(name + 2, &di_flags);
5177 if (*vimvaridx < 0)
5178 {
5179 semsg(_(e_variable_not_found_str), name);
5180 return FAIL;
5181 }
5182 // We use the current value of "sandbox" here, is that OK?
5183 if (var_check_ro(di_flags, name, FALSE))
5184 return FAIL;
5185 *dest = dest_vimvar;
5186 vtv = get_vim_var_tv(*vimvaridx);
5187 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5188 }
5189 return OK;
5190}
5191
5192/*
5193 * Generate a STORE instruction for "dest", not being "dest_local".
5194 * Return FAIL when out of memory.
5195 */
5196 static int
5197generate_store_var(
5198 cctx_T *cctx,
5199 assign_dest_T dest,
5200 int opt_flags,
5201 int vimvaridx,
5202 int scriptvar_idx,
5203 int scriptvar_sid,
5204 type_T *type,
5205 char_u *name)
5206{
5207 switch (dest)
5208 {
5209 case dest_option:
5210 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5211 opt_flags);
5212 case dest_global:
5213 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005214 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5215 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005216 case dest_buffer:
5217 // include b: with the name, easier to execute that way
5218 return generate_STORE(cctx, ISN_STOREB, 0, name);
5219 case dest_window:
5220 // include w: with the name, easier to execute that way
5221 return generate_STORE(cctx, ISN_STOREW, 0, name);
5222 case dest_tab:
5223 // include t: with the name, easier to execute that way
5224 return generate_STORE(cctx, ISN_STORET, 0, name);
5225 case dest_env:
5226 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5227 case dest_reg:
5228 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5229 case dest_vimvar:
5230 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5231 case dest_script:
5232 if (scriptvar_idx < 0)
5233 {
5234 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005235 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005236
Bram Moolenaar41d61962020-12-06 20:12:43 +01005237 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005238 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5239 scriptvar_sid, type);
5240 if (name_s != name)
5241 vim_free(name_s);
5242 return r;
5243 }
5244 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5245 scriptvar_sid, scriptvar_idx, type);
5246 case dest_local:
5247 case dest_expr:
5248 // cannot happen
5249 break;
5250 }
5251 return FAIL;
5252}
5253
5254/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005255 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005256 * "let name"
5257 * "var name = expr"
5258 * "final name = expr"
5259 * "const name = expr"
5260 * "name = expr"
5261 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005262 * Return NULL for an error.
5263 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 */
5265 static char_u *
5266compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5267{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005268 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005270 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 char_u *ret = NULL;
5272 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005274 int scriptvar_sid = 0;
5275 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005278 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005279 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005280 int oplen = 0;
5281 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005282 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005283 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005284 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005285 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005287 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5288 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005289
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 // Skip over the "var" or "[var, var]" to get to any "=".
5291 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5292 if (p == NULL)
5293 return *arg == '[' ? arg : NULL;
5294
5295 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005297 // TODO: should we allow this, and figure out type inference from list
5298 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005299 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 return NULL;
5301 }
5302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 sp = p;
5304 p = skipwhite(p);
5305 op = p;
5306 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005307
5308 if (var_count > 0 && oplen == 0)
5309 // can be something like "[1, 2]->func()"
5310 return arg;
5311
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005312 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005313 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005314 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005315 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005316 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 if (heredoc)
5319 {
5320 list_T *l;
5321 listitem_T *li;
5322
5323 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005324 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005326 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005327 if (l == NULL)
5328 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329
Bram Moolenaar078269b2020-09-21 20:35:55 +02005330 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005332 // Push each line and the create the list.
5333 FOR_ALL_LIST_ITEMS(l, li)
5334 {
5335 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5336 li->li_tv.vval.v_string = NULL;
5337 }
5338 generate_NEWLIST(cctx, l->lv_len);
5339 type = &t_list_string;
5340 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 list_free(l);
5343 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005344 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005346 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005348 char_u *wp;
5349
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005350 // for "[var, var] = expr" evaluate the expression here, loop over the
5351 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005352 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005353
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005354 wp = op + oplen;
5355 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005356 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005357 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005358 if (compile_expr0(&p, cctx) == FAIL)
5359 return NULL;
5360 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005362 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005364 type_T *stacktype;
5365
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005366 stacktype = stack->ga_len == 0 ? &t_void
5367 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005368 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005370 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005371 goto theend;
5372 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005373 if (need_type(stacktype, &t_list_any, -1, cctx,
5374 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005375 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005376 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005377 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5378 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005379 if (stacktype->tt_member != NULL)
5380 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005381 }
5382 }
5383
5384 /*
5385 * Loop over variables in "[var, var] = expr".
5386 * For "var = expr" and "let var: type" this is done only once.
5387 */
5388 if (var_count > 0)
5389 var_start = skipwhite(arg + 1); // skip over the "["
5390 else
5391 var_start = arg;
5392 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5393 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005394 char_u *var_end;
5395 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005396 size_t varlen;
5397 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005398 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005399 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005400 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005401 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005402 lvar_T *lvar = NULL;
5403 lvar_T arg_lvar;
5404 int has_type = FALSE;
5405 int has_index = FALSE;
5406 int instr_count = -1;
5407
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005408 // "dest_end" is the end of the destination, including "[expr]" or
5409 // ".name".
5410 // "var_end" is the end of the variable/option/etc. name.
5411 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005412 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005413 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005414 else
5415 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005416 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005417 var_end = skip_option_env_lead(var_start);
5418 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005419 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005421 // "a: type" is declaring variable "a" with a type, not dict "a:".
5422 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5423 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005424 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5425 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005426
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005427 // compute the length of the destination without "[expr]" or ".name"
5428 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005429 vim_free(name);
5430 name = vim_strnsave(var_start, varlen);
5431 if (name == NULL)
5432 return NULL;
5433 if (!heredoc)
5434 type = &t_any;
5435
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005436 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005437 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005438 int declare_error = FALSE;
5439
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005440 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5441 &vimvaridx, &type, cctx) == FAIL)
5442 goto theend;
5443 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005444 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005445 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005446 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005447 }
5448 else
5449 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005450 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005451
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005452 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005453 for (idx = 0; reserved[idx] != NULL; ++idx)
5454 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005455 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005456 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005457 goto theend;
5458 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005459
Bram Moolenaar709664c2020-12-12 14:33:41 +01005460
5461 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5462 lvar = &local_lvar;
5463 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 {
5465 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005466 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005467 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5468 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005469 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005470 if (is_decl)
5471 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005472 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005473 goto theend;
5474 }
5475 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005476 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005477 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005478 if (lvar != NULL)
5479 {
5480 if (is_decl)
5481 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005482 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005483 goto theend;
5484 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005485 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005486 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005487 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005488 int script_namespace = varlen > 1
5489 && STRNCMP(var_start, "s:", 2) == 0;
5490 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005491 ? script_var_exists(var_start + 2, varlen - 2,
5492 FALSE, cctx)
5493 : script_var_exists(var_start, varlen,
5494 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005495 imported_T *import =
5496 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005497
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005498 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005500 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5501
5502 if (is_decl)
5503 {
5504 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005505 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005506 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005507 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005508 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005509 name);
5510 goto theend;
5511 }
5512 else if (cctx->ctx_ufunc->uf_script_ctx_version
5513 == SCRIPT_VERSION_VIM9
5514 && script_namespace
5515 && !script_var && import == NULL)
5516 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005517 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005518 goto theend;
5519 }
5520
5521 dest = dest_script;
5522
5523 // existing script-local variables should have a type
5524 scriptvar_sid = current_sctx.sc_sid;
5525 if (import != NULL)
5526 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005527 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005528 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005529 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005530 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005531 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005532 {
5533 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5534 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005535 ((svar_T *)si->sn_var_vals.ga_data)
5536 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005537 type = sv->sv_type;
5538 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005539 }
5540 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005541 else if (check_defined(var_start, varlen, cctx) == FAIL)
5542 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005543 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005544 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005545
5546 if (declare_error)
5547 {
5548 vim9_declare_error(name);
5549 goto theend;
5550 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005551 }
5552
5553 // handle "a:name" as a name, not index "name" on "a"
5554 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005555 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005556
5557 if (dest != dest_option)
5558 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005559 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005560 {
5561 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005562 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005563 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005564 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005565 goto theend;
5566 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005567 p = skipwhite(var_end + 1);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005568 type = parse_type(&p, cctx->ctx_type_list);
5569 has_type = TRUE;
5570 }
5571 else if (lvar != NULL)
5572 type = lvar->lv_type;
5573 }
5574
5575 if (oplen == 3 && !heredoc && dest != dest_global
5576 && type->tt_type != VAR_STRING
5577 && type->tt_type != VAR_ANY)
5578 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005579 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005580 goto theend;
5581 }
5582
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005583 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005584 {
5585 if (oplen > 1 && !heredoc)
5586 {
5587 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005588 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005589 goto theend;
5590 }
5591
5592 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005593 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5594 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005595 goto theend;
5596 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005597 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005598 if (lvar == NULL)
5599 goto theend;
5600 new_local = TRUE;
5601 }
5602
5603 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005604 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005605 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005606 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005607 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005608 if (is_decl)
5609 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005610 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005611 goto theend;
5612 }
5613
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005614 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005615 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005616 char_u *after = var_start + varlen;
5617
5618 // Only the last index is used below, if there are others
5619 // before it generate code for the expression. Thus for
5620 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5621 for (;;)
5622 {
5623 p = skip_index(after);
5624 if (*p != '[' && *p != '.')
5625 break;
5626 after = p;
5627 }
5628 if (after > var_start + varlen)
5629 {
5630 varlen = after - var_start;
5631 dest = dest_expr;
5632 // We don't know the type before evaluating the expression,
5633 // use "any" until then.
5634 type = &t_any;
5635 }
5636
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005637 has_index = TRUE;
5638 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005639 member_type = &t_any;
5640 else
5641 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005642 }
5643 else
5644 {
5645 semsg("Not supported yet: %s", var_start);
5646 goto theend;
5647 }
5648 }
5649 else if (lvar == &arg_lvar)
5650 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005651 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005652 goto theend;
5653 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005654 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5655 {
5656 semsg(_(e_cannot_assign_to_constant), name);
5657 goto theend;
5658 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005659
5660 if (!heredoc)
5661 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005662 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005663 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005664 if (oplen > 0 && var_count == 0)
5665 {
5666 // skip over the "=" and the expression
5667 p = skipwhite(op + oplen);
5668 compile_expr0(&p, cctx);
5669 }
5670 }
5671 else if (oplen > 0)
5672 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005673 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005674 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005675
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005676 // For "var = expr" evaluate the expression.
5677 if (var_count == 0)
5678 {
5679 int r;
5680
5681 // for "+=", "*=", "..=" etc. first load the current value
5682 if (*op != '=')
5683 {
5684 generate_loadvar(cctx, dest, name, lvar, type);
5685
5686 if (has_index)
5687 {
5688 // TODO: get member from list or dict
5689 emsg("Index with operation not supported yet");
5690 goto theend;
5691 }
5692 }
5693
5694 // Compile the expression. Temporarily hide the new local
5695 // variable here, it is not available to this expression.
5696 if (new_local)
5697 --cctx->ctx_locals.ga_len;
5698 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005699 wp = op + oplen;
5700 p = skipwhite(wp);
5701 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005702 {
5703 if (new_local)
5704 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005705 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005706 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005707 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005708 if (new_local)
5709 ++cctx->ctx_locals.ga_len;
5710 if (r == FAIL)
5711 goto theend;
5712 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005713 else if (semicolon && var_idx == var_count - 1)
5714 {
5715 // For "[var; var] = expr" get the rest of the list
5716 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5717 goto theend;
5718 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005719 else
5720 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005721 // For "[var, var] = expr" get the "var_idx" item from the
5722 // list.
5723 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005724 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005725 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005726
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005727 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005728 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005729 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005730 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005731 if ((rhs_type->tt_type == VAR_FUNC
5732 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005733 && var_wrong_func_name(name, TRUE))
5734 goto theend;
5735
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005736 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005737 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005738 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005739 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005740 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005741 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005742 }
5743 else
5744 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005745 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005746 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005747 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005748 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005749 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005750 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005751 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005752 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005753 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005754 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005755 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005756 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005757 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005758 {
5759 type_T *use_type = lvar->lv_type;
5760
Bram Moolenaar71abe482020-09-14 22:28:30 +02005761 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005762 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005763 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005764 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005765 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005766 goto theend;
5767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005768 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005769 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005770 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005771 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005772 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005773 else if (cmdidx == CMD_final)
5774 {
5775 emsg(_(e_final_requires_a_value));
5776 goto theend;
5777 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005778 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005779 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005780 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005781 goto theend;
5782 }
5783 else if (!has_type || dest == dest_option)
5784 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005785 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005786 goto theend;
5787 }
5788 else
5789 {
5790 // variables are always initialized
5791 if (ga_grow(instr, 1) == FAIL)
5792 goto theend;
5793 switch (member_type->tt_type)
5794 {
5795 case VAR_BOOL:
5796 generate_PUSHBOOL(cctx, VVAL_FALSE);
5797 break;
5798 case VAR_FLOAT:
5799#ifdef FEAT_FLOAT
5800 generate_PUSHF(cctx, 0.0);
5801#endif
5802 break;
5803 case VAR_STRING:
5804 generate_PUSHS(cctx, NULL);
5805 break;
5806 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005807 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005808 break;
5809 case VAR_FUNC:
5810 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5811 break;
5812 case VAR_LIST:
5813 generate_NEWLIST(cctx, 0);
5814 break;
5815 case VAR_DICT:
5816 generate_NEWDICT(cctx, 0);
5817 break;
5818 case VAR_JOB:
5819 generate_PUSHJOB(cctx, NULL);
5820 break;
5821 case VAR_CHANNEL:
5822 generate_PUSHCHANNEL(cctx, NULL);
5823 break;
5824 case VAR_NUMBER:
5825 case VAR_UNKNOWN:
5826 case VAR_ANY:
5827 case VAR_PARTIAL:
5828 case VAR_VOID:
5829 case VAR_SPECIAL: // cannot happen
5830 generate_PUSHNR(cctx, 0);
5831 break;
5832 }
5833 }
5834 if (var_count == 0)
5835 end = p;
5836 }
5837
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005838 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005839 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005840 break;
5841
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005842 if (oplen > 0 && *op != '=')
5843 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005844 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005845 type_T *stacktype;
5846
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005847 if (*op == '.')
5848 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005849 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005850 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005851 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005852 if (
5853#ifdef FEAT_FLOAT
5854 // If variable is float operation with number is OK.
5855 !(expected == &t_float && stacktype == &t_number) &&
5856#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005857 need_type(stacktype, expected, -1, cctx,
5858 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005859 goto theend;
5860
5861 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005862 {
5863 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5864 goto theend;
5865 }
5866 else if (*op == '+')
5867 {
5868 if (generate_add_instr(cctx,
5869 operator_type(member_type, stacktype),
5870 member_type, stacktype) == FAIL)
5871 goto theend;
5872 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005873 else if (generate_two_op(cctx, op) == FAIL)
5874 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005876
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005877 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005878 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005879 int r;
5880 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005881
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005882 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005883 p = var_start + varlen;
5884 if (*p == '[')
5885 {
5886 p = skipwhite(p + 1);
5887 r = compile_expr0(&p, cctx);
5888 if (r == OK && *skipwhite(p) != ']')
5889 {
5890 // this should not happen
5891 emsg(_(e_missbrac));
5892 r = FAIL;
5893 }
5894 }
5895 else // if (*p == '.')
5896 {
5897 char_u *key_end = to_name_end(p + 1, TRUE);
5898 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5899
5900 r = generate_PUSHS(cctx, key);
5901 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005902 if (r == FAIL)
5903 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005904
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005905 if (type == &t_any)
5906 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005907 // Index on variable of unknown type: check at runtime.
5908 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005909 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005910 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005911 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005912 dest_type = type->tt_type;
5913 if (dest_type == VAR_DICT
5914 && may_generate_2STRING(-1, cctx) == FAIL)
5915 goto theend;
5916 if (dest_type == VAR_LIST
5917 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5918 != VAR_NUMBER)
5919 {
5920 emsg(_(e_number_exp));
5921 goto theend;
5922 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005923 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005924
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005925 // Load the dict or list. On the stack we then have:
5926 // - value
5927 // - index
5928 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005929 if (dest == dest_expr)
5930 {
5931 int c = var_start[varlen];
5932
5933 // Evaluate "ll[expr]" of "ll[expr][idx]"
5934 p = var_start;
5935 var_start[varlen] = NUL;
5936 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
5937 {
5938 // this should not happen
5939 emsg(_(e_missbrac));
5940 goto theend;
5941 }
5942 var_start[varlen] = c;
5943
5944 type = stack->ga_len == 0 ? &t_void
5945 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
5946 // now we can properly check the type
5947 if (type->tt_member != NULL
5948 && need_type(rhs_type, type->tt_member, -2, cctx,
5949 FALSE, FALSE) == FAIL)
5950 goto theend;
5951 }
5952 else
5953 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005954
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005955 if (dest_type == VAR_LIST || dest_type == VAR_DICT
5956 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005957 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005958 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
5959
5960 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005961 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005962 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005963 }
5964 else
5965 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005966 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005967 goto theend;
5968 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005969 }
5970 else
5971 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005972 if (is_decl && cmdidx == CMD_const
5973 && (dest == dest_script || dest == dest_local))
5974 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02005975 generate_LOCKCONST(cctx);
5976
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005977 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005978 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005979 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
5980 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
5981 goto theend;
5982 }
5983 else if (lvar != NULL)
5984 {
5985 isn_T *isn = ((isn_T *)instr->ga_data)
5986 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005987
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005988 // optimization: turn "var = 123" from ISN_PUSHNR +
5989 // ISN_STORE into ISN_STORENR
5990 if (!lvar->lv_from_outer
5991 && instr->ga_len == instr_count + 1
5992 && isn->isn_type == ISN_PUSHNR)
5993 {
5994 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005995
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005996 isn->isn_type = ISN_STORENR;
5997 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
5998 isn->isn_arg.storenr.stnr_val = val;
5999 if (stack->ga_len > 0)
6000 --stack->ga_len;
6001 }
6002 else if (lvar->lv_from_outer)
6003 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6004 else
6005 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006006 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006007 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006008
6009 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006010 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006011 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006012
6013 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006014 if (var_count > 0 && !semicolon)
6015 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006016 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006017 goto theend;
6018 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006019
Bram Moolenaarb2097502020-07-19 17:17:02 +02006020 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021
6022theend:
6023 vim_free(name);
6024 return ret;
6025}
6026
6027/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006028 * Check if "name" can be "unlet".
6029 */
6030 int
6031check_vim9_unlet(char_u *name)
6032{
6033 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6034 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006035 // "unlet s:var" is allowed in legacy script.
6036 if (*name == 's' && !script_is_vim9())
6037 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006038 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006039 return FAIL;
6040 }
6041 return OK;
6042}
6043
6044/*
6045 * Callback passed to ex_unletlock().
6046 */
6047 static int
6048compile_unlet(
6049 lval_T *lvp,
6050 char_u *name_end,
6051 exarg_T *eap,
6052 int deep UNUSED,
6053 void *coookie)
6054{
6055 cctx_T *cctx = coookie;
6056
6057 if (lvp->ll_tv == NULL)
6058 {
6059 char_u *p = lvp->ll_name;
6060 int cc = *name_end;
6061 int ret = OK;
6062
6063 // Normal name. Only supports g:, w:, t: and b: namespaces.
6064 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006065 if (*p == '$')
6066 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6067 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006068 ret = FAIL;
6069 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006070 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006071
6072 *name_end = cc;
6073 return ret;
6074 }
6075
6076 // TODO: unlet {list}[idx]
6077 // TODO: unlet {dict}[key]
6078 emsg("Sorry, :unlet not fully implemented yet");
6079 return FAIL;
6080}
6081
6082/*
6083 * compile "unlet var", "lock var" and "unlock var"
6084 * "arg" points to "var".
6085 */
6086 static char_u *
6087compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6088{
6089 char_u *p = arg;
6090
6091 if (eap->cmdidx != CMD_unlet)
6092 {
6093 emsg("Sorry, :lock and unlock not implemented yet");
6094 return NULL;
6095 }
6096
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006097 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6098 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6099}
6100
6101/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006102 * Compile an :import command.
6103 */
6104 static char_u *
6105compile_import(char_u *arg, cctx_T *cctx)
6106{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006107 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006108}
6109
6110/*
6111 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6112 */
6113 static int
6114compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6115{
6116 garray_T *instr = &cctx->ctx_instr;
6117 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6118
6119 if (endlabel == NULL)
6120 return FAIL;
6121 endlabel->el_next = *el;
6122 *el = endlabel;
6123 endlabel->el_end_label = instr->ga_len;
6124
6125 generate_JUMP(cctx, when, 0);
6126 return OK;
6127}
6128
6129 static void
6130compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6131{
6132 garray_T *instr = &cctx->ctx_instr;
6133
6134 while (*el != NULL)
6135 {
6136 endlabel_T *cur = (*el);
6137 isn_T *isn;
6138
6139 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6140 isn->isn_arg.jump.jump_where = instr->ga_len;
6141 *el = cur->el_next;
6142 vim_free(cur);
6143 }
6144}
6145
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006146 static void
6147compile_free_jump_to_end(endlabel_T **el)
6148{
6149 while (*el != NULL)
6150 {
6151 endlabel_T *cur = (*el);
6152
6153 *el = cur->el_next;
6154 vim_free(cur);
6155 }
6156}
6157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006158/*
6159 * Create a new scope and set up the generic items.
6160 */
6161 static scope_T *
6162new_scope(cctx_T *cctx, scopetype_T type)
6163{
6164 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6165
6166 if (scope == NULL)
6167 return NULL;
6168 scope->se_outer = cctx->ctx_scope;
6169 cctx->ctx_scope = scope;
6170 scope->se_type = type;
6171 scope->se_local_count = cctx->ctx_locals.ga_len;
6172 return scope;
6173}
6174
6175/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006176 * Free the current scope and go back to the outer scope.
6177 */
6178 static void
6179drop_scope(cctx_T *cctx)
6180{
6181 scope_T *scope = cctx->ctx_scope;
6182
6183 if (scope == NULL)
6184 {
6185 iemsg("calling drop_scope() without a scope");
6186 return;
6187 }
6188 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006189 switch (scope->se_type)
6190 {
6191 case IF_SCOPE:
6192 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6193 case FOR_SCOPE:
6194 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6195 case WHILE_SCOPE:
6196 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6197 case TRY_SCOPE:
6198 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6199 case NO_SCOPE:
6200 case BLOCK_SCOPE:
6201 break;
6202 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006203 vim_free(scope);
6204}
6205
6206/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207 * compile "if expr"
6208 *
6209 * "if expr" Produces instructions:
6210 * EVAL expr Push result of "expr"
6211 * JUMP_IF_FALSE end
6212 * ... body ...
6213 * end:
6214 *
6215 * "if expr | else" Produces instructions:
6216 * EVAL expr Push result of "expr"
6217 * JUMP_IF_FALSE else
6218 * ... body ...
6219 * JUMP_ALWAYS end
6220 * else:
6221 * ... body ...
6222 * end:
6223 *
6224 * "if expr1 | elseif expr2 | else" Produces instructions:
6225 * EVAL expr Push result of "expr"
6226 * JUMP_IF_FALSE elseif
6227 * ... body ...
6228 * JUMP_ALWAYS end
6229 * elseif:
6230 * EVAL expr Push result of "expr"
6231 * JUMP_IF_FALSE else
6232 * ... body ...
6233 * JUMP_ALWAYS end
6234 * else:
6235 * ... body ...
6236 * end:
6237 */
6238 static char_u *
6239compile_if(char_u *arg, cctx_T *cctx)
6240{
6241 char_u *p = arg;
6242 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006243 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006245 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006246 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247
Bram Moolenaara5565e42020-05-09 15:44:01 +02006248 CLEAR_FIELD(ppconst);
6249 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006250 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006251 clear_ppconst(&ppconst);
6252 return NULL;
6253 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006254 if (cctx->ctx_skip == SKIP_YES)
6255 clear_ppconst(&ppconst);
6256 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006257 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006258 int error = FALSE;
6259 int v;
6260
Bram Moolenaara5565e42020-05-09 15:44:01 +02006261 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006262 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006263 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006264 if (error)
6265 return NULL;
6266 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006267 }
6268 else
6269 {
6270 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006271 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006272 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006273 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006274 if (bool_on_stack(cctx) == FAIL)
6275 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277
6278 scope = new_scope(cctx, IF_SCOPE);
6279 if (scope == NULL)
6280 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006281 scope->se_skip_save = skip_save;
6282 // "is_had_return" will be reset if any block does not end in :return
6283 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006285 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006286 {
6287 // "where" is set when ":elseif", "else" or ":endif" is found
6288 scope->se_u.se_if.is_if_label = instr->ga_len;
6289 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6290 }
6291 else
6292 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
6294 return p;
6295}
6296
6297 static char_u *
6298compile_elseif(char_u *arg, cctx_T *cctx)
6299{
6300 char_u *p = arg;
6301 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006302 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 isn_T *isn;
6304 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006305 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006306 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
6308 if (scope == NULL || scope->se_type != IF_SCOPE)
6309 {
6310 emsg(_(e_elseif_without_if));
6311 return NULL;
6312 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006313 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006314 if (!cctx->ctx_had_return)
6315 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006317 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006318 {
6319 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006320 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006321 return NULL;
6322 // previous "if" or "elseif" jumps here
6323 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6324 isn->isn_arg.jump.jump_where = instr->ga_len;
6325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006327 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006328 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006329 if (cctx->ctx_skip == SKIP_YES)
6330 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006331 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006332 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006333 clear_ppconst(&ppconst);
6334 return NULL;
6335 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006336 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006337 if (scope->se_skip_save == SKIP_YES)
6338 clear_ppconst(&ppconst);
6339 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006340 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006341 int error = FALSE;
6342 int v;
6343
Bram Moolenaar7f141552020-05-09 17:35:53 +02006344 // The expression results in a constant.
6345 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006346 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6347 if (error)
6348 return NULL;
6349 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006350 clear_ppconst(&ppconst);
6351 scope->se_u.se_if.is_if_label = -1;
6352 }
6353 else
6354 {
6355 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006356 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006357 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006358 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006359 if (bool_on_stack(cctx) == FAIL)
6360 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006362 // "where" is set when ":elseif", "else" or ":endif" is found
6363 scope->se_u.se_if.is_if_label = instr->ga_len;
6364 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6365 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366
6367 return p;
6368}
6369
6370 static char_u *
6371compile_else(char_u *arg, cctx_T *cctx)
6372{
6373 char_u *p = arg;
6374 garray_T *instr = &cctx->ctx_instr;
6375 isn_T *isn;
6376 scope_T *scope = cctx->ctx_scope;
6377
6378 if (scope == NULL || scope->se_type != IF_SCOPE)
6379 {
6380 emsg(_(e_else_without_if));
6381 return NULL;
6382 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006383 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006384 if (!cctx->ctx_had_return)
6385 scope->se_u.se_if.is_had_return = FALSE;
6386 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006387
Bram Moolenaarefd88552020-06-18 20:50:10 +02006388 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006389 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006390 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006391 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006392 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006393 if (!cctx->ctx_had_return
6394 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6395 JUMP_ALWAYS, cctx) == FAIL)
6396 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006397 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006398
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006399 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006400 {
6401 if (scope->se_u.se_if.is_if_label >= 0)
6402 {
6403 // previous "if" or "elseif" jumps here
6404 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6405 isn->isn_arg.jump.jump_where = instr->ga_len;
6406 scope->se_u.se_if.is_if_label = -1;
6407 }
6408 }
6409
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006410 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006411 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413
6414 return p;
6415}
6416
6417 static char_u *
6418compile_endif(char_u *arg, cctx_T *cctx)
6419{
6420 scope_T *scope = cctx->ctx_scope;
6421 ifscope_T *ifscope;
6422 garray_T *instr = &cctx->ctx_instr;
6423 isn_T *isn;
6424
6425 if (scope == NULL || scope->se_type != IF_SCOPE)
6426 {
6427 emsg(_(e_endif_without_if));
6428 return NULL;
6429 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006430 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006431 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006432 if (!cctx->ctx_had_return)
6433 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006434
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006435 if (scope->se_u.se_if.is_if_label >= 0)
6436 {
6437 // previous "if" or "elseif" jumps here
6438 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6439 isn->isn_arg.jump.jump_where = instr->ga_len;
6440 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006441 // Fill in the "end" label in jumps at the end of the blocks.
6442 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006443 cctx->ctx_skip = scope->se_skip_save;
6444
6445 // If all the blocks end in :return and there is an :else then the
6446 // had_return flag is set.
6447 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006448
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006449 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006450 return arg;
6451}
6452
6453/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006454 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006455 *
6456 * Produces instructions:
6457 * PUSHNR -1
6458 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006459 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006460 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6461 * - if beyond end, jump to "end"
6462 * - otherwise get item from list and push it
6463 * STORE var Store item in "var"
6464 * ... body ...
6465 * JUMP top Jump back to repeat
6466 * end: DROP Drop the result of "expr"
6467 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006468 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6469 * UNPACK 2 Split item in 2
6470 * STORE var1 Store item in "var1"
6471 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006472 */
6473 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006474compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006475{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006476 char_u *arg;
6477 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006478 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006479 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006480 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006481 int var_count = 0;
6482 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006483 size_t varlen;
6484 garray_T *instr = &cctx->ctx_instr;
6485 garray_T *stack = &cctx->ctx_type_stack;
6486 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006487 lvar_T *loop_lvar; // loop iteration variable
6488 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006489 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006490 type_T *item_type = &t_any;
6491 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006492
Bram Moolenaar792f7862020-11-23 08:31:18 +01006493 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6494 if (var_count == 0)
6495 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006496
6497 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006498 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006499 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006500 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6501 return NULL;
6502 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006503 {
6504 emsg(_(e_missing_in));
6505 return NULL;
6506 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006507 wp = p + 2;
6508 p = skipwhite(wp);
6509 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6510 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512 scope = new_scope(cctx, FOR_SCOPE);
6513 if (scope == NULL)
6514 return NULL;
6515
Bram Moolenaar792f7862020-11-23 08:31:18 +01006516 // Reserve a variable to store the loop iteration counter and initialize it
6517 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006518 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6519 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006520 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006521 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006522 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006523 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006524 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006525 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006526
6527 // compile "expr", it remains on the stack until "endfor"
6528 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006529 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006530 {
6531 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006532 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006533 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006534 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006535
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006536 // Now that we know the type of "var", check that it is a list, now or at
6537 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006538 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006539 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006540 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006541 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006542 return NULL;
6543 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006544
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006545 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006546 {
6547 if (var_count == 1)
6548 item_type = vartype->tt_member;
6549 else if (vartype->tt_member->tt_type == VAR_LIST
6550 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6551 item_type = vartype->tt_member->tt_member;
6552 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553
6554 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006555 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006556 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006557
Bram Moolenaar792f7862020-11-23 08:31:18 +01006558 arg = arg_start;
6559 if (var_count > 1)
6560 {
6561 generate_UNPACK(cctx, var_count, semicolon);
6562 arg = skipwhite(arg + 1); // skip white after '['
6563
6564 // the list item is replaced by a number of items
6565 if (ga_grow(stack, var_count - 1) == FAIL)
6566 {
6567 drop_scope(cctx);
6568 return NULL;
6569 }
6570 --stack->ga_len;
6571 for (idx = 0; idx < var_count; ++idx)
6572 {
6573 ((type_T **)stack->ga_data)[stack->ga_len] =
6574 (semicolon && idx == 0) ? vartype : item_type;
6575 ++stack->ga_len;
6576 }
6577 }
6578
6579 for (idx = 0; idx < var_count; ++idx)
6580 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006581 assign_dest_T dest = dest_local;
6582 int opt_flags = 0;
6583 int vimvaridx = -1;
6584 type_T *type = &t_any;
6585
6586 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006587 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006588 name = vim_strnsave(arg, varlen);
6589 if (name == NULL)
6590 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006591
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006592 // TODO: script var not supported?
6593 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6594 &vimvaridx, &type, cctx) == FAIL)
6595 goto failed;
6596 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006597 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006598 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6599 0, 0, type, name) == FAIL)
6600 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006601 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006602 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006603 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006604 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006605 {
6606 semsg(_(e_variable_already_declared), arg);
6607 goto failed;
6608 }
6609
Bram Moolenaarea870692020-12-02 14:24:30 +01006610 if (STRNCMP(name, "s:", 2) == 0)
6611 {
6612 semsg(_(e_cannot_declare_script_variable_in_function), name);
6613 goto failed;
6614 }
6615
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006616 // Reserve a variable to store "var".
6617 // TODO: check for type
6618 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6619 if (var_lvar == NULL)
6620 // out of memory or used as an argument
6621 goto failed;
6622
6623 if (semicolon && idx == var_count - 1)
6624 var_lvar->lv_type = vartype;
6625 else
6626 var_lvar->lv_type = item_type;
6627 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6628 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006629
6630 if (*p == ',' || *p == ';')
6631 ++p;
6632 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006633 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006634 }
6635
6636 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006637
6638failed:
6639 vim_free(name);
6640 drop_scope(cctx);
6641 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006642}
6643
6644/*
6645 * compile "endfor"
6646 */
6647 static char_u *
6648compile_endfor(char_u *arg, cctx_T *cctx)
6649{
6650 garray_T *instr = &cctx->ctx_instr;
6651 scope_T *scope = cctx->ctx_scope;
6652 forscope_T *forscope;
6653 isn_T *isn;
6654
6655 if (scope == NULL || scope->se_type != FOR_SCOPE)
6656 {
6657 emsg(_(e_for));
6658 return NULL;
6659 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006660 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006661 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006662 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006663
6664 // At end of ":for" scope jump back to the FOR instruction.
6665 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6666
6667 // Fill in the "end" label in the FOR statement so it can jump here
6668 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6669 isn->isn_arg.forloop.for_end = instr->ga_len;
6670
6671 // Fill in the "end" label any BREAK statements
6672 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6673
6674 // Below the ":for" scope drop the "expr" list from the stack.
6675 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6676 return NULL;
6677
6678 vim_free(scope);
6679
6680 return arg;
6681}
6682
6683/*
6684 * compile "while expr"
6685 *
6686 * Produces instructions:
6687 * top: EVAL expr Push result of "expr"
6688 * JUMP_IF_FALSE end jump if false
6689 * ... body ...
6690 * JUMP top Jump back to repeat
6691 * end:
6692 *
6693 */
6694 static char_u *
6695compile_while(char_u *arg, cctx_T *cctx)
6696{
6697 char_u *p = arg;
6698 garray_T *instr = &cctx->ctx_instr;
6699 scope_T *scope;
6700
6701 scope = new_scope(cctx, WHILE_SCOPE);
6702 if (scope == NULL)
6703 return NULL;
6704
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006705 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706
6707 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006708 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006709 return NULL;
6710
Bram Moolenaar13106602020-10-04 16:06:05 +02006711 if (bool_on_stack(cctx) == FAIL)
6712 return FAIL;
6713
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006714 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006715 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006716 JUMP_IF_FALSE, cctx) == FAIL)
6717 return FAIL;
6718
6719 return p;
6720}
6721
6722/*
6723 * compile "endwhile"
6724 */
6725 static char_u *
6726compile_endwhile(char_u *arg, cctx_T *cctx)
6727{
6728 scope_T *scope = cctx->ctx_scope;
6729
6730 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6731 {
6732 emsg(_(e_while));
6733 return NULL;
6734 }
6735 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006736 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006737
6738 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006739 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006740
6741 // Fill in the "end" label in the WHILE statement so it can jump here.
6742 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006743 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006744
6745 vim_free(scope);
6746
6747 return arg;
6748}
6749
6750/*
6751 * compile "continue"
6752 */
6753 static char_u *
6754compile_continue(char_u *arg, cctx_T *cctx)
6755{
6756 scope_T *scope = cctx->ctx_scope;
6757
6758 for (;;)
6759 {
6760 if (scope == NULL)
6761 {
6762 emsg(_(e_continue));
6763 return NULL;
6764 }
6765 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6766 break;
6767 scope = scope->se_outer;
6768 }
6769
6770 // Jump back to the FOR or WHILE instruction.
6771 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006772 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6773 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006774 return arg;
6775}
6776
6777/*
6778 * compile "break"
6779 */
6780 static char_u *
6781compile_break(char_u *arg, cctx_T *cctx)
6782{
6783 scope_T *scope = cctx->ctx_scope;
6784 endlabel_T **el;
6785
6786 for (;;)
6787 {
6788 if (scope == NULL)
6789 {
6790 emsg(_(e_break));
6791 return NULL;
6792 }
6793 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6794 break;
6795 scope = scope->se_outer;
6796 }
6797
6798 // Jump to the end of the FOR or WHILE loop.
6799 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006800 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006802 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006803 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6804 return FAIL;
6805
6806 return arg;
6807}
6808
6809/*
6810 * compile "{" start of block
6811 */
6812 static char_u *
6813compile_block(char_u *arg, cctx_T *cctx)
6814{
6815 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6816 return NULL;
6817 return skipwhite(arg + 1);
6818}
6819
6820/*
6821 * compile end of block: drop one scope
6822 */
6823 static void
6824compile_endblock(cctx_T *cctx)
6825{
6826 scope_T *scope = cctx->ctx_scope;
6827
6828 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006829 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006830 vim_free(scope);
6831}
6832
6833/*
6834 * compile "try"
6835 * Creates a new scope for the try-endtry, pointing to the first catch and
6836 * finally.
6837 * Creates another scope for the "try" block itself.
6838 * TRY instruction sets up exception handling at runtime.
6839 *
6840 * "try"
6841 * TRY -> catch1, -> finally push trystack entry
6842 * ... try block
6843 * "throw {exception}"
6844 * EVAL {exception}
6845 * THROW create exception
6846 * ... try block
6847 * " catch {expr}"
6848 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006849 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006850 * EVAL {expr}
6851 * MATCH
6852 * JUMP nomatch -> catch2
6853 * CATCH remove exception
6854 * ... catch block
6855 * " catch"
6856 * JUMP -> finally
6857 * catch2: CATCH remove exception
6858 * ... catch block
6859 * " finally"
6860 * finally:
6861 * ... finally block
6862 * " endtry"
6863 * ENDTRY pop trystack entry, may rethrow
6864 */
6865 static char_u *
6866compile_try(char_u *arg, cctx_T *cctx)
6867{
6868 garray_T *instr = &cctx->ctx_instr;
6869 scope_T *try_scope;
6870 scope_T *scope;
6871
6872 // scope that holds the jumps that go to catch/finally/endtry
6873 try_scope = new_scope(cctx, TRY_SCOPE);
6874 if (try_scope == NULL)
6875 return NULL;
6876
6877 // "catch" is set when the first ":catch" is found.
6878 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006879 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006880 if (generate_instr(cctx, ISN_TRY) == NULL)
6881 return NULL;
6882
6883 // scope for the try block itself
6884 scope = new_scope(cctx, BLOCK_SCOPE);
6885 if (scope == NULL)
6886 return NULL;
6887
6888 return arg;
6889}
6890
6891/*
6892 * compile "catch {expr}"
6893 */
6894 static char_u *
6895compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6896{
6897 scope_T *scope = cctx->ctx_scope;
6898 garray_T *instr = &cctx->ctx_instr;
6899 char_u *p;
6900 isn_T *isn;
6901
6902 // end block scope from :try or :catch
6903 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6904 compile_endblock(cctx);
6905 scope = cctx->ctx_scope;
6906
6907 // Error if not in a :try scope
6908 if (scope == NULL || scope->se_type != TRY_SCOPE)
6909 {
6910 emsg(_(e_catch));
6911 return NULL;
6912 }
6913
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006914 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006915 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006916 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006917 return NULL;
6918 }
6919
6920 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006921 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006922 JUMP_ALWAYS, cctx) == FAIL)
6923 return NULL;
6924
6925 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006926 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006927 if (isn->isn_arg.try.try_catch == 0)
6928 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006929 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006930 {
6931 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006932 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006933 isn->isn_arg.jump.jump_where = instr->ga_len;
6934 }
6935
6936 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02006937 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006938 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006939 scope->se_u.se_try.ts_caught_all = TRUE;
6940 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006941 }
6942 else
6943 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006944 char_u *end;
6945 char_u *pat;
6946 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006947 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006948 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006950 // Push v:exception, push {expr} and MATCH
6951 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
6952
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006953 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006954 if (*end != *p)
6955 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02006956 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006957 vim_free(tofree);
6958 return FAIL;
6959 }
6960 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01006961 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006962 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006963 len = (int)(end - tofree);
6964 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006965 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006966 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01006967 if (pat == NULL)
6968 return FAIL;
6969 if (generate_PUSHS(cctx, pat) == FAIL)
6970 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006972 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
6973 return NULL;
6974
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006975 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006976 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
6977 return NULL;
6978 }
6979
6980 if (generate_instr(cctx, ISN_CATCH) == NULL)
6981 return NULL;
6982
6983 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6984 return NULL;
6985 return p;
6986}
6987
6988 static char_u *
6989compile_finally(char_u *arg, cctx_T *cctx)
6990{
6991 scope_T *scope = cctx->ctx_scope;
6992 garray_T *instr = &cctx->ctx_instr;
6993 isn_T *isn;
6994
6995 // end block scope from :try or :catch
6996 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6997 compile_endblock(cctx);
6998 scope = cctx->ctx_scope;
6999
7000 // Error if not in a :try scope
7001 if (scope == NULL || scope->se_type != TRY_SCOPE)
7002 {
7003 emsg(_(e_finally));
7004 return NULL;
7005 }
7006
7007 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007008 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 if (isn->isn_arg.try.try_finally != 0)
7010 {
7011 emsg(_(e_finally_dup));
7012 return NULL;
7013 }
7014
7015 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007016 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007017
Bram Moolenaar585fea72020-04-02 22:33:21 +02007018 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007019 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007020 {
7021 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007022 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007024 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 }
7026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 // TODO: set index in ts_finally_label jumps
7028
7029 return arg;
7030}
7031
7032 static char_u *
7033compile_endtry(char_u *arg, cctx_T *cctx)
7034{
7035 scope_T *scope = cctx->ctx_scope;
7036 garray_T *instr = &cctx->ctx_instr;
7037 isn_T *isn;
7038
7039 // end block scope from :catch or :finally
7040 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7041 compile_endblock(cctx);
7042 scope = cctx->ctx_scope;
7043
7044 // Error if not in a :try scope
7045 if (scope == NULL || scope->se_type != TRY_SCOPE)
7046 {
7047 if (scope == NULL)
7048 emsg(_(e_no_endtry));
7049 else if (scope->se_type == WHILE_SCOPE)
7050 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007051 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007052 emsg(_(e_endfor));
7053 else
7054 emsg(_(e_endif));
7055 return NULL;
7056 }
7057
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007058 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007059 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7060 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007061 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 return NULL;
7063 }
7064
7065 // Fill in the "end" label in jumps at the end of the blocks, if not done
7066 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007067 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068
7069 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007070 if (isn->isn_arg.try.try_catch == 0)
7071 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 if (isn->isn_arg.try.try_finally == 0)
7073 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007074
7075 if (scope->se_u.se_try.ts_catch_label != 0)
7076 {
7077 // Last catch without match jumps here
7078 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7079 isn->isn_arg.jump.jump_where = instr->ga_len;
7080 }
7081
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082 compile_endblock(cctx);
7083
7084 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7085 return NULL;
7086 return arg;
7087}
7088
7089/*
7090 * compile "throw {expr}"
7091 */
7092 static char_u *
7093compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7094{
7095 char_u *p = skipwhite(arg);
7096
Bram Moolenaara5565e42020-05-09 15:44:01 +02007097 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007098 return NULL;
7099 if (may_generate_2STRING(-1, cctx) == FAIL)
7100 return NULL;
7101 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7102 return NULL;
7103
7104 return p;
7105}
7106
7107/*
7108 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007109 * compile "echomsg expr"
7110 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007111 * compile "execute expr"
7112 */
7113 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007114compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007115{
7116 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007117 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007118 int count = 0;
7119
7120 for (;;)
7121 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007122 if (ends_excmd2(prev, p))
7123 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007124 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007125 return NULL;
7126 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007127 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007128 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007129 }
7130
Bram Moolenaare4984292020-12-13 14:19:25 +01007131 if (count > 0)
7132 {
7133 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7134 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7135 else if (cmdidx == CMD_execute)
7136 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7137 else if (cmdidx == CMD_echomsg)
7138 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7139 else
7140 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007142 return p;
7143}
7144
7145/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007146 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007147 * instruction to compute it and return OK.
7148 * Otherwise return FAIL, the caller must deal with any range.
7149 */
7150 static int
7151compile_variable_range(exarg_T *eap, cctx_T *cctx)
7152{
7153 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7154 char_u *p = skipdigits(eap->cmd);
7155
7156 if (p == range_end)
7157 return FAIL;
7158 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7159}
7160
7161/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007162 * :put r
7163 * :put ={expr}
7164 */
7165 static char_u *
7166compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7167{
7168 char_u *line = arg;
7169 linenr_T lnum;
7170 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007171 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007172
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007173 eap->regname = *line;
7174
7175 if (eap->regname == '=')
7176 {
7177 char_u *p = line + 1;
7178
7179 if (compile_expr0(&p, cctx) == FAIL)
7180 return NULL;
7181 line = p;
7182 }
7183 else if (eap->regname != NUL)
7184 ++line;
7185
Bram Moolenaar08597872020-12-10 19:43:40 +01007186 if (compile_variable_range(eap, cctx) == OK)
7187 {
7188 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7189 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007190 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007191 {
7192 // Either no range or a number.
7193 // "errormsg" will not be set because the range is ADDR_LINES.
7194 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007195 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007196 return NULL;
7197 if (eap->addr_count == 0)
7198 lnum = -1;
7199 else
7200 lnum = eap->line2;
7201 if (above)
7202 --lnum;
7203 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007204
7205 generate_PUT(cctx, eap->regname, lnum);
7206 return line;
7207}
7208
7209/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007210 * A command that is not compiled, execute with legacy code.
7211 */
7212 static char_u *
7213compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7214{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007215 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007216 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007217 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007218
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007219 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007220 goto theend;
7221
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007222 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007223 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007224 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007225 int usefilter = FALSE;
7226
7227 has_expr = argt & (EX_XFILE | EX_EXPAND);
7228
7229 // If the command can be followed by a bar, find the bar and truncate
7230 // it, so that the following command can be compiled.
7231 // The '|' is overwritten with a NUL, it is put back below.
7232 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7233 && *eap->arg == '!')
7234 // :w !filter or :r !filter or :r! filter
7235 usefilter = TRUE;
7236 if ((argt & EX_TRLBAR) && !usefilter)
7237 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007238 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007239 separate_nextcmd(eap);
7240 if (eap->nextcmd != NULL)
7241 nextcmd = eap->nextcmd;
7242 }
7243 }
7244
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007245 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7246 {
7247 // expand filename in "syntax include [@group] filename"
7248 has_expr = TRUE;
7249 eap->arg = skipwhite(eap->arg + 7);
7250 if (*eap->arg == '@')
7251 eap->arg = skiptowhite(eap->arg);
7252 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007253
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007254 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007255 {
7256 int count = 0;
7257 char_u *start = skipwhite(line);
7258
7259 // :cmd xxx`=expr1`yyy`=expr2`zzz
7260 // PUSHS ":cmd xxx"
7261 // eval expr1
7262 // PUSHS "yyy"
7263 // eval expr2
7264 // PUSHS "zzz"
7265 // EXECCONCAT 5
7266 for (;;)
7267 {
7268 if (p > start)
7269 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007270 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007271 ++count;
7272 }
7273 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007274 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007275 return NULL;
7276 may_generate_2STRING(-1, cctx);
7277 ++count;
7278 p = skipwhite(p);
7279 if (*p != '`')
7280 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007281 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007282 return NULL;
7283 }
7284 start = p + 1;
7285
7286 p = (char_u *)strstr((char *)start, "`=");
7287 if (p == NULL)
7288 {
7289 if (*skipwhite(start) != NUL)
7290 {
7291 generate_PUSHS(cctx, vim_strsave(start));
7292 ++count;
7293 }
7294 break;
7295 }
7296 }
7297 generate_EXECCONCAT(cctx, count);
7298 }
7299 else
7300 generate_EXEC(cctx, line);
7301
7302theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007303 if (*nextcmd != NUL)
7304 {
7305 // the parser expects a pointer to the bar, put it back
7306 --nextcmd;
7307 *nextcmd = '|';
7308 }
7309
7310 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007311}
7312
7313/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007314 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007315 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007316 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007317 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007318add_def_function(ufunc_T *ufunc)
7319{
7320 dfunc_T *dfunc;
7321
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007322 if (def_functions.ga_len == 0)
7323 {
7324 // The first position is not used, so that a zero uf_dfunc_idx means it
7325 // wasn't set.
7326 if (ga_grow(&def_functions, 1) == FAIL)
7327 return FAIL;
7328 ++def_functions.ga_len;
7329 }
7330
Bram Moolenaar09689a02020-05-09 22:50:08 +02007331 // Add the function to "def_functions".
7332 if (ga_grow(&def_functions, 1) == FAIL)
7333 return FAIL;
7334 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7335 CLEAR_POINTER(dfunc);
7336 dfunc->df_idx = def_functions.ga_len;
7337 ufunc->uf_dfunc_idx = dfunc->df_idx;
7338 dfunc->df_ufunc = ufunc;
7339 ++def_functions.ga_len;
7340 return OK;
7341}
7342
7343/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 * After ex_function() has collected all the function lines: parse and compile
7345 * the lines into instructions.
7346 * Adds the function to "def_functions".
7347 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
7348 * return statement (used for lambda).
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007349 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007350 * This can be used recursively through compile_lambda(), which may reallocate
7351 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007352 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007354 int
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007355compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 char_u *line = NULL;
7358 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 cctx_T cctx;
7361 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007362 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 int ret = FAIL;
7364 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007365 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007366 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007367 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007369 // When using a function that was compiled before: Free old instructions.
7370 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007371 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007372 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007373 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7374 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007375 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007377 else
7378 {
7379 if (add_def_function(ufunc) == FAIL)
7380 return FAIL;
7381 new_def_function = TRUE;
7382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383
Bram Moolenaar985116a2020-07-12 17:31:09 +02007384 ufunc->uf_def_status = UF_COMPILING;
7385
Bram Moolenaara80faa82020-04-12 19:37:17 +02007386 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 cctx.ctx_ufunc = ufunc;
7388 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007389 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7391 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7392 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7393 cctx.ctx_type_list = &ufunc->uf_type_list;
7394 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7395 instr = &cctx.ctx_instr;
7396
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007397 // Set the context to the function, it may be compiled when called from
7398 // another script. Set the script version to the most modern one.
7399 // The line number will be set in next_line_from_context().
7400 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007401 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7402
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007403 // Make sure error messages are OK.
7404 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7405 if (do_estack_push)
7406 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007407 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007408
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007409 if (ufunc->uf_def_args.ga_len > 0)
7410 {
7411 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007412 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007413 int i;
7414 char_u *arg;
7415 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007416 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007417
7418 // Produce instructions for the default values of optional arguments.
7419 // Store the instruction index in uf_def_arg_idx[] so that we know
7420 // where to start when the function is called, depending on the number
7421 // of arguments.
7422 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7423 if (ufunc->uf_def_arg_idx == NULL)
7424 goto erret;
7425 for (i = 0; i < count; ++i)
7426 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007427 garray_T *stack = &cctx.ctx_type_stack;
7428 type_T *val_type;
7429 int arg_idx = first_def_arg + i;
7430
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007431 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7432 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007433 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007434 goto erret;
7435
7436 // If no type specified use the type of the default value.
7437 // Otherwise check that the default value type matches the
7438 // specified type.
7439 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7440 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007441 {
7442 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007443 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007444 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007445 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7446 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007447 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007448
7449 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007450 goto erret;
7451 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007452 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007453
7454 if (did_set_arg_type)
7455 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007456 }
7457
7458 /*
7459 * Loop over all the lines of the function and generate instructions.
7460 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461 for (;;)
7462 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007463 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007464 int starts_with_colon = FALSE;
7465 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007466 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007467
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007468 // Bail out on the first error to avoid a flood of errors and report
7469 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007470 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007471 goto erret;
7472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 if (line != NULL && *line == '|')
7474 // the line continues after a '|'
7475 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007476 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007477 && !(*line == '#' && (line == cctx.ctx_line_start
7478 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007479 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007480 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007481 goto erret;
7482 }
7483 else
7484 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007485 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007486 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007487 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007488 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007489 }
7490
Bram Moolenaara80faa82020-04-12 19:37:17 +02007491 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 ea.cmdlinep = &line;
7493 ea.cmd = skipwhite(line);
7494
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007495 // Some things can be recognized by the first character.
7496 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007497 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007498 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007499 // "#" starts a comment
7500 line = (char_u *)"";
7501 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007503 case '}':
7504 {
7505 // "}" ends a block scope
7506 scopetype_T stype = cctx.ctx_scope == NULL
7507 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007508
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007509 if (stype == BLOCK_SCOPE)
7510 {
7511 compile_endblock(&cctx);
7512 line = ea.cmd;
7513 }
7514 else
7515 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007516 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007517 goto erret;
7518 }
7519 if (line != NULL)
7520 line = skipwhite(ea.cmd + 1);
7521 continue;
7522 }
7523
7524 case '{':
7525 // "{" starts a block scope
7526 // "{'a': 1}->func() is something else
7527 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7528 {
7529 line = compile_block(ea.cmd, &cctx);
7530 continue;
7531 }
7532 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 }
7534
7535 /*
7536 * COMMAND MODIFIERS
7537 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007538 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007539 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7540 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007541 {
7542 if (errormsg != NULL)
7543 goto erret;
7544 // empty line or comment
7545 line = (char_u *)"";
7546 continue;
7547 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007548 generate_cmdmods(&cctx, &local_cmdmod);
7549 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007550
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007551 // Check if there was a colon after the last command modifier or before
7552 // the current position.
7553 for (p = ea.cmd; p >= line; --p)
7554 {
7555 if (*p == ':')
7556 starts_with_colon = TRUE;
7557 if (p < ea.cmd && !VIM_ISWHITE(*p))
7558 break;
7559 }
7560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007561 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007562 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007563 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007564 {
7565 if (*ea.cmd == '(')
7566 // not for "call()"
7567 ea.cmd = p;
7568 else
7569 ea.cmd = skipwhite(ea.cmd);
7570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007571
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007572 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007573 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007574 char_u *pskip;
7575
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007576 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007577 // find what follows.
7578 // Skip over "var.member", "var[idx]" and the like.
7579 // Also "&opt = val", "$ENV = val" and "@r = val".
7580 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007581 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007582 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007583 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007584 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007585 char_u *var_end;
7586 int oplen;
7587 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007588
Bram Moolenaar65821722020-08-02 18:58:54 +02007589 if (ea.cmd[0] == '@')
7590 var_end = ea.cmd + 2;
7591 else
7592 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007593 FNE_CHECK_START | FNE_INCL_BR);
7594 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007595 if (oplen > 0)
7596 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007597 size_t len = p - ea.cmd;
7598
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007599 // Recognize an assignment if we recognize the variable
7600 // name:
7601 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007602 // "local = expr" where "local" is a local var.
7603 // "script = expr" where "script" is a script-local var.
7604 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007605 // "&opt = expr"
7606 // "$ENV = expr"
7607 // "@r = expr"
7608 if (*ea.cmd == '&'
7609 || *ea.cmd == '$'
7610 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007611 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007612 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007613 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007614 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007615 || script_var_exists(ea.cmd, len,
7616 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007617 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007618 {
7619 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007620 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007621 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007622 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007624 }
7625 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007626
7627 if (*ea.cmd == '[')
7628 {
7629 // [var, var] = expr
7630 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7631 if (line == NULL)
7632 goto erret;
7633 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007634 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007636 }
7637
7638 /*
7639 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007640 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007641 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007642 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007643 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007644 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007645 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007646 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007647 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007648 if (!starts_with_colon)
7649 {
7650 emsg(_(e_colon_required_before_a_range));
7651 goto erret;
7652 }
7653 if (ends_excmd2(line, ea.cmd))
7654 {
7655 // A range without a command: jump to the line.
7656 // TODO: compile to a more efficient command, possibly
7657 // calling parse_cmd_address().
7658 ea.cmdidx = CMD_SIZE;
7659 line = compile_exec(line, &ea, &cctx);
7660 goto nextline;
7661 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007662 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007663 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007664 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007665 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007666 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007667
7668 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7669 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007670 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007671 {
7672 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007673 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007674 }
7675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007676 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007677 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007678 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007679 // CMD_var cannot happen, compile_assignment() above would be
7680 // used. Most likely an assignment to a non-existing variable.
7681 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007682 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007684 }
7685
Bram Moolenaar3988f642020-08-27 22:43:03 +02007686 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007687 && ea.cmdidx != CMD_elseif
7688 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007689 && ea.cmdidx != CMD_endif
7690 && ea.cmdidx != CMD_endfor
7691 && ea.cmdidx != CMD_endwhile
7692 && ea.cmdidx != CMD_catch
7693 && ea.cmdidx != CMD_finally
7694 && ea.cmdidx != CMD_endtry)
7695 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007696 emsg(_(e_unreachable_code_after_return));
7697 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007698 }
7699
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007700 p = skipwhite(p);
7701 if (ea.cmdidx != CMD_SIZE
7702 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7703 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007704 if (ea.cmdidx >= 0)
7705 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007706 if ((ea.argt & EX_BANG) && *p == '!')
7707 {
7708 ea.forceit = TRUE;
7709 p = skipwhite(p + 1);
7710 }
7711 }
7712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007713 switch (ea.cmdidx)
7714 {
7715 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007716 ea.arg = p;
7717 line = compile_nested_function(&ea, &cctx);
7718 break;
7719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007720 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007721 // TODO: should we allow this, e.g. to declare a global
7722 // function?
7723 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007724 goto erret;
7725
7726 case CMD_return:
7727 line = compile_return(p, set_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007728 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007729 break;
7730
7731 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007732 emsg(_(e_cannot_use_let_in_vim9_script));
7733 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007734 case CMD_var:
7735 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007736 case CMD_const:
7737 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007738 if (line == p)
7739 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007740 break;
7741
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007742 case CMD_unlet:
7743 case CMD_unlockvar:
7744 case CMD_lockvar:
7745 line = compile_unletlock(p, &ea, &cctx);
7746 break;
7747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007748 case CMD_import:
7749 line = compile_import(p, &cctx);
7750 break;
7751
7752 case CMD_if:
7753 line = compile_if(p, &cctx);
7754 break;
7755 case CMD_elseif:
7756 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007757 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007758 break;
7759 case CMD_else:
7760 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007761 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007762 break;
7763 case CMD_endif:
7764 line = compile_endif(p, &cctx);
7765 break;
7766
7767 case CMD_while:
7768 line = compile_while(p, &cctx);
7769 break;
7770 case CMD_endwhile:
7771 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007772 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007773 break;
7774
7775 case CMD_for:
7776 line = compile_for(p, &cctx);
7777 break;
7778 case CMD_endfor:
7779 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007780 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007781 break;
7782 case CMD_continue:
7783 line = compile_continue(p, &cctx);
7784 break;
7785 case CMD_break:
7786 line = compile_break(p, &cctx);
7787 break;
7788
7789 case CMD_try:
7790 line = compile_try(p, &cctx);
7791 break;
7792 case CMD_catch:
7793 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007794 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007795 break;
7796 case CMD_finally:
7797 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007798 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007799 break;
7800 case CMD_endtry:
7801 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007802 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 break;
7804 case CMD_throw:
7805 line = compile_throw(p, &cctx);
7806 break;
7807
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007808 case CMD_eval:
7809 if (compile_expr0(&p, &cctx) == FAIL)
7810 goto erret;
7811
Bram Moolenaar3988f642020-08-27 22:43:03 +02007812 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007813 generate_instr_drop(&cctx, ISN_DROP, 1);
7814
7815 line = skipwhite(p);
7816 break;
7817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007818 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007819 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007820 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007821 case CMD_echomsg:
7822 case CMD_echoerr:
7823 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007824 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007826 case CMD_put:
7827 ea.cmd = cmd;
7828 line = compile_put(p, &ea, &cctx);
7829 break;
7830
Bram Moolenaar3988f642020-08-27 22:43:03 +02007831 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007832
Bram Moolenaarae616492020-07-28 20:07:27 +02007833 case CMD_append:
7834 case CMD_change:
7835 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007836 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007837 case CMD_xit:
7838 not_in_vim9(&ea);
7839 goto erret;
7840
Bram Moolenaar002262f2020-07-08 17:47:57 +02007841 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007842 if (cctx.ctx_skip != SKIP_YES)
7843 {
7844 semsg(_(e_invalid_command_str), ea.cmd);
7845 goto erret;
7846 }
7847 // We don't check for a next command here.
7848 line = (char_u *)"";
7849 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007851 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007852 if (cctx.ctx_skip == SKIP_YES)
7853 {
7854 // We don't check for a next command here.
7855 line = (char_u *)"";
7856 }
7857 else
7858 {
7859 // Not recognized, execute with do_cmdline_cmd().
7860 ea.arg = p;
7861 line = compile_exec(line, &ea, &cctx);
7862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007863 break;
7864 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007865nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 if (line == NULL)
7867 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007868 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007869
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007870 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007871 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007873 if (cctx.ctx_type_stack.ga_len < 0)
7874 {
7875 iemsg("Type stack underflow");
7876 goto erret;
7877 }
7878 }
7879
7880 if (cctx.ctx_scope != NULL)
7881 {
7882 if (cctx.ctx_scope->se_type == IF_SCOPE)
7883 emsg(_(e_endif));
7884 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7885 emsg(_(e_endwhile));
7886 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7887 emsg(_(e_endfor));
7888 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007889 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007890 goto erret;
7891 }
7892
Bram Moolenaarefd88552020-06-18 20:50:10 +02007893 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007894 {
7895 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7896 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007897 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007898 goto erret;
7899 }
7900
7901 // Return zero if there is no return at the end.
7902 generate_PUSHNR(&cctx, 0);
7903 generate_instr(&cctx, ISN_RETURN);
7904 }
7905
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007906 {
7907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7908 + ufunc->uf_dfunc_idx;
7909 dfunc->df_deleted = FALSE;
7910 dfunc->df_instr = instr->ga_data;
7911 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007912 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007913 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007914 if (cctx.ctx_outer_used)
7915 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007916 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918
7919 ret = OK;
7920
7921erret:
7922 if (ret == FAIL)
7923 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007924 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007925 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7926 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007927
7928 for (idx = 0; idx < instr->ga_len; ++idx)
7929 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007930 ga_clear(instr);
Bram Moolenaar20431c92020-03-20 18:39:46 +01007931
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007932 // If using the last entry in the table and it was added above, we
7933 // might as well remove it.
7934 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02007935 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007936 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007937 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007938 ufunc->uf_dfunc_idx = 0;
7939 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007940 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007941
Bram Moolenaar3cca2992020-04-02 22:57:36 +02007942 while (cctx.ctx_scope != NULL)
7943 drop_scope(&cctx);
7944
Bram Moolenaar20431c92020-03-20 18:39:46 +01007945 // Don't execute this function body.
7946 ga_clear_strings(&ufunc->uf_lines);
7947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007948 if (errormsg != NULL)
7949 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01007950 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02007951 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007952 }
7953
7954 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007955 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007956 if (do_estack_push)
7957 estack_pop();
7958
Bram Moolenaar20431c92020-03-20 18:39:46 +01007959 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007960 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02007962 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007963}
7964
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02007965 void
7966set_function_type(ufunc_T *ufunc)
7967{
7968 int varargs = ufunc->uf_va_name != NULL;
7969 int argcount = ufunc->uf_args.ga_len;
7970
7971 // Create a type for the function, with the return type and any
7972 // argument types.
7973 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
7974 // The type is included in "tt_args".
7975 if (argcount > 0 || varargs)
7976 {
7977 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
7978 argcount, &ufunc->uf_type_list);
7979 // Add argument types to the function type.
7980 if (func_type_add_arg_types(ufunc->uf_func_type,
7981 argcount + varargs,
7982 &ufunc->uf_type_list) == FAIL)
7983 return;
7984 ufunc->uf_func_type->tt_argcount = argcount + varargs;
7985 ufunc->uf_func_type->tt_min_argcount =
7986 argcount - ufunc->uf_def_args.ga_len;
7987 if (ufunc->uf_arg_types == NULL)
7988 {
7989 int i;
7990
7991 // lambda does not have argument types.
7992 for (i = 0; i < argcount; ++i)
7993 ufunc->uf_func_type->tt_args[i] = &t_any;
7994 }
7995 else
7996 mch_memmove(ufunc->uf_func_type->tt_args,
7997 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
7998 if (varargs)
7999 {
8000 ufunc->uf_func_type->tt_args[argcount] =
8001 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8002 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8003 }
8004 }
8005 else
8006 // No arguments, can use a predefined type.
8007 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8008 argcount, &ufunc->uf_type_list);
8009}
8010
8011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008012/*
8013 * Delete an instruction, free what it contains.
8014 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008015 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008016delete_instr(isn_T *isn)
8017{
8018 switch (isn->isn_type)
8019 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008020 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008022 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008023 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008024 case ISN_LOADENV:
8025 case ISN_LOADG:
8026 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008027 case ISN_LOADT:
8028 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008029 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008030 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008031 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008032 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008033 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008034 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008035 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008036 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008037 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008038 case ISN_STOREW:
8039 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008040 vim_free(isn->isn_arg.string);
8041 break;
8042
8043 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008044 case ISN_STORES:
8045 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008046 break;
8047
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008048 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008049 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008050 vim_free(isn->isn_arg.unlet.ul_name);
8051 break;
8052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008053 case ISN_STOREOPT:
8054 vim_free(isn->isn_arg.storeopt.so_name);
8055 break;
8056
8057 case ISN_PUSHBLOB: // push blob isn_arg.blob
8058 blob_unref(isn->isn_arg.blob);
8059 break;
8060
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008061 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008062#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008063 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008064#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008065 break;
8066
8067 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008069 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008070#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008071 break;
8072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008073 case ISN_UCALL:
8074 vim_free(isn->isn_arg.ufunc.cuf_name);
8075 break;
8076
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008077 case ISN_FUNCREF:
8078 {
8079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8080 + isn->isn_arg.funcref.fr_func;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008081
8082 if (func_name_refcount(dfunc->df_ufunc->uf_name))
8083 func_ptr_unref(dfunc->df_ufunc);
8084 }
8085 break;
8086
8087 case ISN_DCALL:
8088 {
8089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8090 + isn->isn_arg.dfunc.cdf_idx;
8091
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008092 if (dfunc->df_ufunc != NULL
8093 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008094 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008095 }
8096 break;
8097
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008098 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008099 {
8100 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8101 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8102
8103 if (ufunc != NULL)
8104 {
8105 // Clear uf_dfunc_idx so that the function is deleted.
8106 clear_def_function(ufunc);
8107 ufunc->uf_dfunc_idx = 0;
8108 func_ptr_unref(ufunc);
8109 }
8110
8111 vim_free(lambda);
8112 vim_free(isn->isn_arg.newfunc.nf_global);
8113 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008114 break;
8115
Bram Moolenaar5e654232020-09-16 15:22:00 +02008116 case ISN_CHECKTYPE:
8117 free_type(isn->isn_arg.type.ct_type);
8118 break;
8119
Bram Moolenaar02194d22020-10-24 23:08:38 +02008120 case ISN_CMDMOD:
8121 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8122 ->cmod_filter_regmatch.regprog);
8123 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8124 break;
8125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008126 case ISN_2BOOL:
8127 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008128 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008129 case ISN_ADDBLOB:
8130 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008131 case ISN_ANYINDEX:
8132 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008133 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008134 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008135 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008136 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008137 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008138 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008139 case ISN_COMPAREANY:
8140 case ISN_COMPAREBLOB:
8141 case ISN_COMPAREBOOL:
8142 case ISN_COMPAREDICT:
8143 case ISN_COMPAREFLOAT:
8144 case ISN_COMPAREFUNC:
8145 case ISN_COMPARELIST:
8146 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008147 case ISN_COMPARESPECIAL:
8148 case ISN_COMPARESTRING:
8149 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008150 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008151 case ISN_DROP:
8152 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008153 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008154 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008155 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008156 case ISN_EXECCONCAT:
8157 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008158 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008159 case ISN_GETITEM:
8160 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008161 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008162 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008163 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008164 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008165 case ISN_LOADBDICT:
8166 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008167 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008168 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008169 case ISN_LOADSCRIPT:
8170 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008171 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008172 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008173 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008174 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008175 case ISN_NEGATENR:
8176 case ISN_NEWDICT:
8177 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008178 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008179 case ISN_OPFLOAT:
8180 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008181 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008182 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008183 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008184 case ISN_PUSHF:
8185 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008186 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008187 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008188 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008189 case ISN_SHUFFLE:
8190 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008191 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008192 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008193 case ISN_STORENR:
8194 case ISN_STOREOUTER:
8195 case ISN_STOREREG:
8196 case ISN_STORESCRIPT:
8197 case ISN_STOREV:
8198 case ISN_STRINDEX:
8199 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008200 case ISN_THROW:
8201 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008202 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008203 // nothing allocated
8204 break;
8205 }
8206}
8207
8208/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01008209 * Free all instructions for "dfunc".
8210 */
8211 static void
8212delete_def_function_contents(dfunc_T *dfunc)
8213{
8214 int idx;
8215
8216 ga_clear(&dfunc->df_def_args_isn);
8217
8218 if (dfunc->df_instr != NULL)
8219 {
8220 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8221 delete_instr(dfunc->df_instr + idx);
8222 VIM_CLEAR(dfunc->df_instr);
8223 }
8224
8225 dfunc->df_deleted = TRUE;
8226}
8227
8228/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008229 * When a user function is deleted, clear the contents of any associated def
8230 * function. The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 */
8232 void
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008233clear_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008234{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008235 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008236 {
8237 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8238 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008239
Bram Moolenaar20431c92020-03-20 18:39:46 +01008240 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008241 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008242 }
8243}
8244
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008245/*
8246 * Used when a user function is about to be deleted: remove the pointer to it.
8247 * The entry in def_functions is then unused.
8248 */
8249 void
8250unlink_def_function(ufunc_T *ufunc)
8251{
8252 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
8253
8254 dfunc->df_ufunc = NULL;
8255}
8256
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008257#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008258/*
8259 * Free all functions defined with ":def".
8260 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008261 void
8262free_def_functions(void)
8263{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008264 int idx;
8265
8266 for (idx = 0; idx < def_functions.ga_len; ++idx)
8267 {
8268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8269
8270 delete_def_function_contents(dfunc);
8271 }
8272
8273 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274}
8275#endif
8276
8277
8278#endif // FEAT_EVAL