blob: 53a61051cbc77bd432b34e379a06c384a9c1b845 [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)
Bram Moolenaar29a86ff2020-09-09 14:55:31 +0200934 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar3868f592020-12-25 13:20:41 +0100935 ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 return OK;
937}
938
939/*
940 * Generate an ISN_PUSHBOOL instruction.
941 */
942 static int
943generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
944{
945 isn_T *isn;
946
Bram Moolenaar080457c2020-03-03 21:53:32 +0100947 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
949 return FAIL;
950 isn->isn_arg.number = number;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_PUSHSPEC instruction.
957 */
958 static int
959generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
960{
961 isn_T *isn;
962
Bram Moolenaar080457c2020-03-03 21:53:32 +0100963 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
965 return FAIL;
966 isn->isn_arg.number = number;
967
968 return OK;
969}
970
971#ifdef FEAT_FLOAT
972/*
973 * Generate an ISN_PUSHF instruction.
974 */
975 static int
976generate_PUSHF(cctx_T *cctx, float_T fnumber)
977{
978 isn_T *isn;
979
Bram Moolenaar080457c2020-03-03 21:53:32 +0100980 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
982 return FAIL;
983 isn->isn_arg.fnumber = fnumber;
984
985 return OK;
986}
987#endif
988
989/*
990 * Generate an ISN_PUSHS instruction.
991 * Consumes "str".
992 */
993 static int
994generate_PUSHS(cctx_T *cctx, char_u *str)
995{
996 isn_T *isn;
997
Bram Moolenaar080457c2020-03-03 21:53:32 +0100998 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
1000 return FAIL;
1001 isn->isn_arg.string = str;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001007 * Generate an ISN_PUSHCHANNEL instruction.
1008 * Consumes "channel".
1009 */
1010 static int
1011generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
1012{
1013 isn_T *isn;
1014
Bram Moolenaar080457c2020-03-03 21:53:32 +01001015 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001016 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.channel = channel;
1019
1020 return OK;
1021}
1022
1023/*
1024 * Generate an ISN_PUSHJOB instruction.
1025 * Consumes "job".
1026 */
1027 static int
1028generate_PUSHJOB(cctx_T *cctx, job_T *job)
1029{
1030 isn_T *isn;
1031
Bram Moolenaar080457c2020-03-03 21:53:32 +01001032 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001033 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001034 return FAIL;
1035 isn->isn_arg.job = job;
1036
1037 return OK;
1038}
1039
1040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 * Generate an ISN_PUSHBLOB instruction.
1042 * Consumes "blob".
1043 */
1044 static int
1045generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
1046{
1047 isn_T *isn;
1048
Bram Moolenaar080457c2020-03-03 21:53:32 +01001049 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
1051 return FAIL;
1052 isn->isn_arg.blob = blob;
1053
1054 return OK;
1055}
1056
1057/*
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001058 * Generate an ISN_PUSHFUNC instruction with name "name".
1059 * Consumes "name".
1060 */
1061 static int
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001062generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001063{
1064 isn_T *isn;
1065
Bram Moolenaar080457c2020-03-03 21:53:32 +01001066 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001067 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001068 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02001069 isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001075 * Generate an ISN_GETITEM instruction with "index".
1076 */
1077 static int
1078generate_GETITEM(cctx_T *cctx, int index)
1079{
1080 isn_T *isn;
1081 garray_T *stack = &cctx->ctx_type_stack;
1082 type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1083 type_T *item_type = &t_any;
1084
1085 RETURN_OK_IF_SKIP(cctx);
1086
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001087 if (type->tt_type != VAR_LIST)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001088 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001089 // cannot happen, caller has checked the type
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001090 emsg(_(e_listreq));
1091 return FAIL;
1092 }
Bram Moolenaarc7db5772020-07-21 20:55:50 +02001093 item_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001094 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1095 return FAIL;
1096 isn->isn_arg.number = index;
1097
1098 // add the item type to the type stack
1099 if (ga_grow(stack, 1) == FAIL)
1100 return FAIL;
1101 ((type_T **)stack->ga_data)[stack->ga_len] = item_type;
1102 ++stack->ga_len;
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar9af78762020-06-16 11:34:42 +02001107 * Generate an ISN_SLICE instruction with "count".
1108 */
1109 static int
1110generate_SLICE(cctx_T *cctx, int count)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1116 return FAIL;
1117 isn->isn_arg.number = count;
1118 return OK;
1119}
1120
1121/*
1122 * Generate an ISN_CHECKLEN instruction with "min_len".
1123 */
1124 static int
1125generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1126{
1127 isn_T *isn;
1128
1129 RETURN_OK_IF_SKIP(cctx);
1130
1131 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1132 return FAIL;
1133 isn->isn_arg.checklen.cl_min_len = min_len;
1134 isn->isn_arg.checklen.cl_more_OK = more_OK;
1135
1136 return OK;
1137}
1138
1139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 * Generate an ISN_STORE instruction.
1141 */
1142 static int
1143generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1144{
1145 isn_T *isn;
1146
Bram Moolenaar080457c2020-03-03 21:53:32 +01001147 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1149 return FAIL;
1150 if (name != NULL)
1151 isn->isn_arg.string = vim_strsave(name);
1152 else
1153 isn->isn_arg.number = idx;
1154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1160 */
1161 static int
1162generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1163{
1164 isn_T *isn;
1165
Bram Moolenaar080457c2020-03-03 21:53:32 +01001166 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1168 return FAIL;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001169 isn->isn_arg.storenr.stnr_idx = idx;
1170 isn->isn_arg.storenr.stnr_val = value;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171
1172 return OK;
1173}
1174
1175/*
1176 * Generate an ISN_STOREOPT instruction
1177 */
1178 static int
1179generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
1180{
1181 isn_T *isn;
1182
Bram Moolenaar080457c2020-03-03 21:53:32 +01001183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1c199f92020-08-07 21:28:34 +02001184 if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 return FAIL;
1186 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1187 isn->isn_arg.storeopt.so_flags = opt_flags;
1188
1189 return OK;
1190}
1191
1192/*
1193 * Generate an ISN_LOAD or similar instruction.
1194 */
1195 static int
1196generate_LOAD(
1197 cctx_T *cctx,
1198 isntype_T isn_type,
1199 int idx,
1200 char_u *name,
1201 type_T *type)
1202{
1203 isn_T *isn;
1204
Bram Moolenaar080457c2020-03-03 21:53:32 +01001205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
1207 return FAIL;
1208 if (name != NULL)
1209 isn->isn_arg.string = vim_strsave(name);
1210 else
1211 isn->isn_arg.number = idx;
1212
1213 return OK;
1214}
1215
1216/*
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001217 * Generate an ISN_LOADV instruction for v:var.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 */
1219 static int
1220generate_LOADV(
1221 cctx_T *cctx,
1222 char_u *name,
1223 int error)
1224{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001225 int di_flags;
1226 int vidx = find_vim_var(name, &di_flags);
1227 type_T *type;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001228
Bram Moolenaar080457c2020-03-03 21:53:32 +01001229 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001230 if (vidx < 0)
1231 {
1232 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001233 semsg(_(e_variable_not_found_str), name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001234 return FAIL;
1235 }
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001236 type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001237
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001238 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001239}
1240
1241/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001242 * Generate an ISN_UNLET instruction.
1243 */
1244 static int
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001245generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001246{
1247 isn_T *isn;
1248
1249 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001250 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001251 return FAIL;
1252 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1253 isn->isn_arg.unlet.ul_forceit = forceit;
1254
1255 return OK;
1256}
1257
1258/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001259 * Generate an ISN_LOCKCONST instruction.
1260 */
1261 static int
1262generate_LOCKCONST(cctx_T *cctx)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
1267 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1268 return FAIL;
1269 return OK;
1270}
1271
1272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 * Generate an ISN_LOADS instruction.
1274 */
1275 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276generate_OLDSCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 cctx_T *cctx,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001278 isntype_T isn_type,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 char_u *name,
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 int sid,
1281 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282{
1283 isn_T *isn;
1284
Bram Moolenaar080457c2020-03-03 21:53:32 +01001285 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286 if (isn_type == ISN_LOADS)
1287 isn = generate_instr_type(cctx, isn_type, type);
1288 else
1289 isn = generate_instr_drop(cctx, isn_type, 1);
1290 if (isn == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1293 isn->isn_arg.loadstore.ls_sid = sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294
1295 return OK;
1296}
1297
1298/*
1299 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1300 */
1301 static int
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302generate_VIM9SCRIPT(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 cctx_T *cctx,
1304 isntype_T isn_type,
1305 int sid,
1306 int idx,
1307 type_T *type)
1308{
1309 isn_T *isn;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001310 scriptref_T *sref;
1311 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312
Bram Moolenaar080457c2020-03-03 21:53:32 +01001313 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 if (isn_type == ISN_LOADSCRIPT)
1315 isn = generate_instr_type(cctx, isn_type, type);
1316 else
1317 isn = generate_instr_drop(cctx, isn_type, 1);
1318 if (isn == NULL)
1319 return FAIL;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001320
1321 // This requires three arguments, which doesn't fit in an instruction, thus
1322 // we need to allocate a struct for this.
1323 sref = ALLOC_ONE(scriptref_T);
1324 if (sref == NULL)
1325 return FAIL;
1326 isn->isn_arg.script.scriptref = sref;
1327 sref->sref_sid = sid;
1328 sref->sref_idx = idx;
1329 sref->sref_seq = si->sn_script_seq;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 return OK;
1331}
1332
1333/*
1334 * Generate an ISN_NEWLIST instruction.
1335 */
1336 static int
1337generate_NEWLIST(cctx_T *cctx, int count)
1338{
1339 isn_T *isn;
1340 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 type_T *type;
1342 type_T *member;
1343
Bram Moolenaar080457c2020-03-03 21:53:32 +01001344 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1346 return FAIL;
1347 isn->isn_arg.number = count;
1348
Bram Moolenaar127542b2020-08-09 17:22:04 +02001349 // get the member type from all the items on the stack.
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001350 if (count == 0)
1351 member = &t_void;
1352 else
1353 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001354 ((type_T **)stack->ga_data) + stack->ga_len, count, 1,
1355 cctx->ctx_type_list);
1356 type = get_list_type(member, cctx->ctx_type_list);
1357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 // drop the value types
1359 stack->ga_len -= count;
1360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 // add the list type to the type stack
1362 if (ga_grow(stack, 1) == FAIL)
1363 return FAIL;
1364 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1365 ++stack->ga_len;
1366
1367 return OK;
1368}
1369
1370/*
1371 * Generate an ISN_NEWDICT instruction.
1372 */
1373 static int
1374generate_NEWDICT(cctx_T *cctx, int count)
1375{
1376 isn_T *isn;
1377 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 type_T *type;
1379 type_T *member;
1380
Bram Moolenaar080457c2020-03-03 21:53:32 +01001381 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1383 return FAIL;
1384 isn->isn_arg.number = count;
1385
Bram Moolenaar9c2b0662020-09-01 19:56:15 +02001386 if (count == 0)
1387 member = &t_void;
1388 else
1389 member = get_member_type_from_stack(
Bram Moolenaar127542b2020-08-09 17:22:04 +02001390 ((type_T **)stack->ga_data) + stack->ga_len, count, 2,
1391 cctx->ctx_type_list);
1392 type = get_dict_type(member, cctx->ctx_type_list);
1393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 // drop the key and value types
1395 stack->ga_len -= 2 * count;
1396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 // add the dict type to the type stack
1398 if (ga_grow(stack, 1) == FAIL)
1399 return FAIL;
1400 ((type_T **)stack->ga_data)[stack->ga_len] = type;
1401 ++stack->ga_len;
1402
1403 return OK;
1404}
1405
1406/*
1407 * Generate an ISN_FUNCREF instruction.
1408 */
1409 static int
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001410generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411{
1412 isn_T *isn;
1413 garray_T *stack = &cctx->ctx_type_stack;
1414
Bram Moolenaar080457c2020-03-03 21:53:32 +01001415 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1417 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001418 isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001419 cctx->ctx_has_closure = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 if (ga_grow(stack, 1) == FAIL)
1422 return FAIL;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001423 ((type_T **)stack->ga_data)[stack->ga_len] =
1424 ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 ++stack->ga_len;
1426
1427 return OK;
1428}
1429
1430/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001431 * Generate an ISN_NEWFUNC instruction.
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001432 * "lambda_name" and "func_name" must be in allocated memory and will be
1433 * consumed.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001434 */
1435 static int
1436generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1437{
1438 isn_T *isn;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001439
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001440 if (cctx->ctx_skip == SKIP_YES)
1441 {
1442 vim_free(lambda_name);
1443 vim_free(func_name);
1444 return OK;
1445 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001446 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001447 {
1448 vim_free(lambda_name);
1449 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001450 return FAIL;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001451 }
1452 isn->isn_arg.newfunc.nf_lambda = lambda_name;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001453 isn->isn_arg.newfunc.nf_global = func_name;
1454
1455 return OK;
1456}
1457
1458/*
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001459 * Generate an ISN_DEF instruction: list functions
1460 */
1461 static int
1462generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1463{
1464 isn_T *isn;
1465
1466 RETURN_OK_IF_SKIP(cctx);
1467 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1468 return FAIL;
1469 if (len > 0)
1470 {
1471 isn->isn_arg.string = vim_strnsave(name, len);
1472 if (isn->isn_arg.string == NULL)
1473 return FAIL;
1474 }
1475 return OK;
1476}
1477
1478/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 * Generate an ISN_JUMP instruction.
1480 */
1481 static int
1482generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1483{
1484 isn_T *isn;
1485 garray_T *stack = &cctx->ctx_type_stack;
1486
Bram Moolenaar080457c2020-03-03 21:53:32 +01001487 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1489 return FAIL;
1490 isn->isn_arg.jump.jump_when = when;
1491 isn->isn_arg.jump.jump_where = where;
1492
1493 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1494 --stack->ga_len;
1495
1496 return OK;
1497}
1498
1499 static int
1500generate_FOR(cctx_T *cctx, int loop_idx)
1501{
1502 isn_T *isn;
1503 garray_T *stack = &cctx->ctx_type_stack;
1504
Bram Moolenaar080457c2020-03-03 21:53:32 +01001505 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1507 return FAIL;
1508 isn->isn_arg.forloop.for_idx = loop_idx;
1509
1510 if (ga_grow(stack, 1) == FAIL)
1511 return FAIL;
1512 // type doesn't matter, will be stored next
1513 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1514 ++stack->ga_len;
1515
1516 return OK;
1517}
1518
1519/*
1520 * Generate an ISN_BCALL instruction.
Bram Moolenaar389df252020-07-09 21:20:47 +02001521 * "method_call" is TRUE for "value->method()"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 * Return FAIL if the number of arguments is wrong.
1523 */
1524 static int
Bram Moolenaar389df252020-07-09 21:20:47 +02001525generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526{
1527 isn_T *isn;
1528 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar389df252020-07-09 21:20:47 +02001529 int argoff;
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001530 type_T **argtypes = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531
Bram Moolenaar080457c2020-03-03 21:53:32 +01001532 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar389df252020-07-09 21:20:47 +02001533 argoff = check_internal_func(func_idx, argcount);
1534 if (argoff < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 return FAIL;
1536
Bram Moolenaar389df252020-07-09 21:20:47 +02001537 if (method_call && argoff > 1)
1538 {
1539 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1540 return FAIL;
1541 isn->isn_arg.shuffle.shfl_item = argcount;
1542 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1543 }
1544
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001545 if (argcount > 0)
1546 {
1547 // Check the types of the arguments.
1548 argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
1549 if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001550 return FAIL;
Bram Moolenaaraf7a9062020-10-21 16:49:17 +02001551 }
Bram Moolenaar94738d82020-10-21 14:25:07 +02001552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1554 return FAIL;
1555 isn->isn_arg.bfunc.cbf_idx = func_idx;
1556 isn->isn_arg.bfunc.cbf_argcount = argcount;
1557
Bram Moolenaar94738d82020-10-21 14:25:07 +02001558 // Drop the argument types and push the return type.
1559 stack->ga_len -= argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 if (ga_grow(stack, 1) == FAIL)
1561 return FAIL;
1562 ((type_T **)stack->ga_data)[stack->ga_len] =
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001563 internal_func_ret_type(func_idx, argcount, argtypes);
Bram Moolenaar94738d82020-10-21 14:25:07 +02001564 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565
1566 return OK;
1567}
1568
1569/*
Bram Moolenaar1dcae592020-10-19 19:02:42 +02001570 * Generate an ISN_LISTAPPEND instruction. Works like add().
1571 * Argument count is already checked.
1572 */
1573 static int
1574generate_LISTAPPEND(cctx_T *cctx)
1575{
1576 garray_T *stack = &cctx->ctx_type_stack;
1577 type_T *list_type;
1578 type_T *item_type;
1579 type_T *expected;
1580
1581 // Caller already checked that list_type is a list.
1582 list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
1583 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1584 expected = list_type->tt_member;
1585 if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL)
1586 return FAIL;
1587
1588 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1589 return FAIL;
1590
1591 --stack->ga_len; // drop the argument
1592 return OK;
1593}
1594
1595/*
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02001596 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1597 * Argument count is already checked.
1598 */
1599 static int
1600generate_BLOBAPPEND(cctx_T *cctx)
1601{
1602 garray_T *stack = &cctx->ctx_type_stack;
1603 type_T *item_type;
1604
1605 // Caller already checked that blob_type is a blob.
1606 item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
1607 if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL)
1608 return FAIL;
1609
1610 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1611 return FAIL;
1612
1613 --stack->ga_len; // drop the argument
1614 return OK;
1615}
1616
1617/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 * Generate an ISN_DCALL or ISN_UCALL instruction.
1619 * Return FAIL if the number of arguments is wrong.
1620 */
1621 static int
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001622generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623{
1624 isn_T *isn;
1625 garray_T *stack = &cctx->ctx_type_stack;
1626 int regular_args = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001627 int argcount = pushed_argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628
Bram Moolenaar080457c2020-03-03 21:53:32 +01001629 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 if (argcount > regular_args && !has_varargs(ufunc))
1631 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001632 semsg(_(e_toomanyarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 return FAIL;
1634 }
1635 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1636 {
Bram Moolenaarb185a402020-09-18 22:42:00 +02001637 semsg(_(e_toofewarg), printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 return FAIL;
1639 }
1640
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001641 if (ufunc->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001642 {
1643 int i;
1644
1645 for (i = 0; i < argcount; ++i)
1646 {
1647 type_T *expected;
1648 type_T *actual;
1649
1650 if (i < regular_args)
1651 {
1652 if (ufunc->uf_arg_types == NULL)
1653 continue;
1654 expected = ufunc->uf_arg_types[i];
1655 }
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001656 else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any)
1657 // possibly a lambda or "...: any"
Bram Moolenaar79e8db92020-08-14 22:16:33 +02001658 expected = &t_any;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001659 else
1660 expected = ufunc->uf_va_type->tt_member;
1661 actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02001662 if (need_type(actual, expected, -argcount + i, cctx,
1663 TRUE, FALSE) == FAIL)
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001664 {
1665 arg_type_mismatch(expected, actual, i + 1);
1666 return FAIL;
1667 }
1668 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001669 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02001670 if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL)
1671 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001672 return FAIL;
Bram Moolenaar0b76b422020-04-07 22:05:08 +02001673 }
1674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 if ((isn = generate_instr(cctx,
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001676 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
Bram Moolenaar822ba242020-05-24 23:00:18 +02001677 : ISN_UCALL)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 return FAIL;
Bram Moolenaara05e5242020-09-19 18:19:19 +02001679 if (isn->isn_type == ISN_DCALL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 {
1681 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1682 isn->isn_arg.dfunc.cdf_argcount = argcount;
1683 }
1684 else
1685 {
1686 // A user function may be deleted and redefined later, can't use the
1687 // ufunc pointer, need to look it up again at runtime.
1688 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1689 isn->isn_arg.ufunc.cuf_argcount = argcount;
1690 }
1691
1692 stack->ga_len -= argcount; // drop the arguments
1693 if (ga_grow(stack, 1) == FAIL)
1694 return FAIL;
1695 // add return value
1696 ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
1697 ++stack->ga_len;
1698
1699 return OK;
1700}
1701
1702/*
1703 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1704 */
1705 static int
1706generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1707{
1708 isn_T *isn;
1709 garray_T *stack = &cctx->ctx_type_stack;
1710
Bram Moolenaar080457c2020-03-03 21:53:32 +01001711 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1713 return FAIL;
1714 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1715 isn->isn_arg.ufunc.cuf_argcount = argcount;
1716
1717 stack->ga_len -= argcount; // drop the arguments
Bram Moolenaar26e117e2020-02-04 21:24:15 +01001718 if (ga_grow(stack, 1) == FAIL)
1719 return FAIL;
1720 // add return value
1721 ((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
1722 ++stack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723
1724 return OK;
1725}
1726
1727/*
1728 * Generate an ISN_PCALL instruction.
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001729 * "type" is the type of the FuncRef.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 */
1731 static int
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001732generate_PCALL(
1733 cctx_T *cctx,
1734 int argcount,
1735 char_u *name,
1736 type_T *type,
1737 int at_top)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738{
1739 isn_T *isn;
1740 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001741 type_T *ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
Bram Moolenaar080457c2020-03-03 21:53:32 +01001743 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001744
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001745 if (type->tt_type == VAR_ANY)
1746 ret_type = &t_any;
1747 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001748 {
1749 if (type->tt_argcount != -1)
1750 {
1751 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1752
1753 if (argcount < type->tt_min_argcount - varargs)
1754 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001755 semsg(_(e_toofewarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001756 return FAIL;
1757 }
1758 if (!varargs && argcount > type->tt_argcount)
1759 {
Bram Moolenaar6cf7e3b2020-10-28 14:31:16 +01001760 semsg(_(e_toomanyarg), name);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001761 return FAIL;
1762 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001763 if (type->tt_args != NULL)
1764 {
1765 int i;
1766
1767 for (i = 0; i < argcount; ++i)
1768 {
1769 int offset = -argcount + i - 1;
1770 type_T *actual = ((type_T **)stack->ga_data)[
1771 stack->ga_len + offset];
1772 type_T *expected;
1773
1774 if (varargs && i >= type->tt_min_argcount - 1)
1775 expected = type->tt_args[
1776 type->tt_min_argcount - 1]->tt_member;
1777 else
1778 expected = type->tt_args[i];
1779 if (need_type(actual, expected, offset,
1780 cctx, TRUE, FALSE) == FAIL)
1781 {
1782 arg_type_mismatch(expected, actual, i + 1);
1783 return FAIL;
1784 }
1785 }
1786 }
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001787 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001788 ret_type = type->tt_member;
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02001789 }
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001790 else
1791 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001792 semsg(_(e_not_callable_type_str), name);
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001793 return FAIL;
1794 }
1795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1797 return FAIL;
1798 isn->isn_arg.pfunc.cpf_top = at_top;
1799 isn->isn_arg.pfunc.cpf_argcount = argcount;
1800
1801 stack->ga_len -= argcount; // drop the arguments
1802
1803 // drop the funcref/partial, get back the return value
Bram Moolenaara0a9f432020-04-28 21:29:34 +02001804 ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001806 // If partial is above the arguments it must be cleared and replaced with
1807 // the return value.
1808 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1809 return FAIL;
1810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 return OK;
1812}
1813
1814/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001815 * Generate an ISN_STRINGMEMBER instruction.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 */
1817 static int
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001818generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819{
1820 isn_T *isn;
1821 garray_T *stack = &cctx->ctx_type_stack;
1822 type_T *type;
1823
Bram Moolenaar080457c2020-03-03 21:53:32 +01001824 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001825 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 return FAIL;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001827 isn->isn_arg.string = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001829 // check for dict type
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar0062c2d2020-02-20 22:14:31 +01001831 if (type->tt_type != VAR_DICT && type != &t_any)
1832 {
1833 emsg(_(e_dictreq));
1834 return FAIL;
1835 }
1836 // change dict type to dict member type
1837 if (type->tt_type == VAR_DICT)
1838 ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839
1840 return OK;
1841}
1842
1843/*
1844 * Generate an ISN_ECHO instruction.
1845 */
1846 static int
1847generate_ECHO(cctx_T *cctx, int with_white, int count)
1848{
1849 isn_T *isn;
1850
Bram Moolenaar080457c2020-03-03 21:53:32 +01001851 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1853 return FAIL;
1854 isn->isn_arg.echo.echo_with_white = with_white;
1855 isn->isn_arg.echo.echo_count = count;
1856
1857 return OK;
1858}
1859
Bram Moolenaarad39c092020-02-26 18:23:43 +01001860/*
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001861 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
Bram Moolenaarad39c092020-02-26 18:23:43 +01001862 */
1863 static int
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001864generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001865{
1866 isn_T *isn;
1867
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001868 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001869 return FAIL;
1870 isn->isn_arg.number = count;
1871
1872 return OK;
1873}
1874
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001875/*
1876 * Generate an ISN_PUT instruction.
1877 */
1878 static int
1879generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1880{
1881 isn_T *isn;
1882
1883 RETURN_OK_IF_SKIP(cctx);
1884 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1885 return FAIL;
1886 isn->isn_arg.put.put_regname = regname;
1887 isn->isn_arg.put.put_lnum = lnum;
1888 return OK;
1889}
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 static int
1892generate_EXEC(cctx_T *cctx, char_u *line)
1893{
1894 isn_T *isn;
1895
Bram Moolenaar080457c2020-03-03 21:53:32 +01001896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
1898 return FAIL;
1899 isn->isn_arg.string = vim_strsave(line);
1900 return OK;
1901}
1902
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001903 static int
1904generate_EXECCONCAT(cctx_T *cctx, int count)
1905{
1906 isn_T *isn;
1907
1908 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1909 return FAIL;
1910 isn->isn_arg.number = count;
1911 return OK;
1912}
1913
Bram Moolenaar08597872020-12-10 19:43:40 +01001914/*
1915 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1916 */
1917 static int
1918generate_RANGE(cctx_T *cctx, char_u *range)
1919{
1920 isn_T *isn;
1921 garray_T *stack = &cctx->ctx_type_stack;
1922
1923 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1924 return FAIL;
1925 isn->isn_arg.string = range;
1926
1927 if (ga_grow(stack, 1) == FAIL)
1928 return FAIL;
1929 ((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
1930 ++stack->ga_len;
1931 return OK;
1932}
1933
Bram Moolenaar792f7862020-11-23 08:31:18 +01001934 static int
1935generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1936{
1937 isn_T *isn;
1938
1939 RETURN_OK_IF_SKIP(cctx);
1940 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1941 return FAIL;
1942 isn->isn_arg.unpack.unp_count = var_count;
1943 isn->isn_arg.unpack.unp_semicolon = semicolon;
1944 return OK;
1945}
1946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001948 * Generate an instruction for any command modifiers.
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001949 */
1950 static int
Bram Moolenaare1004402020-10-24 20:49:43 +02001951generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001952{
1953 isn_T *isn;
1954
Bram Moolenaar02194d22020-10-24 23:08:38 +02001955 if (cmod->cmod_flags != 0
1956 || cmod->cmod_split != 0
1957 || cmod->cmod_verbose != 0
1958 || cmod->cmod_tab != 0
1959 || cmod->cmod_filter_regmatch.regprog != NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001960 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001961 cctx->ctx_has_cmdmod = TRUE;
1962
1963 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001964 return FAIL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001965 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1966 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1967 return FAIL;
1968 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001969 // filter program now belongs to the instruction
Bram Moolenaar02194d22020-10-24 23:08:38 +02001970 cmod->cmod_filter_regmatch.regprog = NULL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001971 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001972
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001973 return OK;
1974}
1975
1976 static int
Bram Moolenaar02194d22020-10-24 23:08:38 +02001977generate_undo_cmdmods(cctx_T *cctx)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001978{
Bram Moolenaarf665e972020-12-05 19:17:16 +01001979 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1980 return FAIL;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02001981 return OK;
1982}
1983
1984/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 * Reserve space for a local variable.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001986 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02001988 static lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +02001989reserve_local(
1990 cctx_T *cctx,
1991 char_u *name,
1992 size_t len,
1993 int isConst,
1994 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 lvar_T *lvar;
1997
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001998 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002000 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002001 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 }
2003
2004 if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002005 return NULL;
2006 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +02002007 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002009 // Every local variable uses the next entry on the stack. We could re-use
2010 // the last ones when leaving a scope, but then variables used in a closure
2011 // might get overwritten. To keep things simple do not re-use stack
2012 // entries. This is less efficient, but memory is cheap these days.
2013 lvar->lv_idx = cctx->ctx_locals_count++;
2014
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002015 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 lvar->lv_const = isConst;
2017 lvar->lv_type = type;
2018
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002019 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020}
2021
2022/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002023 * Remove local variables above "new_top".
2024 */
2025 static void
2026unwind_locals(cctx_T *cctx, int new_top)
2027{
2028 if (cctx->ctx_locals.ga_len > new_top)
2029 {
2030 int idx;
2031 lvar_T *lvar;
2032
2033 for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
2034 {
2035 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
2036 vim_free(lvar->lv_name);
2037 }
2038 }
2039 cctx->ctx_locals.ga_len = new_top;
2040}
2041
2042/*
2043 * Free all local variables.
2044 */
2045 static void
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002046free_locals(cctx_T *cctx)
Bram Moolenaar20431c92020-03-20 18:39:46 +01002047{
2048 unwind_locals(cctx, 0);
2049 ga_clear(&cctx->ctx_locals);
2050}
2051
2052/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053 * Find "name" in script-local items of script "sid".
2054 * Returns the index in "sn_var_vals" if found.
2055 * If found but not in "sn_var_vals" returns -1.
2056 * If not found returns -2.
2057 */
2058 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002059get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060{
2061 hashtab_T *ht;
2062 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002063 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002064 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 int idx;
2066
Bram Moolenaare3d46852020-08-29 13:39:17 +02002067 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002069 if (sid == current_sctx.sc_sid)
2070 {
Bram Moolenaar209f0202020-10-15 13:57:56 +02002071 sallvar_T *sav = find_script_var(name, 0, cctx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002072
2073 if (sav == NULL)
2074 return -2;
2075 idx = sav->sav_var_vals_idx;
2076 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2077 if (check_writable && sv->sv_const)
2078 semsg(_(e_readonlyvar), name);
2079 return idx;
2080 }
2081
2082 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 ht = &SCRIPT_VARS(sid);
2084 di = find_var_in_ht(ht, 0, name, TRUE);
2085 if (di == NULL)
2086 return -2;
2087
2088 // Now find the svar_T index in sn_var_vals.
2089 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
2090 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002091 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 if (sv->sv_tv == &di->di_tv)
2093 {
2094 if (check_writable && sv->sv_const)
2095 semsg(_(e_readonlyvar), name);
2096 return idx;
2097 }
2098 }
2099 return -1;
2100}
2101
2102/*
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02002103 * Find "name" in imported items of the current script or in "cctx" if not
2104 * NULL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 */
2106 imported_T *
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002107find_imported(char_u *name, size_t len, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 int idx;
2110
Bram Moolenaare3d46852020-08-29 13:39:17 +02002111 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +02002112 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 if (cctx != NULL)
2114 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2115 {
2116 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
2117 + idx;
2118
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002119 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2120 : STRLEN(import->imp_name) == len
2121 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 return import;
2123 }
2124
Bram Moolenaarefa94442020-08-08 22:16:00 +02002125 return find_imported_in_script(name, len, current_sctx.sc_sid);
2126}
2127
2128 imported_T *
2129find_imported_in_script(char_u *name, size_t len, int sid)
2130{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002131 scriptitem_T *si;
Bram Moolenaarefa94442020-08-08 22:16:00 +02002132 int idx;
2133
Bram Moolenaare3d46852020-08-29 13:39:17 +02002134 if (!SCRIPT_ID_VALID(sid))
2135 return NULL;
2136 si = SCRIPT_ITEM(sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
2138 {
2139 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
2140
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002141 if (len == 0 ? STRCMP(name, import->imp_name) == 0
2142 : STRLEN(import->imp_name) == len
2143 && STRNCMP(name, import->imp_name, len) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 return import;
2145 }
2146 return NULL;
2147}
2148
2149/*
Bram Moolenaar20431c92020-03-20 18:39:46 +01002150 * Free all imported variables.
2151 */
2152 static void
2153free_imported(cctx_T *cctx)
2154{
2155 int idx;
2156
2157 for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
2158 {
2159 imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
2160
2161 vim_free(import->imp_name);
2162 }
2163 ga_clear(&cctx->ctx_imports);
2164}
2165
2166/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01002167 * Return TRUE if "p" points at a "#". Does not check for white space.
Bram Moolenaar23c55272020-06-21 16:58:13 +02002168 */
Bram Moolenaar75783bd2020-07-19 14:41:58 +02002169 int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002170vim9_comment_start(char_u *p)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002171{
Bram Moolenaare0de1712020-12-02 17:36:54 +01002172 return p[0] == '#';
Bram Moolenaar23c55272020-06-21 16:58:13 +02002173}
2174
2175/*
2176 * Return a pointer to the next line that isn't empty or only contains a
2177 * comment. Skips over white space.
2178 * Returns NULL if there is none.
2179 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002180 char_u *
2181peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +02002182{
2183 int lnum = cctx->ctx_lnum;
2184
2185 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
2186 {
2187 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +02002188 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002189
Bram Moolenaarba60cc42020-08-12 19:15:33 +02002190 // ignore NULLs inserted for continuation lines
2191 if (line != NULL)
2192 {
2193 p = skipwhite(line);
2194 if (*p != NUL && !vim9_comment_start(p))
2195 return p;
2196 }
Bram Moolenaar23c55272020-06-21 16:58:13 +02002197 }
2198 return NULL;
2199}
2200
2201/*
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002202 * Called when checking for a following operator at "arg". When the rest of
2203 * the line is empty or only a comment, peek the next line. If there is a next
2204 * line return a pointer to it and set "nextp".
2205 * Otherwise skip over white space.
2206 */
2207 static char_u *
2208may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
2209{
2210 char_u *p = skipwhite(arg);
2211
2212 *nextp = NULL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002213 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002214 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002215 *nextp = peek_next_line_from_context(cctx);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02002216 if (*nextp != NULL)
2217 return *nextp;
2218 }
2219 return p;
2220}
2221
2222/*
Bram Moolenaare6085c52020-04-12 20:19:16 +02002223 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +02002224 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +02002225 * Returns NULL when at the end.
2226 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002227 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +02002228next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +02002229{
Bram Moolenaar7a092242020-04-16 22:10:49 +02002230 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002231
2232 do
2233 {
2234 ++cctx->ctx_lnum;
2235 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +02002236 {
2237 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002238 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +02002239 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002240 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +02002241 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002242 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +02002243 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02002244 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +02002245 return line;
2246}
2247
2248/*
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002249 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +02002250 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002251 * Return FAIL if beyond the last line, "*arg" is unmodified then.
2252 */
2253 static int
Bram Moolenaar2c330432020-04-13 14:41:35 +02002254may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002255{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002256 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002257 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002258 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002259
2260 if (next == NULL)
2261 return FAIL;
2262 *arg = skipwhite(next);
2263 }
2264 return OK;
2265}
2266
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002267/*
2268 * Idem, and give an error when failed.
2269 */
2270 static int
2271may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
2272{
2273 if (may_get_next_line(whitep, arg, cctx) == FAIL)
2274 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01002275 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002276 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +02002277 return FAIL;
2278 }
2279 return OK;
2280}
2281
2282
Bram Moolenaara5565e42020-05-09 15:44:01 +02002283// Structure passed between the compile_expr* functions to keep track of
2284// constants that have been parsed but for which no code was produced yet. If
2285// possible expressions on these constants are applied at compile time. If
2286// that is not possible, the code to push the constants needs to be generated
2287// before other instructions.
Bram Moolenaar1c747212020-05-09 18:28:34 +02002288// Using 50 should be more than enough of 5 levels of ().
2289#define PPSIZE 50
Bram Moolenaara5565e42020-05-09 15:44:01 +02002290typedef struct {
Bram Moolenaar1c747212020-05-09 18:28:34 +02002291 typval_T pp_tv[PPSIZE]; // stack of ppconst constants
Bram Moolenaara5565e42020-05-09 15:44:01 +02002292 int pp_used; // active entries in pp_tv[]
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002293 int pp_is_const; // all generated code was constants, used for a
2294 // list or dict with constant members
Bram Moolenaara5565e42020-05-09 15:44:01 +02002295} ppconst_T;
2296
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002297static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
Bram Moolenaar1c747212020-05-09 18:28:34 +02002298static int compile_expr0(char_u **arg, cctx_T *cctx);
2299static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
2300
Bram Moolenaara5565e42020-05-09 15:44:01 +02002301/*
2302 * Generate a PUSH instruction for "tv".
2303 * "tv" will be consumed or cleared.
2304 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
2305 */
2306 static int
2307generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
2308{
2309 if (tv != NULL)
2310 {
2311 switch (tv->v_type)
2312 {
2313 case VAR_UNKNOWN:
2314 break;
2315 case VAR_BOOL:
2316 generate_PUSHBOOL(cctx, tv->vval.v_number);
2317 break;
2318 case VAR_SPECIAL:
2319 generate_PUSHSPEC(cctx, tv->vval.v_number);
2320 break;
2321 case VAR_NUMBER:
2322 generate_PUSHNR(cctx, tv->vval.v_number);
2323 break;
2324#ifdef FEAT_FLOAT
2325 case VAR_FLOAT:
2326 generate_PUSHF(cctx, tv->vval.v_float);
2327 break;
2328#endif
2329 case VAR_BLOB:
2330 generate_PUSHBLOB(cctx, tv->vval.v_blob);
2331 tv->vval.v_blob = NULL;
2332 break;
2333 case VAR_STRING:
2334 generate_PUSHS(cctx, tv->vval.v_string);
2335 tv->vval.v_string = NULL;
2336 break;
2337 default:
2338 iemsg("constant type not supported");
2339 clear_tv(tv);
2340 return FAIL;
2341 }
2342 tv->v_type = VAR_UNKNOWN;
2343 }
2344 return OK;
2345}
2346
2347/*
2348 * Generate code for any ppconst entries.
2349 */
2350 static int
2351generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
2352{
2353 int i;
2354 int ret = OK;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002355 int save_skip = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002356
Bram Moolenaar9b68c822020-06-18 19:31:08 +02002357 cctx->ctx_skip = SKIP_NOT;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002358 for (i = 0; i < ppconst->pp_used; ++i)
2359 if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
2360 ret = FAIL;
2361 ppconst->pp_used = 0;
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002362 cctx->ctx_skip = save_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02002363 return ret;
2364}
2365
2366/*
2367 * Clear ppconst constants. Used when failing.
2368 */
2369 static void
2370clear_ppconst(ppconst_T *ppconst)
2371{
2372 int i;
2373
2374 for (i = 0; i < ppconst->pp_used; ++i)
2375 clear_tv(&ppconst->pp_tv[i]);
2376 ppconst->pp_used = 0;
2377}
2378
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02002379/*
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002380 * Generate an instruction to load script-local variable "name", without the
2381 * leading "s:".
2382 * Also finds imported variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 */
2384 static int
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002385compile_load_scriptvar(
2386 cctx_T *cctx,
2387 char_u *name, // variable NUL terminated
2388 char_u *start, // start of variable
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002389 char_u **end, // end of variable
2390 int error) // when TRUE may give error
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391{
Bram Moolenaare3d46852020-08-29 13:39:17 +02002392 scriptitem_T *si;
2393 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 imported_T *import;
2395
Bram Moolenaare3d46852020-08-29 13:39:17 +02002396 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
2397 return FAIL;
2398 si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002399 idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx);
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002400 if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 {
Bram Moolenaarfd1823e2020-02-19 20:23:11 +01002402 // variable is not in sn_var_vals: old style script.
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002403 return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
2404 &t_any);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 }
2406 if (idx >= 0)
2407 {
2408 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
2409
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002410 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 current_sctx.sc_sid, idx, sv->sv_type);
2412 return OK;
2413 }
2414
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002415 import = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (import != NULL)
2417 {
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002418 if (import->imp_all)
2419 {
2420 char_u *p = skipwhite(*end);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002421 char_u *exp_name;
2422 int cc;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002423 ufunc_T *ufunc;
2424 type_T *type;
2425
2426 // Used "import * as Name", need to lookup the member.
2427 if (*p != '.')
2428 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002429 semsg(_(e_expected_dot_after_name_str), start);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002430 return FAIL;
2431 }
2432 ++p;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002433 if (VIM_ISWHITE(*p))
2434 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002435 emsg(_(e_no_white_space_allowed_after_dot));
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002436 return FAIL;
2437 }
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002438
Bram Moolenaar1c991142020-07-04 13:15:31 +02002439 // isolate one name
2440 exp_name = p;
2441 while (eval_isnamec(*p))
2442 ++p;
2443 cc = *p;
2444 *p = NUL;
2445
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002446 idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx);
Bram Moolenaar1c991142020-07-04 13:15:31 +02002447 *p = cc;
2448 p = skipwhite(p);
2449
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002450 // TODO: what if it is a function?
2451 if (idx < 0)
2452 return FAIL;
2453 *end = p;
2454
2455 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2456 import->imp_sid,
2457 idx,
2458 type);
2459 }
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002460 else if (import->imp_funcname != NULL)
2461 generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002462 else
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002463 generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
2464 import->imp_sid,
2465 import->imp_var_vals_idx,
2466 import->imp_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 return OK;
2468 }
2469
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002470 if (error)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002471 semsg(_(e_item_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 return FAIL;
2473}
2474
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002475 static int
2476generate_funcref(cctx_T *cctx, char_u *name)
2477{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002478 ufunc_T *ufunc = find_func(name, FALSE, cctx);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002479
2480 if (ufunc == NULL)
2481 return FAIL;
2482
Bram Moolenaarb8070e32020-07-23 20:56:04 +02002483 // Need to compile any default values to get the argument types.
2484 if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
2485 if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
2486 return FAIL;
Bram Moolenaar40f4f7a2020-07-23 22:41:43 +02002487 return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002488}
2489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490/*
2491 * Compile a variable name into a load instruction.
2492 * "end" points to just after the name.
Bram Moolenaar0f769812020-09-12 18:32:34 +02002493 * "is_expr" is TRUE when evaluating an expression, might be a funcref.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 * When "error" is FALSE do not give an error when not found.
2495 */
2496 static int
Bram Moolenaar0f769812020-09-12 18:32:34 +02002497compile_load(
2498 char_u **arg,
2499 char_u *end_arg,
2500 cctx_T *cctx,
2501 int is_expr,
2502 int error)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503{
2504 type_T *type;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002505 char_u *name = NULL;
Bram Moolenaarf2d5c242020-02-23 21:25:54 +01002506 char_u *end = end_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 int res = FAIL;
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002508 int prev_called_emsg = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
2510 if (*(*arg + 1) == ':')
2511 {
2512 // load namespaced variable
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002513 if (end <= *arg + 2)
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002514 {
2515 isntype_T isn_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002517 switch (**arg)
2518 {
2519 case 'g': isn_type = ISN_LOADGDICT; break;
2520 case 'w': isn_type = ISN_LOADWDICT; break;
2521 case 't': isn_type = ISN_LOADTDICT; break;
2522 case 'b': isn_type = ISN_LOADBDICT; break;
2523 default:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002524 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002525 goto theend;
2526 }
2527 if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
2528 goto theend;
2529 res = OK;
Bram Moolenaar33fa29c2020-03-28 19:41:33 +01002530 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 else
2532 {
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002533 isntype_T isn_type = ISN_DROP;
2534
2535 name = vim_strnsave(*arg + 2, end - (*arg + 2));
2536 if (name == NULL)
2537 return FAIL;
2538
2539 switch (**arg)
2540 {
2541 case 'v': res = generate_LOADV(cctx, name, error);
2542 break;
2543 case 's': res = compile_load_scriptvar(cctx, name,
2544 NULL, NULL, error);
2545 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002546 case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
2547 isn_type = ISN_LOADG;
2548 else
2549 {
2550 isn_type = ISN_LOADAUTO;
2551 vim_free(name);
2552 name = vim_strnsave(*arg, end - *arg);
2553 if (name == NULL)
2554 return FAIL;
2555 }
2556 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002557 case 'w': isn_type = ISN_LOADW; break;
2558 case 't': isn_type = ISN_LOADT; break;
2559 case 'b': isn_type = ISN_LOADB; break;
Bram Moolenaar918a4242020-12-06 14:37:08 +01002560 default: // cannot happen, just in case
2561 semsg(_(e_namespace_not_supported_str), *arg);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002562 goto theend;
2563 }
2564 if (isn_type != ISN_DROP)
2565 {
2566 // Global, Buffer-local, Window-local and Tabpage-local
2567 // variables can be defined later, thus we don't check if it
2568 // exists, give error at runtime.
2569 res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
2570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 }
2573 else
2574 {
2575 size_t len = end - *arg;
2576 int idx;
2577 int gen_load = FALSE;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002578 int gen_load_outer = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579
2580 name = vim_strnsave(*arg, end - *arg);
2581 if (name == NULL)
2582 return FAIL;
2583
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002584 if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 {
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +02002586 if (!gen_load_outer)
2587 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 }
2589 else
2590 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002591 lvar_T lvar;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002592
Bram Moolenaar709664c2020-12-12 14:33:41 +01002593 if (lookup_local(*arg, len, &lvar, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01002595 type = lvar.lv_type;
2596 idx = lvar.lv_idx;
2597 if (lvar.lv_from_outer)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002598 gen_load_outer = TRUE;
2599 else
2600 gen_load = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 }
2602 else
2603 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02002604 // "var" can be script-local even without using "s:" if it
Bram Moolenaar53900992020-08-22 19:02:02 +02002605 // already exists in a Vim9 script or when it's imported.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002606 if (script_var_exists(*arg, len, TRUE, cctx) == OK
Bram Moolenaar53900992020-08-22 19:02:02 +02002607 || find_imported(name, 0, cctx) != NULL)
2608 res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002609
Bram Moolenaar0f769812020-09-12 18:32:34 +02002610 // When evaluating an expression and the name starts with an
2611 // uppercase letter or "x:" it can be a user defined function.
Bram Moolenaar53900992020-08-22 19:02:02 +02002612 // TODO: this is just guessing
Bram Moolenaar0f769812020-09-12 18:32:34 +02002613 if (res == FAIL && is_expr
2614 && (ASCII_ISUPPER(*name) || name[1] == ':'))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002615 res = generate_funcref(cctx, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 }
2617 }
2618 if (gen_load)
2619 res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002620 if (gen_load_outer)
Bram Moolenaarfd777482020-08-12 19:42:01 +02002621 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002622 res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
Bram Moolenaarfd777482020-08-12 19:42:01 +02002623 cctx->ctx_outer_used = TRUE;
2624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 }
2626
2627 *arg = end;
2628
2629theend:
Bram Moolenaar599c89c2020-03-28 14:53:20 +01002630 if (res == FAIL && error && called_emsg == prev_called_emsg)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002631 semsg(_(e_variable_not_found_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 vim_free(name);
2633 return res;
2634}
2635
2636/*
2637 * Compile the argument expressions.
2638 * "arg" points to just after the "(" and is advanced to after the ")"
2639 */
2640 static int
2641compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
2642{
Bram Moolenaar2c330432020-04-13 14:41:35 +02002643 char_u *p = *arg;
2644 char_u *whitep = *arg;
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002645 int must_end = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646
Bram Moolenaare6085c52020-04-12 20:19:16 +02002647 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002649 if (may_get_next_line(whitep, &p, cctx) == FAIL)
2650 goto failret;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002651 if (*p == ')')
2652 {
2653 *arg = p + 1;
2654 return OK;
2655 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002656 if (must_end)
2657 {
2658 semsg(_(e_missing_comma_before_argument_str), p);
2659 return FAIL;
2660 }
Bram Moolenaare6085c52020-04-12 20:19:16 +02002661
Bram Moolenaara5565e42020-05-09 15:44:01 +02002662 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 return FAIL;
2664 ++*argcount;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002665
2666 if (*p != ',' && *skipwhite(p) == ',')
2667 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002668 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002669 p = skipwhite(p);
2670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 if (*p == ',')
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002672 {
2673 ++p;
Bram Moolenaare6085c52020-04-12 20:19:16 +02002674 if (*p != NUL && !VIM_ISWHITE(*p))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002675 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002676 }
Bram Moolenaar10e4f122020-09-20 22:43:52 +02002677 else
2678 must_end = TRUE;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002679 whitep = p;
Bram Moolenaar38a5f512020-02-19 12:40:39 +01002680 p = skipwhite(p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002682failret:
Bram Moolenaare6085c52020-04-12 20:19:16 +02002683 emsg(_(e_missing_close));
2684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685}
2686
2687/*
2688 * Compile a function call: name(arg1, arg2)
2689 * "arg" points to "name", "arg + varlen" to the "(".
2690 * "argcount_init" is 1 for "value->method()"
2691 * Instructions:
2692 * EVAL arg1
2693 * EVAL arg2
2694 * BCALL / DCALL / UCALL
2695 */
2696 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02002697compile_call(
2698 char_u **arg,
2699 size_t varlen,
2700 cctx_T *cctx,
2701 ppconst_T *ppconst,
2702 int argcount_init)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703{
2704 char_u *name = *arg;
Bram Moolenaar0b76ad52020-01-31 21:20:51 +01002705 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 int argcount = argcount_init;
2707 char_u namebuf[100];
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002708 char_u fname_buf[FLEN_FIXED + 1];
2709 char_u *tofree = NULL;
2710 int error = FCERR_NONE;
Bram Moolenaarb3a01942020-11-17 19:56:09 +01002711 ufunc_T *ufunc = NULL;
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002712 int res = FAIL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002713 int is_autoload;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714
Bram Moolenaara5565e42020-05-09 15:44:01 +02002715 // we can evaluate "has('name')" at compile time
2716 if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
2717 {
2718 char_u *s = skipwhite(*arg + varlen + 1);
2719 typval_T argvars[2];
2720
2721 argvars[0].v_type = VAR_UNKNOWN;
2722 if (*s == '"')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002723 (void)eval_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002724 else if (*s == '\'')
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002725 (void)eval_lit_string(&s, &argvars[0], TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002726 s = skipwhite(s);
Bram Moolenaar8cebd432020-11-08 12:49:47 +01002727 if (*s == ')' && argvars[0].v_type == VAR_STRING
2728 && !dynamic_feature(argvars[0].vval.v_string))
Bram Moolenaara5565e42020-05-09 15:44:01 +02002729 {
2730 typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
2731
2732 *arg = s + 1;
2733 argvars[1].v_type = VAR_UNKNOWN;
2734 tv->v_type = VAR_NUMBER;
2735 tv->vval.v_number = 0;
2736 f_has(argvars, tv);
2737 clear_tv(&argvars[0]);
2738 ++ppconst->pp_used;
2739 return OK;
2740 }
Bram Moolenaar497f76b2020-05-09 16:44:22 +02002741 clear_tv(&argvars[0]);
Bram Moolenaara5565e42020-05-09 15:44:01 +02002742 }
2743
2744 if (generate_ppconst(cctx, ppconst) == FAIL)
2745 return FAIL;
2746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 if (varlen >= sizeof(namebuf))
2748 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002749 semsg(_(e_name_too_long_str), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 return FAIL;
2751 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002752 vim_strncpy(namebuf, *arg, varlen);
2753 name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002754
2755 *arg = skipwhite(*arg + varlen + 1);
2756 if (compile_arguments(arg, cctx, &argcount) == FAIL)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002757 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758
Bram Moolenaar03290b82020-12-19 16:30:44 +01002759 is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
Bram Moolenaara1773442020-08-12 15:21:22 +02002760 if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 {
2762 int idx;
2763
2764 // builtin function
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002765 idx = find_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 if (idx >= 0)
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002767 {
2768 if (STRCMP(name, "add") == 0 && argcount == 2)
2769 {
2770 garray_T *stack = &cctx->ctx_type_stack;
2771 type_T *type = ((type_T **)stack->ga_data)[
2772 stack->ga_len - 2];
2773
Bram Moolenaare88c8e82020-11-01 17:03:37 +01002774 // add() can be compiled to instructions if we know the type
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002775 if (type->tt_type == VAR_LIST)
2776 {
2777 // inline "add(list, item)" so that the type can be checked
2778 res = generate_LISTAPPEND(cctx);
2779 idx = -1;
2780 }
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002781 else if (type->tt_type == VAR_BLOB)
2782 {
2783 // inline "add(blob, nr)" so that the type can be checked
2784 res = generate_BLOBAPPEND(cctx);
2785 idx = -1;
2786 }
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002787 }
2788
2789 if (idx >= 0)
2790 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
2791 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002792 else
2793 semsg(_(e_unknownfunc), namebuf);
2794 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 }
2796
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002797 // An argument or local variable can be a function reference, this
2798 // overrules a function name.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002799 if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002800 && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002801 {
Bram Moolenaar52bf81c2020-11-17 18:50:44 +01002802 // If we can find the function by name generate the right call.
2803 // Skip global functions here, a local funcref takes precedence.
2804 ufunc = find_func(name, FALSE, cctx);
2805 if (ufunc != NULL && !func_is_global(ufunc))
2806 {
2807 res = generate_CALL(cctx, ufunc, argcount);
2808 goto theend;
2809 }
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002810 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811
2812 // If the name is a variable, load it and use PCALL.
Bram Moolenaara26b9702020-04-18 19:53:28 +02002813 // Not for g:Func(), we don't know if it is a variable or not.
Bram Moolenaara1773442020-08-12 15:21:22 +02002814 // Not for eome#Func(), it will be loaded later.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 p = namebuf;
Bram Moolenaara1773442020-08-12 15:21:22 +02002816 if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
Bram Moolenaar0f769812020-09-12 18:32:34 +02002817 && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002818 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002819 garray_T *stack = &cctx->ctx_type_stack;
2820 type_T *type;
2821
2822 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
2823 res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002824 goto theend;
2825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826
Bram Moolenaar0f769812020-09-12 18:32:34 +02002827 // If we can find a global function by name generate the right call.
2828 if (ufunc != NULL)
2829 {
2830 res = generate_CALL(cctx, ufunc, argcount);
2831 goto theend;
2832 }
2833
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002834 // A global function may be defined only later. Need to figure out at
Bram Moolenaara0a9f432020-04-28 21:29:34 +02002835 // runtime. Also handles a FuncRef at runtime.
Bram Moolenaara1773442020-08-12 15:21:22 +02002836 if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02002837 res = generate_UCALL(cctx, name, argcount);
2838 else
2839 semsg(_(e_unknownfunc), namebuf);
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002840
2841theend:
2842 vim_free(tofree);
2843 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844}
2845
2846// like NAMESPACE_CHAR but with 'a' and 'l'.
2847#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
2848
2849/*
2850 * Find the end of a variable or function name. Unlike find_name_end() this
2851 * does not recognize magic braces.
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002852 * When "use_namespace" is TRUE recognize "b:", "s:", etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 * Return a pointer to just after the name. Equal to "arg" if there is no
2854 * valid name.
2855 */
Bram Moolenaar2bede172020-11-19 18:53:18 +01002856 char_u *
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002857to_name_end(char_u *arg, int use_namespace)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858{
2859 char_u *p;
2860
2861 // Quick check for valid starting character.
2862 if (!eval_isnamec1(*arg))
2863 return arg;
2864
2865 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
2866 // Include a namespace such as "s:var" and "v:var". But "n:" is not
2867 // and can be used in slice "[n:]".
2868 if (*p == ':' && (p != arg + 1
Bram Moolenaarbebaa0d2020-11-20 18:59:19 +01002869 || !use_namespace
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
2871 break;
2872 return p;
2873}
2874
2875/*
2876 * Like to_name_end() but also skip over a list or dict constant.
Bram Moolenaar1c991142020-07-04 13:15:31 +02002877 * This intentionally does not handle line continuation.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 */
2879 char_u *
2880to_name_const_end(char_u *arg)
2881{
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01002882 char_u *p = to_name_end(arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 typval_T rettv;
2884
2885 if (p == arg && *arg == '[')
2886 {
2887
2888 // Can be "[1, 2, 3]->Func()".
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002889 if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 p = arg;
2891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 return p;
2893}
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895/*
2896 * parse a list: [expr, expr]
2897 * "*arg" points to the '['.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002898 * ppconst->pp_is_const is set if all items are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 */
2900 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002901compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902{
2903 char_u *p = skipwhite(*arg + 1);
Bram Moolenaar2c330432020-04-13 14:41:35 +02002904 char_u *whitep = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 int count = 0;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002906 int is_const;
2907 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002909 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002911 if (may_get_next_line(whitep, &p, cctx) == FAIL)
Bram Moolenaara30590d2020-03-28 22:06:23 +01002912 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02002913 semsg(_(e_list_end), *arg);
2914 return FAIL;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002915 }
Bram Moolenaardb199212020-08-12 18:01:53 +02002916 if (*p == ',')
2917 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002918 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02002919 return FAIL;
2920 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002921 if (*p == ']')
2922 {
2923 ++p;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002924 break;
Bram Moolenaara30590d2020-03-28 22:06:23 +01002925 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002926 if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02002927 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002928 if (!is_const)
2929 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 ++count;
2931 if (*p == ',')
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002932 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 ++p;
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002934 if (*p != ']' && !IS_WHITE_OR_NUL(*p))
2935 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002936 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar6b7a0a82020-07-08 18:38:08 +02002937 return FAIL;
2938 }
2939 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02002940 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 p = skipwhite(p);
2942 }
Bram Moolenaar4fdae992020-04-12 16:38:57 +02002943 *arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
Bram Moolenaar334a8b42020-10-19 16:07:42 +02002945 ppconst->pp_is_const = is_all_const;
2946 return generate_NEWLIST(cctx, count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947}
2948
2949/*
2950 * parse a lambda: {arg, arg -> expr}
2951 * "*arg" points to the '{'.
2952 */
2953 static int
2954compile_lambda(char_u **arg, cctx_T *cctx)
2955{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 typval_T rettv;
2957 ufunc_T *ufunc;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002958 evalarg_T evalarg;
2959
2960 CLEAR_FIELD(evalarg);
2961 evalarg.eval_flags = EVAL_EVALUATE;
2962 evalarg.eval_cctx = cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01002965 if (get_lambda_tv(arg, &rettv, TRUE, &evalarg) != OK)
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002966 {
2967 clear_evalarg(&evalarg, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 return FAIL;
Bram Moolenaar2ea79ad2020-10-18 23:32:13 +02002969 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01002970
Bram Moolenaar65c44152020-12-24 15:14:01 +01002971 // "rettv" will now be a partial referencing the function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 ufunc = rettv.vval.v_partial->pt_func;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002973 ++ufunc->uf_refcount;
2974 clear_tv(&rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975
Bram Moolenaar65c44152020-12-24 15:14:01 +01002976 // Compile the function into instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002977 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002979 clear_evalarg(&evalarg, NULL);
2980
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002981 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002982 {
2983 // The return type will now be known.
2984 set_function_type(ufunc);
2985
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002986 // The function reference count will be 1. When the ISN_FUNCREF
2987 // instruction is deleted the reference count is decremented and the
2988 // function is freed.
Bram Moolenaar5a849da2020-08-08 16:47:30 +02002989 return generate_FUNCREF(cctx, ufunc);
2990 }
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002991
2992 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 return FAIL;
2994}
2995
2996/*
2997 * Compile a lamda call: expr->{lambda}(args)
2998 * "arg" points to the "{".
2999 */
3000 static int
3001compile_lambda_call(char_u **arg, cctx_T *cctx)
3002{
3003 ufunc_T *ufunc;
3004 typval_T rettv;
3005 int argcount = 1;
3006 int ret = FAIL;
3007
3008 // Get the funcref in "rettv".
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003009 if (get_lambda_tv(arg, &rettv, TRUE, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 return FAIL;
3011
3012 if (**arg != '(')
3013 {
3014 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003015 emsg(_(e_nowhitespace));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 else
3017 semsg(_(e_missing_paren), "lambda");
3018 clear_tv(&rettv);
3019 return FAIL;
3020 }
3021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 ufunc = rettv.vval.v_partial->pt_func;
3023 ++ufunc->uf_refcount;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003024 clear_tv(&rettv);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003025 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003026
Bram Moolenaara05e5242020-09-19 18:19:19 +02003027 // The function will have one line: "return {expr}". Compile it into
3028 // instructions so that we get any errors right now.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003029 compile_def_function(ufunc, TRUE, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030
3031 // compile the arguments
3032 *arg = skipwhite(*arg + 1);
3033 if (compile_arguments(arg, cctx, &argcount) == OK)
3034 // call the compiled function
3035 ret = generate_CALL(cctx, ufunc, argcount);
3036
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003037 if (ret == FAIL)
3038 func_ptr_unref(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 return ret;
3040}
3041
3042/*
Bram Moolenaare0de1712020-12-02 17:36:54 +01003043 * parse a dict: {key: val, [key]: val}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 * "*arg" points to the '{'.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003045 * ppconst->pp_is_const is set if all item values are a constant.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 */
3047 static int
Bram Moolenaare0de1712020-12-02 17:36:54 +01003048compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049{
3050 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003051 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 int count = 0;
3053 dict_T *d = dict_alloc();
3054 dictitem_T *item;
Bram Moolenaar2c330432020-04-13 14:41:35 +02003055 char_u *whitep = *arg;
3056 char_u *p;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003057 int is_const;
3058 int is_all_const = TRUE; // reset when non-const encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059
3060 if (d == NULL)
3061 return FAIL;
3062 *arg = skipwhite(*arg + 1);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003063 for (;;)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003065 char_u *key = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
Bram Moolenaar23c55272020-06-21 16:58:13 +02003067 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003068 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003069 *arg = NULL;
3070 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003071 }
3072
3073 if (**arg == '}')
3074 break;
3075
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003076 if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 {
Bram Moolenaar2bede172020-11-19 18:53:18 +01003078 isn_T *isn;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003080 // {[expr]: value} uses an evaluated key.
Bram Moolenaare0de1712020-12-02 17:36:54 +01003081 *arg = skipwhite(*arg + 1);
Bram Moolenaara5565e42020-05-09 15:44:01 +02003082 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
3085 if (isn->isn_type == ISN_PUSHS)
3086 key = isn->isn_arg.string;
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003087 else
3088 {
3089 type_T *keytype = ((type_T **)stack->ga_data)
Bram Moolenaar2bede172020-11-19 18:53:18 +01003090 [stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003091 if (need_type(keytype, &t_string, -1, cctx,
Bram Moolenaar2bede172020-11-19 18:53:18 +01003092 FALSE, FALSE) == FAIL)
Bram Moolenaarf1a23682020-07-13 18:55:48 +02003093 return FAIL;
3094 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003095 *arg = skipwhite(*arg);
3096 if (**arg != ']')
Bram Moolenaar2bede172020-11-19 18:53:18 +01003097 {
Bram Moolenaare0de1712020-12-02 17:36:54 +01003098 emsg(_(e_missing_matching_bracket_after_dict_key));
3099 return FAIL;
Bram Moolenaar2bede172020-11-19 18:53:18 +01003100 }
Bram Moolenaare0de1712020-12-02 17:36:54 +01003101 ++*arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +01003103 else
3104 {
3105 // {"name": value},
3106 // {'name': value},
3107 // {name: value} use "name" as a literal key
3108 key = get_literal_key(arg);
3109 if (key == NULL)
3110 return FAIL;
3111 if (generate_PUSHS(cctx, key) == FAIL)
3112 return FAIL;
3113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114
3115 // Check for duplicate keys, if using string keys.
3116 if (key != NULL)
3117 {
3118 item = dict_find(d, key, -1);
3119 if (item != NULL)
3120 {
3121 semsg(_(e_duplicate_key), key);
3122 goto failret;
3123 }
3124 item = dictitem_alloc(key);
3125 if (item != NULL)
3126 {
3127 item->di_tv.v_type = VAR_UNKNOWN;
3128 item->di_tv.v_lock = 0;
3129 if (dict_add(d, item) == FAIL)
3130 dictitem_free(item);
3131 }
3132 }
3133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if (**arg != ':')
3135 {
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003136 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003137 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaar17a836c2020-08-12 17:35:58 +02003138 else
3139 semsg(_(e_missing_dict_colon), *arg);
3140 return FAIL;
3141 }
3142 whitep = *arg + 1;
3143 if (!IS_WHITE_OR_NUL(*whitep))
3144 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003145 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 return FAIL;
3147 }
3148
3149 *arg = skipwhite(*arg + 1);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003150 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003151 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003152 *arg = NULL;
3153 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003154 }
3155
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003156 if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003158 if (!is_const)
3159 is_all_const = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 ++count;
3161
Bram Moolenaar2c330432020-04-13 14:41:35 +02003162 whitep = *arg;
Bram Moolenaar23c55272020-06-21 16:58:13 +02003163 *arg = skipwhite(*arg);
3164 if (may_get_next_line(whitep, arg, cctx) == FAIL)
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003165 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003166 *arg = NULL;
3167 goto failret;
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 if (**arg == '}')
3170 break;
3171 if (**arg != ',')
3172 {
3173 semsg(_(e_missing_dict_comma), *arg);
3174 goto failret;
3175 }
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003176 if (IS_WHITE_OR_NUL(*whitep))
3177 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003178 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +02003179 return FAIL;
3180 }
Bram Moolenaar2c330432020-04-13 14:41:35 +02003181 whitep = *arg + 1;
Bram Moolenaar9a13e182020-10-19 21:45:07 +02003182 if (!IS_WHITE_OR_NUL(*whitep))
3183 {
3184 semsg(_(e_white_space_required_after_str), ",");
3185 return FAIL;
3186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 *arg = skipwhite(*arg + 1);
3188 }
3189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 *arg = *arg + 1;
3191
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003192 // Allow for following comment, after at least one space.
Bram Moolenaar2c330432020-04-13 14:41:35 +02003193 p = skipwhite(*arg);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003194 if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003195 *arg += STRLEN(*arg);
3196
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 dict_unref(d);
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003198 ppconst->pp_is_const = is_all_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return generate_NEWDICT(cctx, count);
3200
3201failret:
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003202 if (*arg == NULL)
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003203 {
Bram Moolenaar4fdae992020-04-12 16:38:57 +02003204 semsg(_(e_missing_dict_end), _("[end of lines]"));
Bram Moolenaar44aefff2020-10-05 19:23:59 +02003205 *arg = (char_u *)"";
3206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 dict_unref(d);
3208 return FAIL;
3209}
3210
3211/*
3212 * Compile "&option".
3213 */
3214 static int
3215compile_get_option(char_u **arg, cctx_T *cctx)
3216{
3217 typval_T rettv;
3218 char_u *start = *arg;
3219 int ret;
3220
3221 // parse the option and get the current value to get the type.
3222 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003223 ret = eval_option(arg, &rettv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 if (ret == OK)
3225 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003226 // include the '&' in the name, eval_option() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 char_u *name = vim_strnsave(start, *arg - start);
3228 type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
3229
3230 ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
3231 vim_free(name);
3232 }
3233 clear_tv(&rettv);
3234
3235 return ret;
3236}
3237
3238/*
3239 * Compile "$VAR".
3240 */
3241 static int
3242compile_get_env(char_u **arg, cctx_T *cctx)
3243{
3244 char_u *start = *arg;
3245 int len;
3246 int ret;
3247 char_u *name;
3248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 ++*arg;
3250 len = get_env_len(arg);
3251 if (len == 0)
3252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003253 semsg(_(e_syntax_error_at_str), start - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 return FAIL;
3255 }
3256
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003257 // include the '$' in the name, eval_env_var() expects it.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 name = vim_strnsave(start, len + 1);
3259 ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
3260 vim_free(name);
3261 return ret;
3262}
3263
3264/*
3265 * Compile "@r".
3266 */
3267 static int
3268compile_get_register(char_u **arg, cctx_T *cctx)
3269{
3270 int ret;
3271
3272 ++*arg;
3273 if (**arg == NUL)
3274 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003275 semsg(_(e_syntax_error_at_str), *arg - 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 return FAIL;
3277 }
Bram Moolenaar7226e5b2020-08-02 17:33:26 +02003278 if (!valid_yank_reg(**arg, FALSE))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 {
3280 emsg_invreg(**arg);
3281 return FAIL;
3282 }
3283 ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
3284 ++*arg;
3285 return ret;
3286}
3287
3288/*
3289 * Apply leading '!', '-' and '+' to constant "rettv".
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003290 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 */
3292 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003293apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003295 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
3297 // this works from end to start
3298 while (p > start)
3299 {
3300 --p;
3301 if (*p == '-' || *p == '+')
3302 {
3303 // only '-' has an effect, for '+' we only check the type
3304#ifdef FEAT_FLOAT
3305 if (rettv->v_type == VAR_FLOAT)
3306 {
3307 if (*p == '-')
3308 rettv->vval.v_float = -rettv->vval.v_float;
3309 }
3310 else
3311#endif
3312 {
3313 varnumber_T val;
3314 int error = FALSE;
3315
3316 // tv_get_number_chk() accepts a string, but we don't want that
3317 // here
3318 if (check_not_string(rettv) == FAIL)
3319 return FAIL;
3320 val = tv_get_number_chk(rettv, &error);
3321 clear_tv(rettv);
3322 if (error)
3323 return FAIL;
3324 if (*p == '-')
3325 val = -val;
3326 rettv->v_type = VAR_NUMBER;
3327 rettv->vval.v_number = val;
3328 }
3329 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003330 else if (numeric_only)
3331 {
3332 ++p;
3333 break;
3334 }
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003335 else if (*p == '!')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 {
3337 int v = tv2bool(rettv);
3338
3339 // '!' is permissive in the type.
3340 clear_tv(rettv);
3341 rettv->v_type = VAR_BOOL;
3342 rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
3343 }
3344 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003345 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 return OK;
3347}
3348
3349/*
3350 * Recognize v: variables that are constants and set "rettv".
3351 */
3352 static void
3353get_vim_constant(char_u **arg, typval_T *rettv)
3354{
3355 if (STRNCMP(*arg, "v:true", 6) == 0)
3356 {
3357 rettv->v_type = VAR_BOOL;
3358 rettv->vval.v_number = VVAL_TRUE;
3359 *arg += 6;
3360 }
3361 else if (STRNCMP(*arg, "v:false", 7) == 0)
3362 {
3363 rettv->v_type = VAR_BOOL;
3364 rettv->vval.v_number = VVAL_FALSE;
3365 *arg += 7;
3366 }
3367 else if (STRNCMP(*arg, "v:null", 6) == 0)
3368 {
3369 rettv->v_type = VAR_SPECIAL;
3370 rettv->vval.v_number = VVAL_NULL;
3371 *arg += 6;
3372 }
3373 else if (STRNCMP(*arg, "v:none", 6) == 0)
3374 {
3375 rettv->v_type = VAR_SPECIAL;
3376 rettv->vval.v_number = VVAL_NONE;
3377 *arg += 6;
3378 }
3379}
3380
Bram Moolenaar696ba232020-07-29 21:20:41 +02003381 exptype_T
Bram Moolenaar61a89812020-05-07 16:58:17 +02003382get_compare_type(char_u *p, int *len, int *type_is)
3383{
3384 exptype_T type = EXPR_UNKNOWN;
3385 int i;
3386
3387 switch (p[0])
3388 {
3389 case '=': if (p[1] == '=')
3390 type = EXPR_EQUAL;
3391 else if (p[1] == '~')
3392 type = EXPR_MATCH;
3393 break;
3394 case '!': if (p[1] == '=')
3395 type = EXPR_NEQUAL;
3396 else if (p[1] == '~')
3397 type = EXPR_NOMATCH;
3398 break;
3399 case '>': if (p[1] != '=')
3400 {
3401 type = EXPR_GREATER;
3402 *len = 1;
3403 }
3404 else
3405 type = EXPR_GEQUAL;
3406 break;
3407 case '<': if (p[1] != '=')
3408 {
3409 type = EXPR_SMALLER;
3410 *len = 1;
3411 }
3412 else
3413 type = EXPR_SEQUAL;
3414 break;
3415 case 'i': if (p[1] == 's')
3416 {
3417 // "is" and "isnot"; but not a prefix of a name
3418 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3419 *len = 5;
3420 i = p[*len];
3421 if (!isalnum(i) && i != '_')
3422 {
3423 type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
3424 *type_is = TRUE;
3425 }
3426 }
3427 break;
3428 }
3429 return type;
3430}
3431
3432/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 * Compile code to apply '-', '+' and '!'.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003434 * When "numeric_only" is TRUE do not apply '!'.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 */
3436 static int
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003437compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003439 char_u *p = *end;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440
3441 // this works from end to start
3442 while (p > start)
3443 {
3444 --p;
Bram Moolenaar79cdf802020-11-18 17:39:05 +01003445 while (VIM_ISWHITE(*p))
3446 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 if (*p == '-' || *p == '+')
3448 {
3449 int negate = *p == '-';
3450 isn_T *isn;
3451
3452 // TODO: check type
3453 while (p > start && (p[-1] == '-' || p[-1] == '+'))
3454 {
3455 --p;
3456 if (*p == '-')
3457 negate = !negate;
3458 }
3459 // only '-' has an effect, for '+' we only check the type
3460 if (negate)
3461 isn = generate_instr(cctx, ISN_NEGATENR);
3462 else
3463 isn = generate_instr(cctx, ISN_CHECKNR);
3464 if (isn == NULL)
3465 return FAIL;
3466 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003467 else if (numeric_only)
3468 {
3469 ++p;
3470 break;
3471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 else
3473 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003474 int invert = *p == '!';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003476 while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 {
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003478 if (p[-1] == '!')
3479 invert = !invert;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 --p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 }
3482 if (generate_2BOOL(cctx, invert) == FAIL)
3483 return FAIL;
3484 }
3485 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003486 *end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 return OK;
3488}
3489
3490/*
3491 * Compile whatever comes after "name" or "name()".
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003492 * Advances "*arg" only when something was recognized.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 */
3494 static int
3495compile_subscript(
3496 char_u **arg,
3497 cctx_T *cctx,
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003498 char_u *start_leader,
3499 char_u **end_leader,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003500 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501{
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003502 char_u *name_start = *end_leader;
3503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 for (;;)
3505 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02003506 char_u *p = skipwhite(*arg);
3507
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02003508 if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003509 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003510 char_u *next = peek_next_line_from_context(cctx);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003511
3512 // If a following line starts with "->{" or "->X" advance to that
3513 // line, so that a line break before "->" is allowed.
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003514 // Also if a following line starts with ".x".
3515 if (next != NULL &&
3516 ((next[0] == '-' && next[1] == '>'
3517 && (next[2] == '{' || ASCII_ISALPHA(next[2])))
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003518 || (next[0] == '.' && eval_isdictc(next[1]))))
Bram Moolenaar23c55272020-06-21 16:58:13 +02003519 {
3520 next = next_line_from_context(cctx, TRUE);
3521 if (next == NULL)
3522 return FAIL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003523 *arg = next;
3524 p = skipwhite(*arg);
Bram Moolenaar23c55272020-06-21 16:58:13 +02003525 }
3526 }
3527
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003528 // Do not skip over white space to find the "(", "execute 'x' ()" is
Bram Moolenaar2d6b20d2020-07-25 19:30:59 +02003529 // not a function call.
3530 if (**arg == '(')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 {
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003532 garray_T *stack = &cctx->ctx_type_stack;
3533 type_T *type;
3534 int argcount = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003536 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003537 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003538 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 // funcref(arg)
Bram Moolenaara0a9f432020-04-28 21:29:34 +02003541 type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3542
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003543 *arg = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 if (compile_arguments(arg, cctx, &argcount) == FAIL)
3545 return FAIL;
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003546 if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 return FAIL;
3548 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003549 else if (*p == '-' && p[1] == '>')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003551 char_u *pstart = p;
3552
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003553 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003554 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003555 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 // something->method()
3558 // Apply the '!', '-' and '+' first:
3559 // -1.0->func() works like (-1.0)->func()
Bram Moolenaar59eccb92020-08-10 23:09:37 +02003560 if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003563 p += 2;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003564 *arg = skipwhite(p);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003565 // No line break supported right after "->".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 if (**arg == '{')
3567 {
3568 // lambda call: list->{lambda}
Bram Moolenaar65c44152020-12-24 15:14:01 +01003569 // TODO: remove this
3570 if (compile_lambda_call(arg, cctx) == FAIL)
3571 return FAIL;
3572 }
3573 else if (**arg == '(')
3574 {
3575 // Funcref call: list->(Refs[2])()
3576 // or lambda: list->((arg) => expr)()
3577 // TODO: make this work
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 if (compile_lambda_call(arg, cctx) == FAIL)
3579 return FAIL;
3580 }
3581 else
3582 {
3583 // method call: list->method()
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003584 p = *arg;
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003585 if (!eval_isnamec1(*p))
3586 {
Bram Moolenaar637cd7d2020-07-23 19:06:23 +02003587 semsg(_(e_trailing_arg), pstart);
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02003588 return FAIL;
3589 }
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003590 if (ASCII_ISALPHA(*p) && p[1] == ':')
3591 p += 2;
Bram Moolenaarc5da1fb2020-08-05 15:43:44 +02003592 for ( ; eval_isnamec(*p); ++p)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 ;
3594 if (*p != '(')
3595 {
Bram Moolenaar0b37a2f2020-03-29 21:38:15 +02003596 semsg(_(e_missing_paren), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 return FAIL;
3598 }
3599 // TODO: base value may not be the first argument
Bram Moolenaara5565e42020-05-09 15:44:01 +02003600 if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 return FAIL;
3602 }
3603 }
Bram Moolenaarbadd8482020-07-31 22:38:17 +02003604 else if (**arg == '[')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003606 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003607 type_T **typep;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003608 type_T *valtype;
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003609 vartype_T vtype;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003610 int is_slice = FALSE;
Bram Moolenaarb13af502020-02-17 21:12:08 +01003611
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003612 // list index: list[123]
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003613 // dict member: dict[key]
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003614 // string index: text[123]
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003615 // TODO: blob index
3616 // TODO: more arguments
3617 // TODO: recognize list or dict at runtime
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003618 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003619 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003620 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003621
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003622 ++p;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003623 *arg = skipwhite(p);
Bram Moolenaara7eedf32020-07-10 21:50:41 +02003624 if (may_get_next_line_error(p, arg, cctx) == FAIL)
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003625 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003626 if (**arg == ':')
3627 // missing first index is equal to zero
3628 generate_PUSHNR(cctx, 0);
3629 else
3630 {
3631 if (compile_expr0(arg, cctx) == FAIL)
3632 return FAIL;
3633 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3634 return FAIL;
3635 *arg = skipwhite(*arg);
3636 }
3637 if (**arg == ':')
3638 {
3639 *arg = skipwhite(*arg + 1);
3640 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3641 return FAIL;
3642 if (**arg == ']')
3643 // missing second index is equal to end of string
3644 generate_PUSHNR(cctx, -1);
3645 else
3646 {
3647 if (compile_expr0(arg, cctx) == FAIL)
Bram Moolenaar918a4242020-12-06 14:37:08 +01003648 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003649 if (may_get_next_line_error(p, arg, cctx) == FAIL)
3650 return FAIL;
3651 *arg = skipwhite(*arg);
3652 }
3653 is_slice = TRUE;
3654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655
3656 if (**arg != ']')
3657 {
3658 emsg(_(e_missbrac));
3659 return FAIL;
3660 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003661 *arg = *arg + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003663 // We can index a list and a dict. If we don't know the type
3664 // we can use the index value type.
3665 // TODO: If we don't know use an instruction to figure it out at
3666 // runtime.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003667 typep = ((type_T **)stack->ga_data) + stack->ga_len
3668 - (is_slice ? 3 : 2);
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003669 vtype = (*typep)->tt_type;
Bram Moolenaar56acb092020-08-16 14:48:19 +02003670 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
3671 // If the index is a string, the variable must be a Dict.
3672 if (*typep == &t_any && valtype == &t_string)
3673 vtype = VAR_DICT;
3674 if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB)
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003675 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003676 if (need_type(valtype, &t_number, -1, cctx,
3677 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003678 return FAIL;
3679 if (is_slice)
3680 {
3681 valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003682 if (need_type(valtype, &t_number, -2, cctx,
3683 FALSE, FALSE) == FAIL)
Bram Moolenaar56acb092020-08-16 14:48:19 +02003684 return FAIL;
3685 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003686 }
Bram Moolenaar56acb092020-08-16 14:48:19 +02003687
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003688 if (vtype == VAR_DICT)
3689 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003690 if (is_slice)
3691 {
3692 emsg(_(e_cannot_slice_dictionary));
3693 return FAIL;
3694 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003695 if ((*typep)->tt_type == VAR_DICT)
3696 *typep = (*typep)->tt_member;
Bram Moolenaar7892b952020-07-20 22:09:34 +02003697 else
3698 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003699 if (need_type(*typep, &t_dict_any, -2, cctx,
3700 FALSE, FALSE) == FAIL)
Bram Moolenaar7892b952020-07-20 22:09:34 +02003701 return FAIL;
3702 *typep = &t_any;
3703 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003704 if (may_generate_2STRING(-1, cctx) == FAIL)
3705 return FAIL;
3706 if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
3707 return FAIL;
3708 }
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003709 else if (vtype == VAR_STRING)
3710 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003711 *typep = &t_string;
3712 if ((is_slice
3713 ? generate_instr_drop(cctx, ISN_STRSLICE, 2)
3714 : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003715 return FAIL;
3716 }
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003717 else if (vtype == VAR_BLOB)
3718 {
3719 emsg("Sorry, blob index and slice not implemented yet");
3720 return FAIL;
3721 }
Bram Moolenaar6802cce2020-07-19 15:49:49 +02003722 else if (vtype == VAR_LIST || *typep == &t_any)
Bram Moolenaarb13af502020-02-17 21:12:08 +01003723 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003724 if (is_slice)
3725 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003726 if (generate_instr_drop(cctx,
3727 vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
3728 2) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003729 return FAIL;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003730 }
Bram Moolenaared591872020-08-15 22:14:53 +02003731 else
3732 {
3733 if ((*typep)->tt_type == VAR_LIST)
3734 *typep = (*typep)->tt_member;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003735 if (generate_instr_drop(cctx,
3736 vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX,
3737 1) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003738 return FAIL;
3739 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003740 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003741 else
3742 {
Bram Moolenaar56acb092020-08-16 14:48:19 +02003743 emsg(_(e_string_list_dict_or_blob_required));
Bram Moolenaarb13af502020-02-17 21:12:08 +01003744 return FAIL;
3745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003747 else if (*p == '.' && p[1] != '.')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003749 // dictionary member: dict.name
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003750 if (generate_ppconst(cctx, ppconst) == FAIL)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003751 return FAIL;
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003752 ppconst->pp_is_const = FALSE;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003753
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02003754 *arg = p + 1;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003755 if (may_get_next_line(*arg, arg, cctx) == FAIL)
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003756 {
3757 emsg(_(e_missing_name_after_dot));
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003758 return FAIL;
Bram Moolenaarc1f00662020-10-03 13:41:53 +02003759 }
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02003760 p = *arg;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003761 if (eval_isdictc(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 while (eval_isnamec(*p))
3763 MB_PTR_ADV(p);
3764 if (p == *arg)
3765 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003766 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 return FAIL;
3768 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003769 if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 return FAIL;
3771 *arg = p;
3772 }
3773 else
3774 break;
3775 }
3776
3777 // TODO - see handle_subscript():
3778 // Turn "dict.Func" into a partial for "Func" bound to "dict".
3779 // Don't do this when "Func" is already a partial that was bound
3780 // explicitly (pt_auto is FALSE).
3781
3782 return OK;
3783}
3784
3785/*
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003786 * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
3787 * "arg" is advanced until after the expression, skipping white space.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 *
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003789 * If the value is a constant "ppconst->pp_used" will be non-zero.
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003790 * Before instructions are generated, any values in "ppconst" will generated.
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003791 *
3792 * This is the compiling equivalent of eval1(), eval2(), etc.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 */
3794
3795/*
3796 * number number constant
3797 * 0zFFFFFFFF Blob constant
3798 * "string" string constant
3799 * 'string' literal string constant
3800 * &option-name option value
3801 * @r register contents
3802 * identifier variable value
3803 * function() function call
3804 * $VAR environment variable
3805 * (expression) nested expression
3806 * [expr, expr] List
Bram Moolenaare0de1712020-12-02 17:36:54 +01003807 * {key: val, [key]: val} Dictionary
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 *
3809 * Also handle:
3810 * ! in front logical NOT
3811 * - in front unary minus
3812 * + in front unary plus (ignored)
3813 * trailing (arg) funcref/partial call
3814 * trailing [] subscript in String or List
3815 * trailing .name entry in Dictionary
3816 * trailing ->name() method call
3817 */
3818 static int
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02003819compile_expr7(
3820 char_u **arg,
3821 cctx_T *cctx,
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003822 ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 char_u *start_leader, *end_leader;
3825 int ret = OK;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003826 typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
Bram Moolenaar1c747212020-05-09 18:28:34 +02003827 int used_before = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003829 ppconst->pp_is_const = FALSE;
3830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 /*
3832 * Skip '!', '-' and '+' characters. They are handled later.
3833 */
3834 start_leader = *arg;
3835 while (**arg == '!' || **arg == '-' || **arg == '+')
3836 *arg = skipwhite(*arg + 1);
3837 end_leader = *arg;
3838
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003839 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 switch (**arg)
3841 {
3842 /*
3843 * Number constant.
3844 */
3845 case '0': // also for blob starting with 0z
3846 case '1':
3847 case '2':
3848 case '3':
3849 case '4':
3850 case '5':
3851 case '6':
3852 case '7':
3853 case '8':
3854 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003855 case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 return FAIL;
Bram Moolenaar4301a722020-08-11 20:51:08 +02003857 // Apply "-" and "+" just before the number now, right to
3858 // left. Matters especially when "->" follows. Stops at
3859 // '!'.
3860 if (apply_leader(rettv, TRUE,
3861 start_leader, &end_leader) == FAIL)
3862 {
3863 clear_tv(rettv);
3864 return FAIL;
3865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 break;
3867
3868 /*
3869 * String constant: "string".
3870 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003871 case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 return FAIL;
3873 break;
3874
3875 /*
3876 * Literal string constant: 'str''ing'.
3877 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003878 case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 return FAIL;
3880 break;
3881
3882 /*
3883 * Constant Vim variable.
3884 */
Bram Moolenaar7d131b02020-05-08 19:10:34 +02003885 case 'v': get_vim_constant(arg, rettv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 ret = NOTDONE;
3887 break;
3888
3889 /*
Bram Moolenaara5565e42020-05-09 15:44:01 +02003890 * "true" constant
3891 */
3892 case 't': if (STRNCMP(*arg, "true", 4) == 0
3893 && !eval_isnamec((*arg)[4]))
3894 {
3895 *arg += 4;
3896 rettv->v_type = VAR_BOOL;
3897 rettv->vval.v_number = VVAL_TRUE;
3898 }
3899 else
3900 ret = NOTDONE;
3901 break;
3902
3903 /*
3904 * "false" constant
3905 */
3906 case 'f': if (STRNCMP(*arg, "false", 5) == 0
3907 && !eval_isnamec((*arg)[5]))
3908 {
3909 *arg += 5;
3910 rettv->v_type = VAR_BOOL;
3911 rettv->vval.v_number = VVAL_FALSE;
3912 }
3913 else
3914 ret = NOTDONE;
3915 break;
3916
3917 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 * List: [expr, expr]
3919 */
Bram Moolenaar334a8b42020-10-19 16:07:42 +02003920 case '[': ret = compile_list(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 break;
3922
3923 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 * Lambda: {arg, arg -> expr}
3925 * Dictionary: {'key': val, 'key': val}
3926 */
3927 case '{': {
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003928 char_u *start = skipwhite(*arg + 1);
3929 char_u *after = start;
3930 garray_T ga_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931
3932 // Find out what comes after the arguments.
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003933 ret = get_function_args(&after, '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003934 &ga_arg, TRUE, NULL, NULL,
3935 TRUE, NULL, NULL);
Bram Moolenaar27bf7af2020-12-23 20:27:31 +01003936 if (ret != FAIL && after[0] == '>'
3937 && ((after > start + 2
3938 && VIM_ISWHITE(after[-2]))
3939 || after == start + 1)
3940 && IS_WHITE_OR_NUL(after[1]))
Bram Moolenaar65c44152020-12-24 15:14:01 +01003941 // TODO: if we go with the "(arg) => expr" syntax
3942 // remove this
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 ret = compile_lambda(arg, cctx);
3944 else
Bram Moolenaare0de1712020-12-02 17:36:54 +01003945 ret = compile_dict(arg, cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003946 }
3947 break;
3948
3949 /*
3950 * Option value: &name
3951 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003952 case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
3953 return FAIL;
3954 ret = compile_get_option(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 break;
3956
3957 /*
3958 * Environment variable: $VAR.
3959 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003960 case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
3961 return FAIL;
3962 ret = compile_get_env(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 break;
3964
3965 /*
3966 * Register contents: @r.
3967 */
Bram Moolenaar3fc71282020-08-21 20:43:17 +02003968 case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
3969 return FAIL;
3970 ret = compile_get_register(arg, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 break;
3972 /*
3973 * nested expression: (expression).
Bram Moolenaar65c44152020-12-24 15:14:01 +01003974 * lambda: (arg, arg) => expr
3975 * funcref: (arg, arg) => { statement }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 */
Bram Moolenaar65c44152020-12-24 15:14:01 +01003977 case '(': {
3978 char_u *start = skipwhite(*arg + 1);
3979 char_u *after = start;
3980 garray_T ga_arg;
Bram Moolenaar1c747212020-05-09 18:28:34 +02003981
Bram Moolenaar65c44152020-12-24 15:14:01 +01003982 // Find out if "=>" comes after the ().
3983 ret = get_function_args(&after, ')', NULL,
3984 &ga_arg, TRUE, NULL, NULL,
3985 TRUE, NULL, NULL);
3986 if (ret == OK && VIM_ISWHITE(
3987 *after == ':' ? after[1] : *after))
3988 {
3989 if (*after == ':')
3990 // Skip over type in "(arg): type".
3991 after = skip_type(skipwhite(after + 1), TRUE);
3992
3993 after = skipwhite(after);
3994 if (after[0] == '=' && after[1] == '>'
3995 && IS_WHITE_OR_NUL(after[2]))
3996 {
3997 ret = compile_lambda(arg, cctx);
3998 break;
3999 }
4000 }
4001
4002 // (expression): recursive!
4003 *arg = skipwhite(*arg + 1);
4004 if (ppconst->pp_used <= PPSIZE - 10)
4005 {
4006 ret = compile_expr1(arg, cctx, ppconst);
4007 }
4008 else
4009 {
4010 // Not enough space in ppconst, flush constants.
4011 if (generate_ppconst(cctx, ppconst) == FAIL)
4012 return FAIL;
4013 ret = compile_expr0(arg, cctx);
4014 }
4015 *arg = skipwhite(*arg);
4016 if (**arg == ')')
4017 ++*arg;
4018 else if (ret == OK)
4019 {
4020 emsg(_(e_missing_close));
4021 ret = FAIL;
4022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 }
4024 break;
4025
4026 default: ret = NOTDONE;
4027 break;
4028 }
4029 if (ret == FAIL)
4030 return FAIL;
4031
Bram Moolenaar1c747212020-05-09 18:28:34 +02004032 if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004034 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004035 clear_tv(rettv);
4036 else
4037 // A constant expression can possibly be handled compile time,
4038 // return the value instead of generating code.
4039 ++ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
4041 else if (ret == NOTDONE)
4042 {
4043 char_u *p;
4044 int r;
4045
4046 if (!eval_isnamec1(**arg))
4047 {
Bram Moolenaare4984292020-12-13 14:19:25 +01004048 if (ends_excmd(*skipwhite(*arg)))
4049 semsg(_(e_empty_expression_str), *arg);
4050 else
4051 semsg(_(e_name_expected_str), *arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052 return FAIL;
4053 }
4054
4055 // "name" or "name()"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01004056 p = to_name_end(*arg, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 if (*p == '(')
Bram Moolenaara5565e42020-05-09 15:44:01 +02004058 {
4059 r = compile_call(arg, p - *arg, cctx, ppconst, 0);
4060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 else
Bram Moolenaara5565e42020-05-09 15:44:01 +02004062 {
4063 if (generate_ppconst(cctx, ppconst) == FAIL)
4064 return FAIL;
Bram Moolenaar0f769812020-09-12 18:32:34 +02004065 r = compile_load(arg, p, cctx, TRUE, TRUE);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 if (r == FAIL)
4068 return FAIL;
4069 }
4070
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004071 // Handle following "[]", ".member", etc.
4072 // Then deal with prefixed '-', '+' and '!', if not done already.
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004073 if (compile_subscript(arg, cctx, start_leader, &end_leader,
Bram Moolenaara5565e42020-05-09 15:44:01 +02004074 ppconst) == FAIL)
4075 return FAIL;
4076 if (ppconst->pp_used > 0)
4077 {
4078 // apply the '!', '-' and '+' before the constant
4079 rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004080 if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004081 return FAIL;
4082 return OK;
4083 }
Bram Moolenaar59eccb92020-08-10 23:09:37 +02004084 if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 return FAIL;
Bram Moolenaar5c2fe642020-05-07 23:20:21 +02004086 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087}
4088
4089/*
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004090 * Give the "white on both sides" error, taking the operator from "p[len]".
4091 */
4092 void
4093error_white_both(char_u *op, int len)
4094{
4095 char_u buf[10];
4096
4097 vim_strncpy(buf, op, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004098 semsg(_(e_white_space_required_before_and_after_str), buf);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004099}
4100
4101/*
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004102 * <type>expr7: runtime type check / conversion
4103 */
4104 static int
4105compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
4106{
4107 type_T *want_type = NULL;
4108
4109 // Recognize <type>
4110 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4111 {
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004112 ++*arg;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004113 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4114 if (want_type == NULL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004115 return FAIL;
4116
4117 if (**arg != '>')
4118 {
4119 if (*skipwhite(*arg) == '>')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004120 semsg(_(e_no_white_space_allowed_before_str), ">");
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004121 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004122 emsg(_(e_missing_gt));
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004123 return FAIL;
4124 }
4125 ++*arg;
4126 if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
4127 return FAIL;
4128 }
4129
4130 if (compile_expr7(arg, cctx, ppconst) == FAIL)
4131 return FAIL;
4132
4133 if (want_type != NULL)
4134 {
4135 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaard1103582020-08-14 22:44:25 +02004136 type_T *actual;
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004137
Bram Moolenaard1103582020-08-14 22:44:25 +02004138 generate_ppconst(cctx, ppconst);
4139 actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar8b565c22020-08-30 23:24:20 +02004140 if (check_type(want_type, actual, FALSE, 0) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004141 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004142 if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004143 return FAIL;
4144 }
4145 }
4146
4147 return OK;
4148}
4149
4150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 * * number multiplication
4152 * / number division
4153 * % number modulo
4154 */
4155 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004156compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157{
4158 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004159 char_u *next;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004160 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004162 // get the first expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004163 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 return FAIL;
4165
4166 /*
4167 * Repeat computing, until no "*", "/" or "%" is following.
4168 */
4169 for (;;)
4170 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004171 op = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 if (*op != '*' && *op != '/' && *op != '%')
4173 break;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004174 if (next != NULL)
4175 {
4176 *arg = next_line_from_context(cctx, TRUE);
4177 op = skipwhite(*arg);
4178 }
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004179
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004180 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004182 error_white_both(op, 1);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004183 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184 }
4185 *arg = skipwhite(op + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004186 if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004187 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004189 // get the second expression
Bram Moolenaar64d662d2020-08-09 19:02:50 +02004190 if (compile_expr7t(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 return FAIL;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004192
4193 if (ppconst->pp_used == ppconst_used + 2
4194 && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4195 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004196 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004197 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4198 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004199 varnumber_T res = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004201 // both are numbers: compute the result
4202 switch (*op)
4203 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004204 case '*': res = tv1->vval.v_number * tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004205 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004206 case '/': res = tv1->vval.v_number / tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004207 break;
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004208 case '%': res = tv1->vval.v_number % tv2->vval.v_number;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004209 break;
4210 }
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004211 tv1->vval.v_number = res;
4212 --ppconst->pp_used;
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004213 }
4214 else
4215 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004216 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004217 generate_two_op(cctx, op);
4218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * + number addition
4226 * - number subtraction
4227 * .. string concatenation
4228 */
4229 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004230compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231{
4232 char_u *op;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004233 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 int oplen;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004235 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236
4237 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004238 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 return FAIL;
4240
4241 /*
4242 * Repeat computing, until no "+", "-" or ".." is following.
4243 */
4244 for (;;)
4245 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004246 op = may_peek_next_line(cctx, *arg, &next);
4247 if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 break;
4249 oplen = (*op == '.' ? 2 : 1);
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004250 if (next != NULL)
4251 {
4252 *arg = next_line_from_context(cctx, TRUE);
4253 op = skipwhite(*arg);
4254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004256 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004258 error_white_both(op, oplen);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004259 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 }
4261
4262 *arg = skipwhite(op + oplen);
Bram Moolenaare0de1712020-12-02 17:36:54 +01004263 if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004264 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004266 // get the second expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004267 if (compile_expr6(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 return FAIL;
4269
Bram Moolenaara5565e42020-05-09 15:44:01 +02004270 if (ppconst->pp_used == ppconst_used + 2
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004271 && (*op == '.'
Bram Moolenaara5565e42020-05-09 15:44:01 +02004272 ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
4273 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
4274 : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
4275 && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004277 typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
4278 typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004279
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004280 // concat/subtract/add constant numbers
4281 if (*op == '+')
4282 tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
4283 else if (*op == '-')
4284 tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
4285 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004286 {
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004287 // concatenate constant strings
4288 char_u *s1 = tv1->vval.v_string;
4289 char_u *s2 = tv2->vval.v_string;
4290 size_t len1 = STRLEN(s1);
4291
4292 tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
4293 if (tv1->vval.v_string == NULL)
4294 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004295 clear_ppconst(ppconst);
Bram Moolenaar7d131b02020-05-08 19:10:34 +02004296 return FAIL;
4297 }
4298 mch_memmove(tv1->vval.v_string, s1, len1);
4299 STRCPY(tv1->vval.v_string + len1, s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004300 vim_free(s1);
4301 vim_free(s2);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004302 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004303 --ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 }
4305 else
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004306 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02004307 generate_ppconst(cctx, ppconst);
Bram Moolenaarf0eefce2020-05-07 22:19:01 +02004308 if (*op == '.')
4309 {
4310 if (may_generate_2STRING(-2, cctx) == FAIL
4311 || may_generate_2STRING(-1, cctx) == FAIL)
4312 return FAIL;
4313 generate_instr_drop(cctx, ISN_CONCAT, 1);
4314 }
4315 else
4316 generate_two_op(cctx, op);
4317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 }
4319
4320 return OK;
4321}
4322
4323/*
4324 * expr5a == expr5b
4325 * expr5a =~ expr5b
4326 * expr5a != expr5b
4327 * expr5a !~ expr5b
4328 * expr5a > expr5b
4329 * expr5a >= expr5b
4330 * expr5a < expr5b
4331 * expr5a <= expr5b
4332 * expr5a is expr5b
4333 * expr5a isnot expr5b
4334 *
4335 * Produces instructions:
4336 * EVAL expr5a Push result of "expr5a"
4337 * EVAL expr5b Push result of "expr5b"
4338 * COMPARE one of the compare instructions
4339 */
4340 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004341compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342{
4343 exptype_T type = EXPR_UNKNOWN;
4344 char_u *p;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004345 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 int len = 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 int type_is = FALSE;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004348 int ppconst_used = ppconst->pp_used;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349
4350 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004351 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 return FAIL;
4353
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004354 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar080457c2020-03-03 21:53:32 +01004355 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004356
4357 /*
4358 * If there is a comparative operator, use it.
4359 */
4360 if (type != EXPR_UNKNOWN)
4361 {
4362 int ic = FALSE; // Default: do not ignore case
4363
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004364 if (next != NULL)
4365 {
4366 *arg = next_line_from_context(cctx, TRUE);
4367 p = skipwhite(*arg);
4368 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 if (type_is && (p[len] == '?' || p[len] == '#'))
4370 {
4371 semsg(_(e_invexpr2), *arg);
4372 return FAIL;
4373 }
4374 // extra question mark appended: ignore case
4375 if (p[len] == '?')
4376 {
4377 ic = TRUE;
4378 ++len;
4379 }
4380 // extra '#' appended: match case (ignored)
4381 else if (p[len] == '#')
4382 ++len;
4383 // nothing appended: match case
4384
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004385 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004387 error_white_both(p, len);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004388 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390
4391 // get the second variable
4392 *arg = skipwhite(p + len);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004393 if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004394 return FAIL;
4395
Bram Moolenaara5565e42020-05-09 15:44:01 +02004396 if (compile_expr5(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 return FAIL;
4398
Bram Moolenaara5565e42020-05-09 15:44:01 +02004399 if (ppconst->pp_used == ppconst_used + 2)
4400 {
4401 typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
4402 typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
4403 int ret;
4404
4405 // Both sides are a constant, compute the result now.
4406 // First check for a valid combination of types, this is more
4407 // strict than typval_compare().
Bram Moolenaar543e6f32020-07-10 22:45:38 +02004408 if (check_compare_types(type, tv1, tv2) == FAIL)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004409 ret = FAIL;
4410 else
4411 {
4412 ret = typval_compare(tv1, tv2, type, ic);
4413 tv1->v_type = VAR_BOOL;
4414 tv1->vval.v_number = tv1->vval.v_number
4415 ? VVAL_TRUE : VVAL_FALSE;
4416 clear_tv(tv2);
4417 --ppconst->pp_used;
4418 }
4419 return ret;
4420 }
4421
4422 generate_ppconst(cctx, ppconst);
4423 return generate_COMPARE(cctx, type, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 }
4425
4426 return OK;
4427}
4428
Bram Moolenaar7f141552020-05-09 17:35:53 +02004429static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
4430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431/*
4432 * Compile || or &&.
4433 */
4434 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004435compile_and_or(
4436 char_u **arg,
4437 cctx_T *cctx,
4438 char *op,
4439 ppconst_T *ppconst,
4440 int ppconst_used UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441{
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004442 char_u *next;
4443 char_u *p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 int opchar = *op;
4445
4446 if (p[0] == opchar && p[1] == opchar)
4447 {
4448 garray_T *instr = &cctx->ctx_instr;
4449 garray_T end_ga;
4450
4451 /*
4452 * Repeat until there is no following "||" or "&&"
4453 */
4454 ga_init2(&end_ga, sizeof(int), 10);
4455 while (p[0] == opchar && p[1] == opchar)
4456 {
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004457 if (next != NULL)
4458 {
4459 *arg = next_line_from_context(cctx, TRUE);
4460 p = skipwhite(*arg);
4461 }
4462
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004463 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
4464 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004465 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004466 return FAIL;
4467 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004469 // TODO: use ppconst if the value is a constant and check
4470 // evaluating to bool
Bram Moolenaara5565e42020-05-09 15:44:01 +02004471 generate_ppconst(cctx, ppconst);
4472
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004473 // Every part must evaluate to a bool.
4474 if (bool_on_stack(cctx) == FAIL)
4475 {
4476 ga_clear(&end_ga);
4477 return FAIL;
4478 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 if (ga_grow(&end_ga, 1) == FAIL)
4481 {
4482 ga_clear(&end_ga);
4483 return FAIL;
4484 }
4485 *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
4486 ++end_ga.ga_len;
4487 generate_JUMP(cctx, opchar == '|'
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004488 ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489
4490 // eval the next expression
4491 *arg = skipwhite(p + 2);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004492 if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004493 {
4494 ga_clear(&end_ga);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004495 return FAIL;
Bram Moolenaar8bb0f542020-12-06 16:03:55 +01004496 }
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004497
Bram Moolenaara5565e42020-05-09 15:44:01 +02004498 if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
4499 : compile_expr4(arg, cctx, ppconst)) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 {
4501 ga_clear(&end_ga);
4502 return FAIL;
4503 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004504
4505 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004507 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004509 // Every part must evaluate to a bool.
4510 if (bool_on_stack(cctx) == FAIL)
4511 {
4512 ga_clear(&end_ga);
4513 return FAIL;
4514 }
4515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 // Fill in the end label in all jumps.
4517 while (end_ga.ga_len > 0)
4518 {
4519 isn_T *isn;
4520
4521 --end_ga.ga_len;
4522 isn = ((isn_T *)instr->ga_data)
4523 + *(((int *)end_ga.ga_data) + end_ga.ga_len);
4524 isn->isn_arg.jump.jump_where = instr->ga_len;
4525 }
4526 ga_clear(&end_ga);
4527 }
4528
4529 return OK;
4530}
4531
4532/*
4533 * expr4a && expr4a && expr4a logical AND
4534 *
4535 * Produces instructions:
4536 * EVAL expr4a Push result of "expr4a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004537 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004538 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539 * EVAL expr4b Push result of "expr4b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004540 * JUMP_IF_COND_FALSE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 * EVAL expr4c Push result of "expr4c"
4542 * end:
4543 */
4544 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004545compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004547 int ppconst_used = ppconst->pp_used;
4548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 // get the first variable
Bram Moolenaara5565e42020-05-09 15:44:01 +02004550 if (compile_expr4(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 return FAIL;
4552
4553 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004554 return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555}
4556
4557/*
4558 * expr3a || expr3b || expr3c logical OR
4559 *
4560 * Produces instructions:
4561 * EVAL expr3a Push result of "expr3a"
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004562 * COND2BOOL convert to bool if needed
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004563 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 * EVAL expr3b Push result of "expr3b"
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004565 * JUMP_IF_COND_TRUE end
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 * EVAL expr3c Push result of "expr3c"
4567 * end:
4568 */
4569 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004570compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571{
Bram Moolenaara5565e42020-05-09 15:44:01 +02004572 int ppconst_used = ppconst->pp_used;
4573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 // eval the first expression
Bram Moolenaara5565e42020-05-09 15:44:01 +02004575 if (compile_expr3(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 return FAIL;
4577
4578 // || and && work almost the same
Bram Moolenaara5565e42020-05-09 15:44:01 +02004579 return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580}
4581
4582/*
4583 * Toplevel expression: expr2 ? expr1a : expr1b
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 * Produces instructions:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004585 * EVAL expr2 Push result of "expr2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 * JUMP_IF_FALSE alt jump if false
4587 * EVAL expr1a
4588 * JUMP_ALWAYS end
4589 * alt: EVAL expr1b
4590 * end:
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004591 *
4592 * Toplevel expression: expr2 ?? expr1
4593 * Produces instructions:
4594 * EVAL expr2 Push result of "expr2"
4595 * JUMP_AND_KEEP_IF_TRUE end jump if true
4596 * EVAL expr1
4597 * end:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 */
4599 static int
Bram Moolenaara5565e42020-05-09 15:44:01 +02004600compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601{
4602 char_u *p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004603 int ppconst_used = ppconst->pp_used;
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004604 char_u *next;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605
Bram Moolenaar3988f642020-08-27 22:43:03 +02004606 // Ignore all kinds of errors when not producing code.
4607 if (cctx->ctx_skip == SKIP_YES)
4608 {
Bram Moolenaar683581e2020-10-22 21:22:58 +02004609 evalarg_T evalarg;
4610
4611 CLEAR_FIELD(evalarg);
4612 evalarg.eval_cctx = cctx;
4613 skip_expr(arg, &evalarg);
Bram Moolenaar3988f642020-08-27 22:43:03 +02004614 return OK;
4615 }
4616
Bram Moolenaar61a89812020-05-07 16:58:17 +02004617 // Evaluate the first expression.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004618 if (compile_expr2(arg, cctx, ppconst) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 return FAIL;
4620
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004621 p = may_peek_next_line(cctx, *arg, &next);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 if (*p == '?')
4623 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004624 int op_falsy = p[1] == '?';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 garray_T *instr = &cctx->ctx_instr;
4626 garray_T *stack = &cctx->ctx_type_stack;
4627 int alt_idx = instr->ga_len;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004628 int end_idx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 isn_T *isn;
Bram Moolenaar38041da2020-06-21 22:17:18 +02004630 type_T *type1 = NULL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004631 int has_const_expr = FALSE;
4632 int const_value = FALSE;
4633 int save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004634
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004635 if (next != NULL)
4636 {
4637 *arg = next_line_from_context(cctx, TRUE);
4638 p = skipwhite(*arg);
4639 }
4640
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004641 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004642 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004643 semsg(_(e_white_space_required_before_and_after_str),
4644 op_falsy ? "??" : "?");
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004645 return FAIL;
4646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647
Bram Moolenaara5565e42020-05-09 15:44:01 +02004648 if (ppconst->pp_used == ppconst_used + 1)
4649 {
4650 // the condition is a constant, we know whether the ? or the :
4651 // expression is to be evaluated.
4652 has_const_expr = TRUE;
Bram Moolenaar13106602020-10-04 16:06:05 +02004653 if (op_falsy)
4654 const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
4655 else
4656 {
4657 int error = FALSE;
4658
4659 const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
4660 &error);
4661 if (error)
4662 return FAIL;
4663 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004664 cctx->ctx_skip = save_skip == SKIP_YES ||
4665 (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
4666
4667 if (op_falsy && cctx->ctx_skip == SKIP_YES)
4668 // "left ?? right" and "left" is truthy: produce "left"
4669 generate_ppconst(cctx, ppconst);
4670 else
4671 {
4672 clear_tv(&ppconst->pp_tv[ppconst_used]);
4673 --ppconst->pp_used;
4674 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004675 }
4676 else
4677 {
4678 generate_ppconst(cctx, ppconst);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004679 if (op_falsy)
4680 end_idx = instr->ga_len;
4681 generate_JUMP(cctx, op_falsy
4682 ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
4683 if (op_falsy)
4684 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaara5565e42020-05-09 15:44:01 +02004685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686
4687 // evaluate the second expression; any type is accepted
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004688 *arg = skipwhite(p + 1 + op_falsy);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004689 if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +02004690 return FAIL;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004691 if (compile_expr1(arg, cctx, ppconst) == FAIL)
Bram Moolenaara6d53682020-01-28 23:04:06 +01004692 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693
Bram Moolenaara5565e42020-05-09 15:44:01 +02004694 if (!has_const_expr)
4695 {
4696 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004698 if (!op_falsy)
4699 {
4700 // remember the type and drop it
4701 --stack->ga_len;
4702 type1 = ((type_T **)stack->ga_data)[stack->ga_len];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004704 end_idx = instr->ga_len;
4705 generate_JUMP(cctx, JUMP_ALWAYS, 0);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004706
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004707 // jump here from JUMP_IF_FALSE
4708 isn = ((isn_T *)instr->ga_data) + alt_idx;
4709 isn->isn_arg.jump.jump_where = instr->ga_len;
4710 }
Bram Moolenaara5565e42020-05-09 15:44:01 +02004711 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004713 if (!op_falsy)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004714 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004715 // Check for the ":".
4716 p = may_peek_next_line(cctx, *arg, &next);
4717 if (*p != ':')
4718 {
4719 emsg(_(e_missing_colon));
4720 return FAIL;
4721 }
4722 if (next != NULL)
4723 {
4724 *arg = next_line_from_context(cctx, TRUE);
4725 p = skipwhite(*arg);
4726 }
Bram Moolenaar67fbdfe2020-06-23 22:26:05 +02004727
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004728 if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
4729 {
4730 semsg(_(e_white_space_required_before_and_after_str), ":");
4731 return FAIL;
4732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004734 // evaluate the third expression
4735 if (has_const_expr)
4736 cctx->ctx_skip = save_skip == SKIP_YES || const_value
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004737 ? SKIP_YES : SKIP_NOT;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004738 *arg = skipwhite(p + 1);
Bram Moolenaar918a4242020-12-06 14:37:08 +01004739 if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004740 return FAIL;
4741 if (compile_expr1(arg, cctx, ppconst) == FAIL)
4742 return FAIL;
4743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744
Bram Moolenaara5565e42020-05-09 15:44:01 +02004745 if (!has_const_expr)
4746 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004747 type_T **typep;
4748
Bram Moolenaara5565e42020-05-09 15:44:01 +02004749 generate_ppconst(cctx, ppconst);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750
Bram Moolenaara5565e42020-05-09 15:44:01 +02004751 // If the types differ, the result has a more generic type.
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004752 typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
4753 common_type(type1, *typep, typep, cctx->ctx_type_list);
Bram Moolenaara5565e42020-05-09 15:44:01 +02004754
Bram Moolenaar92f26c22020-10-03 20:17:30 +02004755 // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
Bram Moolenaara5565e42020-05-09 15:44:01 +02004756 isn = ((isn_T *)instr->ga_data) + end_idx;
4757 isn->isn_arg.jump.jump_where = instr->ga_len;
4758 }
4759
4760 cctx->ctx_skip = save_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 }
4762 return OK;
4763}
4764
4765/*
Bram Moolenaara5565e42020-05-09 15:44:01 +02004766 * Toplevel expression.
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004767 * Sets "is_const" (if not NULL) to indicate the value is a constant.
4768 * Returns OK or FAIL.
Bram Moolenaara5565e42020-05-09 15:44:01 +02004769 */
4770 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004771compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
Bram Moolenaara5565e42020-05-09 15:44:01 +02004772{
4773 ppconst_T ppconst;
4774
4775 CLEAR_FIELD(ppconst);
4776 if (compile_expr1(arg, cctx, &ppconst) == FAIL)
4777 {
4778 clear_ppconst(&ppconst);
4779 return FAIL;
4780 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004781 if (is_const != NULL)
4782 *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
Bram Moolenaara5565e42020-05-09 15:44:01 +02004783 if (generate_ppconst(cctx, &ppconst) == FAIL)
4784 return FAIL;
4785 return OK;
4786}
4787
4788/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004789 * Toplevel expression.
4790 */
4791 static int
4792compile_expr0(char_u **arg, cctx_T *cctx)
4793{
4794 return compile_expr0_ext(arg, cctx, NULL);
4795}
4796
4797/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 * compile "return [expr]"
4799 */
4800 static char_u *
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004801compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802{
4803 char_u *p = arg;
4804 garray_T *stack = &cctx->ctx_type_stack;
4805 type_T *stack_type;
4806
4807 if (*p != NUL && *p != '|' && *p != '\n')
4808 {
4809 // compile return argument into instructions
Bram Moolenaara5565e42020-05-09 15:44:01 +02004810 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004811 return NULL;
4812
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004813 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar05a55512020-07-05 15:52:19 +02004814 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004815 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004816 if (check_return_type && cctx->ctx_ufunc->uf_ret_type == NULL)
4817 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004818 cctx->ctx_ufunc->uf_ret_type = stack_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004819 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004820 else
Bram Moolenaar05a55512020-07-05 15:52:19 +02004821 {
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004822 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4823 && stack_type->tt_type != VAR_VOID
4824 && stack_type->tt_type != VAR_UNKNOWN)
4825 {
4826 emsg(_(e_returning_value_in_function_without_return_type));
4827 return NULL;
4828 }
4829 if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02004830 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004831 return NULL;
4832 }
Bram Moolenaar05a55512020-07-05 15:52:19 +02004833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 }
4835 else
4836 {
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004837 // "check_return_type" cannot be TRUE, only used for a lambda which
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02004838 // always has an argument.
Bram Moolenaar4c683752020-04-05 21:38:23 +02004839 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4840 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004842 emsg(_(e_missing_return_value));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 return NULL;
4844 }
4845
4846 // No argument, return zero.
4847 generate_PUSHNR(cctx, 0);
4848 }
Bram Moolenaar8e02faf2020-11-18 16:35:02 +01004849 if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 return NULL;
4851
4852 // "return val | endif" is possible
4853 return skipwhite(p);
4854}
4855
4856/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004857 * Get a line from the compilation context, compatible with exarg_T getline().
4858 * Return a pointer to the line in allocated memory.
4859 * Return NULL for end-of-file or some error.
4860 */
4861 static char_u *
4862exarg_getline(
4863 int c UNUSED,
4864 void *cookie,
4865 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004866 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004867{
4868 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +02004869 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004870
Bram Moolenaar66250c92020-08-20 15:02:42 +02004871 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +02004872 {
Bram Moolenaar2914a202020-09-27 18:24:03 +02004873 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +02004874 return NULL;
4875 ++cctx->ctx_lnum;
4876 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
4877 // Comment lines result in NULL pointers, skip them.
4878 if (p != NULL)
4879 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004880 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004881}
4882
4883/*
4884 * Compile a nested :def command.
4885 */
4886 static char_u *
4887compile_nested_function(exarg_T *eap, cctx_T *cctx)
4888{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004889 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02004890 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004891 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004892 char_u *lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004893 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004894 int r = FAIL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004895
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004896 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02004897 {
4898 emsg(_(e_cannot_use_bang_with_nested_def));
4899 return NULL;
4900 }
4901
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004902 if (*name_start == '/')
4903 {
4904 name_end = skip_regexp(name_start + 1, '/', TRUE);
4905 if (*name_end == '/')
4906 ++name_end;
4907 eap->nextcmd = check_nextcmd(name_end);
4908 }
4909 if (name_end == name_start || *skipwhite(name_end) != '(')
4910 {
4911 if (!ends_excmd2(name_start, name_end))
4912 {
4913 semsg(_(e_invalid_command_str), eap->cmd);
4914 return NULL;
4915 }
4916
4917 // "def" or "def Name": list functions
4918 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
4919 return NULL;
4920 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
4921 }
4922
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004923 // Only g:Func() can use a namespace.
4924 if (name_start[1] == ':' && !is_global)
4925 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004926 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02004927 return NULL;
4928 }
Bram Moolenaareef21022020-08-01 22:16:43 +02004929 if (check_defined(name_start, name_end - name_start, cctx) == FAIL)
4930 return NULL;
4931
Bram Moolenaar04b12692020-05-04 23:24:44 +02004932 eap->arg = name_end;
4933 eap->getline = exarg_getline;
4934 eap->cookie = cctx;
Bram Moolenaar9b68c822020-06-18 19:31:08 +02004935 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004936 eap->forceit = FALSE;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004937 lambda_name = vim_strsave(get_lambda_name());
4938 if (lambda_name == NULL)
4939 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004940 ufunc = define_function(eap, lambda_name);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004941
Bram Moolenaar822ba242020-05-24 23:00:18 +02004942 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004943 {
4944 r = eap->skip ? OK : FAIL;
4945 goto theend;
4946 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004947 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02004948 && compile_def_function(ufunc, TRUE, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004949 {
4950 func_ptr_unref(ufunc);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004951 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02004952 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02004953
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004954 if (is_global)
4955 {
4956 char_u *func_name = vim_strnsave(name_start + 2,
4957 name_end - name_start - 2);
Bram Moolenaar0e65d3d2020-05-05 17:53:16 +02004958
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004959 if (func_name == NULL)
4960 r = FAIL;
4961 else
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004962 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004963 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004964 lambda_name = NULL;
4965 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004966 }
4967 else
4968 {
4969 // Define a local variable for the function reference.
Bram Moolenaare8211a32020-10-09 22:04:29 +02004970 lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004971 TRUE, ufunc->uf_func_type);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004972 int block_depth = cctx->ctx_ufunc->uf_block_depth;
Bram Moolenaare8211a32020-10-09 22:04:29 +02004973
Bram Moolenaareef21022020-08-01 22:16:43 +02004974 if (lvar == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004975 goto theend;
Bram Moolenaar5a849da2020-08-08 16:47:30 +02004976 if (generate_FUNCREF(cctx, ufunc) == FAIL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004977 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004978 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004979
4980 // copy over the block scope IDs
4981 if (block_depth > 0)
4982 {
4983 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
4984 if (ufunc->uf_block_ids != NULL)
4985 {
4986 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
4987 sizeof(int) * block_depth);
4988 ufunc->uf_block_depth = block_depth;
4989 }
4990 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004991 }
Bram Moolenaar61a89812020-05-07 16:58:17 +02004992 // TODO: warning for trailing text?
Bram Moolenaar58a52f22020-12-22 18:56:55 +01004993 r = OK;
4994
4995theend:
4996 vim_free(lambda_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004997 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02004998}
4999
5000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 * Return the length of an assignment operator, or zero if there isn't one.
5002 */
5003 int
5004assignment_len(char_u *p, int *heredoc)
5005{
5006 if (*p == '=')
5007 {
5008 if (p[1] == '<' && p[2] == '<')
5009 {
5010 *heredoc = TRUE;
5011 return 3;
5012 }
5013 return 1;
5014 }
5015 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
5016 return 2;
5017 if (STRNCMP(p, "..=", 3) == 0)
5018 return 3;
5019 return 0;
5020}
5021
5022// words that cannot be used as a variable
5023static char *reserved[] = {
5024 "true",
5025 "false",
5026 NULL
5027};
5028
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005029typedef enum {
5030 dest_local,
5031 dest_option,
5032 dest_env,
5033 dest_global,
Bram Moolenaard3aac292020-04-19 14:32:17 +02005034 dest_buffer,
5035 dest_window,
5036 dest_tab,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005037 dest_vimvar,
5038 dest_script,
5039 dest_reg,
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005040 dest_expr,
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01005041} assign_dest_T;
5042
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005044 * Generate the load instruction for "name".
5045 */
5046 static void
5047generate_loadvar(
5048 cctx_T *cctx,
5049 assign_dest_T dest,
5050 char_u *name,
5051 lvar_T *lvar,
5052 type_T *type)
5053{
5054 switch (dest)
5055 {
5056 case dest_option:
5057 // TODO: check the option exists
5058 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
5059 break;
5060 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01005061 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
5062 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
5063 else
5064 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005065 break;
5066 case dest_buffer:
5067 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
5068 break;
5069 case dest_window:
5070 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
5071 break;
5072 case dest_tab:
5073 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
5074 break;
5075 case dest_script:
5076 compile_load_scriptvar(cctx,
5077 name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
5078 break;
5079 case dest_env:
5080 // Include $ in the name here
5081 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
5082 break;
5083 case dest_reg:
5084 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
5085 break;
5086 case dest_vimvar:
5087 generate_LOADV(cctx, name + 2, TRUE);
5088 break;
5089 case dest_local:
5090 if (lvar->lv_from_outer)
5091 generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
5092 NULL, type);
5093 else
5094 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
5095 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005096 case dest_expr:
5097 // list or dict value should already be on the stack.
5098 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005099 }
5100}
5101
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005102/*
5103 * Skip over "[expr]" or ".member".
5104 * Does not check for any errors.
5105 */
5106 static char_u *
5107skip_index(char_u *start)
5108{
5109 char_u *p = start;
5110
5111 if (*p == '[')
5112 {
5113 p = skipwhite(p + 1);
5114 (void)skip_expr(&p, NULL);
5115 p = skipwhite(p);
5116 if (*p == ']')
5117 return p + 1;
5118 return p;
5119 }
5120 // if (*p == '.')
5121 return to_name_end(p + 1, TRUE);
5122}
5123
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005124 void
5125vim9_declare_error(char_u *name)
5126{
5127 char *scope = "";
5128
5129 switch (*name)
5130 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005131 case 'g': scope = _("global"); break;
5132 case 'b': scope = _("buffer"); break;
5133 case 'w': scope = _("window"); break;
5134 case 't': scope = _("tab"); break;
5135 case 'v': scope = "v:"; break;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005136 case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005137 return;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005138 case '&': semsg(_(e_cannot_declare_an_option), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005139 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005140 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005141 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02005142 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005143 }
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005144 semsg(_(e_cannot_declare_a_scope_variable), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02005145}
5146
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005147/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005148 * For one assignment figure out the type of destination. Return it in "dest".
5149 * When not recognized "dest" is not set.
5150 * For an option "opt_flags" is set.
5151 * For a v:var "vimvaridx" is set.
5152 * "type" is set to the destination type if known, unchanted otherwise.
5153 * Return FAIL if an error message was given.
5154 */
5155 static int
5156get_var_dest(
5157 char_u *name,
5158 assign_dest_T *dest,
5159 int cmdidx,
5160 int *opt_flags,
5161 int *vimvaridx,
5162 type_T **type,
5163 cctx_T *cctx)
5164{
5165 char_u *p;
5166
5167 if (*name == '&')
5168 {
5169 int cc;
5170 long numval;
5171 int opt_type;
5172
5173 *dest = dest_option;
5174 if (cmdidx == CMD_final || cmdidx == CMD_const)
5175 {
5176 emsg(_(e_const_option));
5177 return FAIL;
5178 }
5179 p = name;
5180 p = find_option_end(&p, opt_flags);
5181 if (p == NULL)
5182 {
5183 // cannot happen?
5184 emsg(_(e_letunexp));
5185 return FAIL;
5186 }
5187 cc = *p;
5188 *p = NUL;
5189 opt_type = get_option_value(skip_option_env_lead(name),
5190 &numval, NULL, *opt_flags);
5191 *p = cc;
5192 if (opt_type == -3)
5193 {
5194 semsg(_(e_unknown_option), name);
5195 return FAIL;
5196 }
5197 if (opt_type == -2 || opt_type == 0)
5198 *type = &t_string;
5199 else
5200 *type = &t_number; // both number and boolean option
5201 }
5202 else if (*name == '$')
5203 {
5204 *dest = dest_env;
5205 *type = &t_string;
5206 }
5207 else if (*name == '@')
5208 {
5209 if (!valid_yank_reg(name[1], FALSE) || name[1] == '.')
5210 {
5211 emsg_invreg(name[1]);
5212 return FAIL;
5213 }
5214 *dest = dest_reg;
5215 *type = &t_string;
5216 }
5217 else if (STRNCMP(name, "g:", 2) == 0)
5218 {
5219 *dest = dest_global;
5220 }
5221 else if (STRNCMP(name, "b:", 2) == 0)
5222 {
5223 *dest = dest_buffer;
5224 }
5225 else if (STRNCMP(name, "w:", 2) == 0)
5226 {
5227 *dest = dest_window;
5228 }
5229 else if (STRNCMP(name, "t:", 2) == 0)
5230 {
5231 *dest = dest_tab;
5232 }
5233 else if (STRNCMP(name, "v:", 2) == 0)
5234 {
5235 typval_T *vtv;
5236 int di_flags;
5237
5238 *vimvaridx = find_vim_var(name + 2, &di_flags);
5239 if (*vimvaridx < 0)
5240 {
5241 semsg(_(e_variable_not_found_str), name);
5242 return FAIL;
5243 }
5244 // We use the current value of "sandbox" here, is that OK?
5245 if (var_check_ro(di_flags, name, FALSE))
5246 return FAIL;
5247 *dest = dest_vimvar;
5248 vtv = get_vim_var_tv(*vimvaridx);
5249 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
5250 }
5251 return OK;
5252}
5253
5254/*
5255 * Generate a STORE instruction for "dest", not being "dest_local".
5256 * Return FAIL when out of memory.
5257 */
5258 static int
5259generate_store_var(
5260 cctx_T *cctx,
5261 assign_dest_T dest,
5262 int opt_flags,
5263 int vimvaridx,
5264 int scriptvar_idx,
5265 int scriptvar_sid,
5266 type_T *type,
5267 char_u *name)
5268{
5269 switch (dest)
5270 {
5271 case dest_option:
5272 return generate_STOREOPT(cctx, skip_option_env_lead(name),
5273 opt_flags);
5274 case dest_global:
5275 // include g: with the name, easier to execute that way
Bram Moolenaar03290b82020-12-19 16:30:44 +01005276 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
5277 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005278 case dest_buffer:
5279 // include b: with the name, easier to execute that way
5280 return generate_STORE(cctx, ISN_STOREB, 0, name);
5281 case dest_window:
5282 // include w: with the name, easier to execute that way
5283 return generate_STORE(cctx, ISN_STOREW, 0, name);
5284 case dest_tab:
5285 // include t: with the name, easier to execute that way
5286 return generate_STORE(cctx, ISN_STORET, 0, name);
5287 case dest_env:
5288 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
5289 case dest_reg:
5290 return generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
5291 case dest_vimvar:
5292 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
5293 case dest_script:
5294 if (scriptvar_idx < 0)
5295 {
5296 char_u *name_s = name;
Bram Moolenaar41d61962020-12-06 20:12:43 +01005297 int r;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005298
Bram Moolenaar41d61962020-12-06 20:12:43 +01005299 // "s:" is included in the name.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005300 r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s,
5301 scriptvar_sid, type);
5302 if (name_s != name)
5303 vim_free(name_s);
5304 return r;
5305 }
5306 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
5307 scriptvar_sid, scriptvar_idx, type);
5308 case dest_local:
5309 case dest_expr:
5310 // cannot happen
5311 break;
5312 }
5313 return FAIL;
5314}
5315
5316/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005317 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005318 * "let name"
5319 * "var name = expr"
5320 * "final name = expr"
5321 * "const name = expr"
5322 * "name = expr"
5323 * "arg" points to "name".
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 * Return NULL for an error.
5325 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005326 */
5327 static char_u *
5328compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
5329{
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005330 char_u *var_start;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 char_u *p;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005332 char_u *end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 char_u *ret = NULL;
5334 int var_count = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005335 int var_idx;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005336 int scriptvar_sid = 0;
5337 int scriptvar_idx = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338 int semicolon = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005340 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 char_u *op;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005342 int oplen = 0;
5343 int heredoc = FALSE;
Bram Moolenaara8c17702020-04-01 21:17:24 +02005344 type_T *type = &t_any;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005345 type_T *member_type = &t_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005346 type_T *rhs_type = &t_any;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005347 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 char_u *sp;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005349 int is_decl = cmdidx == CMD_let || cmdidx == CMD_var
5350 || cmdidx == CMD_final || cmdidx == CMD_const;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005352 // Skip over the "var" or "[var, var]" to get to any "=".
5353 p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
5354 if (p == NULL)
5355 return *arg == '[' ? arg : NULL;
5356
5357 if (var_count > 0 && is_decl)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005358 {
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005359 // TODO: should we allow this, and figure out type inference from list
5360 // members?
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005361 emsg(_(e_cannot_use_list_for_declaration));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 return NULL;
5363 }
5364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 sp = p;
5366 p = skipwhite(p);
5367 op = p;
5368 oplen = assignment_len(p, &heredoc);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005369
5370 if (var_count > 0 && oplen == 0)
5371 // can be something like "[1, 2]->func()"
5372 return arg;
5373
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005374 if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005376 error_white_both(op, oplen);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005377 return NULL;
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02005378 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 if (heredoc)
5381 {
5382 list_T *l;
5383 listitem_T *li;
5384
5385 // [let] varname =<< [trim] {end}
Bram Moolenaar04b12692020-05-04 23:24:44 +02005386 eap->getline = exarg_getline;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 eap->cookie = cctx;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02005388 l = heredoc_get(eap, op + 3, FALSE);
Bram Moolenaarc0e29012020-09-27 14:22:48 +02005389 if (l == NULL)
5390 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391
Bram Moolenaar078269b2020-09-21 20:35:55 +02005392 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 {
Bram Moolenaar078269b2020-09-21 20:35:55 +02005394 // Push each line and the create the list.
5395 FOR_ALL_LIST_ITEMS(l, li)
5396 {
5397 generate_PUSHS(cctx, li->li_tv.vval.v_string);
5398 li->li_tv.vval.v_string = NULL;
5399 }
5400 generate_NEWLIST(cctx, l->lv_len);
5401 type = &t_list_string;
5402 member_type = &t_list_string;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005404 list_free(l);
5405 p += STRLEN(p);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005406 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005407 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005408 else if (var_count > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005409 {
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005410 char_u *wp;
5411
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005412 // for "[var, var] = expr" evaluate the expression here, loop over the
5413 // list of variables below.
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005414 // A line break may follow the "=".
Bram Moolenaard25ec2c2020-03-30 21:05:45 +02005415
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005416 wp = op + oplen;
5417 p = skipwhite(wp);
Bram Moolenaar8ff16e02020-12-07 21:49:52 +01005418 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar004d9b02020-11-30 21:40:03 +01005419 return FAIL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005420 if (compile_expr0(&p, cctx) == FAIL)
5421 return NULL;
5422 end = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005424 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005425 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 type_T *stacktype;
5427
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005428 stacktype = stack->ga_len == 0 ? &t_void
5429 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005430 if (stacktype->tt_type == VAR_VOID)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005432 emsg(_(e_cannot_use_void_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005433 goto theend;
5434 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005435 if (need_type(stacktype, &t_list_any, -1, cctx,
5436 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005437 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02005438 // TODO: check the length of a constant list here
Bram Moolenaar9af78762020-06-16 11:34:42 +02005439 generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
5440 semicolon);
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005441 if (stacktype->tt_member != NULL)
5442 rhs_type = stacktype->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005443 }
5444 }
5445
5446 /*
5447 * Loop over variables in "[var, var] = expr".
5448 * For "var = expr" and "let var: type" this is done only once.
5449 */
5450 if (var_count > 0)
5451 var_start = skipwhite(arg + 1); // skip over the "["
5452 else
5453 var_start = arg;
5454 for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
5455 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005456 char_u *var_end;
5457 char_u *dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005458 size_t varlen;
5459 int new_local = FALSE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005460 assign_dest_T dest = dest_local;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005461 int opt_flags = 0;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005462 int vimvaridx = -1;
Bram Moolenaar709664c2020-12-12 14:33:41 +01005463 lvar_T local_lvar;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005464 lvar_T *lvar = NULL;
5465 lvar_T arg_lvar;
5466 int has_type = FALSE;
5467 int has_index = FALSE;
5468 int instr_count = -1;
5469
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005470 // "dest_end" is the end of the destination, including "[expr]" or
5471 // ".name".
5472 // "var_end" is the end of the variable/option/etc. name.
5473 dest_end = skip_var_one(var_start, FALSE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005474 if (*var_start == '@')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005475 var_end = var_start + 2;
Bram Moolenaar65821722020-08-02 18:58:54 +02005476 else
5477 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02005478 // skip over the leading "&", "&l:", "&g:" and "$"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005479 var_end = skip_option_env_lead(var_start);
5480 var_end = to_name_end(var_end, TRUE);
Bram Moolenaar65821722020-08-02 18:58:54 +02005481 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005482
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005483 // "a: type" is declaring variable "a" with a type, not dict "a:".
5484 if (is_decl && dest_end == var_start + 2 && dest_end[-1] == ':')
5485 --dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005486 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
5487 --var_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005488
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005489 // compute the length of the destination without "[expr]" or ".name"
5490 varlen = var_end - var_start;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005491 vim_free(name);
5492 name = vim_strnsave(var_start, varlen);
5493 if (name == NULL)
5494 return NULL;
5495 if (!heredoc)
5496 type = &t_any;
5497
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005498 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005499 {
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005500 int declare_error = FALSE;
5501
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005502 if (get_var_dest(name, &dest, cmdidx, &opt_flags,
5503 &vimvaridx, &type, cctx) == FAIL)
5504 goto theend;
5505 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005506 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005507 // Specific kind of variable recognized.
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005508 declare_error = is_decl;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005509 }
5510 else
5511 {
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005512 int idx;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005513
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005514 // No specific kind of variable recognized, just a name.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005515 for (idx = 0; reserved[idx] != NULL; ++idx)
5516 if (STRCMP(reserved[idx], name) == 0)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005517 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005518 semsg(_(e_cannot_use_reserved_name), name);
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005519 goto theend;
5520 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005521
Bram Moolenaar709664c2020-12-12 14:33:41 +01005522
5523 if (lookup_local(var_start, varlen, &local_lvar, cctx) == OK)
5524 lvar = &local_lvar;
5525 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005526 {
5527 CLEAR_FIELD(arg_lvar);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005528 if (arg_exists(var_start, varlen,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005529 &arg_lvar.lv_idx, &arg_lvar.lv_type,
5530 &arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005531 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005532 if (is_decl)
5533 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005534 semsg(_(e_str_is_used_as_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005535 goto theend;
5536 }
5537 lvar = &arg_lvar;
Bram Moolenaarec5929d2020-04-07 20:53:39 +02005538 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005539 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005540 if (lvar != NULL)
5541 {
5542 if (is_decl)
5543 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005544 semsg(_(e_variable_already_declared), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005545 goto theend;
5546 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005547 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005548 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005549 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005550 int script_namespace = varlen > 1
5551 && STRNCMP(var_start, "s:", 2) == 0;
5552 int script_var = (script_namespace
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005553 ? script_var_exists(var_start + 2, varlen - 2,
5554 FALSE, cctx)
5555 : script_var_exists(var_start, varlen,
5556 TRUE, cctx)) == OK;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005557 imported_T *import =
5558 find_imported(var_start, varlen, cctx);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02005559
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005560 if (script_namespace || script_var || import != NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005561 {
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005562 char_u *rawname = name + (name[1] == ':' ? 2 : 0);
5563
5564 if (is_decl)
5565 {
5566 if (script_namespace)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005567 semsg(_(e_cannot_declare_script_variable_in_function),
Bram Moolenaar33afa242020-07-29 19:18:00 +02005568 name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005569 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005570 semsg(_(e_variable_already_declared_in_script),
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005571 name);
5572 goto theend;
5573 }
5574 else if (cctx->ctx_ufunc->uf_script_ctx_version
5575 == SCRIPT_VERSION_VIM9
5576 && script_namespace
5577 && !script_var && import == NULL)
5578 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005579 semsg(_(e_unknown_variable_str), name);
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005580 goto theend;
5581 }
5582
5583 dest = dest_script;
5584
5585 // existing script-local variables should have a type
5586 scriptvar_sid = current_sctx.sc_sid;
5587 if (import != NULL)
5588 scriptvar_sid = import->imp_sid;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005589 if (SCRIPT_ID_VALID(scriptvar_sid))
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005590 {
Bram Moolenaare3d46852020-08-29 13:39:17 +02005591 scriptvar_idx = get_script_item_idx(scriptvar_sid,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005592 rawname, TRUE, cctx);
Bram Moolenaar06f9c692020-09-27 21:27:40 +02005593 if (scriptvar_idx >= 0)
Bram Moolenaare3d46852020-08-29 13:39:17 +02005594 {
5595 scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid);
5596 svar_T *sv =
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005597 ((svar_T *)si->sn_var_vals.ga_data)
5598 + scriptvar_idx;
Bram Moolenaare3d46852020-08-29 13:39:17 +02005599 type = sv->sv_type;
5600 }
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005601 }
5602 }
Bram Moolenaarfa211f32020-08-07 22:00:26 +02005603 else if (check_defined(var_start, varlen, cctx) == FAIL)
5604 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005605 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005606 }
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02005607
5608 if (declare_error)
5609 {
5610 vim9_declare_error(name);
5611 goto theend;
5612 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005613 }
5614
5615 // handle "a:name" as a name, not index "name" on "a"
5616 if (varlen > 1 || var_start[varlen] != ':')
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005617 var_end = dest_end;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005618
5619 if (dest != dest_option)
5620 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005621 if (is_decl && *var_end == ':')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005622 {
5623 // parse optional type: "let var: type = expr"
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005624 if (!VIM_ISWHITE(var_end[1]))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005625 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005626 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005627 goto theend;
5628 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005629 p = skipwhite(var_end + 1);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01005630 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5631 if (type == NULL)
5632 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005633 has_type = TRUE;
5634 }
5635 else if (lvar != NULL)
5636 type = lvar->lv_type;
5637 }
5638
5639 if (oplen == 3 && !heredoc && dest != dest_global
5640 && type->tt_type != VAR_STRING
5641 && type->tt_type != VAR_ANY)
5642 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005643 emsg(_(e_can_only_concatenate_to_string));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005644 goto theend;
5645 }
5646
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005647 if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005648 {
5649 if (oplen > 1 && !heredoc)
5650 {
5651 // +=, /=, etc. require an existing variable
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005652 semsg(_(e_cannot_use_operator_on_new_variable), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005653 goto theend;
5654 }
5655
5656 // new local variable
Bram Moolenaar98b4f142020-08-08 15:46:01 +02005657 if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
5658 && var_wrong_func_name(name, TRUE))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005659 goto theend;
5660 lvar = reserve_local(cctx, var_start, varlen,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005661 cmdidx == CMD_final || cmdidx == CMD_const, type);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005662 if (lvar == NULL)
5663 goto theend;
5664 new_local = TRUE;
5665 }
5666
5667 member_type = type;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01005668 if (dest_end > var_start + varlen)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005669 {
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005670 // Something follows after the variable: "var[idx]" or "var.key".
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005671 // TODO: should we also handle "->func()" here?
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005672 if (is_decl)
5673 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005674 emsg(_(e_cannot_use_index_when_declaring_variable));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005675 goto theend;
5676 }
5677
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005678 if (var_start[varlen] == '[' || var_start[varlen] == '.')
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005679 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005680 char_u *after = var_start + varlen;
5681
5682 // Only the last index is used below, if there are others
5683 // before it generate code for the expression. Thus for
5684 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
5685 for (;;)
5686 {
5687 p = skip_index(after);
5688 if (*p != '[' && *p != '.')
5689 break;
5690 after = p;
5691 }
5692 if (after > var_start + varlen)
5693 {
5694 varlen = after - var_start;
5695 dest = dest_expr;
5696 // We don't know the type before evaluating the expression,
5697 // use "any" until then.
5698 type = &t_any;
5699 }
5700
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005701 has_index = TRUE;
5702 if (type->tt_member == NULL)
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005703 member_type = &t_any;
5704 else
5705 member_type = type->tt_member;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005706 }
5707 else
5708 {
5709 semsg("Not supported yet: %s", var_start);
5710 goto theend;
5711 }
5712 }
5713 else if (lvar == &arg_lvar)
5714 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005715 semsg(_(e_cannot_assign_to_argument), name);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005716 goto theend;
5717 }
Bram Moolenaardbeecb22020-09-14 18:15:09 +02005718 if (!is_decl && lvar != NULL && lvar->lv_const && !has_index)
5719 {
5720 semsg(_(e_cannot_assign_to_constant), name);
5721 goto theend;
5722 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005723
5724 if (!heredoc)
5725 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005726 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005727 {
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005728 if (oplen > 0 && var_count == 0)
5729 {
5730 // skip over the "=" and the expression
5731 p = skipwhite(op + oplen);
5732 compile_expr0(&p, cctx);
5733 }
5734 }
5735 else if (oplen > 0)
5736 {
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005737 int is_const = FALSE;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005738 char_u *wp;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005739
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005740 // For "var = expr" evaluate the expression.
5741 if (var_count == 0)
5742 {
5743 int r;
5744
5745 // for "+=", "*=", "..=" etc. first load the current value
5746 if (*op != '=')
5747 {
5748 generate_loadvar(cctx, dest, name, lvar, type);
5749
5750 if (has_index)
5751 {
5752 // TODO: get member from list or dict
5753 emsg("Index with operation not supported yet");
5754 goto theend;
5755 }
5756 }
5757
5758 // Compile the expression. Temporarily hide the new local
5759 // variable here, it is not available to this expression.
5760 if (new_local)
5761 --cctx->ctx_locals.ga_len;
5762 instr_count = instr->ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005763 wp = op + oplen;
5764 p = skipwhite(wp);
5765 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
Bram Moolenaar21e51222020-12-04 12:43:29 +01005766 {
5767 if (new_local)
5768 ++cctx->ctx_locals.ga_len;
Bram Moolenaar7f764942020-12-02 15:11:18 +01005769 goto theend;
Bram Moolenaar21e51222020-12-04 12:43:29 +01005770 }
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005771 r = compile_expr0_ext(&p, cctx, &is_const);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005772 if (new_local)
5773 ++cctx->ctx_locals.ga_len;
5774 if (r == FAIL)
5775 goto theend;
5776 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02005777 else if (semicolon && var_idx == var_count - 1)
5778 {
5779 // For "[var; var] = expr" get the rest of the list
5780 if (generate_SLICE(cctx, var_count - 1) == FAIL)
5781 goto theend;
5782 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005783 else
5784 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005785 // For "[var, var] = expr" get the "var_idx" item from the
5786 // list.
5787 if (generate_GETITEM(cctx, var_idx) == FAIL)
Bram Moolenaar7f764942020-12-02 15:11:18 +01005788 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005789 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005790
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005791 rhs_type = stack->ga_len == 0 ? &t_void
Bram Moolenaar6802cce2020-07-19 15:49:49 +02005792 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005793 if (lvar != NULL && (is_decl || !has_type))
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005794 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005795 if ((rhs_type->tt_type == VAR_FUNC
5796 || rhs_type->tt_type == VAR_PARTIAL)
Bram Moolenaar0f769812020-09-12 18:32:34 +02005797 && var_wrong_func_name(name, TRUE))
5798 goto theend;
5799
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005800 if (new_local && !has_type)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005801 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005802 if (rhs_type->tt_type == VAR_VOID)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005803 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005804 emsg(_(e_cannot_use_void_value));
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005805 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005806 }
5807 else
5808 {
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005809 // An empty list or dict has a &t_unknown member,
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005810 // for a variable that implies &t_any.
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005811 if (rhs_type == &t_list_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005812 lvar->lv_type = &t_list_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005813 else if (rhs_type == &t_dict_empty)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005814 lvar->lv_type = &t_dict_any;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005815 else if (rhs_type == &t_unknown)
Bram Moolenaar04bdd572020-09-23 13:25:32 +02005816 lvar->lv_type = &t_any;
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005817 else
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005818 lvar->lv_type = rhs_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005819 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005820 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005821 else if (*op == '=')
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005822 {
5823 type_T *use_type = lvar->lv_type;
5824
Bram Moolenaar71abe482020-09-14 22:28:30 +02005825 // without operator check type here, otherwise below
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005826 if (has_index)
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005827 use_type = member_type;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005828 if (need_type(rhs_type, use_type, -1, cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005829 FALSE, is_const) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005830 goto theend;
5831 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005832 }
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005833 else if (*p != '=' && need_type(rhs_type, member_type, -1,
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005834 cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005835 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02005837 else if (cmdidx == CMD_final)
5838 {
5839 emsg(_(e_final_requires_a_value));
5840 goto theend;
5841 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005842 else if (cmdidx == CMD_const)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005843 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005844 emsg(_(e_const_requires_a_value));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005845 goto theend;
5846 }
5847 else if (!has_type || dest == dest_option)
5848 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02005849 emsg(_(e_type_or_initialization_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005850 goto theend;
5851 }
5852 else
5853 {
5854 // variables are always initialized
5855 if (ga_grow(instr, 1) == FAIL)
5856 goto theend;
5857 switch (member_type->tt_type)
5858 {
5859 case VAR_BOOL:
5860 generate_PUSHBOOL(cctx, VVAL_FALSE);
5861 break;
5862 case VAR_FLOAT:
5863#ifdef FEAT_FLOAT
5864 generate_PUSHF(cctx, 0.0);
5865#endif
5866 break;
5867 case VAR_STRING:
5868 generate_PUSHS(cctx, NULL);
5869 break;
5870 case VAR_BLOB:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005871 generate_PUSHBLOB(cctx, blob_alloc());
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005872 break;
5873 case VAR_FUNC:
5874 generate_PUSHFUNC(cctx, NULL, &t_func_void);
5875 break;
5876 case VAR_LIST:
5877 generate_NEWLIST(cctx, 0);
5878 break;
5879 case VAR_DICT:
5880 generate_NEWDICT(cctx, 0);
5881 break;
5882 case VAR_JOB:
5883 generate_PUSHJOB(cctx, NULL);
5884 break;
5885 case VAR_CHANNEL:
5886 generate_PUSHCHANNEL(cctx, NULL);
5887 break;
5888 case VAR_NUMBER:
5889 case VAR_UNKNOWN:
5890 case VAR_ANY:
5891 case VAR_PARTIAL:
5892 case VAR_VOID:
5893 case VAR_SPECIAL: // cannot happen
5894 generate_PUSHNR(cctx, 0);
5895 break;
5896 }
5897 }
5898 if (var_count == 0)
5899 end = p;
5900 }
5901
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005902 // no need to parse more when skipping
Bram Moolenaar9b68c822020-06-18 19:31:08 +02005903 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaar72abcf42020-06-18 18:26:24 +02005904 break;
5905
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005906 if (oplen > 0 && *op != '=')
5907 {
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005908 type_T *expected;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005909 type_T *stacktype;
5910
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005911 if (*op == '.')
5912 expected = &t_string;
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005913 else
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005914 expected = member_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005915 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005916 if (
5917#ifdef FEAT_FLOAT
5918 // If variable is float operation with number is OK.
5919 !(expected == &t_float && stacktype == &t_number) &&
5920#endif
Bram Moolenaar334a8b42020-10-19 16:07:42 +02005921 need_type(stacktype, expected, -1, cctx,
5922 FALSE, FALSE) == FAIL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005923 goto theend;
5924
5925 if (*op == '.')
Bram Moolenaardd29f1b2020-08-07 20:46:20 +02005926 {
5927 if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
5928 goto theend;
5929 }
5930 else if (*op == '+')
5931 {
5932 if (generate_add_instr(cctx,
5933 operator_type(member_type, stacktype),
5934 member_type, stacktype) == FAIL)
5935 goto theend;
5936 }
Bram Moolenaar93ad1472020-08-19 22:02:41 +02005937 else if (generate_two_op(cctx, op) == FAIL)
5938 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005940
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005941 if (has_index)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005942 {
Bram Moolenaard24602f2020-12-20 15:20:56 +01005943 int r;
5944 vartype_T dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005945
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005946 // Compile the "idx" in "var[idx]" or "key" in "var.key".
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005947 p = var_start + varlen;
5948 if (*p == '[')
5949 {
5950 p = skipwhite(p + 1);
5951 r = compile_expr0(&p, cctx);
5952 if (r == OK && *skipwhite(p) != ']')
5953 {
5954 // this should not happen
5955 emsg(_(e_missbrac));
5956 r = FAIL;
5957 }
5958 }
5959 else // if (*p == '.')
5960 {
5961 char_u *key_end = to_name_end(p + 1, TRUE);
5962 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
5963
5964 r = generate_PUSHS(cctx, key);
5965 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005966 if (r == FAIL)
5967 goto theend;
Bram Moolenaarfc74d032020-11-16 22:11:49 +01005968
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005969 if (type == &t_any)
5970 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005971 // Index on variable of unknown type: check at runtime.
5972 dest_type = VAR_ANY;
Bram Moolenaar2caa1592020-08-01 15:53:19 +02005973 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005974 else
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005975 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005976 dest_type = type->tt_type;
5977 if (dest_type == VAR_DICT
5978 && may_generate_2STRING(-1, cctx) == FAIL)
5979 goto theend;
5980 if (dest_type == VAR_LIST
5981 && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
5982 != VAR_NUMBER)
5983 {
5984 emsg(_(e_number_exp));
5985 goto theend;
5986 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005987 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005988
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005989 // Load the dict or list. On the stack we then have:
5990 // - value
5991 // - index
5992 // - variable
Bram Moolenaardc234ca2020-11-28 18:52:33 +01005993 if (dest == dest_expr)
5994 {
5995 int c = var_start[varlen];
5996
5997 // Evaluate "ll[expr]" of "ll[expr][idx]"
5998 p = var_start;
5999 var_start[varlen] = NUL;
6000 if (compile_expr0(&p, cctx) == OK && p != var_start + varlen)
6001 {
6002 // this should not happen
6003 emsg(_(e_missbrac));
6004 goto theend;
6005 }
6006 var_start[varlen] = c;
6007
6008 type = stack->ga_len == 0 ? &t_void
6009 : ((type_T **)stack->ga_data)[stack->ga_len - 1];
6010 // now we can properly check the type
6011 if (type->tt_member != NULL
6012 && need_type(rhs_type, type->tt_member, -2, cctx,
6013 FALSE, FALSE) == FAIL)
6014 goto theend;
6015 }
6016 else
6017 generate_loadvar(cctx, dest, name, lvar, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006018
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006019 if (dest_type == VAR_LIST || dest_type == VAR_DICT
6020 || dest_type == VAR_ANY)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006021 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006022 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
6023
6024 if (isn == NULL)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006025 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01006026 isn->isn_arg.vartype = dest_type;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006027 }
6028 else
6029 {
Bram Moolenaardc234ca2020-11-28 18:52:33 +01006030 emsg(_(e_indexable_type_required));
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006031 goto theend;
6032 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006033 }
6034 else
6035 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02006036 if (is_decl && cmdidx == CMD_const
6037 && (dest == dest_script || dest == dest_local))
6038 // ":const var": lock the value, but not referenced variables
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02006039 generate_LOCKCONST(cctx);
6040
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006041 if (dest != dest_local)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006042 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006043 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6044 scriptvar_idx, scriptvar_sid, type, name) == FAIL)
6045 goto theend;
6046 }
6047 else if (lvar != NULL)
6048 {
6049 isn_T *isn = ((isn_T *)instr->ga_data)
6050 + instr->ga_len - 1;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006051
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006052 // optimization: turn "var = 123" from ISN_PUSHNR +
6053 // ISN_STORE into ISN_STORENR
6054 if (!lvar->lv_from_outer
6055 && instr->ga_len == instr_count + 1
6056 && isn->isn_type == ISN_PUSHNR)
6057 {
6058 varnumber_T val = isn->isn_arg.number;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006059
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006060 isn->isn_type = ISN_STORENR;
6061 isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
6062 isn->isn_arg.storenr.stnr_val = val;
6063 if (stack->ga_len > 0)
6064 --stack->ga_len;
6065 }
6066 else if (lvar->lv_from_outer)
6067 generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, NULL);
6068 else
6069 generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006070 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02006071 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006072
6073 if (var_idx + 1 < var_count)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006074 var_start = skipwhite(dest_end + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006075 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006076
6077 // for "[var, var] = expr" drop the "expr" value
Bram Moolenaar9af78762020-06-16 11:34:42 +02006078 if (var_count > 0 && !semicolon)
6079 {
Bram Moolenaarec792292020-12-13 21:26:56 +01006080 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02006081 goto theend;
6082 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02006083
Bram Moolenaarb2097502020-07-19 17:17:02 +02006084 ret = skipwhite(end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006085
6086theend:
6087 vim_free(name);
6088 return ret;
6089}
6090
6091/*
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006092 * Check if "name" can be "unlet".
6093 */
6094 int
6095check_vim9_unlet(char_u *name)
6096{
6097 if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
6098 {
Bram Moolenaar84367732020-08-23 15:21:55 +02006099 // "unlet s:var" is allowed in legacy script.
6100 if (*name == 's' && !script_is_vim9())
6101 return OK;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006102 semsg(_(e_cannot_unlet_str), name);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006103 return FAIL;
6104 }
6105 return OK;
6106}
6107
6108/*
6109 * Callback passed to ex_unletlock().
6110 */
6111 static int
6112compile_unlet(
6113 lval_T *lvp,
6114 char_u *name_end,
6115 exarg_T *eap,
6116 int deep UNUSED,
6117 void *coookie)
6118{
6119 cctx_T *cctx = coookie;
6120
6121 if (lvp->ll_tv == NULL)
6122 {
6123 char_u *p = lvp->ll_name;
6124 int cc = *name_end;
6125 int ret = OK;
6126
6127 // Normal name. Only supports g:, w:, t: and b: namespaces.
6128 *name_end = NUL;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006129 if (*p == '$')
6130 ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
6131 else if (check_vim9_unlet(p) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006132 ret = FAIL;
6133 else
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02006134 ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006135
6136 *name_end = cc;
6137 return ret;
6138 }
6139
6140 // TODO: unlet {list}[idx]
6141 // TODO: unlet {dict}[key]
6142 emsg("Sorry, :unlet not fully implemented yet");
6143 return FAIL;
6144}
6145
6146/*
6147 * compile "unlet var", "lock var" and "unlock var"
6148 * "arg" points to "var".
6149 */
6150 static char_u *
6151compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
6152{
6153 char_u *p = arg;
6154
6155 if (eap->cmdidx != CMD_unlet)
6156 {
6157 emsg("Sorry, :lock and unlock not implemented yet");
6158 return NULL;
6159 }
6160
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006161 ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
6162 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
6163}
6164
6165/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 * Compile an :import command.
6167 */
6168 static char_u *
6169compile_import(char_u *arg, cctx_T *cctx)
6170{
Bram Moolenaar1c991142020-07-04 13:15:31 +02006171 return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172}
6173
6174/*
6175 * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
6176 */
6177 static int
6178compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
6179{
6180 garray_T *instr = &cctx->ctx_instr;
6181 endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
6182
6183 if (endlabel == NULL)
6184 return FAIL;
6185 endlabel->el_next = *el;
6186 *el = endlabel;
6187 endlabel->el_end_label = instr->ga_len;
6188
6189 generate_JUMP(cctx, when, 0);
6190 return OK;
6191}
6192
6193 static void
6194compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
6195{
6196 garray_T *instr = &cctx->ctx_instr;
6197
6198 while (*el != NULL)
6199 {
6200 endlabel_T *cur = (*el);
6201 isn_T *isn;
6202
6203 isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
6204 isn->isn_arg.jump.jump_where = instr->ga_len;
6205 *el = cur->el_next;
6206 vim_free(cur);
6207 }
6208}
6209
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006210 static void
6211compile_free_jump_to_end(endlabel_T **el)
6212{
6213 while (*el != NULL)
6214 {
6215 endlabel_T *cur = (*el);
6216
6217 *el = cur->el_next;
6218 vim_free(cur);
6219 }
6220}
6221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006222/*
6223 * Create a new scope and set up the generic items.
6224 */
6225 static scope_T *
6226new_scope(cctx_T *cctx, scopetype_T type)
6227{
6228 scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
6229
6230 if (scope == NULL)
6231 return NULL;
6232 scope->se_outer = cctx->ctx_scope;
6233 cctx->ctx_scope = scope;
6234 scope->se_type = type;
6235 scope->se_local_count = cctx->ctx_locals.ga_len;
6236 return scope;
6237}
6238
6239/*
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006240 * Free the current scope and go back to the outer scope.
6241 */
6242 static void
6243drop_scope(cctx_T *cctx)
6244{
6245 scope_T *scope = cctx->ctx_scope;
6246
6247 if (scope == NULL)
6248 {
6249 iemsg("calling drop_scope() without a scope");
6250 return;
6251 }
6252 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar3cca2992020-04-02 22:57:36 +02006253 switch (scope->se_type)
6254 {
6255 case IF_SCOPE:
6256 compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
6257 case FOR_SCOPE:
6258 compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
6259 case WHILE_SCOPE:
6260 compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
6261 case TRY_SCOPE:
6262 compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
6263 case NO_SCOPE:
6264 case BLOCK_SCOPE:
6265 break;
6266 }
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006267 vim_free(scope);
6268}
6269
6270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 * compile "if expr"
6272 *
6273 * "if expr" Produces instructions:
6274 * EVAL expr Push result of "expr"
6275 * JUMP_IF_FALSE end
6276 * ... body ...
6277 * end:
6278 *
6279 * "if expr | else" Produces instructions:
6280 * EVAL expr Push result of "expr"
6281 * JUMP_IF_FALSE else
6282 * ... body ...
6283 * JUMP_ALWAYS end
6284 * else:
6285 * ... body ...
6286 * end:
6287 *
6288 * "if expr1 | elseif expr2 | else" Produces instructions:
6289 * EVAL expr Push result of "expr"
6290 * JUMP_IF_FALSE elseif
6291 * ... body ...
6292 * JUMP_ALWAYS end
6293 * elseif:
6294 * EVAL expr Push result of "expr"
6295 * JUMP_IF_FALSE else
6296 * ... body ...
6297 * JUMP_ALWAYS end
6298 * else:
6299 * ... body ...
6300 * end:
6301 */
6302 static char_u *
6303compile_if(char_u *arg, cctx_T *cctx)
6304{
6305 char_u *p = arg;
6306 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006307 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 scope_T *scope;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006309 skip_T skip_save = cctx->ctx_skip;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006310 ppconst_T ppconst;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311
Bram Moolenaara5565e42020-05-09 15:44:01 +02006312 CLEAR_FIELD(ppconst);
6313 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006314 {
Bram Moolenaara5565e42020-05-09 15:44:01 +02006315 clear_ppconst(&ppconst);
6316 return NULL;
6317 }
Bram Moolenaarefd88552020-06-18 20:50:10 +02006318 if (cctx->ctx_skip == SKIP_YES)
6319 clear_ppconst(&ppconst);
6320 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaara5565e42020-05-09 15:44:01 +02006321 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006322 int error = FALSE;
6323 int v;
6324
Bram Moolenaara5565e42020-05-09 15:44:01 +02006325 // The expression results in a constant.
Bram Moolenaar13106602020-10-04 16:06:05 +02006326 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
Bram Moolenaardda749c2020-10-04 17:24:29 +02006327 clear_ppconst(&ppconst);
Bram Moolenaar13106602020-10-04 16:06:05 +02006328 if (error)
6329 return NULL;
6330 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006331 }
6332 else
6333 {
6334 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006335 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006336 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006337 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006338 if (bool_on_stack(cctx) == FAIL)
6339 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006340 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341
6342 scope = new_scope(cctx, IF_SCOPE);
6343 if (scope == NULL)
6344 return NULL;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006345 scope->se_skip_save = skip_save;
6346 // "is_had_return" will be reset if any block does not end in :return
6347 scope->se_u.se_if.is_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006349 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006350 {
6351 // "where" is set when ":elseif", "else" or ":endif" is found
6352 scope->se_u.se_if.is_if_label = instr->ga_len;
6353 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6354 }
6355 else
6356 scope->se_u.se_if.is_if_label = -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357
6358 return p;
6359}
6360
6361 static char_u *
6362compile_elseif(char_u *arg, cctx_T *cctx)
6363{
6364 char_u *p = arg;
6365 garray_T *instr = &cctx->ctx_instr;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006366 int instr_count = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 isn_T *isn;
6368 scope_T *scope = cctx->ctx_scope;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006369 ppconst_T ppconst;
Bram Moolenaar749639e2020-08-27 23:08:47 +02006370 skip_T save_skip = cctx->ctx_skip;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006371
6372 if (scope == NULL || scope->se_type != IF_SCOPE)
6373 {
6374 emsg(_(e_elseif_without_if));
6375 return NULL;
6376 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006377 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006378 if (!cctx->ctx_had_return)
6379 scope->se_u.se_if.is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006381 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006382 {
6383 if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 JUMP_ALWAYS, cctx) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006385 return NULL;
6386 // previous "if" or "elseif" jumps here
6387 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6388 isn->isn_arg.jump.jump_where = instr->ga_len;
6389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006390
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006391 // compile "expr"; if we know it evaluates to FALSE skip the block
Bram Moolenaar7f141552020-05-09 17:35:53 +02006392 CLEAR_FIELD(ppconst);
Bram Moolenaar749639e2020-08-27 23:08:47 +02006393 if (cctx->ctx_skip == SKIP_YES)
6394 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006395 if (compile_expr1(&p, cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006396 {
Bram Moolenaar7f141552020-05-09 17:35:53 +02006397 clear_ppconst(&ppconst);
6398 return NULL;
6399 }
Bram Moolenaar749639e2020-08-27 23:08:47 +02006400 cctx->ctx_skip = save_skip;
Bram Moolenaarefd88552020-06-18 20:50:10 +02006401 if (scope->se_skip_save == SKIP_YES)
6402 clear_ppconst(&ppconst);
6403 else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
Bram Moolenaar7f141552020-05-09 17:35:53 +02006404 {
Bram Moolenaar13106602020-10-04 16:06:05 +02006405 int error = FALSE;
6406 int v;
6407
Bram Moolenaar7f141552020-05-09 17:35:53 +02006408 // The expression results in a constant.
6409 // TODO: how about nesting?
Bram Moolenaar13106602020-10-04 16:06:05 +02006410 v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
6411 if (error)
6412 return NULL;
6413 cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006414 clear_ppconst(&ppconst);
6415 scope->se_u.se_if.is_if_label = -1;
6416 }
6417 else
6418 {
6419 // Not a constant, generate instructions for the expression.
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006420 cctx->ctx_skip = SKIP_UNKNOWN;
Bram Moolenaar7f141552020-05-09 17:35:53 +02006421 if (generate_ppconst(cctx, &ppconst) == FAIL)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006422 return NULL;
Bram Moolenaar13106602020-10-04 16:06:05 +02006423 if (bool_on_stack(cctx) == FAIL)
6424 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006425
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006426 // "where" is set when ":elseif", "else" or ":endif" is found
6427 scope->se_u.se_if.is_if_label = instr->ga_len;
6428 generate_JUMP(cctx, JUMP_IF_FALSE, 0);
6429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006430
6431 return p;
6432}
6433
6434 static char_u *
6435compile_else(char_u *arg, cctx_T *cctx)
6436{
6437 char_u *p = arg;
6438 garray_T *instr = &cctx->ctx_instr;
6439 isn_T *isn;
6440 scope_T *scope = cctx->ctx_scope;
6441
6442 if (scope == NULL || scope->se_type != IF_SCOPE)
6443 {
6444 emsg(_(e_else_without_if));
6445 return NULL;
6446 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01006447 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006448 if (!cctx->ctx_had_return)
6449 scope->se_u.se_if.is_had_return = FALSE;
6450 scope->se_u.se_if.is_seen_else = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006451
Bram Moolenaarefd88552020-06-18 20:50:10 +02006452 if (scope->se_skip_save != SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006453 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006454 // jump from previous block to the end, unless the else block is empty
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006455 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006456 {
Bram Moolenaarefd88552020-06-18 20:50:10 +02006457 if (!cctx->ctx_had_return
6458 && compile_jump_to_end(&scope->se_u.se_if.is_end_label,
6459 JUMP_ALWAYS, cctx) == FAIL)
6460 return NULL;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006461 }
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006462
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006463 if (cctx->ctx_skip == SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006464 {
6465 if (scope->se_u.se_if.is_if_label >= 0)
6466 {
6467 // previous "if" or "elseif" jumps here
6468 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6469 isn->isn_arg.jump.jump_where = instr->ga_len;
6470 scope->se_u.se_if.is_if_label = -1;
6471 }
6472 }
6473
Bram Moolenaar280b0dc2020-06-20 13:29:03 +02006474 if (cctx->ctx_skip != SKIP_UNKNOWN)
Bram Moolenaarefd88552020-06-18 20:50:10 +02006475 cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
6476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006477
6478 return p;
6479}
6480
6481 static char_u *
6482compile_endif(char_u *arg, cctx_T *cctx)
6483{
6484 scope_T *scope = cctx->ctx_scope;
6485 ifscope_T *ifscope;
6486 garray_T *instr = &cctx->ctx_instr;
6487 isn_T *isn;
6488
6489 if (scope == NULL || scope->se_type != IF_SCOPE)
6490 {
6491 emsg(_(e_endif_without_if));
6492 return NULL;
6493 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006494 ifscope = &scope->se_u.se_if;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006495 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006496 if (!cctx->ctx_had_return)
6497 ifscope->is_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006498
Bram Moolenaara259d8d2020-01-31 20:10:50 +01006499 if (scope->se_u.se_if.is_if_label >= 0)
6500 {
6501 // previous "if" or "elseif" jumps here
6502 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
6503 isn->isn_arg.jump.jump_where = instr->ga_len;
6504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006505 // Fill in the "end" label in jumps at the end of the blocks.
6506 compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02006507 cctx->ctx_skip = scope->se_skip_save;
6508
6509 // If all the blocks end in :return and there is an :else then the
6510 // had_return flag is set.
6511 cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006512
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006513 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006514 return arg;
6515}
6516
6517/*
Bram Moolenaar792f7862020-11-23 08:31:18 +01006518 * Compile "for var in expr":
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006519 *
6520 * Produces instructions:
6521 * PUSHNR -1
6522 * STORE loop-idx Set index to -1
Bram Moolenaar792f7862020-11-23 08:31:18 +01006523 * EVAL expr result of "expr" on top of stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 * top: FOR loop-idx, end Increment index, use list on bottom of stack
6525 * - if beyond end, jump to "end"
6526 * - otherwise get item from list and push it
6527 * STORE var Store item in "var"
6528 * ... body ...
6529 * JUMP top Jump back to repeat
6530 * end: DROP Drop the result of "expr"
6531 *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006532 * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
6533 * UNPACK 2 Split item in 2
6534 * STORE var1 Store item in "var1"
6535 * STORE var2 Store item in "var2"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006536 */
6537 static char_u *
Bram Moolenaar792f7862020-11-23 08:31:18 +01006538compile_for(char_u *arg_start, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006539{
Bram Moolenaar792f7862020-11-23 08:31:18 +01006540 char_u *arg;
6541 char_u *arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006542 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006543 char_u *p;
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006544 char_u *wp;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006545 int var_count = 0;
6546 int semicolon = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006547 size_t varlen;
6548 garray_T *instr = &cctx->ctx_instr;
6549 garray_T *stack = &cctx->ctx_type_stack;
6550 scope_T *scope;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006551 lvar_T *loop_lvar; // loop iteration variable
6552 lvar_T *var_lvar; // variable for "var"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006553 type_T *vartype;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006554 type_T *item_type = &t_any;
6555 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006556
Bram Moolenaar792f7862020-11-23 08:31:18 +01006557 p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
6558 if (var_count == 0)
6559 var_count = 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006560
6561 // consume "in"
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006562 wp = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006563 p = skipwhite(p);
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006564 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6565 return NULL;
6566 if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006567 {
6568 emsg(_(e_missing_in));
6569 return NULL;
6570 }
Bram Moolenaar38bd8de2020-12-02 13:23:36 +01006571 wp = p + 2;
6572 p = skipwhite(wp);
6573 if (may_get_next_line_error(wp, &p, cctx) == FAIL)
6574 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006576 scope = new_scope(cctx, FOR_SCOPE);
6577 if (scope == NULL)
6578 return NULL;
6579
Bram Moolenaar792f7862020-11-23 08:31:18 +01006580 // Reserve a variable to store the loop iteration counter and initialize it
6581 // to -1.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006582 loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
6583 if (loop_lvar == NULL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006584 {
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006585 // out of memory
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006586 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006587 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006588 }
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006589 generate_STORENR(cctx, loop_lvar->lv_idx, -1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590
6591 // compile "expr", it remains on the stack until "endfor"
6592 arg = p;
Bram Moolenaara5565e42020-05-09 15:44:01 +02006593 if (compile_expr0(&arg, cctx) == FAIL)
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006594 {
6595 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 return NULL;
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006597 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006598 arg_end = arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006600 // Now that we know the type of "var", check that it is a list, now or at
6601 // runtime.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006602 vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
Bram Moolenaar334a8b42020-10-19 16:07:42 +02006603 if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 {
Bram Moolenaar25b70c72020-04-01 16:34:17 +02006605 drop_scope(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006606 return NULL;
6607 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006608
Bram Moolenaar0ad3e892020-07-05 21:38:11 +02006609 if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006610 {
6611 if (var_count == 1)
6612 item_type = vartype->tt_member;
6613 else if (vartype->tt_member->tt_type == VAR_LIST
6614 && vartype->tt_member->tt_member->tt_type != VAR_ANY)
6615 item_type = vartype->tt_member->tt_member;
6616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006617
6618 // "for_end" is set when ":endfor" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006619 scope->se_u.se_for.fs_top_label = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02006620 generate_FOR(cctx, loop_lvar->lv_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006621
Bram Moolenaar792f7862020-11-23 08:31:18 +01006622 arg = arg_start;
6623 if (var_count > 1)
6624 {
6625 generate_UNPACK(cctx, var_count, semicolon);
6626 arg = skipwhite(arg + 1); // skip white after '['
6627
6628 // the list item is replaced by a number of items
6629 if (ga_grow(stack, var_count - 1) == FAIL)
6630 {
6631 drop_scope(cctx);
6632 return NULL;
6633 }
6634 --stack->ga_len;
6635 for (idx = 0; idx < var_count; ++idx)
6636 {
6637 ((type_T **)stack->ga_data)[stack->ga_len] =
6638 (semicolon && idx == 0) ? vartype : item_type;
6639 ++stack->ga_len;
6640 }
6641 }
6642
6643 for (idx = 0; idx < var_count; ++idx)
6644 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006645 assign_dest_T dest = dest_local;
6646 int opt_flags = 0;
6647 int vimvaridx = -1;
6648 type_T *type = &t_any;
6649
6650 p = skip_var_one(arg, FALSE);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006651 varlen = p - arg;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006652 name = vim_strnsave(arg, varlen);
6653 if (name == NULL)
6654 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006655
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006656 // TODO: script var not supported?
6657 if (get_var_dest(name, &dest, CMD_for, &opt_flags,
6658 &vimvaridx, &type, cctx) == FAIL)
6659 goto failed;
6660 if (dest != dest_local)
Bram Moolenaar792f7862020-11-23 08:31:18 +01006661 {
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006662 if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
6663 0, 0, type, name) == FAIL)
6664 goto failed;
Bram Moolenaar792f7862020-11-23 08:31:18 +01006665 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006666 else
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006667 {
Bram Moolenaar709664c2020-12-12 14:33:41 +01006668 if (lookup_local(arg, varlen, NULL, cctx) == OK)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006669 {
6670 semsg(_(e_variable_already_declared), arg);
6671 goto failed;
6672 }
6673
Bram Moolenaarea870692020-12-02 14:24:30 +01006674 if (STRNCMP(name, "s:", 2) == 0)
6675 {
6676 semsg(_(e_cannot_declare_script_variable_in_function), name);
6677 goto failed;
6678 }
6679
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006680 // Reserve a variable to store "var".
6681 // TODO: check for type
6682 var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
6683 if (var_lvar == NULL)
6684 // out of memory or used as an argument
6685 goto failed;
6686
6687 if (semicolon && idx == var_count - 1)
6688 var_lvar->lv_type = vartype;
6689 else
6690 var_lvar->lv_type = item_type;
6691 generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
6692 }
Bram Moolenaar792f7862020-11-23 08:31:18 +01006693
6694 if (*p == ',' || *p == ';')
6695 ++p;
6696 arg = skipwhite(p);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006697 vim_free(name);
Bram Moolenaar792f7862020-11-23 08:31:18 +01006698 }
6699
6700 return arg_end;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01006701
6702failed:
6703 vim_free(name);
6704 drop_scope(cctx);
6705 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006706}
6707
6708/*
6709 * compile "endfor"
6710 */
6711 static char_u *
6712compile_endfor(char_u *arg, cctx_T *cctx)
6713{
6714 garray_T *instr = &cctx->ctx_instr;
6715 scope_T *scope = cctx->ctx_scope;
6716 forscope_T *forscope;
6717 isn_T *isn;
6718
6719 if (scope == NULL || scope->se_type != FOR_SCOPE)
6720 {
6721 emsg(_(e_for));
6722 return NULL;
6723 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006724 forscope = &scope->se_u.se_for;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006725 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006726 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006727
6728 // At end of ":for" scope jump back to the FOR instruction.
6729 generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
6730
6731 // Fill in the "end" label in the FOR statement so it can jump here
6732 isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
6733 isn->isn_arg.forloop.for_end = instr->ga_len;
6734
6735 // Fill in the "end" label any BREAK statements
6736 compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
6737
6738 // Below the ":for" scope drop the "expr" list from the stack.
6739 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
6740 return NULL;
6741
6742 vim_free(scope);
6743
6744 return arg;
6745}
6746
6747/*
6748 * compile "while expr"
6749 *
6750 * Produces instructions:
6751 * top: EVAL expr Push result of "expr"
6752 * JUMP_IF_FALSE end jump if false
6753 * ... body ...
6754 * JUMP top Jump back to repeat
6755 * end:
6756 *
6757 */
6758 static char_u *
6759compile_while(char_u *arg, cctx_T *cctx)
6760{
6761 char_u *p = arg;
6762 garray_T *instr = &cctx->ctx_instr;
6763 scope_T *scope;
6764
6765 scope = new_scope(cctx, WHILE_SCOPE);
6766 if (scope == NULL)
6767 return NULL;
6768
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006769 scope->se_u.se_while.ws_top_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006770
6771 // compile "expr"
Bram Moolenaara5565e42020-05-09 15:44:01 +02006772 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006773 return NULL;
6774
Bram Moolenaar13106602020-10-04 16:06:05 +02006775 if (bool_on_stack(cctx) == FAIL)
6776 return FAIL;
6777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006778 // "while_end" is set when ":endwhile" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006779 if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006780 JUMP_IF_FALSE, cctx) == FAIL)
6781 return FAIL;
6782
6783 return p;
6784}
6785
6786/*
6787 * compile "endwhile"
6788 */
6789 static char_u *
6790compile_endwhile(char_u *arg, cctx_T *cctx)
6791{
6792 scope_T *scope = cctx->ctx_scope;
6793
6794 if (scope == NULL || scope->se_type != WHILE_SCOPE)
6795 {
6796 emsg(_(e_while));
6797 return NULL;
6798 }
6799 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006800 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006801
6802 // At end of ":for" scope jump back to the FOR instruction.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006803 generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006804
6805 // Fill in the "end" label in the WHILE statement so it can jump here.
6806 // And in any jumps for ":break"
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006807 compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006808
6809 vim_free(scope);
6810
6811 return arg;
6812}
6813
6814/*
6815 * compile "continue"
6816 */
6817 static char_u *
6818compile_continue(char_u *arg, cctx_T *cctx)
6819{
6820 scope_T *scope = cctx->ctx_scope;
6821
6822 for (;;)
6823 {
6824 if (scope == NULL)
6825 {
6826 emsg(_(e_continue));
6827 return NULL;
6828 }
6829 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6830 break;
6831 scope = scope->se_outer;
6832 }
6833
6834 // Jump back to the FOR or WHILE instruction.
6835 generate_JUMP(cctx, JUMP_ALWAYS,
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006836 scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
6837 : scope->se_u.se_while.ws_top_label);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006838 return arg;
6839}
6840
6841/*
6842 * compile "break"
6843 */
6844 static char_u *
6845compile_break(char_u *arg, cctx_T *cctx)
6846{
6847 scope_T *scope = cctx->ctx_scope;
6848 endlabel_T **el;
6849
6850 for (;;)
6851 {
6852 if (scope == NULL)
6853 {
6854 emsg(_(e_break));
6855 return NULL;
6856 }
6857 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
6858 break;
6859 scope = scope->se_outer;
6860 }
6861
6862 // Jump to the end of the FOR or WHILE loop.
6863 if (scope->se_type == FOR_SCOPE)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006864 el = &scope->se_u.se_for.fs_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006865 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006866 el = &scope->se_u.se_while.ws_end_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006867 if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
6868 return FAIL;
6869
6870 return arg;
6871}
6872
6873/*
6874 * compile "{" start of block
6875 */
6876 static char_u *
6877compile_block(char_u *arg, cctx_T *cctx)
6878{
6879 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
6880 return NULL;
6881 return skipwhite(arg + 1);
6882}
6883
6884/*
6885 * compile end of block: drop one scope
6886 */
6887 static void
6888compile_endblock(cctx_T *cctx)
6889{
6890 scope_T *scope = cctx->ctx_scope;
6891
6892 cctx->ctx_scope = scope->se_outer;
Bram Moolenaar20431c92020-03-20 18:39:46 +01006893 unwind_locals(cctx, scope->se_local_count);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006894 vim_free(scope);
6895}
6896
6897/*
6898 * compile "try"
6899 * Creates a new scope for the try-endtry, pointing to the first catch and
6900 * finally.
6901 * Creates another scope for the "try" block itself.
6902 * TRY instruction sets up exception handling at runtime.
6903 *
6904 * "try"
6905 * TRY -> catch1, -> finally push trystack entry
6906 * ... try block
6907 * "throw {exception}"
6908 * EVAL {exception}
6909 * THROW create exception
6910 * ... try block
6911 * " catch {expr}"
6912 * JUMP -> finally
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006913 * catch1: PUSH exception
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006914 * EVAL {expr}
6915 * MATCH
6916 * JUMP nomatch -> catch2
6917 * CATCH remove exception
6918 * ... catch block
6919 * " catch"
6920 * JUMP -> finally
6921 * catch2: CATCH remove exception
6922 * ... catch block
6923 * " finally"
6924 * finally:
6925 * ... finally block
6926 * " endtry"
6927 * ENDTRY pop trystack entry, may rethrow
6928 */
6929 static char_u *
6930compile_try(char_u *arg, cctx_T *cctx)
6931{
6932 garray_T *instr = &cctx->ctx_instr;
6933 scope_T *try_scope;
6934 scope_T *scope;
6935
6936 // scope that holds the jumps that go to catch/finally/endtry
6937 try_scope = new_scope(cctx, TRY_SCOPE);
6938 if (try_scope == NULL)
6939 return NULL;
6940
6941 // "catch" is set when the first ":catch" is found.
6942 // "finally" is set when ":finally" or ":endtry" is found
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006943 try_scope->se_u.se_try.ts_try_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006944 if (generate_instr(cctx, ISN_TRY) == NULL)
6945 return NULL;
6946
6947 // scope for the try block itself
6948 scope = new_scope(cctx, BLOCK_SCOPE);
6949 if (scope == NULL)
6950 return NULL;
6951
6952 return arg;
6953}
6954
6955/*
6956 * compile "catch {expr}"
6957 */
6958 static char_u *
6959compile_catch(char_u *arg, cctx_T *cctx UNUSED)
6960{
6961 scope_T *scope = cctx->ctx_scope;
6962 garray_T *instr = &cctx->ctx_instr;
6963 char_u *p;
6964 isn_T *isn;
6965
6966 // end block scope from :try or :catch
6967 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
6968 compile_endblock(cctx);
6969 scope = cctx->ctx_scope;
6970
6971 // Error if not in a :try scope
6972 if (scope == NULL || scope->se_type != TRY_SCOPE)
6973 {
6974 emsg(_(e_catch));
6975 return NULL;
6976 }
6977
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006978 if (scope->se_u.se_try.ts_caught_all)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006979 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006980 emsg(_(e_catch_unreachable_after_catch_all));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006981 return NULL;
6982 }
6983
6984 // Jump from end of previous block to :finally or :endtry
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006985 if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006986 JUMP_ALWAYS, cctx) == FAIL)
6987 return NULL;
6988
6989 // End :try or :catch scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006990 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006991 if (isn->isn_arg.try.try_catch == 0)
6992 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006993 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006994 {
6995 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006996 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006997 isn->isn_arg.jump.jump_where = instr->ga_len;
6998 }
6999
7000 p = skipwhite(arg);
Bram Moolenaar7a092242020-04-16 22:10:49 +02007001 if (ends_excmd2(arg, p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007002 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007003 scope->se_u.se_try.ts_caught_all = TRUE;
7004 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007005 }
7006 else
7007 {
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007008 char_u *end;
7009 char_u *pat;
7010 char_u *tofree = NULL;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007011 int dropped = 0;
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007012 int len;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007013
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007014 // Push v:exception, push {expr} and MATCH
7015 generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
7016
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007017 end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007018 if (*end != *p)
7019 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02007020 semsg(_(e_separator_mismatch_str), p);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007021 vim_free(tofree);
7022 return FAIL;
7023 }
7024 if (tofree == NULL)
Bram Moolenaar3dd64602020-02-13 20:31:28 +01007025 len = (int)(end - (p + 1));
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007026 else
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007027 len = (int)(end - tofree);
7028 pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007029 vim_free(tofree);
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007030 p += len + 2 + dropped;
Bram Moolenaarff80cb62020-02-05 22:10:05 +01007031 if (pat == NULL)
7032 return FAIL;
7033 if (generate_PUSHS(cctx, pat) == FAIL)
7034 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007036 if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
7037 return NULL;
7038
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007039 scope->se_u.se_try.ts_catch_label = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
7041 return NULL;
7042 }
7043
7044 if (generate_instr(cctx, ISN_CATCH) == NULL)
7045 return NULL;
7046
7047 if (new_scope(cctx, BLOCK_SCOPE) == NULL)
7048 return NULL;
7049 return p;
7050}
7051
7052 static char_u *
7053compile_finally(char_u *arg, cctx_T *cctx)
7054{
7055 scope_T *scope = cctx->ctx_scope;
7056 garray_T *instr = &cctx->ctx_instr;
7057 isn_T *isn;
7058
7059 // end block scope from :try or :catch
7060 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7061 compile_endblock(cctx);
7062 scope = cctx->ctx_scope;
7063
7064 // Error if not in a :try scope
7065 if (scope == NULL || scope->se_type != TRY_SCOPE)
7066 {
7067 emsg(_(e_finally));
7068 return NULL;
7069 }
7070
7071 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007072 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007073 if (isn->isn_arg.try.try_finally != 0)
7074 {
7075 emsg(_(e_finally_dup));
7076 return NULL;
7077 }
7078
7079 // Fill in the "end" label in jumps at the end of the blocks.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007080 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007081
Bram Moolenaar585fea72020-04-02 22:33:21 +02007082 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007083 if (scope->se_u.se_try.ts_catch_label != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 {
7085 // Previous catch without match jumps here
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007086 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007087 isn->isn_arg.jump.jump_where = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007088 scope->se_u.se_try.ts_catch_label = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 }
7090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 // TODO: set index in ts_finally_label jumps
7092
7093 return arg;
7094}
7095
7096 static char_u *
7097compile_endtry(char_u *arg, cctx_T *cctx)
7098{
7099 scope_T *scope = cctx->ctx_scope;
7100 garray_T *instr = &cctx->ctx_instr;
7101 isn_T *isn;
7102
7103 // end block scope from :catch or :finally
7104 if (scope != NULL && scope->se_type == BLOCK_SCOPE)
7105 compile_endblock(cctx);
7106 scope = cctx->ctx_scope;
7107
7108 // Error if not in a :try scope
7109 if (scope == NULL || scope->se_type != TRY_SCOPE)
7110 {
7111 if (scope == NULL)
7112 emsg(_(e_no_endtry));
7113 else if (scope->se_type == WHILE_SCOPE)
7114 emsg(_(e_endwhile));
Bram Moolenaar5b18c242020-01-28 22:30:32 +01007115 else if (scope->se_type == FOR_SCOPE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116 emsg(_(e_endfor));
7117 else
7118 emsg(_(e_endif));
7119 return NULL;
7120 }
7121
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007122 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007123 if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
7124 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007125 emsg(_(e_missing_catch_or_finally));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007126 return NULL;
7127 }
7128
7129 // Fill in the "end" label in jumps at the end of the blocks, if not done
7130 // by ":finally".
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007131 compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132
7133 // End :catch or :finally scope: set value in ISN_TRY instruction
Bram Moolenaare8593122020-07-18 15:17:02 +02007134 if (isn->isn_arg.try.try_catch == 0)
7135 isn->isn_arg.try.try_catch = instr->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007136 if (isn->isn_arg.try.try_finally == 0)
7137 isn->isn_arg.try.try_finally = instr->ga_len;
Bram Moolenaare8593122020-07-18 15:17:02 +02007138
7139 if (scope->se_u.se_try.ts_catch_label != 0)
7140 {
7141 // Last catch without match jumps here
7142 isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
7143 isn->isn_arg.jump.jump_where = instr->ga_len;
7144 }
7145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007146 compile_endblock(cctx);
7147
7148 if (generate_instr(cctx, ISN_ENDTRY) == NULL)
7149 return NULL;
7150 return arg;
7151}
7152
7153/*
7154 * compile "throw {expr}"
7155 */
7156 static char_u *
7157compile_throw(char_u *arg, cctx_T *cctx UNUSED)
7158{
7159 char_u *p = skipwhite(arg);
7160
Bram Moolenaara5565e42020-05-09 15:44:01 +02007161 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162 return NULL;
7163 if (may_generate_2STRING(-1, cctx) == FAIL)
7164 return NULL;
7165 if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
7166 return NULL;
7167
7168 return p;
7169}
7170
7171/*
7172 * compile "echo expr"
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007173 * compile "echomsg expr"
7174 * compile "echoerr expr"
Bram Moolenaarad39c092020-02-26 18:23:43 +01007175 * compile "execute expr"
7176 */
7177 static char_u *
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007178compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007179{
7180 char_u *p = arg;
Bram Moolenaare4984292020-12-13 14:19:25 +01007181 char_u *prev = arg;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007182 int count = 0;
7183
7184 for (;;)
7185 {
Bram Moolenaare4984292020-12-13 14:19:25 +01007186 if (ends_excmd2(prev, p))
7187 break;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007188 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarad39c092020-02-26 18:23:43 +01007189 return NULL;
7190 ++count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02007191 prev = p;
Bram Moolenaarad39c092020-02-26 18:23:43 +01007192 p = skipwhite(p);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007193 }
7194
Bram Moolenaare4984292020-12-13 14:19:25 +01007195 if (count > 0)
7196 {
7197 if (cmdidx == CMD_echo || cmdidx == CMD_echon)
7198 generate_ECHO(cctx, cmdidx == CMD_echo, count);
7199 else if (cmdidx == CMD_execute)
7200 generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
7201 else if (cmdidx == CMD_echomsg)
7202 generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
7203 else
7204 generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
7205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007206 return p;
7207}
7208
7209/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007210 * If "eap" has a range that is not a constant generate an ISN_RANGE
Bram Moolenaar08597872020-12-10 19:43:40 +01007211 * instruction to compute it and return OK.
7212 * Otherwise return FAIL, the caller must deal with any range.
7213 */
7214 static int
7215compile_variable_range(exarg_T *eap, cctx_T *cctx)
7216{
7217 char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
7218 char_u *p = skipdigits(eap->cmd);
7219
7220 if (p == range_end)
7221 return FAIL;
7222 return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
7223}
7224
7225/*
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007226 * :put r
7227 * :put ={expr}
7228 */
7229 static char_u *
7230compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
7231{
7232 char_u *line = arg;
7233 linenr_T lnum;
7234 char *errormsg;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007235 int above = eap->forceit;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007236
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007237 eap->regname = *line;
7238
7239 if (eap->regname == '=')
7240 {
7241 char_u *p = line + 1;
7242
7243 if (compile_expr0(&p, cctx) == FAIL)
7244 return NULL;
7245 line = p;
7246 }
7247 else if (eap->regname != NUL)
7248 ++line;
7249
Bram Moolenaar08597872020-12-10 19:43:40 +01007250 if (compile_variable_range(eap, cctx) == OK)
7251 {
7252 lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
7253 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007254 else
Bram Moolenaar08597872020-12-10 19:43:40 +01007255 {
7256 // Either no range or a number.
7257 // "errormsg" will not be set because the range is ADDR_LINES.
7258 if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
Bram Moolenaar399ea812020-12-15 21:28:57 +01007259 // cannot happen
Bram Moolenaar08597872020-12-10 19:43:40 +01007260 return NULL;
7261 if (eap->addr_count == 0)
7262 lnum = -1;
7263 else
7264 lnum = eap->line2;
7265 if (above)
7266 --lnum;
7267 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007268
7269 generate_PUT(cctx, eap->regname, lnum);
7270 return line;
7271}
7272
7273/*
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007274 * A command that is not compiled, execute with legacy code.
7275 */
7276 static char_u *
7277compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
7278{
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007279 char_u *p;
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007280 int has_expr = FALSE;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007281 char_u *nextcmd = (char_u *)"";
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007282
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007283 if (cctx->ctx_skip == SKIP_YES)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007284 goto theend;
7285
Bram Moolenaar7d41aa82020-04-26 14:29:56 +02007286 if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007287 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007288 long argt = eap->argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007289 int usefilter = FALSE;
7290
7291 has_expr = argt & (EX_XFILE | EX_EXPAND);
7292
7293 // If the command can be followed by a bar, find the bar and truncate
7294 // it, so that the following command can be compiled.
7295 // The '|' is overwritten with a NUL, it is put back below.
7296 if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
7297 && *eap->arg == '!')
7298 // :w !filter or :r !filter or :r! filter
7299 usefilter = TRUE;
7300 if ((argt & EX_TRLBAR) && !usefilter)
7301 {
Bram Moolenaarb8a92962020-08-20 18:02:47 +02007302 eap->argt = argt;
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007303 separate_nextcmd(eap);
7304 if (eap->nextcmd != NULL)
7305 nextcmd = eap->nextcmd;
7306 }
7307 }
7308
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007309 if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
7310 {
7311 // expand filename in "syntax include [@group] filename"
7312 has_expr = TRUE;
7313 eap->arg = skipwhite(eap->arg + 7);
7314 if (*eap->arg == '@')
7315 eap->arg = skiptowhite(eap->arg);
7316 }
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007317
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02007318 if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007319 {
7320 int count = 0;
7321 char_u *start = skipwhite(line);
7322
7323 // :cmd xxx`=expr1`yyy`=expr2`zzz
7324 // PUSHS ":cmd xxx"
7325 // eval expr1
7326 // PUSHS "yyy"
7327 // eval expr2
7328 // PUSHS "zzz"
7329 // EXECCONCAT 5
7330 for (;;)
7331 {
7332 if (p > start)
7333 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02007334 generate_PUSHS(cctx, vim_strnsave(start, p - start));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007335 ++count;
7336 }
7337 p += 2;
Bram Moolenaara5565e42020-05-09 15:44:01 +02007338 if (compile_expr0(&p, cctx) == FAIL)
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007339 return NULL;
7340 may_generate_2STRING(-1, cctx);
7341 ++count;
7342 p = skipwhite(p);
7343 if (*p != '`')
7344 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007345 emsg(_(e_missing_backtick));
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007346 return NULL;
7347 }
7348 start = p + 1;
7349
7350 p = (char_u *)strstr((char *)start, "`=");
7351 if (p == NULL)
7352 {
7353 if (*skipwhite(start) != NUL)
7354 {
7355 generate_PUSHS(cctx, vim_strsave(start));
7356 ++count;
7357 }
7358 break;
7359 }
7360 }
7361 generate_EXECCONCAT(cctx, count);
7362 }
7363 else
7364 generate_EXEC(cctx, line);
7365
7366theend:
Bram Moolenaare9f262b2020-07-05 14:57:51 +02007367 if (*nextcmd != NUL)
7368 {
7369 // the parser expects a pointer to the bar, put it back
7370 --nextcmd;
7371 *nextcmd = '|';
7372 }
7373
7374 return nextcmd;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02007375}
7376
7377/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02007378 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007379 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02007380 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007381 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02007382add_def_function(ufunc_T *ufunc)
7383{
7384 dfunc_T *dfunc;
7385
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007386 if (def_functions.ga_len == 0)
7387 {
7388 // The first position is not used, so that a zero uf_dfunc_idx means it
7389 // wasn't set.
7390 if (ga_grow(&def_functions, 1) == FAIL)
7391 return FAIL;
7392 ++def_functions.ga_len;
7393 }
7394
Bram Moolenaar09689a02020-05-09 22:50:08 +02007395 // Add the function to "def_functions".
7396 if (ga_grow(&def_functions, 1) == FAIL)
7397 return FAIL;
7398 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
7399 CLEAR_POINTER(dfunc);
7400 dfunc->df_idx = def_functions.ga_len;
7401 ufunc->uf_dfunc_idx = dfunc->df_idx;
7402 dfunc->df_ufunc = ufunc;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007403 dfunc->df_name = vim_strsave(ufunc->uf_name);
7404 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007405 ++def_functions.ga_len;
7406 return OK;
7407}
7408
7409/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007410 * After ex_function() has collected all the function lines: parse and compile
7411 * the lines into instructions.
7412 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007413 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7414 * the return statement (used for lambda). When uf_ret_type is already set
7415 * then check that it matches.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007416 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007417 * This can be used recursively through compile_lambda(), which may reallocate
7418 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02007419 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007420 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02007421 int
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007422compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424 char_u *line = NULL;
7425 char_u *p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 cctx_T cctx;
7428 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01007429 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007430 int ret = FAIL;
7431 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007432 int save_estack_compiling = estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007433 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007434 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007436 // When using a function that was compiled before: Free old instructions.
7437 // Otherwise add a new entry in "def_functions".
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007438 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 {
Bram Moolenaar09689a02020-05-09 22:50:08 +02007440 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7441 + ufunc->uf_dfunc_idx;
Bram Moolenaar09689a02020-05-09 22:50:08 +02007442 delete_def_function_contents(dfunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007443 }
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02007444 else
7445 {
7446 if (add_def_function(ufunc) == FAIL)
7447 return FAIL;
7448 new_def_function = TRUE;
7449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450
Bram Moolenaar985116a2020-07-12 17:31:09 +02007451 ufunc->uf_def_status = UF_COMPILING;
7452
Bram Moolenaara80faa82020-04-12 19:37:17 +02007453 CLEAR_FIELD(cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454 cctx.ctx_ufunc = ufunc;
7455 cctx.ctx_lnum = -1;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007456 cctx.ctx_outer = outer_cctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007457 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
7458 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
7459 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
7460 cctx.ctx_type_list = &ufunc->uf_type_list;
7461 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
7462 instr = &cctx.ctx_instr;
7463
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007464 // Set the context to the function, it may be compiled when called from
7465 // another script. Set the script version to the most modern one.
7466 // The line number will be set in next_line_from_context().
7467 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
7469
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007470 // Make sure error messages are OK.
7471 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
7472 if (do_estack_push)
7473 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02007474 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02007475
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007476 if (ufunc->uf_def_args.ga_len > 0)
7477 {
7478 int count = ufunc->uf_def_args.ga_len;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007479 int first_def_arg = ufunc->uf_args.ga_len - count;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007480 int i;
7481 char_u *arg;
7482 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007483 int did_set_arg_type = FALSE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007484
7485 // Produce instructions for the default values of optional arguments.
7486 // Store the instruction index in uf_def_arg_idx[] so that we know
7487 // where to start when the function is called, depending on the number
7488 // of arguments.
7489 ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
7490 if (ufunc->uf_def_arg_idx == NULL)
7491 goto erret;
7492 for (i = 0; i < count; ++i)
7493 {
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007494 garray_T *stack = &cctx.ctx_type_stack;
7495 type_T *val_type;
7496 int arg_idx = first_def_arg + i;
7497
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007498 ufunc->uf_def_arg_idx[i] = instr->ga_len;
7499 arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
Bram Moolenaara5565e42020-05-09 15:44:01 +02007500 if (compile_expr0(&arg, &cctx) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007501 goto erret;
7502
7503 // If no type specified use the type of the default value.
7504 // Otherwise check that the default value type matches the
7505 // specified type.
7506 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
7507 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007508 {
7509 did_set_arg_type = TRUE;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007510 ufunc->uf_arg_types[arg_idx] = val_type;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007511 }
Bram Moolenaar8b565c22020-08-30 23:24:20 +02007512 else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
7513 TRUE, arg_idx + 1) == FAIL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007514 goto erret;
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02007515
7516 if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007517 goto erret;
7518 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007519 ufunc->uf_def_arg_idx[count] = instr->ga_len;
Bram Moolenaarb8070e32020-07-23 20:56:04 +02007520
7521 if (did_set_arg_type)
7522 set_function_type(ufunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01007523 }
7524
7525 /*
7526 * Loop over all the lines of the function and generate instructions.
7527 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 for (;;)
7529 {
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007530 exarg_T ea;
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +02007531 int starts_with_colon = FALSE;
7532 char_u *cmd;
Bram Moolenaar02194d22020-10-24 23:08:38 +02007533 cmdmod_T local_cmdmod;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007534
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007535 // Bail out on the first error to avoid a flood of errors and report
7536 // the right line number when inside try/catch.
Bram Moolenaard66960b2020-10-30 20:46:26 +01007537 if (did_emsg_before != did_emsg)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02007538 goto erret;
7539
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007540 if (line != NULL && *line == '|')
7541 // the line continues after a '|'
7542 ++line;
Bram Moolenaara3b7fdc2020-06-21 14:12:17 +02007543 else if (line != NULL && *skipwhite(line) != NUL
Bram Moolenaar7a092242020-04-16 22:10:49 +02007544 && !(*line == '#' && (line == cctx.ctx_line_start
7545 || VIM_ISWHITE(line[-1]))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007546 {
Bram Moolenaardd1a9af2020-07-23 15:38:03 +02007547 semsg(_(e_trailing_arg), line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 goto erret;
7549 }
7550 else
7551 {
Bram Moolenaar23c55272020-06-21 16:58:13 +02007552 line = next_line_from_context(&cctx, FALSE);
Bram Moolenaar4fdae992020-04-12 16:38:57 +02007553 if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007554 // beyond the last line
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007555 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007556 }
7557
Bram Moolenaara80faa82020-04-12 19:37:17 +02007558 CLEAR_FIELD(ea);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007559 ea.cmdlinep = &line;
7560 ea.cmd = skipwhite(line);
7561
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007562 // Some things can be recognized by the first character.
7563 switch (*ea.cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007564 {
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007565 case '#':
Bram Moolenaare0de1712020-12-02 17:36:54 +01007566 // "#" starts a comment
7567 line = (char_u *)"";
7568 continue;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007569
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007570 case '}':
7571 {
7572 // "}" ends a block scope
7573 scopetype_T stype = cctx.ctx_scope == NULL
7574 ? NO_SCOPE : cctx.ctx_scope->se_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007575
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007576 if (stype == BLOCK_SCOPE)
7577 {
7578 compile_endblock(&cctx);
7579 line = ea.cmd;
7580 }
7581 else
7582 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007583 emsg(_(e_using_rcurly_outside_if_block_scope));
Bram Moolenaarcb711ab2020-04-16 13:00:29 +02007584 goto erret;
7585 }
7586 if (line != NULL)
7587 line = skipwhite(ea.cmd + 1);
7588 continue;
7589 }
7590
7591 case '{':
7592 // "{" starts a block scope
7593 // "{'a': 1}->func() is something else
7594 if (ends_excmd(*skipwhite(ea.cmd + 1)))
7595 {
7596 line = compile_block(ea.cmd, &cctx);
7597 continue;
7598 }
7599 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007600 }
7601
7602 /*
7603 * COMMAND MODIFIERS
7604 */
Bram Moolenaar02194d22020-10-24 23:08:38 +02007605 cctx.ctx_has_cmdmod = FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02007606 if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
7607 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007608 {
7609 if (errormsg != NULL)
7610 goto erret;
7611 // empty line or comment
7612 line = (char_u *)"";
7613 continue;
7614 }
Bram Moolenaare1004402020-10-24 20:49:43 +02007615 generate_cmdmods(&cctx, &local_cmdmod);
7616 undo_cmdmod(&local_cmdmod);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007617
Bram Moolenaare88c8e82020-11-01 17:03:37 +01007618 // Check if there was a colon after the last command modifier or before
7619 // the current position.
7620 for (p = ea.cmd; p >= line; --p)
7621 {
7622 if (*p == ':')
7623 starts_with_colon = TRUE;
7624 if (p < ea.cmd && !VIM_ISWHITE(*p))
7625 break;
7626 }
7627
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007628 // Skip ":call" to get to the function name.
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007629 p = ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007630 if (checkforcmd(&ea.cmd, "call", 3))
Bram Moolenaar575f24b2020-08-12 14:21:11 +02007631 {
7632 if (*ea.cmd == '(')
7633 // not for "call()"
7634 ea.cmd = p;
7635 else
7636 ea.cmd = skipwhite(ea.cmd);
7637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007638
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007639 if (!starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007640 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007641 char_u *pskip;
7642
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007643 // Assuming the command starts with a variable or function name,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007644 // find what follows.
7645 // Skip over "var.member", "var[idx]" and the like.
7646 // Also "&opt = val", "$ENV = val" and "@r = val".
7647 pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007648 ? ea.cmd + 1 : ea.cmd;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007649 p = to_name_end(pskip, TRUE);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02007650 if (p > ea.cmd && *p != NUL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007651 {
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007652 char_u *var_end;
7653 int oplen;
7654 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007655
Bram Moolenaar65821722020-08-02 18:58:54 +02007656 if (ea.cmd[0] == '@')
7657 var_end = ea.cmd + 2;
7658 else
7659 var_end = find_name_end(pskip, NULL, NULL,
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007660 FNE_CHECK_START | FNE_INCL_BR);
7661 oplen = assignment_len(skipwhite(var_end), &heredoc);
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007662 if (oplen > 0)
7663 {
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007664 size_t len = p - ea.cmd;
7665
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007666 // Recognize an assignment if we recognize the variable
7667 // name:
7668 // "g:var = expr"
Bram Moolenaar5381c7a2020-03-02 22:53:32 +01007669 // "local = expr" where "local" is a local var.
7670 // "script = expr" where "script" is a script-local var.
7671 // "import = expr" where "import" is an imported var
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007672 // "&opt = expr"
7673 // "$ENV = expr"
7674 // "@r = expr"
7675 if (*ea.cmd == '&'
7676 || *ea.cmd == '$'
7677 || *ea.cmd == '@'
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007678 || ((len) > 2 && ea.cmd[1] == ':')
Bram Moolenaar709664c2020-12-12 14:33:41 +01007679 || lookup_local(ea.cmd, len, NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007680 || arg_exists(ea.cmd, len, NULL, NULL,
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007681 NULL, &cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02007682 || script_var_exists(ea.cmd, len,
7683 FALSE, &cctx) == OK
Bram Moolenaarcb2bdb12020-05-10 22:53:56 +02007684 || find_imported(ea.cmd, len, &cctx) != NULL)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007685 {
7686 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007687 if (line == NULL || line == ea.cmd)
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007688 goto erret;
Bram Moolenaarf665e972020-12-05 19:17:16 +01007689 goto nextline;
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007691 }
7692 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007693
7694 if (*ea.cmd == '[')
7695 {
7696 // [var, var] = expr
7697 line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
7698 if (line == NULL)
7699 goto erret;
7700 if (line != ea.cmd)
Bram Moolenaarf665e972020-12-05 19:17:16 +01007701 goto nextline;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007703 }
7704
7705 /*
7706 * COMMAND after range
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007707 * 'text'->func() should not be confused with 'a mark
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007708 */
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007709 cmd = ea.cmd;
Bram Moolenaar7c5ad342020-08-12 15:48:55 +02007710 if (*cmd != '\'' || starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007711 {
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02007712 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007713 if (ea.cmd > cmd)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007714 {
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007715 if (!starts_with_colon)
7716 {
7717 emsg(_(e_colon_required_before_a_range));
7718 goto erret;
7719 }
7720 if (ends_excmd2(line, ea.cmd))
7721 {
7722 // A range without a command: jump to the line.
7723 // TODO: compile to a more efficient command, possibly
7724 // calling parse_cmd_address().
7725 ea.cmdidx = CMD_SIZE;
7726 line = compile_exec(line, &ea, &cctx);
7727 goto nextline;
7728 }
Bram Moolenaar3d48e252020-07-15 14:15:52 +02007729 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007730 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02007731 p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +01007732 : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local,
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +01007733 &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007734
7735 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
7736 {
Bram Moolenaar9b68c822020-06-18 19:31:08 +02007737 if (cctx.ctx_skip == SKIP_YES)
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007738 {
7739 line += STRLEN(line);
Bram Moolenaarf665e972020-12-05 19:17:16 +01007740 goto nextline;
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007741 }
7742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007743 // Expression or function call.
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007744 if (ea.cmdidx != CMD_eval)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007745 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01007746 // CMD_var cannot happen, compile_assignment() above would be
7747 // used. Most likely an assignment to a non-existing variable.
7748 semsg(_(e_command_not_recognized_str), ea.cmd);
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007749 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007751 }
7752
Bram Moolenaar3988f642020-08-27 22:43:03 +02007753 if (cctx.ctx_had_return
Bram Moolenaara259d8d2020-01-31 20:10:50 +01007754 && ea.cmdidx != CMD_elseif
7755 && ea.cmdidx != CMD_else
Bram Moolenaarefd88552020-06-18 20:50:10 +02007756 && ea.cmdidx != CMD_endif
7757 && ea.cmdidx != CMD_endfor
7758 && ea.cmdidx != CMD_endwhile
7759 && ea.cmdidx != CMD_catch
7760 && ea.cmdidx != CMD_finally
7761 && ea.cmdidx != CMD_endtry)
7762 {
Bram Moolenaar3988f642020-08-27 22:43:03 +02007763 emsg(_(e_unreachable_code_after_return));
7764 goto erret;
Bram Moolenaarefd88552020-06-18 20:50:10 +02007765 }
7766
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007767 p = skipwhite(p);
7768 if (ea.cmdidx != CMD_SIZE
7769 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
7770 {
Bram Moolenaar9b123d82020-09-14 22:39:11 +02007771 if (ea.cmdidx >= 0)
7772 ea.argt = excmd_get_argt(ea.cmdidx);
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02007773 if ((ea.argt & EX_BANG) && *p == '!')
7774 {
7775 ea.forceit = TRUE;
7776 p = skipwhite(p + 1);
7777 }
7778 }
7779
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007780 switch (ea.cmdidx)
7781 {
7782 case CMD_def:
Bram Moolenaar04b12692020-05-04 23:24:44 +02007783 ea.arg = p;
7784 line = compile_nested_function(&ea, &cctx);
7785 break;
7786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007787 case CMD_function:
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007788 // TODO: should we allow this, e.g. to declare a global
7789 // function?
7790 emsg(_(e_cannot_use_function_inside_def));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007791 goto erret;
7792
7793 case CMD_return:
Bram Moolenaar9e68c322020-12-25 12:38:04 +01007794 line = compile_return(p, check_return_type, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007795 cctx.ctx_had_return = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007796 break;
7797
7798 case CMD_let:
Bram Moolenaarc58f5452020-10-21 20:58:52 +02007799 emsg(_(e_cannot_use_let_in_vim9_script));
7800 break;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02007801 case CMD_var:
7802 case CMD_final:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007803 case CMD_const:
7804 line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02007805 if (line == p)
7806 line = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007807 break;
7808
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02007809 case CMD_unlet:
7810 case CMD_unlockvar:
7811 case CMD_lockvar:
7812 line = compile_unletlock(p, &ea, &cctx);
7813 break;
7814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007815 case CMD_import:
7816 line = compile_import(p, &cctx);
7817 break;
7818
7819 case CMD_if:
7820 line = compile_if(p, &cctx);
7821 break;
7822 case CMD_elseif:
7823 line = compile_elseif(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007824 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007825 break;
7826 case CMD_else:
7827 line = compile_else(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007828 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007829 break;
7830 case CMD_endif:
7831 line = compile_endif(p, &cctx);
7832 break;
7833
7834 case CMD_while:
7835 line = compile_while(p, &cctx);
7836 break;
7837 case CMD_endwhile:
7838 line = compile_endwhile(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007839 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007840 break;
7841
7842 case CMD_for:
7843 line = compile_for(p, &cctx);
7844 break;
7845 case CMD_endfor:
7846 line = compile_endfor(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007847 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007848 break;
7849 case CMD_continue:
7850 line = compile_continue(p, &cctx);
7851 break;
7852 case CMD_break:
7853 line = compile_break(p, &cctx);
7854 break;
7855
7856 case CMD_try:
7857 line = compile_try(p, &cctx);
7858 break;
7859 case CMD_catch:
7860 line = compile_catch(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007861 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007862 break;
7863 case CMD_finally:
7864 line = compile_finally(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007865 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007866 break;
7867 case CMD_endtry:
7868 line = compile_endtry(p, &cctx);
Bram Moolenaarefd88552020-06-18 20:50:10 +02007869 cctx.ctx_had_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007870 break;
7871 case CMD_throw:
7872 line = compile_throw(p, &cctx);
7873 break;
7874
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007875 case CMD_eval:
7876 if (compile_expr0(&p, &cctx) == FAIL)
7877 goto erret;
7878
Bram Moolenaar3988f642020-08-27 22:43:03 +02007879 // drop the result
Bram Moolenaar007f9d62020-07-06 23:04:49 +02007880 generate_instr_drop(&cctx, ISN_DROP, 1);
7881
7882 line = skipwhite(p);
7883 break;
7884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007885 case CMD_echo:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007886 case CMD_echon:
Bram Moolenaarad39c092020-02-26 18:23:43 +01007887 case CMD_execute:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007888 case CMD_echomsg:
7889 case CMD_echoerr:
7890 line = compile_mult_expr(p, ea.cmdidx, &cctx);
Bram Moolenaarad39c092020-02-26 18:23:43 +01007891 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007892
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007893 case CMD_put:
7894 ea.cmd = cmd;
7895 line = compile_put(p, &ea, &cctx);
7896 break;
7897
Bram Moolenaar3988f642020-08-27 22:43:03 +02007898 // TODO: any other commands with an expression argument?
Bram Moolenaardf069ee2020-06-22 23:02:51 +02007899
Bram Moolenaarae616492020-07-28 20:07:27 +02007900 case CMD_append:
7901 case CMD_change:
7902 case CMD_insert:
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007903 case CMD_t:
Bram Moolenaarae616492020-07-28 20:07:27 +02007904 case CMD_xit:
7905 not_in_vim9(&ea);
7906 goto erret;
7907
Bram Moolenaar002262f2020-07-08 17:47:57 +02007908 case CMD_SIZE:
Bram Moolenaar3988f642020-08-27 22:43:03 +02007909 if (cctx.ctx_skip != SKIP_YES)
7910 {
7911 semsg(_(e_invalid_command_str), ea.cmd);
7912 goto erret;
7913 }
7914 // We don't check for a next command here.
7915 line = (char_u *)"";
7916 break;
Bram Moolenaar002262f2020-07-08 17:47:57 +02007917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007918 default:
Bram Moolenaar5163fcc2020-08-27 23:37:09 +02007919 if (cctx.ctx_skip == SKIP_YES)
7920 {
7921 // We don't check for a next command here.
7922 line = (char_u *)"";
7923 }
7924 else
7925 {
7926 // Not recognized, execute with do_cmdline_cmd().
7927 ea.arg = p;
7928 line = compile_exec(line, &ea, &cctx);
7929 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007930 break;
7931 }
Bram Moolenaar5d72ce62020-08-20 23:04:06 +02007932nextline:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007933 if (line == NULL)
7934 goto erret;
Bram Moolenaar585fea72020-04-02 22:33:21 +02007935 line = skipwhite(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007936
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007937 // Undo any command modifiers.
Bram Moolenaar02194d22020-10-24 23:08:38 +02007938 generate_undo_cmdmods(&cctx);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02007939
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007940 if (cctx.ctx_type_stack.ga_len < 0)
7941 {
7942 iemsg("Type stack underflow");
7943 goto erret;
7944 }
7945 }
7946
7947 if (cctx.ctx_scope != NULL)
7948 {
7949 if (cctx.ctx_scope->se_type == IF_SCOPE)
7950 emsg(_(e_endif));
7951 else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
7952 emsg(_(e_endwhile));
7953 else if (cctx.ctx_scope->se_type == FOR_SCOPE)
7954 emsg(_(e_endfor));
7955 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007956 emsg(_(e_missing_rcurly));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007957 goto erret;
7958 }
7959
Bram Moolenaarefd88552020-06-18 20:50:10 +02007960 if (!cctx.ctx_had_return)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007961 {
7962 if (ufunc->uf_ret_type->tt_type != VAR_VOID)
7963 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007964 emsg(_(e_missing_return_statement));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007965 goto erret;
7966 }
7967
7968 // Return zero if there is no return at the end.
7969 generate_PUSHNR(&cctx, 0);
7970 generate_instr(&cctx, ISN_RETURN);
7971 }
7972
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007973 {
7974 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7975 + ufunc->uf_dfunc_idx;
7976 dfunc->df_deleted = FALSE;
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01007977 dfunc->df_script_seq = current_sctx.sc_seq;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007978 dfunc->df_instr = instr->ga_data;
7979 dfunc->df_instr_count = instr->ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02007980 dfunc->df_varcount = cctx.ctx_locals_count;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02007981 dfunc->df_has_closure = cctx.ctx_has_closure;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02007982 if (cctx.ctx_outer_used)
7983 ufunc->uf_flags |= FC_CLOSURE;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007984 ufunc->uf_def_status = UF_COMPILED;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007986
7987 ret = OK;
7988
7989erret:
7990 if (ret == FAIL)
7991 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01007992 int idx;
Bram Moolenaar05afcee2020-03-31 23:32:31 +02007993 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
7994 + ufunc->uf_dfunc_idx;
Bram Moolenaar20431c92020-03-20 18:39:46 +01007995
7996 for (idx = 0; idx < instr->ga_len; ++idx)
7997 delete_instr(((isn_T *)instr->ga_data) + idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007998 ga_clear(instr);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01007999 VIM_CLEAR(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008000
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008001 // If using the last entry in the table and it was added above, we
8002 // might as well remove it.
8003 if (!dfunc->df_deleted && new_def_function
Bram Moolenaar45a15082020-05-25 00:28:33 +02008004 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008005 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01008006 --def_functions.ga_len;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02008007 ufunc->uf_dfunc_idx = 0;
8008 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008009 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar20431c92020-03-20 18:39:46 +01008010
Bram Moolenaar3cca2992020-04-02 22:57:36 +02008011 while (cctx.ctx_scope != NULL)
8012 drop_scope(&cctx);
8013
Bram Moolenaar20431c92020-03-20 18:39:46 +01008014 // Don't execute this function body.
8015 ga_clear_strings(&ufunc->uf_lines);
8016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008017 if (errormsg != NULL)
8018 emsg(errormsg);
Bram Moolenaard66960b2020-10-30 20:46:26 +01008019 else if (did_emsg == did_emsg_before)
Bram Moolenaare13bdec2020-10-16 23:16:47 +02008020 emsg(_(e_compiling_def_function_failed));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008021 }
8022
8023 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02008024 estack_compiling = save_estack_compiling;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02008025 if (do_estack_push)
8026 estack_pop();
8027
Bram Moolenaar20431c92020-03-20 18:39:46 +01008028 free_imported(&cctx);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02008029 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008030 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02008031 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008032}
8033
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02008034 void
8035set_function_type(ufunc_T *ufunc)
8036{
8037 int varargs = ufunc->uf_va_name != NULL;
8038 int argcount = ufunc->uf_args.ga_len;
8039
8040 // Create a type for the function, with the return type and any
8041 // argument types.
8042 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
8043 // The type is included in "tt_args".
8044 if (argcount > 0 || varargs)
8045 {
8046 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
8047 argcount, &ufunc->uf_type_list);
8048 // Add argument types to the function type.
8049 if (func_type_add_arg_types(ufunc->uf_func_type,
8050 argcount + varargs,
8051 &ufunc->uf_type_list) == FAIL)
8052 return;
8053 ufunc->uf_func_type->tt_argcount = argcount + varargs;
8054 ufunc->uf_func_type->tt_min_argcount =
8055 argcount - ufunc->uf_def_args.ga_len;
8056 if (ufunc->uf_arg_types == NULL)
8057 {
8058 int i;
8059
8060 // lambda does not have argument types.
8061 for (i = 0; i < argcount; ++i)
8062 ufunc->uf_func_type->tt_args[i] = &t_any;
8063 }
8064 else
8065 mch_memmove(ufunc->uf_func_type->tt_args,
8066 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
8067 if (varargs)
8068 {
8069 ufunc->uf_func_type->tt_args[argcount] =
8070 ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
8071 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
8072 }
8073 }
8074 else
8075 // No arguments, can use a predefined type.
8076 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
8077 argcount, &ufunc->uf_type_list);
8078}
8079
8080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008081/*
8082 * Delete an instruction, free what it contains.
8083 */
Bram Moolenaar20431c92020-03-20 18:39:46 +01008084 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008085delete_instr(isn_T *isn)
8086{
8087 switch (isn->isn_type)
8088 {
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008089 case ISN_DEF:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008090 case ISN_EXEC:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008091 case ISN_LOADAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008092 case ISN_LOADB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008093 case ISN_LOADENV:
8094 case ISN_LOADG:
8095 case ISN_LOADOPT:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008096 case ISN_LOADT:
8097 case ISN_LOADW:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008098 case ISN_PUSHEXC:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008099 case ISN_PUSHFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008100 case ISN_PUSHS:
Bram Moolenaar08597872020-12-10 19:43:40 +01008101 case ISN_RANGE:
Bram Moolenaar03290b82020-12-19 16:30:44 +01008102 case ISN_STOREAUTO:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008103 case ISN_STOREB:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008104 case ISN_STOREENV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008105 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02008106 case ISN_STORET:
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01008107 case ISN_STOREW:
8108 case ISN_STRINGMEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008109 vim_free(isn->isn_arg.string);
8110 break;
8111
8112 case ISN_LOADS:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01008113 case ISN_STORES:
8114 vim_free(isn->isn_arg.loadstore.ls_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008115 break;
8116
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008117 case ISN_UNLET:
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02008118 case ISN_UNLETENV:
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02008119 vim_free(isn->isn_arg.unlet.ul_name);
8120 break;
8121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008122 case ISN_STOREOPT:
8123 vim_free(isn->isn_arg.storeopt.so_name);
8124 break;
8125
8126 case ISN_PUSHBLOB: // push blob isn_arg.blob
8127 blob_unref(isn->isn_arg.blob);
8128 break;
8129
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008130 case ISN_PUSHJOB:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008132 job_unref(isn->isn_arg.job);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008133#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008134 break;
8135
8136 case ISN_PUSHCHANNEL:
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008138 channel_unref(isn->isn_arg.channel);
Bram Moolenaarf4f190d2020-03-01 13:01:16 +01008139#endif
Bram Moolenaar42a480b2020-02-29 23:23:47 +01008140 break;
8141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008142 case ISN_UCALL:
8143 vim_free(isn->isn_arg.ufunc.cuf_name);
8144 break;
8145
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008146 case ISN_FUNCREF:
8147 {
8148 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8149 + isn->isn_arg.funcref.fr_func;
Bram Moolenaar077a4232020-12-22 18:33:27 +01008150 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaara05e5242020-09-19 18:19:19 +02008151
Bram Moolenaar077a4232020-12-22 18:33:27 +01008152 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
8153 func_ptr_unref(ufunc);
Bram Moolenaara05e5242020-09-19 18:19:19 +02008154 }
8155 break;
8156
8157 case ISN_DCALL:
8158 {
8159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8160 + isn->isn_arg.dfunc.cdf_idx;
8161
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02008162 if (dfunc->df_ufunc != NULL
8163 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaara05e5242020-09-19 18:19:19 +02008164 func_ptr_unref(dfunc->df_ufunc);
Bram Moolenaar221fcc72020-05-05 19:46:20 +02008165 }
8166 break;
8167
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008168 case ISN_NEWFUNC:
Bram Moolenaarce658352020-07-31 23:47:12 +02008169 {
8170 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
8171 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
8172
8173 if (ufunc != NULL)
8174 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008175 unlink_def_function(ufunc);
Bram Moolenaarce658352020-07-31 23:47:12 +02008176 func_ptr_unref(ufunc);
8177 }
8178
8179 vim_free(lambda);
8180 vim_free(isn->isn_arg.newfunc.nf_global);
8181 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02008182 break;
8183
Bram Moolenaar5e654232020-09-16 15:22:00 +02008184 case ISN_CHECKTYPE:
8185 free_type(isn->isn_arg.type.ct_type);
8186 break;
8187
Bram Moolenaar02194d22020-10-24 23:08:38 +02008188 case ISN_CMDMOD:
8189 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
8190 ->cmod_filter_regmatch.regprog);
8191 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
8192 break;
8193
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01008194 case ISN_LOADSCRIPT:
8195 case ISN_STORESCRIPT:
8196 vim_free(isn->isn_arg.script.scriptref);
8197 break;
8198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008199 case ISN_2BOOL:
8200 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02008201 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008202 case ISN_ADDBLOB:
8203 case ISN_ADDLIST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008204 case ISN_ANYINDEX:
8205 case ISN_ANYSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008206 case ISN_BCALL:
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02008207 case ISN_BLOBAPPEND:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008208 case ISN_CATCH:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008209 case ISN_CHECKLEN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008210 case ISN_CHECKNR:
Bram Moolenaar02194d22020-10-24 23:08:38 +02008211 case ISN_CMDMOD_REV:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008212 case ISN_COMPAREANY:
8213 case ISN_COMPAREBLOB:
8214 case ISN_COMPAREBOOL:
8215 case ISN_COMPAREDICT:
8216 case ISN_COMPAREFLOAT:
8217 case ISN_COMPAREFUNC:
8218 case ISN_COMPARELIST:
8219 case ISN_COMPARENR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008220 case ISN_COMPARESPECIAL:
8221 case ISN_COMPARESTRING:
8222 case ISN_CONCAT:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02008223 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008224 case ISN_DROP:
8225 case ISN_ECHO:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02008226 case ISN_ECHOERR:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008227 case ISN_ECHOMSG:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008228 case ISN_ENDTRY:
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02008229 case ISN_EXECCONCAT:
8230 case ISN_EXECUTE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008231 case ISN_FOR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008232 case ISN_GETITEM:
8233 case ISN_JUMP:
Bram Moolenaar1dcae592020-10-19 19:02:42 +02008234 case ISN_LISTAPPEND:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02008235 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02008236 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008237 case ISN_LOAD:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008238 case ISN_LOADBDICT:
8239 case ISN_LOADGDICT:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02008240 case ISN_LOADOUTER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008241 case ISN_LOADREG:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008242 case ISN_LOADTDICT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008243 case ISN_LOADV:
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02008244 case ISN_LOADWDICT:
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02008245 case ISN_LOCKCONST:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008246 case ISN_MEMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008247 case ISN_NEGATENR:
8248 case ISN_NEWDICT:
8249 case ISN_NEWLIST:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008250 case ISN_OPANY:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008251 case ISN_OPFLOAT:
8252 case ISN_OPNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008253 case ISN_PCALL:
Bram Moolenaarbd5da372020-03-31 23:13:10 +02008254 case ISN_PCALL_END:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008255 case ISN_PUSHBOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008256 case ISN_PUSHF:
8257 case ISN_PUSHNR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008258 case ISN_PUSHSPEC:
Bram Moolenaarc3516f72020-09-08 22:45:35 +02008259 case ISN_PUT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008260 case ISN_RETURN:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008261 case ISN_SHUFFLE:
8262 case ISN_SLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008263 case ISN_STORE:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01008264 case ISN_STOREINDEX:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008265 case ISN_STORENR:
8266 case ISN_STOREOUTER:
8267 case ISN_STOREREG:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02008268 case ISN_STOREV:
8269 case ISN_STRINDEX:
8270 case ISN_STRSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008271 case ISN_THROW:
8272 case ISN_TRY:
Bram Moolenaar792f7862020-11-23 08:31:18 +01008273 case ISN_UNPACK:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008274 // nothing allocated
8275 break;
8276 }
8277}
8278
8279/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008280 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01008281 */
8282 static void
8283delete_def_function_contents(dfunc_T *dfunc)
8284{
8285 int idx;
8286
8287 ga_clear(&dfunc->df_def_args_isn);
8288
8289 if (dfunc->df_instr != NULL)
8290 {
8291 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
8292 delete_instr(dfunc->df_instr + idx);
8293 VIM_CLEAR(dfunc->df_instr);
8294 }
8295
8296 dfunc->df_deleted = TRUE;
8297}
8298
8299/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008300 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008301 * function, unless another user function still uses it.
8302 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008303 */
8304 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008305unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008306{
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008307 if (ufunc->uf_dfunc_idx > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008308 {
8309 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8310 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008311
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008312 if (--dfunc->df_refcount <= 0)
8313 delete_def_function_contents(dfunc);
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02008314 ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008315 ufunc->uf_dfunc_idx = 0;
8316 if (dfunc->df_ufunc == ufunc)
8317 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008318 }
8319}
8320
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008321/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008322 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008323 */
8324 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008325link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008326{
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008327 if (ufunc->uf_dfunc_idx > 0)
8328 {
8329 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
8330 + ufunc->uf_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008331
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008332 ++dfunc->df_refcount;
8333 }
Bram Moolenaarfdeab652020-09-19 15:16:50 +02008334}
8335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008336#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01008337/*
8338 * Free all functions defined with ":def".
8339 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008340 void
8341free_def_functions(void)
8342{
Bram Moolenaar20431c92020-03-20 18:39:46 +01008343 int idx;
8344
8345 for (idx = 0; idx < def_functions.ga_len; ++idx)
8346 {
8347 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
8348
8349 delete_def_function_contents(dfunc);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01008350 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01008351 }
8352
8353 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008354}
8355#endif
8356
8357
8358#endif // FEAT_EVAL